Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion oss-fuzz/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
84 changes: 84 additions & 0 deletions oss-fuzz/oss-fuzz-client-raw.dict
Original file line number Diff line number Diff line change
@@ -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(<stdio.h>)"
"__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 T>"
"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<T*>"
"reinterpret_cast<T*>"
"const_cast<T*>"
"static_cast<T*>"
"co_await"
"co_return"
"co_yield"
"requires"
"concept"
"export module m;"
"import m;"
"#embed <stdio.h>"
"sig_atomic_t"
"..."

93 changes: 93 additions & 0 deletions oss-fuzz/raw_fuzzer.cpp
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

// 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 <cstddef>
#include <cstdint>
#include <string>

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<const char*>(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;
}
Loading