Skip to content

Commit f7b5899

Browse files
committed
OSS-Fuzz: Add new fuzzer/dict/corpus targets raw source processing
Signed-off-by: Arthur Chan <arthur.chan@adalogics.com>
1 parent 68bf94a commit f7b5899

3 files changed

Lines changed: 184 additions & 1 deletion

File tree

‎oss-fuzz/Makefile‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ translate: translate.o type2.o
124124
${CXX} -std=c++11 -g ${CXXFLAGS} -o $@ type2.cpp translate.cpp
125125

126126
clean:
127-
rm -f *.o build/*.o oss-fuzz-client no-fuzz translate
127+
rm -f *.o build/*.o oss-fuzz-client oss-fuzz-client-raw no-fuzz translate
128128

129129
preprare-samples:
130130
rm -rf samples
@@ -378,3 +378,9 @@ main.o: main.cpp type2.h
378378

379379
main_nofuzz.o: main.cpp type2.h
380380
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -DNO_FUZZ -c -o $@ main.cpp
381+
382+
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
383+
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ raw_fuzzer.cpp
384+
385+
oss-fuzz-client-raw: $(EXTOBJ) $(LIBOBJ) raw_fuzzer.o
386+
${CXX} ${CXXFLAGS} -o $@ $^ ${LIB_FUZZING_ENGINE}

‎oss-fuzz/oss-fuzz-client-raw.dict‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Dictionary for oss-fuzz-client-raw: C/C++ preprocessor and language tokens
2+
# that gate deep paths in simplecpp/lib/preprocessor.cpp. All tokens are
3+
# multi-byte and traceable to source constructs.
4+
"#include"
5+
"#include_next"
6+
"#define"
7+
"#undef"
8+
"#if"
9+
"#ifdef"
10+
"#ifndef"
11+
"#else"
12+
"#elif"
13+
"#endif"
14+
"#pragma"
15+
"#error"
16+
"#warning"
17+
"#line"
18+
"# 1 \"a.c\""
19+
"#"
20+
"##"
21+
"%:"
22+
"%:%:"
23+
"_Pragma(\"GCC diagnostic push\")"
24+
"__VA_ARGS__"
25+
"__VA_OPT__(x)"
26+
"defined(A)"
27+
"__has_include(<stdio.h>)"
28+
"__attribute__((noinline))"
29+
"[[nodiscard]]"
30+
"[[maybe_unused]]"
31+
"[[fallthrough]]"
32+
"[[noreturn]]"
33+
"R\"(text)\""
34+
"R\"delim(text)delim\""
35+
"u8\"text\""
36+
"u't'"
37+
"L'c'"
38+
"'\\0'"
39+
"0x1p-3"
40+
"0b1010"
41+
"1e5"
42+
".5e+2f"
43+
"42uz"
44+
"65536LL"
45+
"::"
46+
"->*"
47+
".*"
48+
"extern \"C\""
49+
"template<typename T>"
50+
"typename"
51+
"constexpr"
52+
"consteval"
53+
"noexcept"
54+
"nullptr"
55+
"static_assert(1, \"x\")"
56+
"decltype(x)"
57+
"alignof(int)"
58+
"sizeof(int)"
59+
"asm(\"nop\")"
60+
"__asm__(\"nop\")"
61+
"volatile"
62+
"register"
63+
"typedef"
64+
"struct"
65+
"union"
66+
"enum class"
67+
"try"
68+
"catch"
69+
"throw;"
70+
"dynamic_cast<T*>"
71+
"reinterpret_cast<T*>"
72+
"const_cast<T*>"
73+
"static_cast<T*>"
74+
"co_await"
75+
"co_return"
76+
"co_yield"
77+
"requires"
78+
"concept"
79+
"export module m;"
80+
"import m;"
81+
"#embed <stdio.h>"
82+
"sig_atomic_t"
83+
"..."
84+

‎oss-fuzz/raw_fuzzer.cpp‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2026 Cppcheck team.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
// Fuzzing rationale (oss-fuzz-client-raw):
20+
//
21+
// The existing OSS-Fuzz harness (main.cpp) rewrites every fuzzer input through
22+
// the type2 grammar generator before analysis, and that grammar never emits
23+
// preprocessor directives (#include/#define/#if/...), raw string literals,
24+
// line markers or many other C/C++ constructs. The preprocessor
25+
// (lib/preprocessor.cpp + externals/simplecpp) and the diagnostic-formatting
26+
// layer (lib/errorlogger.cpp) are therefore structurally unreachable for the
27+
// mutator even though they parse fully attacker-controlled text in production
28+
// (cppcheck is routinely run on untrusted source files by CI systems).
29+
//
30+
// This harness feeds the raw bytes straight into the same analysis entry
31+
// point (CppCheck::checkBuffer) so the lexer, preprocessor and error
32+
// formatting layers are exercised with unmodified attacker-controlled text.
33+
// A memory-safety or UB finding here is a real bug reachable by anyone
34+
// running cppcheck on untrusted code.
35+
//
36+
// Input format: byte 0 selects the language mode (odd bit: C++, else C);
37+
// bytes 1.. are the source text passed through verbatim.
38+
39+
#include "cppcheck.h"
40+
#include "errorlogger.h"
41+
#include "errortypes.h"
42+
#include "filesettings.h"
43+
#include "settings.h"
44+
#include "standards.h"
45+
#include "suppressions.h"
46+
47+
#include <cstddef>
48+
#include <cstdint>
49+
#include <string>
50+
51+
namespace {
52+
class DummyErrorLogger : public ErrorLogger {
53+
public:
54+
void reportOut(const std::string& /*outmsg*/, Color /*c*/) override {}
55+
void reportErr(const ErrorMessage& /*msg*/) override {}
56+
void reportProgress(const std::string& /*filename*/,
57+
const char /*stage*/[],
58+
const std::size_t /*value*/) override {}
59+
void reportMetric(const std::string & /*metric*/) override {}
60+
};
61+
62+
Settings create_settings()
63+
{
64+
Settings s;
65+
s.templateFormat = "{bold}{file}:{line}:{column}: {red}{inconclusive:{magenta}}{severity}:{inconclusive: inconclusive:}{default} {message} [{id}]{reset}\\n{code}";
66+
s.templateLocation = "{bold}{file}:{line}:{column}: {dim}note:{reset} {info}\\n{code}";
67+
s.addEnabled("all");
68+
s.certainty.setEnabled(Certainty::inconclusive, true);
69+
return s;
70+
}
71+
} // namespace
72+
73+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataSize);
74+
75+
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataSize)
76+
{
77+
// keep analysis cost bounded; mirrors the main.cpp input cap
78+
if (dataSize < 2 || dataSize >= 10000)
79+
return 0;
80+
81+
const bool cppSource = (data[0] & 1) != 0;
82+
const std::string code(reinterpret_cast<const char*>(data + 1), dataSize - 1);
83+
84+
static DummyErrorLogger s_errorLogger;
85+
Suppressions supprs;
86+
const Settings s_settings = create_settings();
87+
CppCheck cppcheck(s_settings, supprs, s_errorLogger, nullptr, false, nullptr);
88+
const FileWithDetails file(cppSource ? "test.cpp" : "test.c",
89+
cppSource ? Standards::Language::CPP : Standards::Language::C,
90+
code.size());
91+
cppcheck.checkBuffer(file, code.data(), code.size());
92+
return 0;
93+
}

0 commit comments

Comments
 (0)