[PATCH 16/21] fuzz: add support for passing fuzz data as r/o ramdisk

Ahmad Fatoum a.fatoum at pengutronix.de
Thu Jun 5 04:35:25 PDT 2025


This will be useful for fuzzing file systems and partition parsing.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 include/fuzz.h | 19 ++++++++++++++++++-
 test/Kconfig   |  2 ++
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/include/fuzz.h b/include/fuzz.h
index 35233f90154b..bd95ae6203b0 100644
--- a/include/fuzz.h
+++ b/include/fuzz.h
@@ -8,7 +8,8 @@
 #define __TEST_FUZZ_H
 
 #include <linux/types.h>
-#include <linux/list.h>
+#include <linux/compiler_types.h>
+#include <ramdisk.h>
 
 /**
  * struct fuzz_test - Information about a fuzz test
@@ -48,6 +49,22 @@ extern const struct fuzz_test __barebox_fuzz_tests_end;
 	static __always_unused void * _unused##_func = _func
 #endif
 
+#define fuzz_test_ramdisk(_name, _func)				\
+	static int _func##_ramdisk(const u8 *data, size_t size)	\
+	{							\
+		static struct ramdisk *ramdisk;			\
+		int ret;					\
+		if (!ramdisk)					\
+			ramdisk = ramdisk_init(512);		\
+		if (!ramdisk)					\
+			return -ENODEV;				\
+		ramdisk_setup_ro(ramdisk, data, size);		\
+		ret = _func(ramdisk_get_block_device(ramdisk));	\
+		ramdisk_setup_ro(ramdisk, NULL, 0);		\
+		return ret;					\
+	}							\
+	fuzz_test(_name, _func##_ramdisk)
+
 static inline int fuzz_test_once(const struct fuzz_test *test, const u8 *data, size_t len)
 {
 	return test->func(data, len);
diff --git a/test/Kconfig b/test/Kconfig
index ca2d75c6b025..07b22f09e99b 100644
--- a/test/Kconfig
+++ b/test/Kconfig
@@ -9,6 +9,8 @@ source "test/self/Kconfig"
 
 config FUZZ
 	bool "fuzz tests"
+	select RAMDISK_BLK
+	select DISK
 	help
 	  Say y here to add include fuzz tests into barebox.
 	  These need to be exercised either via command or
-- 
2.39.5




More information about the barebox mailing list