Skip to content

Commit

Permalink
Fix phar instrumentation
Browse files Browse the repository at this point in the history
And check for dependencies in example targets.
  • Loading branch information
nikic committed Dec 26, 2019
1 parent c911e2a commit 5105965
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 12 deletions.
8 changes: 7 additions & 1 deletion example/target_css_parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

/** @var PhpFuzzer\Fuzzer $fuzzer */

require __DIR__ . '/PHP-CSS-Parser/vendor/autoload.php';
$autoload = __DIR__ . '/PHP-CSS-Parser/vendor/autoload.php';
if (!file_exists($autoload)) {
echo "Cannot find PHP-CSS-Parser installation in " . __DIR__ . "/PHP-CSS_Parser\n";
exit(1);
}

require $autoload;

$fuzzer->setTarget(function(string $input) {
$parser = new Sabberworm\CSS\Parser($input);
Expand Down
21 changes: 16 additions & 5 deletions example/target_php_parser.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
<?php declare(strict_types=1);

// This only works with php-fuzzer.phar, which uses a prefixed version of php-parser!

/** @var PhpFuzzer\Fuzzer $fuzzer */

$fuzzer->setMaxLen(1024);
$fuzzer->addDictionary(__DIR__ . '/php.dict');
if (class_exists(PhpParser\Parser\Php7::class)) {
echo "The PHP-Parser target can only be used with php-fuzzer.phar,\n";
echo "otherwise there is a conflict with php-fuzzer's own use of PHP-Parser.\n";
exit(1);
}

$autoload = __DIR__ . '/PHP-Parser/vendor/autoload.php';
if (!file_exists($autoload)) {
echo "Cannot find PHP-Parser installation in " . __DIR__ . "/PHP-Parser\n";
exit(1);
}

require $autoload;

require __DIR__ . '/../../PHP-Parser/vendor/autoload.php';
$parser = new PhpParser\Parser\Php7(new PhpParser\Lexer);
$prettyPrinter = new PhpParser\PrettyPrinter\Standard();

$fuzzer->setTarget(function(string $input) use($parser, $prettyPrinter) {
$stmts = $parser->parse($input);
$prettyPrinter->prettyPrintFile($stmts);
});

$fuzzer->setMaxLen(1024);
$fuzzer->addDictionary(__DIR__ . '/php.dict');
16 changes: 10 additions & 6 deletions example/target_tolerant_php_parser.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
<?php declare(strict_types=1);

// Using tolerant-php-parser here, because the instrumentation uses php-parser,
// so we can't easily fuzz php-parser itself.

/** @var PhpFuzzer\Fuzzer $fuzzer */

$fuzzer->setMaxLen(1024);
$fuzzer->addDictionary(__DIR__ . '/php.dict');
$autoload = __DIR__ . '/tolerant-php-parser/vendor/autoload.php';
if (!file_exists($autoload)) {
echo "Cannot find tolerant-php-parser installation in " . __DIR__ . "/tolerant-php-parser\n";
exit(1);
}

require $autoload;

require __DIR__ . '/../../tolerant-php-parser/vendor/autoload.php';
$parser = new Microsoft\PhpParser\Parser();

$fuzzer->setTarget(function(string $input) use($parser) {
$parser->parseSourceFile($input);
});

$fuzzer->setMaxLen(1024);
$fuzzer->addDictionary(__DIR__ . '/php.dict');
6 changes: 6 additions & 0 deletions src/Fuzzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ public function __construct() {
// Instrument everything apart from our src/ directory.
$this->interceptor = new Interceptor();
$this->interceptor->addWhiteList('');
var_dump(__DIR__);
$this->interceptor->addBlackList(__DIR__);
// Don't intercept phar://.
// TODO: This would not be necessary if the filtering in interceptor actually worked.
(function() {
$this->protocols = ['file'];
})->call($this->interceptor);
$this->interceptor->addHook(function(string $code, string $path) {
$fileInfo = new FileInfo();
$instrumentedCode = $this->instrumentor->instrument($code, $fileInfo);
Expand Down

0 comments on commit 5105965

Please sign in to comment.