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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
|
package eval
// fuzzops.go — opcode definitions and disassembler for FuzzNixEvalOps.
//
// The opcode format encodes Nix expressions as a byte sequence where
// every byte maps to a semantic decision (opcode, variable index, literal
// value). This bypasses the lexer and parser entirely so the fuzzer's
// coverage signal comes purely from eval code.
//
// See fuzzops_test.go for the AST builder and fuzz target.
import (
"encoding/binary"
"fmt"
"math"
)
// FuzzOp is a single opcode byte in the fuzzer's expression language.
// Values >= NumFuzzOps are treated as unknown and fall back to a literal 0,
// so adding new opcodes at the end never remaps existing corpus bytes.
type FuzzOp uint8
const (
// Literals
FopInt FuzzOp = iota // 8 bytes big-endian int64
FopFloat // 8 bytes big-endian uint64 → math.Float64frombits
FopStr // 1 byte: length N (0–255), then N bytes
FopTrue // -
FopFalse // -
FopNull // -
FopList // 1 byte: count N (1–8), then N sub-exprs
FopAttrs // 1 byte: count N (1–4), then N×(name byte + sub-expr)
FopRecAttrs // same as FopAttrs but recursive=true
// Variables
FopVar // 1 byte: name index 0–255
FopBuiltin // 1 byte: index into DefaultBuiltinNames
// Binary operators
FopAdd // 2 sub-exprs → ExprConcatStrings
FopSub // 2 sub-exprs → __sub call
FopMul // 2 sub-exprs → __mul call
FopDiv // 2 sub-exprs → __div call
FopEq // 2 sub-exprs → ExprOpEq
FopNEq // 2 sub-exprs → ExprOpNEq
FopLt // 2 sub-exprs → __lessThan call
FopAnd // 2 sub-exprs → ExprOpAnd
FopOr // 2 sub-exprs → ExprOpOr
FopImpl // 2 sub-exprs → ExprOpImpl
FopUpdate // 2 sub-exprs → ExprOpUpdate
FopConcat // 2 sub-exprs → ExprOpConcatLists
// Unary
FopNot // 1 sub-expr → ExprOpNot
FopNegate // 1 sub-expr → __sub 0 e
// Attribute operations
FopSelect // 1 byte: name index, 1 sub-expr
FopSelectOr // 1 byte: name index, 2 sub-exprs (expr, default)
FopHasAttr // 1 byte: name index, 1 sub-expr
// Scoping
FopLambda // 1 byte: arg name index, 1 sub-expr body
FopCall // 2 sub-exprs (fun, arg)
FopLet // 1 byte: count N (1–4), N name bytes, N binding sub-exprs, 1 body
FopWith // 2 sub-exprs (attrs, body)
FopIf // 3 sub-exprs (cond, then, else)
FopAssert // 2 sub-exprs (cond, body)
// Builtin applications (avoids needing FopBuiltin + FopCall)
FopCallB1 // 1 byte: builtin index, 1 arg sub-expr
FopCallB2 // 1 byte: builtin index, 2 arg sub-exprs
FopCallB3 // 1 byte: builtin index, 3 arg sub-exprs
FopStrInterp // 1 byte: count N (2–5), then N sub-exprs → interpolated string
// Formals patterns (appended; existing corpus bytes unaffected).
//
// FopFormals:
// 1 byte: N = (raw%4)+1 formals
// N bytes: formal name indices
// 1 byte: default bitfield — bit i set → formal i has a default sub-expr
// [sub-exprs: one per formal with bit set, in order]
// 1 byte: bit 0 = ellipsis
// sub-expr: body (scope: N formals at displ 0..N-1)
FopFormals // { _a, _b ? def, ... }: body
// FopFormalsAt: same as FopFormals but with an additional @-binding.
// 1 byte: @ name index (pushed at displ N, after the formals)
// … then same layout as FopFormals …
FopFormalsAt // args@{ _a, _b }: body
// Extended literals (appended after original opcodes; existing corpus bytes
// below NumFuzzOps are unaffected by the sparse-dispatch scheme above).
FopPath // 1 byte: length N (0–255), then N bytes → path segment /…
NumFuzzOps // must be last
)
var fopNames = [NumFuzzOps]string{
FopInt: "int", FopFloat: "float", FopStr: "str", FopTrue: "true",
FopFalse: "false", FopNull: "null", FopList: "list", FopAttrs: "attrs",
FopRecAttrs: "rec-attrs", FopVar: "var", FopBuiltin: "builtin",
FopAdd: "+", FopSub: "-", FopMul: "*", FopDiv: "/",
FopEq: "==", FopNEq: "!=", FopLt: "<", FopAnd: "&&", FopOr: "||",
FopImpl: "->", FopUpdate: "//", FopConcat: "++",
FopNot: "!", FopNegate: "negate",
FopSelect: ".", FopSelectOr: ".or", FopHasAttr: "?",
FopLambda: "lambda", FopCall: "call", FopLet: "let", FopWith: "with",
FopIf: "if", FopAssert: "assert",
FopCallB1: "callB1", FopCallB2: "callB2", FopCallB3: "callB3",
FopStrInterp: "str-interp",
FopFormals: "formals", FopFormalsAt: "formals@",
FopPath: "path",
}
// DisassembleFuzzOps decodes a FuzzNixEvalOps corpus entry into a
// human-readable Nix-like expression string. Useful for inspecting
// what expressions the fuzzer has discovered:
//
// go run ./cmd/ast-compare corpus
func DisassembleFuzzOps(data []byte) string {
d := &fuzzDisasm{data: data}
s := d.dis(20)
if d.pos < len(data) {
s += fmt.Sprintf(" [+%d bytes]", len(data)-d.pos)
}
return s
}
type fuzzDisasm struct {
data []byte
pos int
}
func (d *fuzzDisasm) next() byte {
if d.pos >= len(d.data) {
return 0
}
v := d.data[d.pos]
d.pos++
return v
}
func (d *fuzzDisasm) next8() uint64 {
var buf [8]byte
for i := range buf {
buf[i] = d.next()
}
return binary.BigEndian.Uint64(buf[:])
}
func (d *fuzzDisasm) dis(depth int) string {
if depth <= 0 || d.pos >= len(d.data) {
return "0"
}
raw := d.next()
op := FuzzOp(raw)
if op >= NumFuzzOps {
return "0" // unknown opcode → fallback literal
}
name := fopNames[op]
if name == "" {
name = fmt.Sprintf("op%d", op)
}
switch op {
case FopInt:
return fmt.Sprintf("%d", int64(d.next8()))
case FopFloat:
return fmt.Sprintf("%g", math.Float64frombits(d.next8()))
case FopStr:
n := int(d.next())
s := make([]byte, n)
for i := range s {
s[i] = d.next()
}
return fmt.Sprintf("%q", string(s))
case FopTrue:
return "true"
case FopFalse:
return "false"
case FopNull:
return "null"
case FopList:
n := int(d.next()%8) + 1
parts := make([]string, n)
for i := range parts {
parts[i] = d.dis(depth - 1)
}
return fmt.Sprintf("[ %s ]", fuzzJoinSp(parts))
case FopAttrs:
return d.disAttrs(depth, false)
case FopRecAttrs:
return d.disAttrs(depth, true)
case FopVar:
return fmt.Sprintf("_%d", d.next())
case FopBuiltin:
idx := int(d.next()) % len(DefaultBuiltinNames)
return DefaultBuiltinNames[idx]
case FopAdd:
l, r := d.dis(depth-1), d.dis(depth-1)
return fmt.Sprintf("(%s + %s)", l, r)
case FopSub:
l, r := d.dis(depth-1), d.dis(depth-1)
return fmt.Sprintf("(%s - %s)", l, r)
case FopMul:
l, r := d.dis(depth-1), d.dis(depth-1)
return fmt.Sprintf("(%s * %s)", l, r)
case FopDiv:
l, r := d.dis(depth-1), d.dis(depth-1)
return fmt.Sprintf("(%s / %s)", l, r)
case FopEq:
l, r := d.dis(depth-1), d.dis(depth-1)
return fmt.Sprintf("(%s == %s)", l, r)
case FopNEq:
l, r := d.dis(depth-1), d.dis(depth-1)
return fmt.Sprintf("(%s != %s)", l, r)
case FopLt:
l, r := d.dis(depth-1), d.dis(depth-1)
return fmt.Sprintf("(%s < %s)", l, r)
case FopAnd:
l, r := d.dis(depth-1), d.dis(depth-1)
return fmt.Sprintf("(%s && %s)", l, r)
case FopOr:
l, r := d.dis(depth-1), d.dis(depth-1)
return fmt.Sprintf("(%s || %s)", l, r)
case FopImpl:
l, r := d.dis(depth-1), d.dis(depth-1)
return fmt.Sprintf("(%s -> %s)", l, r)
case FopUpdate:
l, r := d.dis(depth-1), d.dis(depth-1)
return fmt.Sprintf("(%s // %s)", l, r)
case FopConcat:
l, r := d.dis(depth-1), d.dis(depth-1)
return fmt.Sprintf("(%s ++ %s)", l, r)
case FopNot:
return fmt.Sprintf("(!%s)", d.dis(depth-1))
case FopNegate:
return fmt.Sprintf("(-%s)", d.dis(depth-1))
case FopSelect:
nameIdx := d.next()
e := d.dis(depth - 1)
return fmt.Sprintf("%s._%d", e, nameIdx)
case FopSelectOr:
nameIdx := d.next()
e, def := d.dis(depth-1), d.dis(depth-1)
return fmt.Sprintf("(%s._%d or %s)", e, nameIdx, def)
case FopHasAttr:
nameIdx := d.next()
e := d.dis(depth - 1)
return fmt.Sprintf("(%s ? _%d)", e, nameIdx)
case FopLambda:
nameIdx := d.next()
body := d.dis(depth - 1)
return fmt.Sprintf("(_%d: %s)", nameIdx, body)
case FopCall:
fun, arg := d.dis(depth-1), d.dis(depth-1)
return fmt.Sprintf("(%s %s)", fun, arg)
case FopLet:
n := int(d.next()%4) + 1
names := make([]byte, n)
for i := range names {
names[i] = d.next()
}
binds := make([]string, n)
for i := range binds {
binds[i] = fmt.Sprintf("_%d = %s", names[i], d.dis(depth-1))
}
body := d.dis(depth - 1)
return fmt.Sprintf("(let %s; in %s)", fuzzJoinSemi(binds), body)
case FopWith:
attrs, body := d.dis(depth-1), d.dis(depth-1)
return fmt.Sprintf("(with %s; %s)", attrs, body)
case FopIf:
cond, then, els := d.dis(depth-1), d.dis(depth-1), d.dis(depth-1)
return fmt.Sprintf("(if %s then %s else %s)", cond, then, els)
case FopAssert:
cond, body := d.dis(depth-1), d.dis(depth-1)
return fmt.Sprintf("(assert %s; %s)", cond, body)
case FopCallB1:
idx := int(d.next()) % len(DefaultBuiltinNames)
a := d.dis(depth - 1)
return fmt.Sprintf("(%s %s)", DefaultBuiltinNames[idx], a)
case FopCallB2:
idx := int(d.next()) % len(DefaultBuiltinNames)
a, b := d.dis(depth-1), d.dis(depth-1)
return fmt.Sprintf("(%s %s %s)", DefaultBuiltinNames[idx], a, b)
case FopCallB3:
idx := int(d.next()) % len(DefaultBuiltinNames)
a, b, c := d.dis(depth-1), d.dis(depth-1), d.dis(depth-1)
return fmt.Sprintf("(%s %s %s %s)", DefaultBuiltinNames[idx], a, b, c)
case FopStrInterp:
n := int(d.next()%4) + 2
parts := make([]string, n)
for i := range parts {
parts[i] = d.dis(depth - 1)
}
return fmt.Sprintf("\"${%s}\"", fuzzJoinDollar(parts))
case FopFormals:
return d.disFormals(depth, false, 0)
case FopFormalsAt:
atIdx := d.next()
return d.disFormals(depth, true, atIdx)
case FopPath:
n := int(d.next() % 32)
seg := make([]byte, n)
for i := range seg {
b := d.next()
// Keep only printable ASCII, no slashes or NUL.
if b < 0x20 || b == '/' || b == 0x7f {
b = 'x'
}
seg[i] = b
}
return fmt.Sprintf("/%s", string(seg))
default:
return "0"
}
}
func (d *fuzzDisasm) disFormals(depth int, hasAt bool, atIdx byte) string {
n := int(d.next()%4) + 1
names := make([]byte, n)
for i := range names {
names[i] = d.next()
}
defBits := d.next()
parts := make([]string, n)
for i := range parts {
if defBits&(1<<uint(i)) != 0 {
parts[i] = fmt.Sprintf("_%d ? %s", names[i], d.dis(depth-1))
} else {
parts[i] = fmt.Sprintf("_%d", names[i])
}
}
ellipsis := d.next()&1 != 0
if ellipsis {
parts = append(parts, "...")
}
body := d.dis(depth - 1)
pat := fmt.Sprintf("{ %s }", fuzzJoinComma(parts))
if hasAt {
return fmt.Sprintf("(_%d@%s: %s)", atIdx, pat, body)
}
return fmt.Sprintf("(%s: %s)", pat, body)
}
func (d *fuzzDisasm) disAttrs(depth int, rec bool) string {
n := int(d.next()%4) + 1
names := make([]byte, n)
for i := range names {
names[i] = d.next()
}
binds := make([]string, n)
for i := range binds {
binds[i] = fmt.Sprintf("_%d = %s", names[i], d.dis(depth-1))
}
prefix := ""
if rec {
prefix = "rec "
}
return fmt.Sprintf("%s{ %s; }", prefix, fuzzJoinSemi(binds))
}
func fuzzJoinSp(ss []string) string {
r := ""
for i, s := range ss {
if i > 0 {
r += " "
}
r += s
}
return r
}
func fuzzJoinComma(ss []string) string {
r := ""
for i, s := range ss {
if i > 0 {
r += ", "
}
r += s
}
return r
}
func fuzzJoinSemi(ss []string) string {
r := ""
for i, s := range ss {
if i > 0 {
r += "; "
}
r += s
}
return r
}
func fuzzJoinDollar(ss []string) string {
r := ""
for i, s := range ss {
if i > 0 {
r += "}${"
}
r += s
}
return r
}
|