AC_MSG_ERROR([pkg-config is required. See pkg-config.freedesktop.org])
fi
+dnl Check if the flag is supported by compiler
+dnl CC_CHECK_CFLAGS_SILENT([FLAG], [ACTION-IF-FOUND],[ACTION-IF-NOT-FOUND])
+AC_DEFUN([CC_CHECK_CFLAGS_SILENT], [
+ AC_CACHE_VAL(AS_TR_SH([cc_cv_cflags_$1]),
+ [ac_save_CFLAGS="$CFLAGS"
+ CFLAGS="$CFLAGS $1"
+ AC_LINK_IFELSE([AC_LANG_SOURCE([int main() { return 0; }])],
+ [eval "AS_TR_SH([cc_cv_cflags_$1])='yes'"],
+ [eval "AS_TR_SH([cc_cv_cflags_$1])='no'"])
+ CFLAGS="$ac_save_CFLAGS"
+ ])
+
+ AS_IF([eval test x$]AS_TR_SH([cc_cv_cflags_$1])[ = xyes],
+ [$2], [$3])
+])
+
+dnl Check if the flag is supported by compiler (cacheable)
+dnl CC_CHECK_CFLAG([FLAG], [ACTION-IF-FOUND],[ACTION-IF-NOT-FOUND])
+AC_DEFUN([CC_CHECK_CFLAG], [
+ AC_CACHE_CHECK([if $CC supports $1 flag],
+ AS_TR_SH([cc_cv_cflags_$1]),
+ CC_CHECK_CFLAGS_SILENT([$1]) dnl Don't execute actions here!
+ )
+
+ AS_IF([eval test x$]AS_TR_SH([cc_cv_cflags_$1])[ = xyes],
+ [$2], [$3])
+])
+
+dnl CC_CHECK_CFLAGS([FLAG1 FLAG2], [action-if-found], [action-if-not])
+AC_DEFUN([CC_CHECK_CFLAGS], [
+ for flag in $1; do
+ CC_CHECK_CFLAG($flag, [$2], [$3])
+ done
+])
+
dnl EROFS_UTILS_PARSE_DIRECTORY
dnl Input: $1 = a string to a relative or absolute directory
dnl Output: $2 = the variable to set with the absolute directory
[enable_werror="$enableval"],
[enable_werror="no"])
+AC_ARG_ENABLE([fuzzing],
+ [AS_HELP_STRING([--enable-fuzzing],
+ [set up fuzzing mode @<:@default=no@:>@])],
+ [enable_fuzzing="$enableval"],
+ [enable_fuzzing="no"])
+
AC_ARG_ENABLE(lz4,
[AS_HELP_STRING([--disable-lz4], [disable LZ4 compression support @<:@default=enabled@:>@])],
[enable_lz4="$enableval"], [enable_lz4="yes"])
# Enable 64-bit off_t
CFLAGS+=" -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64"
+# Configure fuzzing mode
+AS_IF([test "x$enable_fuzzing" != "xyes"], [], [
+ CC_CHECK_CFLAGS(["-fsanitize=address,fuzzer-no-link"], [
+ CFLAGS="$CFLAGS -g -O1 -fsanitize=address,fuzzer-no-link"
+ ], [
+ AC_MSG_ERROR([Compiler doesn't support `-fsanitize=address,fuzzer-no-link`])
+ ])
+])
+AM_CONDITIONAL([ENABLE_FUZZING], [test "x${enable_fuzzing}" = "xyes"])
+
# Set up needed symbols, conditionals and compiler/linker flags
AM_CONDITIONAL([ENABLE_LZ4], [test "x${have_lz4}" = "xyes"])
AM_CONDITIONAL([ENABLE_LZ4HC], [test "x${have_lz4hc}" = "xyes"])
static int erofs_check_sb_chksum(void)
{
- int ret;
+#ifndef FUZZING
u8 buf[EROFS_MAX_BLOCK_SIZE];
u32 crc;
struct erofs_super_block *sb;
+ int ret;
ret = blk_read(0, buf, 0, 1);
if (ret) {
fsckcfg.corrupted = true;
return -1;
}
+#endif
return 0;
}
return ret;
}
-int main(int argc, char **argv)
+#ifdef FUZZING
+int erofsfsck_fuzz_one(int argc, char *argv[])
+#else
+int main(int argc, char *argv[])
+#endif
{
int err;
goto exit;
}
+#ifdef FUZZING
+ cfg.c_dbg_lvl = -1;
+#endif
+
err = dev_open_ro(cfg.c_img_path);
if (err) {
erofs_err("failed to open image file");
erofs_exit_configure();
return err ? 1 : 0;
}
+
+#ifdef FUZZING
+int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
+{
+ int fd, ret;
+ char filename[] = "/tmp/erofsfsck_libfuzzer_XXXXXX";
+ char *argv[] = {
+ "fsck.erofs",
+ "--extract",
+ filename,
+ };
+
+ fd = mkstemp(filename);
+ if (fd < 0)
+ return -errno;
+ if (write(fd, Data, Size) != Size) {
+ close(fd);
+ return -EIO;
+ }
+ close(fd);
+ ret = erofsfsck_fuzz_one(ARRAY_SIZE(argv), argv);
+ unlink(filename);
+ return ret ? -1 : 0;
+}
+#endif