Skip to content

Commit 03b6cfd

Browse files
committed
unix: Add aflplusplus variant for fuzzing.
Signed-off-by: Jeff Epler <jepler@gmail.com>
1 parent ab1986e commit 03b6cfd

File tree

6 files changed

+112
-0
lines changed

6 files changed

+112
-0
lines changed

ports/unix/main.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,10 @@ MP_NOINLINE int main_(int argc, char **argv) {
640640
sys_set_excecutable(argv[0]);
641641
#endif
642642

643+
#ifdef __AFL_HAVE_MANUAL_CONTROL
644+
__AFL_INIT();
645+
#endif
646+
643647
const int NOTHING_EXECUTED = -2;
644648
int ret = NOTHING_EXECUTED;
645649
bool inspect = false;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Unix Fuzzing Variant
2+
3+
This variant is for use with the [AFLplusplus](https://github.com/AFLplusplus/AFLplusplus)
4+
fuzzer.
5+
6+
1. Install AFLplusplus so that the program `afl-cc` is on $PATH
7+
1. `cd ports/unix && make VARIANT=aflplusplus -j$(nproc)`
8+
1. Gather your inputs (e.g., from test cases in `tests`)
9+
1. Optionally, minimize them e.g., with `afl-cmin` (see AFLplusplus docs)
10+
1. Run the fuzzer. The simplest single process way is: `afl-fuzz -i inputs -o findings -- ports/unix/build-aflfuzz/micropython @@`
11+
12+
Eventually, if crashing test cases are found, the crashing program(s) are placed in
13+
`findings/default/crashes` and invocations that were determined to hang go to
14+
`findings/default/hangs`.
15+
16+
There are many more advanced ways to run the fuzzer; see the AFLplusplus documentation for info.
17+
18+
# Safety
19+
20+
Functionality that is known to be unsafe (host filesystem write access via vfs_posix)
21+
or is accepted as causing crashes when used improperly (ctypes, certain struct
22+
& array typecodes) is build-time disabled. However, it's always possible that
23+
the fuzzer will find a condition that can cause an unexpected modification to
24+
the runtime environment.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
include("$(PORT_DIR)/variants/manifest.py")
2+
3+
include("$(MPY_DIR)/extmod/asyncio")
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2019 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
// Set base feature level.
28+
#define MICROPY_CONFIG_ROM_LEVEL (MICROPY_CONFIG_ROM_LEVEL_EXTRA_FEATURES)
29+
30+
#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_LONGLONG)
31+
32+
// Disable features known to affect host environment, access arbitrary addresses, or
33+
// execute arbitrary code
34+
#define MICROPY_VFS_POSIX_WRITABLE (0)
35+
#define MICROPY_PY_STRUCT_UNSAFE_TYPECODES (0)
36+
#define MICROPY_PY_UCTYPES (0)
37+
#define MICROPY_PERSISTENT_CODE_LOAD (0)
38+
// https://github.com/micropython/micropython/issues/17818
39+
#define MICROPY_PY_WEBSOCKET (0)
40+
// https://github.com/micropython/micropython/issues/17714
41+
#define MICROPY_PY_MACHINE (0)
42+
43+
#define MICROPY_EMIT_X64 (0)
44+
#define MICROPY_EMIT_X86 (0)
45+
#define MICROPY_EMIT_THUMB (0)
46+
#define MICROPY_EMIT_INLINE_THUMB (0)
47+
#define MICROPY_EMIT_INLINE_THUMB_FLOAT (0)
48+
#define MICROPY_EMIT_ARM (0)
49+
#define MICROPY_EMIT_XTENSA (0)
50+
#define MICROPY_EMIT_INLINE_XTENSA (0)
51+
#define MICROPY_EMIT_XTENSAWIN (0)
52+
#define MICROPY_EMIT_RV32 (0)
53+
#define MICROPY_EMIT_INLINE_RV32 (0)
54+
55+
#define MICROPY_EMIT_NATIVE_DEBUG (1)
56+
#define MICROPY_EMIT_NATIVE_DEBUG_PRINTER (&mp_stderr_print)
57+
58+
59+
// Enable extra Unix features.
60+
#include "../mpconfigvariant_common.h"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# This is for fuzzing with AFLplusplus
2+
3+
# Disable optimisations and enable assert() on coverage builds.
4+
DEBUG ?= 1
5+
6+
CC=afl-cc
7+
8+
CFLAGS += \
9+
-Wformat -Wmissing-declarations -Wmissing-prototypes \
10+
-Wold-style-definition -Wpointer-arith -Wshadow -Wuninitialized -Wunused-parameter \
11+
12+
MICROPY_PY_SSL = 0
13+
MICROPY_PY_FFI = 0
14+
15+
MPY_TOOL_FLAGS = -mlongint-impl longlong

ports/unix/variants/mpconfigvariant_common.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@
7070
#define MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE (256)
7171

7272
// Allow loading of .mpy files.
73+
#ifndef MICROPY_PERSISTENT_CODE_LOAD
7374
#define MICROPY_PERSISTENT_CODE_LOAD (1)
75+
#endif
7476

7577
// Extra memory debugging.
7678
#define MICROPY_MALLOC_USES_ALLOCATED_SIZE (1)
@@ -115,10 +117,14 @@
115117
#define MICROPY_PY_SELECT_SELECT (0)
116118

117119
// Enable the "websocket" module.
120+
#ifndef MICROPY_PY_WEBSOCKET
118121
#define MICROPY_PY_WEBSOCKET (1)
122+
#endif
119123

120124
// Enable the "machine" module, mostly for machine.mem*.
125+
#ifndef MICROPY_PY_MACHINE
121126
#define MICROPY_PY_MACHINE (1)
127+
#endif
122128
#define MICROPY_PY_MACHINE_PULSE (1)
123129
#define MICROPY_PY_MACHINE_PIN_BASE (1)
124130

0 commit comments

Comments
 (0)