Skip to content

Commit

Permalink
Move functions from fuzz_rtpp_utils.h into its own TUs.
Browse files Browse the repository at this point in the history
  • Loading branch information
sobomax committed Sep 4, 2024
1 parent 78550fd commit da1eb72
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 80 deletions.
9 changes: 7 additions & 2 deletions scripts/fuzz/fuzz_command_parser.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#include "fuzz_standalone.h"
#include "fuzz_rtpp_utils.h"
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include "fuzz_standalone.h"
#include "rfz_utils.h"
#include "rfz_command.h"
#include "rfz_chunk.h"

int
Expand Down
16 changes: 15 additions & 1 deletion scripts/fuzz/fuzz_rtp_session.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,37 @@
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>
#include <semaphore.h>
#include <stdio.h>

#include "fuzz_standalone.h"
#include "fuzz_rtpp_utils.h"

#define HAVE_CONFIG_H 1
#include "config_pp.h"

#include "rtpp_types.h"
#include "rtpp_cfg.h"
#include "rtp.h"
#include "rtpp_time.h"
#include "rtp_packet.h"
#include "rtpp_session.h"
#include "rtpp_pipe.h"
#include "rtpp_proc.h"
#include "rtpp_network.h"
#include "rtpp_stream.h"
#include "rtpp_ttl.h"
#include "rtpp_codeptr.h"
#include "rtpp_refcnt.h"
#include "rtpp_command.h"
#include "rtpp_weakref.h"
#include "rtpp_hash_table.h"
#include "advanced/packet_processor.h"
#include "advanced/pproc_manager.h"

#include "rfz_chunk.h"
#include "rfz_utils.h"
#include "rfz_command.h"

static struct {
sem_t wi_proc_done;
Expand Down
23 changes: 0 additions & 23 deletions scripts/fuzz/fuzz_standalone.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,6 @@
#include <unistd.h>
#endif /* FUZZ_STANDALONE */

#if defined(__linux__)
static int optreset; /* Not present in linux */
#endif

static struct opt_save {
char *optarg;
int optind;
int optopt;
int opterr;
int optreset;
} opt_save;

#define OPT_SAVE() (opt_save = (struct opt_save){optarg, optind, optopt, opterr, optreset})
#define OPT_RESTORE() ({ \
optarg = opt_save.optarg; \
optind = opt_save.optind; \
optopt = opt_save.optopt; \
opterr = opt_save.opterr; \
optreset = opt_save.optreset; \
})

#if defined(FUZZ_STANDALONE)
extern int LLVMFuzzerInitialize(int *argc, char ***argv) __attribute__((__weak__));

Expand All @@ -52,7 +31,6 @@ main(int argc, char *argv[])
size_t size;

fflag = 0;
OPT_SAVE();
while ((ch = getopt(argc, argv, "f")) != -1) {
switch (ch) {
case 'f':
Expand All @@ -64,7 +42,6 @@ main(int argc, char *argv[])
}
argc -= optind;
argv += optind;
OPT_RESTORE();

assert(argc == 1);
if (fflag) {
Expand Down
8 changes: 4 additions & 4 deletions scripts/fuzz/oss-fuzz-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,15 @@ do
done
make -C src librtpproxy.la

CFLAGS="${CFLAGS} -flto -fPIE -fPIC"
CXXFLAGS="${CXXFLAGS} -flto -fPIE -fPIC"
CFLAGS="${CFLAGS} -flto -fPIE -fPIC -fvisibility=hidden"
CXXFLAGS="${CXXFLAGS} -flto -fPIE -fPIC -fvisibility=hidden"
RTPPLIB="src/.libs/librtpproxy.a"

for src in rfz_chunk.c
for src in rfz_chunk.c rfz_command.c rfz_utils.c
do
obj="${OUT}/${src%.*}.o"
src=scripts/fuzz/${src}
${CC} ${CFLAGS} ${LIB_FUZZING_ENGINE} -o ${obj} -c ${src}
${CC} ${CFLAGS} ${LIB_FUZZING_ENGINE} -Isrc -o ${obj} -c ${src}
OBJS="${OBJS} ${obj}"
done

Expand Down
45 changes: 45 additions & 0 deletions scripts/fuzz/rfz_command.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <sys/socket.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#define HAVE_CONFIG_H 1
#include "config_pp.h"

#include "rtpp_types.h"
#include "rtpp_cfg.h"
#include "rtpp_refcnt.h"
#include "rtpp_command.h"
#include "rtpp_command_args.h"
#include "rtpp_command_sub.h"
#include "rtpp_command_private.h"
#include "rtpp_command_stats.h"
#include "rtpp_time.h"

#include "rfz_utils.h"
#include "rfz_command.h"

int
ExecuteRTPPCommand(struct rtpp_conf *gcp, const char *data, size_t size)
{
struct rtpp_timestamp dtime = {};
static struct rtpp_command_stats cstat = {};
struct rtpp_command *cmd;
int rval = -1;

if (size >= RTPP_CMD_BUFLEN)
return (-1);

cmd = rtpp_command_ctor(gcp->cfsp, gcp->tfd, &dtime, &cstat, 0);
if (cmd == NULL)
return (-1);
memcpy(cmd->buf, data, size);
cmd->buf[size] = '\0';

rval = rtpp_command_split(cmd, size, &rval, NULL);
if (rval == 0) {
rval = handle_command(gcp->cfsp, cmd);
}
free_command(cmd);
return (rval);
}
3 changes: 3 additions & 0 deletions scripts/fuzz/rfz_command.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

int ExecuteRTPPCommand(struct rtpp_conf *, const char *, size_t);
76 changes: 26 additions & 50 deletions scripts/fuzz/fuzz_rtpp_utils.h → scripts/fuzz/rfz_utils.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#pragma once

#include <sys/socket.h>
#include <assert.h>
#include <fcntl.h>
Expand All @@ -20,12 +18,6 @@
#include "rtpp_refcnt.h"
#include "rtpp_log_stand.h"
#include "rtpp_log_obj.h"
#include "rtpp_command_args.h"
#include "rtpp_command.h"
#include "rtpp_command_sub.h"
#include "rtpp_command_private.h"
#include "rtpp_command_async.h"
#include "rtpp_command_stats.h"
#include "rtpp_proc_async.h"
#include "rtpp_hash_table.h"
#include "rtpp_weakref.h"
Expand All @@ -36,14 +28,32 @@

#include "librtpproxy.h"

struct rtpp_conf {
struct rtpp_cfg *cfsp;
int tfd;
};
#include "rfz_utils.h"

#define howmany(x, y) (sizeof(x) / sizeof(y))

static struct rtpp_conf gconf;
struct rtpp_conf gconf;

#if defined(__linux__)
static int optreset; /* Not present in linux */
#endif

static struct opt_save {
char *optarg;
int optind;
int optopt;
int opterr;
int optreset;
} opt_save;

#define OPT_SAVE() (opt_save = (struct opt_save){optarg, optind, optopt, opterr, optreset})
#define OPT_RESTORE() ({ \
optarg = opt_save.optarg; \
optind = opt_save.optind; \
optopt = opt_save.optopt; \
opterr = opt_save.opterr; \
optreset = opt_save.optreset; \
})

static void
cleanupHandler(void)
Expand Down Expand Up @@ -83,23 +93,14 @@ RAND_METHOD dummy = {
.status = &dRAND_status,
};

static void
void
SeedRNGs(void)
{
offset = 0;
seedrandom();
}

static struct RTPPInitializeParams {
const char *ttl;
const char *setup_ttl;
const char *socket;
const char *debug_level;
const char *notify_socket;
const char *rec_spool_dir;
const char *rec_final_dir;
const char *modules[];
} RTPPInitializeParams = {
struct RTPPInitializeParams RTPPInitializeParams = {
.ttl = "1",
.setup_ttl = "1",
.socket = NULL,
Expand All @@ -112,7 +113,7 @@ static struct RTPPInitializeParams {

extern void __afl_manual_init(void) __attribute__((__weak__));

static int
int
RTPPInitialize(void)
{
const struct RTPPInitializeParams *rp = &RTPPInitializeParams;
Expand Down Expand Up @@ -156,28 +157,3 @@ RTPPInitialize(void)
e0:
return (-1);
}

static int
ExecuteRTPPCommand(struct rtpp_conf *gcp, const char *data, size_t size)
{
struct rtpp_timestamp dtime = {};
static struct rtpp_command_stats cstat = {};
struct rtpp_command *cmd;
int rval = -1;

if (size >= RTPP_CMD_BUFLEN)
return (-1);

cmd = rtpp_command_ctor(gcp->cfsp, gcp->tfd, &dtime, &cstat, 0);
if (cmd == NULL)
return (-1);
memcpy(cmd->buf, data, size);
cmd->buf[size] = '\0';

rval = rtpp_command_split(cmd, size, &rval, NULL);
if (rval == 0) {
rval = handle_command(gcp->cfsp, cmd);
}
free_command(cmd);
return (rval);
}
23 changes: 23 additions & 0 deletions scripts/fuzz/rfz_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

struct RTPPInitializeParams {
const char *ttl;
const char *setup_ttl;
const char *socket;
const char *debug_level;
const char *notify_socket;
const char *rec_spool_dir;
const char *rec_final_dir;
const char *modules[];
};

struct rtpp_conf {
struct rtpp_cfg *cfsp;
int tfd;
};

int RTPPInitialize(void);
void SeedRNGs(void);

extern struct rtpp_conf gconf;
extern struct RTPPInitializeParams RTPPInitializeParams;

0 comments on commit da1eb72

Please sign in to comment.