Skip to content

Commit 8ee8bc2

Browse files
akosthekissrerobika
authored andcommitted
Improve libfuzz integration (#2916)
- Allow command line tools to build together with libfuzzer driver. Compile everything with `-fsanitize=fuzzer-no-link` to prevent linking in libfuzzers's `main` symbol in all executables (causing duplicate symbol errors in command line tools), and add `-fsanitize=fuzzer` to the libfuzzer driver only. - Make ASan optional when building with libfuzzer to allow the user to choose freely from available sanitizers (e.g., UBSan, MSan, HWASan). - Stabilize libfuzzer by resetting PRNG seed at every invocation. JerryScript-DCO-1.0-Signed-off-by: Akos Kiss [email protected]
1 parent 3953fee commit 8ee8bc2

File tree

4 files changed

+6
-13
lines changed

4 files changed

+6
-13
lines changed

CMakeLists.txt

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,6 @@ if(NOT USING_CLANG)
6464
set(JERRY_LIBFUZZER_MESSAGE " (FORCED BY COMPILER)")
6565
endif()
6666

67-
if(JERRY_LIBFUZZER)
68-
set(JERRY_CMDLINE OFF)
69-
set(JERRY_CMDLINE_TEST OFF)
70-
set(JERRY_CMDLINE_SNAPSHOT OFF)
71-
72-
set(JERRY_CMDLINE_MESSAGE " (FORCED BY LIBFUZZER)")
73-
set(JERRY_CMDLINE_TEST_MESSAGE " (FORCED BY LIBFUZZER)")
74-
set(JERRY_CMDLINE_SNAPSHOT_MESSAGE " (FORCED BY LIBFUZZER)")
75-
endif()
76-
7767
if(JERRY_CMDLINE OR JERRY_CMDLINE_TEST OR JERRY_CMDLINE_SNAPSHOT OR JERRY_LIBFUZZER OR UNITTESTS OR DOCTESTS)
7868
set(JERRY_PORT_DEFAULT ON)
7969

@@ -224,7 +214,7 @@ if(USING_MSVC)
224214
endif()
225215

226216
if(JERRY_LIBFUZZER)
227-
jerry_add_compile_flags(-fsanitize=address,fuzzer)
217+
jerry_add_compile_flags(-fsanitize=fuzzer-no-link)
228218
endif()
229219

230220
# Strip binary

docs/01.GETTING-STARTED.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ allocator is used.
123123
**To build with libfuzzer support**
124124

125125
```bash
126-
CC=clang python tools/build.py --libfuzzer=on --lto=off
126+
CC=clang python tools/build.py --libfuzzer=on --compile-flag=-fsanitize=address --lto=off
127127
```
128128

129129
Check the documentation of libfuzzer to get the runtime settings of the created fuzzer

jerry-main/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ endmacro()
5757
# Jerry with libfuzzer support
5858
if(JERRY_LIBFUZZER)
5959
jerry_create_executable("jerry-libfuzzer" "libfuzzer.c")
60-
target_link_libraries("jerry-libfuzzer" jerry-port-default)
60+
target_link_libraries("jerry-libfuzzer" jerry-port-default -fsanitize=fuzzer)
6161
endif()
6262

6363
# Jerry standalones

jerry-main/libfuzzer.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@
1313
* limitations under the License.
1414
*/
1515

16+
#include <stdlib.h>
17+
1618
#include "jerryscript.h"
1719

1820

1921
int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size)
2022
{
23+
srand (0);
2124
jerry_init (JERRY_INIT_EMPTY);
2225

2326
if (jerry_is_valid_utf8_string ((jerry_char_t *) data, (jerry_size_t) size))

0 commit comments

Comments
 (0)