-
Notifications
You must be signed in to change notification settings - Fork 458
Expand file tree
/
Copy pathfuzz_native_test.go
More file actions
222 lines (201 loc) · 6.26 KB
/
Copy pathfuzz_native_test.go
File metadata and controls
222 lines (201 loc) · 6.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Native Go fuzz wrappers around the OSS-Fuzz targets in fuzz.go so they can
// be driven by `go test -fuzz` locally. Panics are recovered and recorded
// (deduplicated by panic msg + innermost parser frame) under
// fuzz_results/crashes/, so a single fuzz run keeps exploring after the first
// crash instead of stopping. See run_fuzz_campaign.sh for batch usage.
package jsonparser
import (
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"os"
"path/filepath"
"runtime/debug"
"strings"
"testing"
)
var nativeFuzzSeeds = []string{
"{}",
"[]",
`{"test":1}`,
`{"test":"hello","other":2}`,
`{"a":{"test":[1,2,3]},"test":null}`,
`{"test":{"nested":{"test":true}}}`,
`[1,2,3,4]`,
`{"test":}`,
`{"x":"test"}`,
`{"test":"\""}`,
`{"test":1.5e10}`,
`{"test":-0.0}`,
`{"test":true,"a":false,"b":null}`,
`,{"test":1{}`,
`,""{"test":0}`,
`{"name":"x","order":1,"nested":{"a":1,"b":2,"nested3":{"b":3}},"nested2":{"a":4},"arr":[{"b":5},{"b":6}],"arrInt":[0,1,2,3,4,5,6]}`,
"",
// --- Deeply-nested seeds (closes Dimension 5 for the OSS-Fuzz harness) ---
// The previous seed set had ZERO deeply-nested inputs, so the native
// fuzz harnesses never exercised stack-overflow / runaway-recursion
// paths on the OSS-Fuzz surface. These seeds target that blind spot.
strings.Repeat("[", 10000), // unclosed deep array
strings.Repeat("[", 1)+strings.Repeat("]", 1), // minimal closed array
strings.Repeat("[", 1000) + "1" + strings.Repeat("]", 1000), // closed deep array
strings.Repeat("{", 100) + `"a":1` + strings.Repeat("}", 100), // closed deep object
strings.Repeat("[", 100000), // very deep unclosed
`{"a":"` + strings.Repeat("x", 100000) + `"}`, // very long string value
`{"\u`, // truncated unicode escape
}
func addSeeds(f *testing.F) {
for _, s := range nativeFuzzSeeds {
f.Add([]byte(s))
}
}
var fuzzCrashDir = func() string {
d := os.Getenv("FUZZ_CRASH_DIR")
if d == "" {
d = "fuzz_results/crashes"
}
_ = os.MkdirAll(d, 0o755)
return d
}()
func crashSignature(panicMsg string, stack []byte) string {
var key strings.Builder
key.WriteString(panicMsg)
for _, line := range strings.Split(string(stack), "\n") {
if strings.Contains(line, "github.com/buger/jsonparser/") &&
!strings.Contains(line, "fuzz_native_test.go") &&
!strings.Contains(line, "/fuzz.go:") {
key.WriteString("|")
key.WriteString(strings.TrimSpace(line))
break
}
}
sum := sha256.Sum256([]byte(key.String()))
return hex.EncodeToString(sum[:])[:12]
}
func recordCrash(target string, panicVal interface{}, stack, input []byte) {
panicMsg := fmt.Sprintf("%v", panicVal)
sig := crashSignature(panicMsg, stack)
fname := filepath.Join(fuzzCrashDir, target+"_"+sig+".json")
f, err := os.OpenFile(fname, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o644)
if err != nil {
return
}
defer f.Close()
inputCopy := make([]byte, len(input))
copy(inputCopy, input)
_ = json.NewEncoder(f).Encode(map[string]interface{}{
"target": target,
"sig": sig,
"panic": panicMsg,
"stack": string(stack),
"input_b64": base64.StdEncoding.EncodeToString(inputCopy),
})
}
func runWithCapture(target string, data []byte, fn func([]byte)) {
defer func() {
if r := recover(); r != nil {
recordCrash(target, r, debug.Stack(), data)
}
}()
fn(data)
}
// Verifies: SYS-REQ-035
func FuzzDeleteNative(f *testing.F) {
addSeeds(f)
f.Fuzz(func(t *testing.T, data []byte) {
runWithCapture("FuzzDeleteNative", data, func(d []byte) { _ = FuzzDelete(d) })
})
}
// Verifies: SYS-REQ-014
func FuzzParseStringNative(f *testing.F) {
addSeeds(f)
f.Fuzz(func(t *testing.T, data []byte) {
runWithCapture("FuzzParseStringNative", data, func(d []byte) { _ = FuzzParseString(d) })
})
}
// Verifies: SYS-REQ-008
func FuzzEachKeyNative(f *testing.F) {
addSeeds(f)
f.Fuzz(func(t *testing.T, data []byte) {
runWithCapture("FuzzEachKeyNative", data, func(d []byte) { _ = FuzzEachKey(d) })
})
}
// Verifies: SYS-REQ-009
func FuzzSetNative(f *testing.F) {
addSeeds(f)
f.Fuzz(func(t *testing.T, data []byte) {
runWithCapture("FuzzSetNative", data, func(d []byte) { _ = FuzzSet(d) })
})
}
// Verifies: SYS-REQ-007
func FuzzObjectEachNative(f *testing.F) {
addSeeds(f)
f.Fuzz(func(t *testing.T, data []byte) {
runWithCapture("FuzzObjectEachNative", data, func(d []byte) { _ = FuzzObjectEach(d) })
})
}
// Verifies: SYS-REQ-013
func FuzzParseFloatNative(f *testing.F) {
addSeeds(f)
f.Fuzz(func(t *testing.T, data []byte) {
runWithCapture("FuzzParseFloatNative", data, func(d []byte) { _ = FuzzParseFloat(d) })
})
}
// Verifies: SYS-REQ-015
func FuzzParseIntNative(f *testing.F) {
addSeeds(f)
f.Fuzz(func(t *testing.T, data []byte) {
runWithCapture("FuzzParseIntNative", data, func(d []byte) { _ = FuzzParseInt(d) })
})
}
// Verifies: SYS-REQ-012
func FuzzParseBoolNative(f *testing.F) {
addSeeds(f)
f.Fuzz(func(t *testing.T, data []byte) {
runWithCapture("FuzzParseBoolNative", data, func(d []byte) { _ = FuzzParseBool(d) })
})
}
// Verifies: SYS-REQ-001
func FuzzTokenStartNative(f *testing.F) {
addSeeds(f)
f.Fuzz(func(t *testing.T, data []byte) {
runWithCapture("FuzzTokenStartNative", data, func(d []byte) { _ = FuzzTokenStart(d) })
})
}
// Verifies: SYS-REQ-002
func FuzzGetStringNative(f *testing.F) {
addSeeds(f)
f.Fuzz(func(t *testing.T, data []byte) {
runWithCapture("FuzzGetStringNative", data, func(d []byte) { _ = FuzzGetString(d) })
})
}
// Verifies: SYS-REQ-004
func FuzzGetFloatNative(f *testing.F) {
addSeeds(f)
f.Fuzz(func(t *testing.T, data []byte) {
runWithCapture("FuzzGetFloatNative", data, func(d []byte) { _ = FuzzGetFloat(d) })
})
}
// Verifies: SYS-REQ-003
func FuzzGetIntNative(f *testing.F) {
addSeeds(f)
f.Fuzz(func(t *testing.T, data []byte) {
runWithCapture("FuzzGetIntNative", data, func(d []byte) { _ = FuzzGetInt(d) })
})
}
// Verifies: SYS-REQ-005
func FuzzGetBooleanNative(f *testing.F) {
addSeeds(f)
f.Fuzz(func(t *testing.T, data []byte) {
runWithCapture("FuzzGetBooleanNative", data, func(d []byte) { _ = FuzzGetBoolean(d) })
})
}
// Verifies: SYS-REQ-011
func FuzzGetUnsafeStringNative(f *testing.F) {
addSeeds(f)
f.Fuzz(func(t *testing.T, data []byte) {
runWithCapture("FuzzGetUnsafeStringNative", data, func(d []byte) { _ = FuzzGetUnsafeString(d) })
})
}