From f7b58991185dfbe6f1dbcbd6013082593e7dd1a3 Mon Sep 17 00:00:00 2001 From: Arthur Chan Date: Tue, 24 Nov 2026 04:57:17 +0000 Subject: [PATCH] OSS-Fuzz: Add new fuzzer/dict/corpus targets raw source processing Signed-off-by: Arthur Chan --- oss-fuzz/Makefile | 8 ++- oss-fuzz/oss-fuzz-client-raw.dict | 84 ++++++++++++++++++++++++++++ oss-fuzz/raw_fuzzer.cpp | 93 +++++++++++++++++++++++++++++++ 3 files changed, 184 insertions(+), 1 deletion(-) create mode 100644 oss-fuzz/oss-fuzz-client-raw.dict create mode 100644 oss-fuzz/raw_fuzzer.cpp diff --git a/oss-fuzz/Makefile b/oss-fuzz/Makefile index a71b026618b..6808baf3206 100644 --- a/oss-fuzz/Makefile +++ b/oss-fuzz/Makefile @@ -124,7 +124,7 @@ translate: translate.o type2.o ${CXX} -std=c++11 -g ${CXXFLAGS} -o $@ type2.cpp translate.cpp clean: - rm -f *.o build/*.o oss-fuzz-client no-fuzz translate + rm -f *.o build/*.o oss-fuzz-client oss-fuzz-client-raw no-fuzz translate preprare-samples: rm -rf samples @@ -378,3 +378,9 @@ main.o: main.cpp type2.h main_nofuzz.o: main.cpp type2.h $(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -DNO_FUZZ -c -o $@ main.cpp + +raw_fuzzer.o: raw_fuzzer.cpp ../lib/config.h ../lib/cppcheck.h ../lib/errorlogger.h ../lib/errortypes.h ../lib/filesettings.h ../lib/settings.h ../lib/standards.h ../lib/suppressions.h + $(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ raw_fuzzer.cpp + +oss-fuzz-client-raw: $(EXTOBJ) $(LIBOBJ) raw_fuzzer.o + ${CXX} ${CXXFLAGS} -o $@ $^ ${LIB_FUZZING_ENGINE} diff --git a/oss-fuzz/oss-fuzz-client-raw.dict b/oss-fuzz/oss-fuzz-client-raw.dict new file mode 100644 index 00000000000..edb9aea95cd --- /dev/null +++ b/oss-fuzz/oss-fuzz-client-raw.dict @@ -0,0 +1,84 @@ +# Dictionary for oss-fuzz-client-raw: C/C++ preprocessor and language tokens +# that gate deep paths in simplecpp/lib/preprocessor.cpp. All tokens are +# multi-byte and traceable to source constructs. +"#include" +"#include_next" +"#define" +"#undef" +"#if" +"#ifdef" +"#ifndef" +"#else" +"#elif" +"#endif" +"#pragma" +"#error" +"#warning" +"#line" +"# 1 \"a.c\"" +"#" +"##" +"%:" +"%:%:" +"_Pragma(\"GCC diagnostic push\")" +"__VA_ARGS__" +"__VA_OPT__(x)" +"defined(A)" +"__has_include()" +"__attribute__((noinline))" +"[[nodiscard]]" +"[[maybe_unused]]" +"[[fallthrough]]" +"[[noreturn]]" +"R\"(text)\"" +"R\"delim(text)delim\"" +"u8\"text\"" +"u't'" +"L'c'" +"'\\0'" +"0x1p-3" +"0b1010" +"1e5" +".5e+2f" +"42uz" +"65536LL" +"::" +"->*" +".*" +"extern \"C\"" +"template" +"typename" +"constexpr" +"consteval" +"noexcept" +"nullptr" +"static_assert(1, \"x\")" +"decltype(x)" +"alignof(int)" +"sizeof(int)" +"asm(\"nop\")" +"__asm__(\"nop\")" +"volatile" +"register" +"typedef" +"struct" +"union" +"enum class" +"try" +"catch" +"throw;" +"dynamic_cast" +"reinterpret_cast" +"const_cast" +"static_cast" +"co_await" +"co_return" +"co_yield" +"requires" +"concept" +"export module m;" +"import m;" +"#embed " +"sig_atomic_t" +"..." + diff --git a/oss-fuzz/raw_fuzzer.cpp b/oss-fuzz/raw_fuzzer.cpp new file mode 100644 index 00000000000..22248f2054e --- /dev/null +++ b/oss-fuzz/raw_fuzzer.cpp @@ -0,0 +1,93 @@ +/* + * Cppcheck - A tool for static C/C++ code analysis + * Copyright (C) 2007-2026 Cppcheck team. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +// Fuzzing rationale (oss-fuzz-client-raw): +// +// The existing OSS-Fuzz harness (main.cpp) rewrites every fuzzer input through +// the type2 grammar generator before analysis, and that grammar never emits +// preprocessor directives (#include/#define/#if/...), raw string literals, +// line markers or many other C/C++ constructs. The preprocessor +// (lib/preprocessor.cpp + externals/simplecpp) and the diagnostic-formatting +// layer (lib/errorlogger.cpp) are therefore structurally unreachable for the +// mutator even though they parse fully attacker-controlled text in production +// (cppcheck is routinely run on untrusted source files by CI systems). +// +// This harness feeds the raw bytes straight into the same analysis entry +// point (CppCheck::checkBuffer) so the lexer, preprocessor and error +// formatting layers are exercised with unmodified attacker-controlled text. +// A memory-safety or UB finding here is a real bug reachable by anyone +// running cppcheck on untrusted code. +// +// Input format: byte 0 selects the language mode (odd bit: C++, else C); +// bytes 1.. are the source text passed through verbatim. + +#include "cppcheck.h" +#include "errorlogger.h" +#include "errortypes.h" +#include "filesettings.h" +#include "settings.h" +#include "standards.h" +#include "suppressions.h" + +#include +#include +#include + +namespace { + class DummyErrorLogger : public ErrorLogger { + public: + void reportOut(const std::string& /*outmsg*/, Color /*c*/) override {} + void reportErr(const ErrorMessage& /*msg*/) override {} + void reportProgress(const std::string& /*filename*/, + const char /*stage*/[], + const std::size_t /*value*/) override {} + void reportMetric(const std::string & /*metric*/) override {} + }; + + Settings create_settings() + { + Settings s; + s.templateFormat = "{bold}{file}:{line}:{column}: {red}{inconclusive:{magenta}}{severity}:{inconclusive: inconclusive:}{default} {message} [{id}]{reset}\\n{code}"; + s.templateLocation = "{bold}{file}:{line}:{column}: {dim}note:{reset} {info}\\n{code}"; + s.addEnabled("all"); + s.certainty.setEnabled(Certainty::inconclusive, true); + return s; + } +} // namespace + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataSize); + +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataSize) +{ + // keep analysis cost bounded; mirrors the main.cpp input cap + if (dataSize < 2 || dataSize >= 10000) + return 0; + + const bool cppSource = (data[0] & 1) != 0; + const std::string code(reinterpret_cast(data + 1), dataSize - 1); + + static DummyErrorLogger s_errorLogger; + Suppressions supprs; + const Settings s_settings = create_settings(); + CppCheck cppcheck(s_settings, supprs, s_errorLogger, nullptr, false, nullptr); + const FileWithDetails file(cppSource ? "test.cpp" : "test.c", + cppSource ? Standards::Language::CPP : Standards::Language::C, + code.size()); + cppcheck.checkBuffer(file, code.data(), code.size()); + return 0; +}