This commit is contained in:
whaffman 2025-01-19 15:55:55 +01:00
commit 2e4a52504c
14 changed files with 132 additions and 0 deletions

54
Makefile Normal file
View File

@ -0,0 +1,54 @@
# **************************************************************************** #
# #
# ::: o_ :::::: ::: #
# Makefile :+: / :+::+: :+: #
# +:+ > +:++:+ +:+ #
# By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ #
# +#+ +#+#+ +#++#+ +#+ \o/ #
# Created: 2024/10/15 11:48:46 by whaffman #+#+# #+#+# #+# #+# | #
# Updated: 2024/11/07 15:28:08 by whaffman ### ### ### ### / \ #
# #
# **************************************************************************** #
NAME = funfail.so
SRC_PATH = src
INC_PATH = inc
OBJ_PATH = obj
VPATH = src:src/utils
SOURCES = $(shell basename -a $(shell find $(SRC_PATH) -type f -name "*.c"))
OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o))
DEPENDS = ${OBJECTS:.o=.d}
CC = cc
RM = rm -rf
INCLUDES = -I./$(INC_PATH)
CFLAGS = -Wall -Wextra -Werror -MMD
LDLIBS = -fPIC -shared -ldl
all: $(NAME)
$(NAME): $(OBJECTS)
$(CC) $(CFLAGS) $(OBJECTS) $(LDLIBS) -o $(NAME)
-include ${DEPENDS}
$(OBJ_PATH):
mkdir -p $@
$(OBJ_PATH)/%.o: %.c | $(OBJ_PATH)
$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@
clean:
$(RM) $(OBJECTS) $(OBJ_PATH)
fclean: clean
$(RM) $(NAME)
re: fclean all
.PHONY: all clean fclean re

BIN
funfail.so Executable file

Binary file not shown.

1
obj/calloc.d Normal file
View File

@ -0,0 +1 @@
obj/calloc.o: src/calloc.c

BIN
obj/calloc.o Normal file

Binary file not shown.

1
obj/malloc.d Normal file
View File

@ -0,0 +1 @@
obj/malloc.o: src/malloc.c

BIN
obj/malloc.o Normal file

Binary file not shown.

1
obj/read.d Normal file
View File

@ -0,0 +1 @@
obj/read.o: src/read.c

BIN
obj/read.o Normal file

Binary file not shown.

1
obj/write.d Normal file
View File

@ -0,0 +1 @@
obj/write.o: src/write.c

BIN
obj/write.o Normal file

Binary file not shown.

25
src/calloc.c Normal file
View File

@ -0,0 +1,25 @@
#include <dlfcn.h>
#include <errno.h>
#include <stdlib.h>
typedef void *(*f)(size_t __nmemb, size_t __size);
void *calloc(size_t __nmemb, size_t __size)
{
static f og_calloc = NULL;
static int calls = 0;
const int MAX_CALLS = getenv("MAX_CALLOC_CALLS") ? atoi(getenv("MAX_CALLOC_CALLS")) : -1;
if (!og_calloc)
{
og_calloc = (f)dlsym(RTLD_NEXT, "calloc");
}
calls++;
if (MAX_CALLS < 0 || calls <= MAX_CALLS)
{
return og_calloc(__nmemb, __size);
}
errno = ENOMEM;
return NULL;
}

23
src/malloc.c Normal file
View File

@ -0,0 +1,23 @@
#include <dlfcn.h>
#include <errno.h>
#include <stdlib.h>
typedef void* (*f)(size_t size);
void* malloc(size_t size) {
static f og_malloc = NULL;
static int calls = 0;
const int MAX_CALLS = getenv("MAX_MALLOC_CALLS") ? atoi(getenv("MAX_MALLOC_CALLS")) : -1;
if (!og_malloc) {
og_malloc = (f)dlsym(RTLD_NEXT, "malloc");
}
calls++;
if (MAX_CALLS < 0 || calls <= MAX_CALLS) {
return og_malloc(size);
}
errno = ENOMEM;
return NULL;
}

0
src/read.c Normal file
View File

26
src/write.c Normal file
View File

@ -0,0 +1,26 @@
#include <dlfcn.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
typedef ssize_t (*f)(int fd, const void *buf, size_t count);
ssize_t write(int fd, const void *buf, size_t count)
{
static f og_write = NULL;
static int calls = 0;
const int MAX_CALLS = getenv("MAX_WRITE_CALLS") ? atoi(getenv("MAX_WRITE_CALLS")) : -1;
if (!og_write)
{
og_write = (f)dlsym(RTLD_NEXT, "write");
}
calls++;
if (MAX_CALLS < 0 || calls <= MAX_CALLS)
{
return og_write(fd, buf, count);
}
errno = EBADF;
return -1;
}