diff --git a/src/include/deepstate/DeepState.h b/src/include/deepstate/DeepState.h index b86b70c..cd977ca 100644 --- a/src/include/deepstate/DeepState.h +++ b/src/include/deepstate/DeepState.h @@ -48,6 +48,8 @@ #define assume DeepState_Assume #define check DeepState_Check +#define rand DeepState_Int + #define MAYBE(...) \ if (DeepState_Bool()) { \ __VA_ARGS__ ; \ @@ -603,36 +605,7 @@ DeepState_ForkAndRunTest(struct DeepState_TestInfo *test) { return DeepState_TestRunCrash; } -/* Run a test case with input initialized by fuzzing. */ -static enum DeepState_TestRunResult -DeepState_FuzzOneTestCase(struct DeepState_TestInfo *test) { - DeepState_InputIndex = 0; - - for (int i = 0; i < DeepState_InputSize; i++) { - DeepState_Input[i] = (char)rand(); - } - - DeepState_Begin(test); - - enum DeepState_TestRunResult result = DeepState_ForkAndRunTest(test); - - if (result == DeepState_TestRunCrash) { - DeepState_LogFormat(DeepState_LogError, "Crashed: %s", test->test_name); - - if (HAS_FLAG_output_test_dir) { - DeepState_SaveCrashingTest(); - } - - DeepState_Crash(); - } - - if (FLAGS_abort_on_fail && ((result == DeepState_TestRunCrash) || - (result == DeepState_TestRunFail))) { - abort(); - } - - return result; -} +enum DeepState_TestRunResult DeepState_FuzzOneTestCase(struct DeepState_TestInfo *test); /* Run a single saved test case with input initialized from the file * `name` in directory `dir`. */ diff --git a/src/lib/DeepState.c b/src/lib/DeepState.c index 2871ecb..3c05a18 100644 --- a/src/lib/DeepState.c +++ b/src/lib/DeepState.c @@ -23,6 +23,8 @@ #include #include +#undef rand + DEEPSTATE_BEGIN_EXTERN_C DEFINE_uint(num_workers, 1, @@ -630,6 +632,37 @@ bool DeepState_CatchAbandoned(void) { return DeepState_CurrentTestRun->result == DeepState_TestRunAbandon; } +/* Run a test case with input initialized by fuzzing. + Has to be defined here since we redefine rand in the header. */ +enum DeepState_TestRunResult DeepState_FuzzOneTestCase(struct DeepState_TestInfo *test) { + DeepState_InputIndex = 0; + + for (int i = 0; i < DeepState_InputSize; i++) { + DeepState_Input[i] = (char)rand(); + } + + DeepState_Begin(test); + + enum DeepState_TestRunResult result = DeepState_ForkAndRunTest(test); + + if (result == DeepState_TestRunCrash) { + DeepState_LogFormat(DeepState_LogError, "Crashed: %s", test->test_name); + + if (HAS_FLAG_output_test_dir) { + DeepState_SaveCrashingTest(); + } + + DeepState_Crash(); + } + + if (FLAGS_abort_on_fail && ((result == DeepState_TestRunCrash) || + (result == DeepState_TestRunFail))) { + abort(); + } + + return result; +} + extern int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { if (Size > sizeof(DeepState_Input)) { return 0; // Just ignore any too-big inputs