Skip to content

Commit

Permalink
Move options.c to the base library
Browse files Browse the repository at this point in the history
  • Loading branch information
atrosinenko committed Jan 6, 2024
1 parent 69bc733 commit 6fe96df
Show file tree
Hide file tree
Showing 23 changed files with 56 additions and 55 deletions.
1 change: 1 addition & 0 deletions runtime/base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@

add_library(base STATIC
hashing.c
options.c
)
9 changes: 1 addition & 8 deletions runtime/common/options.c → runtime/base/options.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "kbdysch/options.h"
#include "kbdysch/base/options.h"

#include <assert.h>
#include <stdio.h>
Expand Down Expand Up @@ -30,10 +30,3 @@ const char *get_string_knob(const char *name, const char *default_value) {
const char *str = get_config(name);
return str ? str : default_value;
}

void show_help_and_exit_if_needed(int argc, const char *argv[], const char *help_message) {
if (argc == 1) {
fprintf(stderr, help_message, argv[0]);
exit(1);
}
}
1 change: 0 additions & 1 deletion runtime/blockfuzz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "kbdysch/invoker-utils.h"
#include "kbdysch/kbdysch.h"
#include "kbdysch/mutator-interface.h"
#include "kbdysch/options.h"
#include "kbdysch/userspace/dm.h"
#include "kbdysch/userspace/files.h"
#include "kbdysch/userspace/ioctl-wrapper.h"
Expand Down
2 changes: 1 addition & 1 deletion runtime/bpffuzz.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "kbdysch/base/options.h"
#include "kbdysch/input.h"
#include "kbdysch/kbdysch.h"
#include "kbdysch/logging.h"
#include "kbdysch/mutator-interface.h"
#include "kbdysch/options.h"

#include <syscall.h>
#include <linux/bpf.h>
Expand Down
1 change: 0 additions & 1 deletion runtime/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ set(COMMON_LIB_SOURCES
kernel.c
logging.c
mutator-interface.c
options.c
packet-dumper.c
resources.c
util.c
Expand Down
2 changes: 1 addition & 1 deletion runtime/common/fake_time.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#define time time_real
#define select select_real

#include "kbdysch/base/options.h"
#include "kbdysch/kbdysch.h"
#include "kbdysch/options.h"
#include <stdint.h>
#include <time.h>
#include <dlfcn.h>
Expand Down
2 changes: 1 addition & 1 deletion runtime/common/invoker-helpers.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "kbdysch/base/options.h"
#include "kbdysch/kbdysch.h"
#include "kbdysch/invoker-utils.h"
#include "kbdysch/options.h"

DECLARE_BITMASK_KNOB(skip_block_mask, "SKIPPED_BLOCKS")
DECLARE_INT_KNOB_DEF(min_consume, "MIN_CONSUME", 1)
Expand Down
2 changes: 1 addition & 1 deletion runtime/common/kernel.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "kbdysch/base/options.h"
#include "kbdysch/kbdysch.h"

#include "kbdysch/block.h"
#include "kbdysch/internal-defs.h"
#include "kbdysch/invoker-utils.h"
#include "kbdysch/options.h"
#include "kbdysch/userspace/btrfs.h"
#include "kbdysch/userspace/files.h"

Expand Down
2 changes: 1 addition & 1 deletion runtime/common/resources.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "kbdysch/kbdysch.h"

#include "kbdysch/base/hashing.h"
#include "kbdysch/base/options.h"
#include "kbdysch/internal-defs.h"
#include "kbdysch/invoker-utils.h"
#include "kbdysch/mutator-interface.h"
#include "kbdysch/options.h"

#include <stdint.h>
#include <unistd.h>
Expand Down
9 changes: 8 additions & 1 deletion runtime/common/util.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "kbdysch/kbdysch.h"

#include "kbdysch/base/options.h"
#include "kbdysch/internal-defs.h"
#include "kbdysch/options.h"

#include <assert.h>
#include <sys/mman.h>
Expand Down Expand Up @@ -156,3 +156,10 @@ void dump_to_file(const char *dump_file_name, const void *data, size_t size)
close(dump_fd);
fprintf(stderr, "OK\n");
}

void show_help_and_exit_if_needed(int argc, const char *argv[], const char *help_message) {
if (argc == 1) {
fprintf(stderr, help_message, argv[0]);
exit(1);
}
}
1 change: 0 additions & 1 deletion runtime/fsfuzz.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include "kbdysch/kbdysch.h"
#include "kbdysch/invoker-utils.h"
#include "kbdysch/mutator-interface.h"
#include "kbdysch/options.h"
#include "kbdysch/userspace/files.h"

static const int MAX_INPUT_OPS = 25;
Expand Down
2 changes: 1 addition & 1 deletion runtime/fusefuzz.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "kbdysch/base/options.h"
#include "kbdysch/input.h"
#include "kbdysch/kbdysch.h"
#include "kbdysch/invoker-utils.h"
#include "kbdysch/mutator-interface.h"
#include "kbdysch/options.h"

#include <fcntl.h>
#include <linux/fuse.h>
Expand Down
1 change: 0 additions & 1 deletion runtime/hidfuzz.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "kbdysch/kbdysch.h"
#include "kbdysch/options.h"

#include <sys/types.h>
#include <sys/stat.h>
Expand Down
7 changes: 7 additions & 0 deletions runtime/include/kbdysch/base/base-defs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef KBDYSCH_BASE_BASE_DEFS_H
#define KBDYSCH_BASE_BASE_DEFS_H

#define CONSTRUCTOR(unique_name) \
static void __attribute__((constructor)) unique_name(void)

#endif // KBDYSCH_BASE_BASE_DEFS_H
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef KBDYSCH_OPTIONS_H
#define KBDYSCH_OPTIONS_H
#ifndef KBDYSCH_BASE_OPTIONS_H
#define KBDYSCH_BASE_OPTIONS_H

#include "kbdysch/common-defs.h"
#include "kbdysch/base/base-defs.h"

#include <stdbool.h>
#include <stdint.h>
Expand All @@ -10,29 +10,7 @@
extern "C" {
#endif

/// \defgroup usage_help
/// @{

#define USAGE_WITH_ARGS(args_str) "%1$s " args_str "\n"
#define USAGE_THEN_DO(descr_str) "\t" descr_str "\n"
#define USAGE_RUN_NATIVELY \
USAGE_WITH_ARGS("native") \
USAGE_THEN_DO("Apply to host kernel")
#define USAGE_LKL_SIMPLE \
USAGE_WITH_ARGS("<kernel command line>") \
USAGE_THEN_DO ("Run test on Linux Kernel Library")

/**
* @brief Either continues execution or shows the help message and exits
* @param argc `argc` passed to `main()`
* @param argv `argv` passed to `main()`
* @param help_message Help message to show if needed
*/
void show_help_and_exit_if_needed(int argc, const char *argv[], const char *help_message);

/// @}

/// \defgroup knobs Retrieving configuretion parameters
/// \defgroup knobs Retrieving configuration parameters
/// @{

typedef uint64_t bitmask_t;
Expand Down Expand Up @@ -62,4 +40,4 @@ const char *get_string_knob(const char *name, const char *default_value);
}
#endif

#endif // KBDYSCH_OPTIONS_H
#endif // KBDYSCH_BASE_OPTIONS_H
5 changes: 2 additions & 3 deletions runtime/include/kbdysch/common-defs.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef KBDYSCH_COMMON_DEFS_H
#define KBDYSCH_COMMON_DEFS_H

#include "kbdysch/base/base-defs.h"

// Hack preventing LKL's BPF definitions from interfering with host ones
#define __LKL__LINUX_BPF_H__
union lkl_bpf_attr;
Expand Down Expand Up @@ -31,9 +33,6 @@ typedef char filename_t[MAX_FILE_NAME_LEN];
typedef char string_t[MAX_STRING_LEN];
typedef uint8_t buffer_t[MAX_BUFFER_LEN + WATERMARK_SIZE];

#define CONSTRUCTOR(unique_name) \
static void __attribute__((constructor)) unique_name(void)

// For raw buffers that could possibly contain aligned structures
#define ALIGNED_ENOUGH __attribute__((aligned(64)))

Expand Down
22 changes: 22 additions & 0 deletions runtime/include/kbdysch/kbdysch.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,28 @@ static inline long checked_syscall_impl(
/// Whether `x` fits in 32 bits
#define IS_U32(x) (((x) & ~0xffffffffLLu) == 0)

/// \defgroup usage_help
/// @{

#define USAGE_WITH_ARGS(args_str) "%1$s " args_str "\n"
#define USAGE_THEN_DO(descr_str) "\t" descr_str "\n"
#define USAGE_RUN_NATIVELY \
USAGE_WITH_ARGS("native") \
USAGE_THEN_DO("Apply to host kernel")
#define USAGE_LKL_SIMPLE \
USAGE_WITH_ARGS("<kernel command line>") \
USAGE_THEN_DO ("Run test on Linux Kernel Library")

/**
* @brief Either continues execution or shows the help message and exits
* @param argc `argc` passed to `main()`
* @param argv `argv` passed to `main()`
* @param help_message Help message to show if needed
*/
void show_help_and_exit_if_needed(int argc, const char *argv[], const char *help_message);

/// @}

#ifdef __cplusplus
}
#endif
Expand Down
1 change: 0 additions & 1 deletion runtime/mutators/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ add_library(afl-generic-mutator SHARED
journal.cpp
mutations.cpp
variables.cpp
../common/options.c
)
target_link_libraries(afl-generic-mutator base)

Expand Down
2 changes: 1 addition & 1 deletion runtime/mutators/afl-generic-mutator.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "kbdysch/base/hashing.h"
#include "kbdysch/base/options.h"
#include "kbdysch/mutator-defs.h"
#include "kbdysch/options.h"
#include "afl-interface-decls.h"
#include "helpers.h"
#include "journal.h"
Expand Down
2 changes: 1 addition & 1 deletion runtime/mutators/helpers.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "helpers.h"

#include "kbdysch/options.h"
#include "kbdysch/base/options.h"

#include <errno.h>
#include <fcntl.h>
Expand Down
2 changes: 1 addition & 1 deletion runtime/mutators/mutations.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "mutations.h"

#include "kbdysch/options.h"
#include "kbdysch/base/options.h"
#include "journal.h"

#include <assert.h>
Expand Down
1 change: 0 additions & 1 deletion runtime/partfuzz.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "kbdysch/kbdysch.h"
#include "kbdysch/invoker-utils.h"
#include "kbdysch/options.h"

#include <sys/types.h>
#include <sys/stat.h>
Expand Down
2 changes: 1 addition & 1 deletion runtime/testfuzz.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "kbdysch/base/options.h"
#include "kbdysch/kbdysch.h"
#include "kbdysch/invoker-utils.h"
#include "kbdysch/mutator-interface.h"
#include "kbdysch/options.h"

DECLARE_BOOL_KNOB(populate_success_rate, "POPULATE_SUCCESS_RATE")
DEBUG_COUNTER(num_tags, "Number of %-prefixed tags processed")
Expand Down

0 comments on commit 6fe96df

Please sign in to comment.