Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fuzzing: Update to afl++, adding persisten-fuzzing example #19562

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Teufelchen1
Copy link
Contributor

Contribution description

Hello! 🦐

This upgrades the fuzzing infrastructure from AFL to AFL++.

The PR also adds an example usage of AFL++ persistent fuzzing, which increases the execution speed by about 4500%; from ~1800 exec/s to 81.000 exec/s, at least on my machine :). I picked, once again, the uri_parser as the example use-case.
To enable persistent fuzzing, one has to use afl-clang-fast instead of the old afl-gcc, I adjusted the Makefile accordingly - which might break backwards compatibility with certain afl-gcc-only setups. All fuzzing examples present in RIOT work fine with this change, with the exception of gcoap, where a small change of the used modules was necessary.

Testing procedure

Testing is done as usual with fuzzing:
cd RIOT/fuzzing/<target>/
make all-asan
make fuzz

@Teufelchen1 Teufelchen1 requested a review from jia200x as a code owner May 9, 2023 11:08
@github-actions github-actions bot added Area: build system Area: Build system Area: doc Area: Documentation Area: tests Area: tests and testing framework Area: toolchain Area: toolchains; everything related to compilation, libc, linking, … labels May 9, 2023
@benpicco benpicco added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label May 11, 2023
@@ -1,5 +1,6 @@
include ../Makefile.fuzzing_common

USEMODULE += nanocoap_resources
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for waiting.

Otherwise coap_resources and coap_resources_numof are not defined. See /sys/net/application_layer/nanocoap/nanocoap.c:448. The file gets pulled in as soon as you use the gcoap module.

With normal llvm/gcc this isn't an issue, only AFL++ is affected.

As far as I understand it, this is usually not a problem as the code paths containing the undefined values aren't used and get removed during build. AFL++ seems to do this differently and immediately complains about the undefined references even though they aren't used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mhm sounds a bit vague, to make my explanation clear:

You can comment out coap_well_known_core_default_handler and coap_handle_req in nanocoap.c and still compile & run examples/gcoap/ using gcc/llvm. In this modified state, the gcoap fuzzing examples also builds fine via AFL++ without the nanocoap_resources module.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's a bit concerning because the if (IS_USED(MODULE_…)) { pattern is used quite a lot in RIOT.
What is the error you are getting?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/usr/bin/ld: RIOT/fuzzing/gcoap/bin/native/nanocoap/nanocoap.o: in function `coap_handle_req':
RIOT/sys/net/application_layer/nanocoap/nanocoap.c:449: undefined reference to `coap_resources_numof'
/usr/bin/ld: RIOT/sys/net/application_layer/nanocoap/nanocoap.c:448: undefined reference to `coap_resources'
/usr/bin/ld: RIOT/fuzzing/gcoap/bin/native/nanocoap/nanocoap.o: in function `coap_well_known_core_default_handler':
RIOT/sys/net/application_layer/nanocoap/nanocoap.c:(.text.coap_well_known_core_default_handler[coap_well_known_core_default_handler]+0x387): undefined reference to `coap_resources_numof'
/usr/bin/ld: RIOT/sys/net/application_layer/nanocoap/nanocoap.c:1262: undefined reference to `coap_resources'

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see what's happening, looks like --gc-sections does not happen with AFL++.

In the 'old style' applications were expected to define coap_resources/coap_resources_numof, now with nanocoap_resources this is an XFA and the definitions are inside nanoCoAP. But to not break applications we keep support for the legacy definitions unless the application requests nanocoap_resources.

The GCoAP fuzzing example doesn't make use of nanoCoAP resources at all (as GCoAP has it's own way to define resources as a linked list of resource arrays) but the functions that would interact with nanocoap_resources are still compiled as they are part of common code, they are just not used by GCoAP so they are normally discarded on linking.

@riot-ci
Copy link

riot-ci commented May 11, 2023

Murdock results

FAILED

4c696ac fuzzing: Update to afl++, adding persisten-fuzzing example

Success Failures Total Runtime
2339 0 6934 04m:05s

Artifacts

@benpicco benpicco added CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR and removed CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Jun 5, 2023
@benpicco
Copy link
Contributor

benpicco commented Jun 5, 2023

CI is not happy - does this need an update of the CI riotdocker?

@Teufelchen1
Copy link
Contributor Author

CI is not happy - does this need an update of the CI riotdocker?

Not sure, it is by design that the macros e.g. __AFL_FUZZ_INIT() are not known outside of AFL. Correct me if I'm wrong but we do not use AFL(any version) within CI right?
The best course of action might be to disable building this fuzzing harness during CI?

@benpicco
Copy link
Contributor

benpicco commented Jun 5, 2023

we do not use AFL(any version) within CI right?

The fuzzing examples get build by CI - and they should be, otherwise they will just bitrot away. If we need to install AFL++ on CI, we should just do that by adding it to the riotdocker Dockerfile.

@Teufelchen1
Copy link
Contributor Author

Remind myself to check in on this again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: build system Area: Build system Area: doc Area: Documentation Area: tests Area: tests and testing framework Area: toolchain Area: toolchains; everything related to compilation, libc, linking, … CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants