Fossil SCM

fossil-scm / test / corpus.tcl
Blame History Raw 181 lines
1
# The test corpus: inputs for "fossil test-fuzz" and for scenario.test,
2
# all kept in the one file corpus/corpus.pack. Many small files would
3
# each add a line to every check-in manifest of the project, so they are
4
# packed and unpacked into the test's own directory when a test runs. The
5
# pack is in a directory of its own because merge2.test runs every file
6
# directly in this directory through a three-way merge.
7
#
8
# tclsh corpus.tcl unpack corpus/corpus.pack DIR write DIR/TYPE/NAME
9
# tclsh corpus.tcl pack DIR corpus/corpus.pack pack DIR/TYPE/NAME
10
#
11
# To add an input: unpack, add the file, pack, and check that the pack
12
# changed only by the new record. A fuzzing engine such as AFL++ or
13
# libFuzzer can use an unpacked directory as its starting corpus, and
14
# "fossil test-fuzz DIR/TYPE" runs one directory through its target.
15
#
16
# The pack is a few lines that begin with "#", then one record per file:
17
#
18
# == TYPE/NAME LENGTH
19
# LENGTH bytes of content, then a newline
20
#
21
# Records are sorted by TYPE/NAME, so packing the same files gives the
22
# same pack.
23
#
24
# TYPE is the fuzz target the file is an input to, or "scenario".
25
#
26
# Each input is a case that its target must handle without a crash, a
27
# report from a run-time checker, or a failed property. When a fuzzer
28
# finds an input that breaks something, the minimized input is added
29
# once the defect is fixed, named for what it showed.
30
#
31
# The delta targets take a pair of files as one input: two bytes giving
32
# the size of the first file, most significant byte first, then the
33
# first file, then the second.
34
#
35
# delta-roundtrip: the pair is a source and a target. A delta is made
36
# from one to the other and must reproduce the target.
37
#
38
# delta-apply, delta-accept, delta-reject: the pair is a source and a
39
# delta. All three apply the delta and check the same properties.
40
# delta-accept also requires that the delta be accepted, and
41
# delta-reject that it be refused, so those inputs are examples with a
42
# known answer. The delta-accept inputs whose names begin with "ok-"
43
# hold deltas made by Fossil. Each delta-reject input is named for what
44
# is wrong with it. A fuzzing engine should be pointed at delta-apply,
45
# with all three as its starting corpus.
46
#
47
# manifest: each input is one structural artifact: a check-in, tag, wiki
48
# page, ticket, technote, attachment or forum post. The inputs whose
49
# names begin with "ok-" were made by Fossil. The rest are one of those
50
# with a single card changed, and a Z card that is still correct so that
51
# the parser reads as far as the change. Names give the cards of the
52
# artifact, the card that was changed, and how.
53
#
54
# manifest-body: each input is an artifact without its Z card. The
55
# target adds the right one, so every input reaches the parser proper.
56
# This is the corpus to give a fuzzing engine, which cannot produce a
57
# matching checksum by changing bytes. The inputs named with card
58
# letters only are the "ok-" inputs of manifest with the Z card removed.
59
# A name with a suffix after the letters is an input for one particular
60
# defect. The "fuzz-" inputs were found by libFuzzer and are kept for
61
# the branches of manifest.c they reach: a run from the other inputs was
62
# reduced with -set_cover_merge=1, and from the result a small set was
63
# chosen that still takes every branch outcome in manifest.c the whole
64
# set takes. Their names give the card letters that begin a line, then
65
# part of the SHA1 of the content.
66
#
67
# Every manifest and manifest-body input begins with two lines that
68
# start with "#". They are not part of the input: the target removes
69
# them. They are there because Fossil treats any file whose content is
70
# a well-formed artifact as a real one. Without them, a repository that
71
# received an unpacked input by sync, or was rebuilt while holding it,
72
# would show it as a check-in, wiki page or ticket of its own. A new
73
# input of either type must begin with the same two lines; fuzz.test
74
# checks that each one does, and that Fossil accepts neither the pack
75
# nor any unpacked input as an artifact as stored.
76
#
77
# xfer: each input is the body of one sync request as a client sends it
78
# to a server, not compressed. The target serves it from a small
79
# repository that is the same for every input; fuzz_xfer_setup() in
80
# src/fuzz.c says what it holds. The pull and push cards give that
81
# repository's project code, and the gimme cards and delta sources are
82
# its artifacts. An input is named for the cards it holds.
83
#
84
# scenario: these are not inputs to a fuzz target. Each is a random
85
# integration test of sync, run by scenario.test: a list of four-byte
86
# records, each one operation on one of three clones of a new
87
# repository: operation, clone, argument, option. See scenario.test for
88
# the operations. A sync can be told to lose its request, lose its
89
# reply or deliver half a reply. A sync, pull, push or clone that is
90
# not told to go wrong must deliver everything at once. The clones are
91
# synced at the end and must then be sound and identical. Each input
92
# takes a second or two, because every operation is a separate run of
93
# fossil. An input is named for what it tests.
94
#
95
96
# Write every record of the pack PACK as a file DIR/TYPE/NAME and return
97
# the list of TYPE directories made, sorted. A malformed pack is an
98
# error.
99
#
100
proc corpus_unpack {pack dir} {
101
set fd [open $pack rb]
102
set data [read $fd]
103
close $fd
104
set at 0
105
set end [string length $data]
106
# The comment lines at the top.
107
while {$at < $end && [string index $data $at] eq "#"} {
108
set nl [string first \n $data $at]
109
if {$nl < 0} { error "$pack: no newline after a comment line" }
110
set at [expr {$nl + 1}]
111
}
112
set types {}
113
while {$at < $end} {
114
set nl [string first \n $data $at]
115
if {$nl < 0} { error "$pack: no newline after a record header" }
116
set header [string range $data $at [expr {$nl - 1}]]
117
if {![regexp {^== ([a-z0-9-]+)/([A-Za-z0-9._+-]+) ([0-9]+)$} $header \
118
all type name len]
119
|| [string index $name 0] eq "."} {
120
error "$pack: malformed record header \"$header\""
121
}
122
set len [scan $len %d]
123
set from [expr {$nl + 1}]
124
set to [expr {$from + $len}]
125
if {$to >= $end || [string index $data $to] ne "\n"} {
126
error "$pack: record $type/$name is cut short"
127
}
128
set path [file join $dir $type $name]
129
if {[file exists $path]} { error "$pack: record $type/$name repeated" }
130
file mkdir [file join $dir $type]
131
set out [open $path wb]
132
puts -nonewline $out [string range $data $from [expr {$to - 1}]]
133
close $out
134
if {$type ni $types} { lappend types $type }
135
set at [expr {$to + 1}]
136
}
137
return [lsort $types]
138
}
139
140
# Pack every file DIR/TYPE/NAME into the pack PACK. Files directly in
141
# DIR are not packed.
142
#
143
proc corpus_pack {dir pack} {
144
set records {}
145
foreach typeDir [glob -nocomplain -types d [file join $dir *]] {
146
foreach f [glob -nocomplain -types f [file join $typeDir *]] {
147
lappend records [list [file tail $typeDir]/[file tail $f] $f]
148
}
149
}
150
set out [open $pack wb]
151
puts -nonewline $out "# Inputs for fuzz.test and scenario.test, packed by corpus.tcl.\n"
152
puts -nonewline $out "# See corpus.tcl for the format and for what each type holds.\n"
153
foreach r [lsort -index 0 $records] {
154
lassign $r key f
155
set fd [open $f rb]
156
set content [read $fd]
157
close $fd
158
puts -nonewline $out "== $key [string length $content]\n$content\n"
159
}
160
close $out
161
return [llength $records]
162
}
163
164
if {[info exists ::argv0]
165
&& [file normalize $::argv0] eq [file normalize [info script]]} {
166
switch -- [lindex $::argv 0] {
167
pack {
168
if {[llength $::argv] != 3} { puts stderr "usage: pack DIR PACK"; exit 2 }
169
puts "[corpus_pack [lindex $::argv 1] [lindex $::argv 2]] files packed"
170
}
171
unpack {
172
if {[llength $::argv] != 3} { puts stderr "usage: unpack PACK DIR"; exit 2 }
173
puts [corpus_unpack [lindex $::argv 1] [lindex $::argv 2]]
174
}
175
default {
176
puts stderr "usage: tclsh corpus.tcl pack DIR PACK | unpack PACK DIR"
177
exit 2
178
}
179
}
180
}
181

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button