fuzz: basic setup

This commit is contained in:
Khaïs COLIN 2025-04-30 16:14:02 +02:00
parent 58a27d712b
commit f0d7dcc752
4 changed files with 49 additions and 1 deletions

3
.gitignore vendored
View file

@ -35,3 +35,6 @@ compile_commands.json
.vscode
report.txt
zms_testeur
fuzz
*CORPUS
crash-*

View file

@ -1,5 +1,6 @@
NAME = minishell
DEBUG = -g -O0
FUZZ = fuzz
DEBUG = -g -O1
# -fno-omit-frame-pointer is to prevent malloc stacktraces from being truncated,
# see "My malloc stacktraces are too short" here:
# https://github.com/google/sanitizers/wiki/AddressSanitizer
@ -118,6 +119,7 @@ srcs = \
objs = $(srcs:.c=.o)
export objs
minishell_objs = $(addsuffix .o,src/$(NAME)) $(objs)
fuzz_objs = $(addsuffix .o,src/$(FUZZ)) $(objs)
all_objs = $(minishell_objs)
deps = $(all_objs:.o=.d)
@ -131,6 +133,10 @@ all: $(NAME)
$(NAME): $(minishell_objs) $(LIBFT)
$(CC) $(CFLAGS) -o $@ $(minishell_objs) $(LINCLUDE) $(LDLIBS)
$(FUZZ): CFLAGS += -fsanitize=fuzzer,address,undefined
$(FUZZ): $(fuzz_objs) $(LIBFT)
$(CC) $(CFLAGS) -o $@ $(fuzz_objs) $(LINCLUDE) $(LDLIBS)
$(LIBFT): CFLAGS+=-DBUFFER_SIZE=1
$(LIBFT):
+$(MAKE) -C $(LIBFTDIR)

BIN
fuzz_hand_tester Executable file

Binary file not shown.

39
src/fuzz.c Normal file
View file

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* fuzz.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/30 16:01:18 by kcolin #+# #+# */
/* Updated: 2025/04/30 17:13:17 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
#include "fcntl.h"
#include "minishell.h"
#include "parser/cmd/cmd_destroy.h"
#include "parser/cmd_parsing.h"
#include "unistd.h"
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
t_minishell app;
bzero(&app, sizeof(t_minishell));
int null = open("/dev/null", O_RDONLY, 0);
char *line = (char *)calloc(size + 1, sizeof(char));
memcpy(line, data, size);
dup2(null, STDIN_FILENO);
close(null);
t_cmd *cmd = minishell_parse(&app, line);
cmd_destroy(cmd);
free(line);
return (0); // Values other than 0 and -1 are reserved for future use.
}