Skip to content

Commit

Permalink
Record refcounter trace into a file in the artefacts dir.
Browse files Browse the repository at this point in the history
  • Loading branch information
sobomax committed Aug 27, 2024
1 parent 309de43 commit d5ef761
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
13 changes: 13 additions & 0 deletions scripts/fuzz/fuzz_command_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@
int
LLVMFuzzerInitialize(int *_argc, char ***_argv)
{
#if 1
char * const *argv = *_argv;

for (int i = 0; i < *_argc; i++) {
int aplen = sizeof("-artifact_prefix=") - 1;
if (memcmp(argv[i], "-artifact_prefix=", aplen))
continue;
RTPPInitializeParams.artifact_prefix = argv[i] + aplen;
fprintf(stderr, "RTPPInitializeParams.artifact_prefix = %s\n", RTPPInitializeParams.artifact_prefix);
}
#else
RTPPInitializeParams.artifact_prefix = "/out";
#endif

return RTPPInitialize();
}
Expand Down
14 changes: 14 additions & 0 deletions scripts/fuzz/fuzz_rtpp_utils.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <sys/socket.h>
#include <sys/stat.h>
#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
Expand Down Expand Up @@ -51,6 +52,10 @@ cleanupHandler(void)
printf("Cleaning up before exit...\n");
rtpp_shutdown(gconf.cfsp);
close(gconf.tfd);
if (rtpp_refcnt_tracefile != NULL) {
fflush(rtpp_refcnt_tracefile);
fclose(rtpp_refcnt_tracefile);
}
}

static unsigned char deterministic_data[32] = {
Expand Down Expand Up @@ -98,6 +103,7 @@ static struct RTPPInitializeParams {
const char *notify_socket;
const char *rec_spool_dir;
const char *rec_final_dir;
const char *artifact_prefix;
const char *modules[];
} RTPPInitializeParams = {
.ttl = "1",
Expand Down Expand Up @@ -137,6 +143,14 @@ RTPPInitialize(void)
if (__afl_manual_init != NULL)
__afl_manual_init();

if (rp->artifact_prefix != NULL) {
char buf[512];
snprintf(buf, sizeof(buf), "%s/rtpp_refcnt.%d.txt", rp->artifact_prefix, getpid());
rtpp_refcnt_tracefile = fopen(buf, "w");
if (rtpp_refcnt_tracefile == NULL)
goto e0;
}

OPT_SAVE();
assert(RAND_set_rand_method(&dummy) == 1);
SeedRNGs();
Expand Down
11 changes: 10 additions & 1 deletion scripts/fuzz/fuzz_standalone.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ extern int LLVMFuzzerInitialize(int *argc, char ***argv) __attribute__((__weak__

int LLVMFuzzerTestOneInput(const char *data, size_t size);

#if 0
__attribute__((constructor)) static void
rtpp_init()
{
Expand All @@ -45,6 +46,7 @@ rtpp_init()
abort();
}
}
#endif

int
main(int argc, char *argv[])
Expand All @@ -53,13 +55,20 @@ main(int argc, char *argv[])
char *cp;
size_t size;

if (LLVMFuzzerInitialize == NULL)
return (-1);
int r = LLVMFuzzerInitialize(&argc, &argv);
if (r != 0)
return (-1);
fflag = 0;
OPT_SAVE();
while ((ch = getopt(argc, argv, "f")) != -1) {
while ((ch = getopt(argc, argv, "a:f")) != -1) {
switch (ch) {
case 'f':
fflag = 1;
break;
case 'a':
break;
default:
return (-1);
}
Expand Down
8 changes: 6 additions & 2 deletions src/rtpp_refcnt.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ struct rtpp_refcnt_priv
int flags;
};
const size_t rtpp_refcnt_osize = sizeof(struct rtpp_refcnt_priv);
FILE *rtpp_refcnt_tracefile = NULL;
#define TRFD ((rtpp_refcnt_tracefile == NULL) ? stderr : rtpp_refcnt_tracefile)

static void rtpp_refcnt_attach(struct rtpp_refcnt *, rtpp_refcnt_dtor_t,
void *);
Expand Down Expand Up @@ -185,7 +187,8 @@ rtpp_refcnt_incref(struct rtpp_refcnt *pub, HERETYPE mlp)
free(dbuf);
}
#else
fprintf(stderr, CODEPTR_FMT(": rtpp_refcnt(%p, %u).incref()\n", mlp, pub, oldcnt));
fprintf(TRFD, CODEPTR_FMT(": rtpp_refcnt(%p, %u).incref()\n", mlp, pub, oldcnt));
fflush(TRFD);
#endif
}
#endif
Expand Down Expand Up @@ -223,7 +226,8 @@ rtpp_refcnt_decref(struct rtpp_refcnt *pub, HERETYPE mlp)
free(dbuf);
}
#else
fprintf(stderr, CODEPTR_FMT(": rtpp_refcnt(%p, %u).decref()\n", mlp, pub, oldcnt));
fprintf(TRFD, CODEPTR_FMT(": rtpp_refcnt(%p, %u).decref()\n", mlp, pub, oldcnt));
fflush(TRFD);
#endif
}
#endif
Expand Down
9 changes: 9 additions & 0 deletions src/rtpp_refcnt.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ struct rtpp_codeptr;

typedef void (*rtpp_refcnt_dtor_t)(void *);

#if defined(__FreeBSD__)
struct __sFILE;
typedef struct __sFILE FILE;
#else
struct _IO_FILE;
typedef struct _IO_FILE FILE;
#endif

DECLARE_CLASS(rtpp_refcnt, void *, rtpp_refcnt_dtor_t);

DECLARE_METHOD(rtpp_refcnt, refcnt_incref, void, const struct rtpp_codeptr *);
Expand Down Expand Up @@ -63,6 +71,7 @@ struct rtpp_refcnt
};

extern const size_t rtpp_refcnt_osize;
extern FILE *rtpp_refcnt_tracefile;
rtpp_refcnt_rot *rtpp_refcnt_ctor_pa(void *);

#define _GET_ARG_3(_1, _2, _3, ...) _3
Expand Down

0 comments on commit d5ef761

Please sign in to comment.