Skip to content

Commit 3b227f2

Browse files
author
TheodorNEngoy
committed
cli11: initial OSS-Fuzz integration (CLI parsing fuzzer)
1 parent eb47f56 commit 3b227f2

File tree

6 files changed

+69
-0
lines changed

6 files changed

+69
-0
lines changed

projects/cli11/Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM gcr.io/oss-fuzz-base/base-builder
2+
RUN apt-get update && apt-get install -y git && rm -rf /var/lib/apt/lists/*
3+
RUN git clone --depth=1 https://github.com/CLIUtils/CLI11.git $SRC/cli11
4+
COPY build.sh $SRC/
5+
COPY fuzzers $SRC/fuzzers
6+
WORKDIR $SRC

projects/cli11/build.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash -eu
2+
set -o pipefail
3+
for f in "$SRC"/fuzzers/*.cc; do
4+
b="$(basename "$f" .cc)"
5+
"$CXX" ${CXXFLAGS:-} -std=c++17 -I"$SRC/cli11/include" \
6+
"$f" -o "$OUT/$b" $LIB_FUZZING_ENGINE ${LDFLAGS:-}
7+
done
8+
# Package seed corpus if present.
9+
[ -d "$SRC/fuzzers/corpus" ] && zip -rq "$OUT/fuzz_cli_parse_seed_corpus.zip" "$SRC/fuzzers/corpus" || true

projects/cli11/fuzzers/corpus/a

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--int 1 --double 2.5 -b

projects/cli11/fuzzers/corpus/b

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sub --sstr hello --si 7
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <cstddef>
2+
#include <cstdint>
3+
#include <string>
4+
#include <vector>
5+
#include <cctype>
6+
#include "CLI/CLI.hpp"
7+
8+
static std::vector<std::string> tokenize_ws(const std::string& s) {
9+
std::vector<std::string> out; std::string cur;
10+
for (unsigned char c : s) {
11+
if (std::isspace(c)) { if (!cur.empty()) { out.push_back(cur); cur.clear(); if (out.size() >= 64) break; } }
12+
else { if (cur.size() < 256) cur.push_back(static_cast<char>(c)); }
13+
}
14+
if (!cur.empty() && out.size() < 64) out.push_back(cur);
15+
return out;
16+
}
17+
18+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
19+
if (size == 0 || size > (1<<16)) return 0;
20+
std::string s(reinterpret_cast<const char*>(data), size);
21+
auto args = tokenize_ws(s);
22+
23+
std::vector<std::string> argv_strings; argv_strings.reserve(args.size()+1);
24+
argv_strings.emplace_back("prog"); for (auto& a: args) argv_strings.push_back(a);
25+
std::vector<char*> argv; for (auto& a: argv_strings) argv.push_back(a.data());
26+
int argc = static_cast<int>(argv.size());
27+
28+
CLI::App app("fuzz");
29+
int i=0, si=0; double d=0; bool b=false;
30+
std::vector<int> ints; std::vector<std::string> strs, sstrs;
31+
app.add_option("-i,--int", i);
32+
app.add_option("-d,--double", d);
33+
app.add_flag("-b,--bool", b);
34+
app.add_option("-n,--ints", ints)->take_all();
35+
app.add_option("-s,--str", strs)->take_all();
36+
app.allow_extras(true);
37+
auto sub = app.add_subcommand("sub", "subcommand");
38+
sub->add_option("--si", si);
39+
sub->add_option("--sstr", sstrs)->take_all();
40+
try { app.parse(argc, argv.data()); } catch (const CLI::ParseError&) {} catch (...) {}
41+
return 0;
42+
}

projects/cli11/project.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
homepage: https://github.com/CLIUtils/CLI11
2+
main_repo: https://github.com/CLIUtils/CLI11
3+
language: c++
4+
fuzzing_engines:
5+
- libfuzzer
6+
sanitizers:
7+
- address
8+
- undefined
9+
architectures:
10+
- x86_64

0 commit comments

Comments
 (0)