-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathphp_fuzzing_seed.php
More file actions
312 lines (261 loc) · 8.66 KB
/
php_fuzzing_seed.php
File metadata and controls
312 lines (261 loc) · 8.66 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
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
#!/usr/bin/env php
<?php
/**
* seed_php_polyglot.php
* Deliberately dense, syntax-heavy PHP 8.1+ script for parser / compiler fuzzing.
*
* Exercises:
* - declare(strict_types), namespaces, use, group use
* - attributes, enums, traits, interfaces, anonymous classes
* - union / intersection types, nullable, generics-ish annotations
* - closures, arrow functions, variadics, references
* - match(), named arguments, unpack, generators, yield from
* - heredoc/nowdoc, magic constants, error control, @, try/catch/finally
* - static, late static binding, magic methods, ArrayAccess, IteratorAggregate
*/
declare(strict_types=1);
namespace PolySeed\Root {
use ArrayAccess;
use IteratorAggregate;
use Traversable;
use stdClass;
use PolySeed\Root\Sub\{InnerClass as RenamedInner, InnerTrait};
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)]
class DemoAttribute {
public function __construct(
public string $name,
public int $level = 0,
) {}
}
enum Status: string {
case Pending = 'pending';
case Active = 'active';
case Done = 'done';
public function isFinal(): bool {
return $this === self::Done;
}
}
interface Identifiable {
public function getId(): string;
}
interface Repository extends Identifiable {
/** @return list<object> */
public function findAll(): array;
}
trait LoggingTrait {
protected function log(string $msg): void {
// runtime irrelevant; presence exercises trait + visibility
$line = __LINE__;
$file = __FILE__;
$ctx = sprintf('[%s:%d] %s', $file, $line, $msg);
// suppress warnings via @, exercises error control operator
@file_put_contents('php://temp', $ctx . PHP_EOL, FILE_APPEND);
}
}
#[DemoAttribute('MainRepo', level: 3)]
class InMemoryRepository implements Repository, ArrayAccess, IteratorAggregate {
use LoggingTrait;
use InnerTrait;
private string $id;
/** @var array<string, object> */
private array $items = [];
public function __construct(?string $id = null) {
$this->id = $id ?? 'repo-' . uniqid('', true);
}
public function getId(): string {
return $this->id;
}
public function add(string $key, object $value): void {
$this->items[$key] = $value;
$this->log("added:$key");
}
public function findAll(): array {
return array_values($this->items);
}
// ArrayAccess
public function offsetExists(mixed $offset): bool {
return array_key_exists((string)$offset, $this->items);
}
public function offsetGet(mixed $offset): mixed {
return $this->items[(string)$offset] ?? null;
}
public function offsetSet(mixed $offset, mixed $value): void {
if (!\is_object($value)) {
throw new \InvalidArgumentException('value must be object');
}
$key = $offset === null ? (string)count($this->items) : (string)$offset;
$this->items[$key] = $value;
}
public function offsetUnset(mixed $offset): void {
unset($this->items[(string)$offset]);
}
// IteratorAggregate
public function getIterator(): Traversable {
return new \ArrayIterator($this->items);
}
// Magic methods
public function __toString(): string {
return 'InMemoryRepository(' . $this->id . ')';
}
public function __invoke(mixed ...$args): int {
return count($args);
}
public function __clone() {
$this->id .= '-clone';
}
}
namespace Sub {
trait InnerTrait {
protected function innerMark(): string {
return 'inner:' . static::class;
}
}
class InnerClass {
public function foo(): string {
return __METHOD__;
}
}
}
}
namespace PolySeed\Root\Util {
use PolySeed\Root\Status;
// Simple generic-ish container with union & nullable types
class Box {
public function __construct(
private int|string|null $value,
) {}
public function get(): int|string|null {
return $this->value;
}
public function map(callable $fn): static {
$this->value = $fn($this->value);
return $this;
}
public function statusFromValue(): Status {
return match (true) {
$this->value === null => Status::Pending,
\is_string($this->value) => Status::Active,
\is_int($this->value) && $this->value > 0 => Status::Done,
default => Status::Pending,
};
}
}
// Generator + yield from
function genRange(int $start, int $end): \Generator {
for ($i = $start; $i <= $end; $i++) {
yield $i => $i * $i;
}
}
function genWrapper(int $n): \Generator {
if ($n <= 0) {
return;
}
yield from genRange(1, $n);
}
// Arrow function, variadics, unpack
$arrowSum = fn (int ...$xs): int => array_sum($xs);
function apply(callable $f, array $args): mixed {
return $f(...$args);
}
// Heredoc / nowdoc
$heredoc = <<<TXT
heredoc with interpolation: status classes
Current enum name: {Status::class}
TXT;
$nowdoc = <<<'TXT'
nowdoc without interpolation: ${nope}
TXT;
}
namespace {
use PolySeed\Root\InMemoryRepository;
use PolySeed\Root\Sub\InnerClass;
use PolySeed\Root\Util\Box;
use PolySeed\Root\Util as Util;
// Reference variables
$x = 10;
$y =& $x;
$y += 5;
// Anonymous class exercising implements/extends
$anon = new class('anon-1') extends InMemoryRepository {
public function __construct(string $id) {
parent::__construct($id);
}
};
$repo = new InMemoryRepository();
$repo->add('a', (object)['k' => 1]);
$repo['b'] = new stdClass();
$all = $repo->findAll();
$iter = [];
foreach ($repo as $k => $v) {
$iter[$k] = $v;
}
// Generators
$genValues = [];
foreach (Util\genWrapper(5) as $k => $v) {
$genValues[$k] = $v;
}
// Box + match
$boxInt = new Box(5);
$boxString = new Box('hi');
$boxNull = new Box(null);
$statuses = [
$boxInt->statusFromValue(),
$boxString->statusFromValue(),
$boxNull->statusFromValue(),
];
// Closures capturing vars
$captured = function () use (&$x, $statuses): array {
$x++;
return [$x, array_map(static fn($s) => $s->value, $statuses)];
};
$captureResult = $captured();
// try / catch / finally + multi-catch
try {
if (rand(0, 1) === 1) {
throw new \RuntimeException('rt');
} else {
throw new \LogicException('lg');
}
} catch (\RuntimeException|\LogicException $e) {
$errClass = $e::class;
} finally {
$finallySeen = true;
}
// Calling __invoke via variable function call
$invokeCount = $repo(1, 2, 3);
// Anonymous function with attribute & named args (PHP 8+)
#[PolySeed\Root\DemoAttribute('fn', level: 1)]
$named = function (int $a, int $b, int $c = 0): int {
return $a + $b + $c;
};
$namedResult = $named(a: 1, b: 2, c: 3);
// Use InnerClass aliased via namespace import
$inner = new InnerClass();
$innerFoo = $inner->foo();
// Data structure snapshot to keep the compiler generating types/code
$snapshot = [
'file' => __FILE__,
'line' => __LINE__,
'dir' => __DIR__,
'x' => $x,
'y' => $y,
'repo' => (string)$repo,
'anon' => (string)$anon,
'all' => $all,
'iter' => $iter,
'gen' => $genValues,
'statuses' => array_map(static fn($s) => $s->value, $statuses),
'capture' => $captureResult,
'errClass' => $errClass ?? null,
'finallySeen' => $finallySeen ?? false,
'invokeCount' => $invokeCount ?? null,
'namedResult' => $namedResult,
'innerFoo' => $innerFoo,
'heredocLen' => strlen(Util::$heredoc ?? ''),
'nowdocLen' => strlen(Util::$nowdoc ?? ''),
];
// No output; for fuzzing we care only that PHP parses / compiles this.
if (false) {
var_dump($snapshot);
}
}