Skip to content

Commit 0df10ce

Browse files
committed
WIP: added custom mutator example
1 parent 9f8bc7c commit 0df10ce

File tree

7 files changed

+85
-13
lines changed

7 files changed

+85
-13
lines changed

src/explore_me/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ foreach(TestType IN ITEMS
1919
simple_checks
2020
complex_checks
2121
structured_input_checks
22+
custom_mutator_example_checks
2223
)
2324

2425
add_executable(${TestType}_test

src/explore_me/custom_mutator_example_checks_fuzz_test_inputs/brilliant_caterpillar-crash-da39a3ee5e6b4b0d3255bfef95601890afd80709

Whitespace-only changes.

src/explore_me/custom_mutator_example_checks_fuzz_test_inputs/tough_pigeon-crash-da39a3ee5e6b4b0d3255bfef95601890afd80709

Whitespace-only changes.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <cifuzz/cifuzz.h>
2+
#include <fuzzer/FuzzedDataProvider.h>
3+
#include <iostream>
4+
5+
#include "explore_me.h"
6+
7+
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
8+
#include <gtest/gtest.h>
9+
10+
TEST(ExploreCustomMutatorExampleChecks, DeveloperTest) {
11+
SpecialRequirementsStruct inputStruct = (SpecialRequirementsStruct) {.a=0, .b= 10, .c= 0, .c_size= 0};
12+
inputStruct.c = malloc(sizeof("Developer"));
13+
inputStruct.c_size = sizeof("Developer");
14+
EXPECT_NO_THROW(ExploreCustomMutatorExampleChecks(inputStruct));
15+
}
16+
17+
TEST(ExploreStructuredInputChecks, MaintainerTest) {
18+
InputStrut inputStruct = (InputStruct) {.a=20, .b= -10, .c=0};
19+
inputStruct.c = malloc(sizeof("Maintainer"));
20+
inputStruct.c_size = sizeof("Maintainer");
21+
EXPECT_NO_THROW(ExploreCustomMutatorExampleChecks(inputStruct));
22+
}
23+
24+
#endif
25+
26+
FUZZ_TEST(const uint8_t *data, size_t size) {
27+
SpecialRequirementsStruct* inputStruct = (SpecialRequirementsStruct*) data;
28+
ExploreCustomMutatorExampleChecks(*inputStruct);
29+
30+
free(inputStruct->c);
31+
}
32+
33+
34+
extern "C" size_t LLVMFuzzerCustomMutator(uint8_t *data, size_t size,
35+
size_t maxSize, unsigned int seed) {
36+
FuzzedDataProvider fdp(data, size);
37+
long a = fdp.ConsumeIntegral<long>();
38+
long b = fdp.ConsumeIntegral<long>();
39+
const char* tempC = fdp.ConsumeRemainingBytesAsString().c_str();
40+
size_t c_size= strlen(tempC) +1;
41+
char* c = (char*) malloc(c_size);
42+
strncpy(c, tempC, c_size);
43+
SpecialRequirementsStruct specialRequirementsStruct = (SpecialRequirementsStruct) {
44+
.a= a, .b=b, .c_size=c_size, .c= c
45+
};
46+
47+
free(data);
48+
data = (uint8_t*) malloc (sizeof(specialRequirementsStruct));
49+
std::memcpy(data, &specialRequirementsStruct, sizeof(specialRequirementsStruct));
50+
51+
std::cout << "In custom mutator.\n";
52+
53+
return sizeof(specialRequirementsStruct);
54+
}

src/explore_me/explore_me.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,26 @@ void ExploreComplexChecks(long a, long b, std::string c) {
3131
}
3232
}
3333

34-
void ExploreStructuredInputChecks(InputStrut inputStrut){
35-
if (EncodeBase64(inputStrut.c) == "SGV5LCB3ZWw=") {
36-
if (insecureEncrypt(inputStrut.a) == 0x4e9e91e6677cfff3L) {
37-
if (insecureEncrypt(inputStrut.b) == 0x4f8b9fb34431d9d3L) {
34+
void ExploreStructuredInputChecks(InputStruct inputStruct){
35+
if (EncodeBase64(inputStruct.c) == "SGV5LCB3ZWw=") {
36+
if (insecureEncrypt(inputStruct.a) == 0x4e9e91e6677cfff3L) {
37+
if (insecureEncrypt(inputStruct.b) == 0x4f8b9fb34431d9d3L) {
3838
trigger_double_free();
3939
}
4040
}
4141
}
4242
}
4343

44+
void ExploreCustomMutatorExampleChecks(SpecialRequirementsStruct specialRequirementsStruct){
45+
strncpy(specialRequirementsStruct.c, "Hello", specialRequirementsStruct.c_size);
46+
47+
if (insecureEncrypt(specialRequirementsStruct.a) == 0x4e9e91e6677cfff3L) {
48+
if (insecureEncrypt(specialRequirementsStruct.b) == 0x4f8b9fb34431d9d3L) {
49+
trigger_memory_leak();
50+
}
51+
}
52+
}
53+
4454
static long insecureEncrypt(long input) {
4555
long key = 0xefe4eb93215cb6b0L;
4656
return input ^ key;

src/explore_me/explore_me.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,23 @@
77

88
#include <string>
99

10-
struct InputStrut {
10+
struct InputStruct {
1111
long a;
1212
long b;
1313
std::string c;
1414
};
1515

16+
struct SpecialRequirementsStruct {
17+
long a;
18+
long b;
19+
size_t c_size;
20+
char* c;
21+
};
22+
1623
void ExploreSimpleChecks(int a, int b, std::string c);
1724

1825
void ExploreComplexChecks(long a, long b, std::string c);
1926

20-
void ExploreStructuredInputChecks(InputStrut inputStrut);
27+
void ExploreStructuredInputChecks(InputStruct inputStrut);
2128

22-
void ExploreCustomMutatorExampleChecks(long a, long b, std::string c);
29+
void ExploreCustomMutatorExampleChecks(SpecialRequirementsStruct specialRequirementsStruct);

src/explore_me/structured_input_checks_test.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
#include <gtest/gtest.h>
88

99
TEST(ExploreStructuredInputChecks, DeveloperTest) {
10-
InputStrut inputStrut = (InputStrut) {.a=0, .b= 10, .c="Developer"};
11-
EXPECT_NO_THROW(ExploreStructuredInputChecks(inputStrut));
10+
InputStruct inputStruct = (InputStrut) {.a=0, .b= 10, .c="Developer"};
11+
EXPECT_NO_THROW(ExploreStructuredInputChecks(inputStruct));
1212
}
1313

1414
TEST(ExploreStructuredInputChecks, MaintainerTest) {
15-
InputStrut inputStrut = (InputStrut) {.a=20, .b= -10, .c="Maintainer"};
16-
EXPECT_NO_THROW(ExploreStructuredInputChecks(inputStrut));
15+
InputStruct inputStruct = (InputStruct) {.a=20, .b= -10, .c="Maintainer"};
16+
EXPECT_NO_THROW(ExploreStructuredInputChecks(inputStruct));
1717
}
1818

1919
#endif
@@ -23,7 +23,7 @@ FUZZ_TEST(const uint8_t *data, size_t size) {
2323
int a = fdp.ConsumeIntegral<int>();
2424
int b = fdp.ConsumeIntegral<int>();
2525
std::string c = fdp.ConsumeRemainingBytesAsString();
26-
InputStrut inputStrut = (InputStrut) {.a=a, .b= b, .c=c};
26+
InputStruct inputStruct = (InputStruct) {.a=a, .b= b, .c=c};
2727

28-
ExploreStructuredInputChecks(inputStrut);
28+
ExploreStructuredInputChecks(inputStruct);
2929
}

0 commit comments

Comments
 (0)