]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
fuzz: Introduce DO_NOT_OPTIMIZE
authorJan Janssen <medhefgo@web.de>
Fri, 23 Sep 2022 07:54:03 +0000 (09:54 +0200)
committerJan Janssen <medhefgo@web.de>
Sun, 25 Sep 2022 12:26:00 +0000 (14:26 +0200)
The compiler may decide computations like these are not doing anything
and decide to optimize them away. This would defeat the whole fuzzing
exercise. This macro will force the compiler to materialize the value
no matter what. It should be less prone to accidents compared to using
log functions, which would either slow things down or still optimize the
value away (or simply move it into the if branch the log macros create).

The benefit over assert_se would be that no requirement is made on the
value itself. If we are fine getting a string of any size (including
zero), an assert_se would either create a noisy compiler warning about
conditions that would alawys be met or yet again optimize the whole
thing away.

src/boot/efi/fuzz-bcd.c
src/fuzz/fuzz.h

index 6d76533e8ff5810316b489ac4d8d503626bd033c..297b71f60cdab0ce1c5f9ac2f9d68c4830986beb 100644 (file)
@@ -2,7 +2,6 @@
 
 #include "alloc-util.h"
 #include "bcd.h"
-#include "fd-util.h"
 #include "fuzz.h"
 #include "utf8.h"
 
@@ -13,14 +12,11 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
         if (outside_size_range(size, 0, 100*1024))
                 return 0;
 
-        if (!getenv("SYSTEMD_LOG_LEVEL"))
-                log_set_max_level(LOG_CRIT);
-
         p = memdup(data, size);
         assert_se(p);
 
         char16_t *title = get_bcd_title(p, size);
-        if (title)
-                (void) char16_strlen(title);
+        /* If we get something, it must be NUL-terminated, but an empty string is still valid! */
+        DO_NOT_OPTIMIZE(title && char16_strlen(title));
         return 0;
 }
index 04c438edafae6c94f2082a85eddd2ae68710c489..a7d3a89fe2c52ac0769bbb4a8be35c9a3825c78f 100644 (file)
@@ -27,3 +27,6 @@ static inline bool outside_size_range(size_t size, size_t lower, size_t upper) {
                 return FUZZ_USE_SIZE_LIMIT;
         return false;
 }
+
+/* Force value to not be optimized away. */
+#define DO_NOT_OPTIMIZE(value) ({ asm volatile("" : : "g"(value) : "memory"); })