Repository: google/fuzzbench

Issue:
`FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION` discarded
It seems that the value of `CFLAGS` set in the builders is discarded while building the benchmarks using the appropriate `fuzzer.py` file. The discard happens here, it would seem:
https://github.com/google/fuzzbench/blob/d156b3579fe882b46fb65ab3f2bc9a5c5aeb3308/fuzzers/utils.py#L179-L180

The value of `CFLAGS` set in the builders contains `-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION`, which is thus discarded while building. Discarding this value makes at least `libxslt_xpath` and `libxml2_libxml2_xml_reader_for_file_fuzzer` compile with features that make executions non-deterministic. Other benchmarks may be affected.


Discussion:

Resolving patch:
diff --git a/fuzzers/utils.py b/fuzzers/utils.py
--- a/fuzzers/utils.py
+++ b/fuzzers/utils.py
@@ -50,6 +50,8 @@
     '-Wno-unused-command-line-argument'
 ]
 
+FUZZING_CFLAGS = ['-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION']
+
 OSS_FUZZ_LIB_FUZZING_ENGINE_PATH = '/usr/lib/libFuzzingEngine.a'
 BENCHMARK_CONFIG_YAML_PATH = '/benchmark.yaml'
 
@@ -181,18 +183,20 @@ def set_compilation_flags(env=None):
 
     if get_config_value('type') == 'bug':
         append_flags('CFLAGS',
-                     SANITIZER_FLAGS + [BUGS_OPTIMIZATION_LEVEL],
+                     FUZZING_CFLAGS + SANITIZER_FLAGS +
+                     [BUGS_OPTIMIZATION_LEVEL],
                      env=env)
         append_flags('CXXFLAGS',
-                     SANITIZER_FLAGS +
+                     FUZZING_CFLAGS + SANITIZER_FLAGS +
                      [LIBCPLUSPLUS_FLAG, BUGS_OPTIMIZATION_LEVEL],
                      env=env)
     else:
         append_flags('CFLAGS',
-                     NO_SANITIZER_COMPAT_CFLAGS + [DEFAULT_OPTIMIZATION_LEVEL],
+                     FUZZING_CFLAGS + NO_SANITIZER_COMPAT_CFLAGS +
+                     [DEFAULT_OPTIMIZATION_LEVEL],
                      env=env)
         append_flags('CXXFLAGS',
-                     NO_SANITIZER_COMPAT_CFLAGS +
+                     FUZZING_CFLAGS + NO_SANITIZER_COMPAT_CFLAGS +
                      [LIBCPLUSPLUS_FLAG, DEFAULT_OPTIMIZATION_LEVEL],
                      env=env)
 


Test patch:
diff --git a/fuzzers/test_utils.py b/fuzzers/test_utils.py
--- a/fuzzers/test_utils.py
+++ b/fuzzers/test_utils.py
@@ -83,9 +83,11 @@ def test_initialize_env_in_environment_without_sanitizer(fs, environ):
     utils.initialize_env()
     assert os.getenv('FUZZ_TARGET') == 'fuzzer'
     assert os.getenv('CFLAGS') == (
+        '-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION '
         '-pthread -Wl,--no-as-needed -Wl,-ldl -Wl,-lm '
         '-Wno-unused-command-line-argument -O3')
     assert os.getenv('CXXFLAGS') == (
+        '-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION '
         '-pthread -Wl,--no-as-needed -Wl,-ldl -Wl,-lm '
         '-Wno-unused-command-line-argument -stdlib=libc++ -O3')
 
@@ -96,11 +98,13 @@ def test_initialize_env_in_environment_with_sanitizer(fs, environ):
     utils.initialize_env()
     assert os.getenv('FUZZ_TARGET') == 'fuzzer'
     assert os.getenv('CFLAGS') == (
+        '-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION '
         '-fsanitize=address -fsanitize=array-bounds,bool,builtin,enum,'
         'float-divide-by-zero,function,integer-divide-by-zero,null,object-size,'
         'return,returns-nonnull-attribute,shift,signed-integer-overflow,'
         'unreachable,vla-bound,vptr -O1')
     assert os.getenv('CXXFLAGS') == (
+        '-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION '
         '-fsanitize=address -fsanitize=array-bounds,bool,builtin,enum,'
         'float-divide-by-zero,function,integer-divide-by-zero,null,object-size,'
         'return,returns-nonnull-attribute,shift,signed-integer-overflow,'
@@ -114,9 +118,11 @@ def test_initialize_env_in_var_without_sanitizer(fs):
     env = {}
     utils.initialize_env(env)
     assert env.get('FUZZ_TARGET') == 'fuzzer'
-    assert env.get('CFLAGS') == ('-pthread -Wl,--no-as-needed -Wl,-ldl -Wl,-lm '
+    assert env.get('CFLAGS') == ('-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION '
+                                 '-pthread -Wl,--no-as-needed -Wl,-ldl -Wl,-lm '
                                  '-Wno-unused-command-line-argument -O3')
     assert env.get('CXXFLAGS') == (
+        '-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION '
         '-pthread -Wl,--no-as-needed -Wl,-ldl -Wl,-lm '
         '-Wno-unused-command-line-argument -stdlib=libc++ -O3')
 
@@ -128,11 +134,13 @@ def test_initialize_env_in_var_with_sanitizer(fs):
     utils.initialize_env(env)
     assert env.get('FUZZ_TARGET') == 'fuzzer'
     assert env.get('CFLAGS') == (
+        '-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION '
         '-fsanitize=address -fsanitize=array-bounds,bool,builtin,enum,'
         'float-divide-by-zero,function,integer-divide-by-zero,null,object-size,'
         'return,returns-nonnull-attribute,shift,signed-integer-overflow,'
         'unreachable,vla-bound,vptr -O1')
     assert env.get('CXXFLAGS') == (
+        '-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION '
         '-fsanitize=address -fsanitize=array-bounds,bool,builtin,enum,'
         'float-divide-by-zero,function,integer-divide-by-zero,null,object-size,'
         'return,returns-nonnull-attribute,shift,signed-integer-overflow,'