● Zion Boggan
repos/Cyclops V2/targets/hunt_llamacpp.sh
zionboggan.com ↗
73 lines · bash
History for this file →
1
set -e
2
export CYCLOPS_HOME=/opt/cyclops-v2
3
PY=/opt/cyclops-venv/bin/python
4
T=/opt/cyclops-v2/targets/src/llama.cpp
5
H=/opt/cyclops-v2/oracle/harnesses
6
W=/opt/cyclops-v2/oracle/work/llamacpp
7
mkdir -p "$H" "$W" "$W/seeds" "$W/corpus"
8
 
9
echo "=== [1/5] BRAIN: joern dataflow on ggml/src/gguf.cpp ==="
10
( $PY /opt/cyclops-v2/brain/brain.py "github.com/ggml-org/llama.cpp" "ml-model-loaders" \
11
    "$T/ggml/src/gguf.cpp" "c" "fdc3db9" > "$W/brain.log" 2>&1 || true ) &
12
BRAIN_PID=$!
13
 
14
echo "=== [2/5] build libggml WITH AddressSanitizer (cmake) ==="
15
cd "$T"
16
cmake -B build-asan -DCMAKE_BUILD_TYPE=Debug -DBUILD_SHARED_LIBS=OFF \
17
  -DGGML_NATIVE=OFF -DGGML_BACKEND_DL=OFF -DLLAMA_BUILD_TESTS=OFF \
18
  -DLLAMA_BUILD_EXAMPLES=OFF -DLLAMA_BUILD_TOOLS=OFF -DLLAMA_BUILD_SERVER=OFF \
19
  -DCMAKE_C_FLAGS="-fsanitize=address,undefined -fno-omit-frame-pointer -g" \
20
  -DCMAKE_CXX_FLAGS="-fsanitize=address,undefined -fno-omit-frame-pointer -g" \
21
  > "$W/cmake.log" 2>&1
22
cmake --build build-asan --target ggml-base -j4 >> "$W/cmake.log" 2>&1 || \
23
  cmake --build build-asan --target ggml -j4 >> "$W/cmake.log" 2>&1
24
echo "built libs:"; find build-asan -name "libggml*.a" 2>/dev/null
25
 
26
echo "=== [3/5] generate a real valid GGUF seed ==="
27
$PY -m pip install --quiet gguf >/dev/null 2>&1 || true
28
$PY - <<'PYEOF'
29
try:
30
    import gguf, numpy as np
31
    w = gguf.GGUFWriter("/opt/cyclops-v2/oracle/work/llamacpp/seeds/seed.gguf", "fuzz")
32
    w.add_uint32("answer", 42); w.add_string("name", "x"); w.add_array("arr", [1,2,3])
33
    w.add_tensor("t0", np.zeros((4,4), dtype=np.float32))
34
    w.write_header_to_file(); w.write_kv_data_to_file(); w.write_tensors_to_file(); w.close()
35
    print("seed.gguf written")
36
except Exception as e:
37
    import struct
38
    b=b"GGUF"+struct.pack("<I",3)+struct.pack("<Q",0)+struct.pack("<Q",1)
39
    b+=struct.pack("<Q",1)+b"a"+struct.pack("<I",4)+struct.pack("<I",7)
40
    open("/opt/cyclops-v2/oracle/work/llamacpp/seeds/seed.gguf","wb").write(b)
41
    print("fallback seed written:", e)
42
PYEOF
43
cp "$W/seeds/"* "$W/corpus/" 2>/dev/null || true
44
 
45
echo "=== [4/5] write + compile libFuzzer harness (in-memory via fmemopen) ==="
46
cat > "$H/llamacpp_gguf_harness.cpp" <<'EOF'
47
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
48
    FILE *f = fmemopen((void*)data, size, "rb");
49
    if (!f) return 0;
50
    struct gguf_init_params p; p.no_alloc = true; p.ctx = nullptr;
51
    struct gguf_context *ctx = gguf_init_from_file_ptr(f, p);
52
    if (ctx) gguf_free(ctx);
53
    fclose(f);
54
    return 0;
55
}
56
EOF
57
LIBS=$(find "$T/build-asan" -name "libggml*.a" 2>/dev/null | tr '\n' ' ')
58
echo "linking: $LIBS"
59
clang++ -g -O1 -fsanitize=fuzzer,address,undefined -fno-omit-frame-pointer \
60
  "$H/llamacpp_gguf_harness.cpp" -I"$T/ggml/include" \
61
  $LIBS -lm -lpthread -fopenmp -lgomp -o "$W/fuzz_llgguf" 2>"$W/compile.log" \
62
  && echo "HARNESS COMPILED: $(ls -la $W/fuzz_llgguf | awk '{print $5}') bytes" \
63
  || { echo "COMPILE FAILED:"; tail -15 "$W/compile.log"; exit 1; }
64
 
65
echo "=== [5/5] launch background fuzz campaign (2h) ==="
66
wait $BRAIN_PID 2>/dev/null || true
67
echo "brain:"; grep -E "HYPOTHESES|paths" "$W/brain.log" 2>/dev/null | tail -3
68
nohup "$W/fuzz_llgguf" -max_total_time=7200 -rss_limit_mb=4096 -timeout=25 \
69
  -print_final_stats=1 -artifact_prefix="$W/crash-" "$W/corpus" "$W/seeds" \
70
  > "$W/fuzz.log" 2>&1 &
71
echo "FUZZ_PID=$!"
72
sleep 12
73
echo "=== early fuzz status ==="; tail -6 "$W/fuzz.log" 2>/dev/null