| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Maintenance\Maintenance; |
| 4 |
use MediaWiki\Parser\ParserOptions; |
| 5 |
use MediaWiki\Settings\SettingsBuilder; |
| 6 |
use MediaWiki\Title\Title; |
| 7 |
use MediaWiki\User\User; |
| 8 |
use Wikimedia\Parsoid\ParserTests\Test as ParserTest; |
| 9 |
use Wikimedia\ScopedCallback; |
| 10 |
|
| 11 |
require_once __DIR__ . '/../../maintenance/Maintenance.php'; |
| 12 |
|
| 13 |
define( 'MW_AUTOLOAD_TEST_CLASSES', true ); |
| 14 |
|
| 15 |
class ParserFuzzTest extends Maintenance { |
| 16 |
|
| 17 |
private $parserTest; |
| 18 |
|
| 19 |
private $maxFuzzTestLength = 300; |
| 20 |
|
| 21 |
private $memoryLimit = 100; |
| 22 |
|
| 23 |
private $seed; |
| 24 |
|
| 25 |
public function __construct() { |
| 26 |
parent::__construct(); |
| 27 |
$this->addDescription( 'Run a fuzz test on the parser, until it segfaults ' . |
| 28 |
'or throws an exception' ); |
| 29 |
$this->addOption( 'file', 'Use the specified file as a dictionary, ' . |
| 30 |
' or leave blank to use parserTests.txt', false, true, true ); |
| 31 |
|
| 32 |
$this->addOption( 'seed', 'Start the fuzz test from the specified seed', false, true ); |
| 33 |
} |
| 34 |
|
| 35 |
public function finalSetup( SettingsBuilder $settingsBuilder ) { |
| 36 |
|
| 37 |
define( 'MW_PARSER_TEST', 1 ); |
| 38 |
|
| 39 |
TestSetup::applyInitialConfig(); |
| 40 |
} |
| 41 |
|
| 42 |
public function execute() { |
| 43 |
$files = $this->getOption( 'file', [ __DIR__ . '/parserTests.txt' ] ); |
| 44 |
$this->seed = intval( $this->getOption( 'seed', 1 ) ) - 1; |
| 45 |
$this->parserTest = new ParserTestRunner( |
| 46 |
new MultiTestRecorder, |
| 47 |
[] ); |
| 48 |
$this->fuzzTest( $files ); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public function fuzzTest( $filenames ) { |
| 57 |
$dict = $this->getFuzzInput( $filenames ); |
| 58 |
$dictSize = strlen( $dict ); |
| 59 |
$logMaxLength = log( $this->maxFuzzTestLength ); |
| 60 |
|
| 61 |
$teardown = $this->parserTest->staticSetup(); |
| 62 |
$teardown = $this->parserTest->setupDatabase( $teardown ); |
| 63 |
$teardown = $this->parserTest->setupUploads( $teardown ); |
| 64 |
|
| 65 |
$fakeTest = new ParserTest( [ |
| 66 |
'testName' => '', |
| 67 |
'wikitext' => '', |
| 68 |
'html' => '', |
| 69 |
'options' => [], |
| 70 |
'config' => [], |
| 71 |
], [], '' ); |
| 72 |
|
| 73 |
ini_set( 'memory_limit', $this->memoryLimit * 1048576 * 2 ); |
| 74 |
|
| 75 |
$numTotal = 0; |
| 76 |
$numSuccess = 0; |
| 77 |
$user = new User; |
| 78 |
$opts = ParserOptions::newFromUser( $user ); |
| 79 |
$title = Title::makeTitle( NS_MAIN, 'Parser_test' ); |
| 80 |
|
| 81 |
while ( true ) { |
| 82 |
|
| 83 |
mt_srand( ++$this->seed ); |
| 84 |
$totalLength = mt_rand( 1, $this->maxFuzzTestLength ); |
| 85 |
$input = ''; |
| 86 |
|
| 87 |
while ( strlen( $input ) < $totalLength ) { |
| 88 |
$logHairLength = mt_rand( 0, 1000000 ) / 1000000 * $logMaxLength; |
| 89 |
$hairLength = min( intval( exp( $logHairLength ) ), $dictSize ); |
| 90 |
$offset = mt_rand( 0, $dictSize - $hairLength ); |
| 91 |
$input .= substr( $dict, $offset, $hairLength ); |
| 92 |
} |
| 93 |
|
| 94 |
$perTestTeardown = $this->parserTest->perTestSetup( $fakeTest ); |
| 95 |
$parser = $this->parserTest->getParser(); |
| 96 |
|
| 97 |
|
| 98 |
try { |
| 99 |
$parser->parse( $input, $title, $opts ); |
| 100 |
$numSuccess++; |
| 101 |
} catch ( Exception $exception ) { |
| 102 |
echo "Test failed with seed {$this->seed}\n"; |
| 103 |
echo "Input:\n"; |
| 104 |
printf( "string(%d) \"%s\"\n\n", strlen( $input ), $input ); |
| 105 |
echo "$exception\n"; |
| 106 |
} |
| 107 |
|
| 108 |
$numTotal++; |
| 109 |
ScopedCallback::consume( $perTestTeardown ); |
| 110 |
|
| 111 |
if ( $numTotal % 100 == 0 ) { |
| 112 |
$usage = intval( memory_get_usage( true ) / $this->memoryLimit / 1048576 * 100 ); |
| 113 |
echo "{$this->seed}: $numSuccess/$numTotal (mem: $usage%)\n"; |
| 114 |
if ( $usage >= 100 ) { |
| 115 |
echo "Out of memory:\n"; |
| 116 |
$memStats = $this->getMemoryBreakdown(); |
| 117 |
|
| 118 |
foreach ( $memStats as $name => $usage ) { |
| 119 |
echo "$name: $usage\n"; |
| 120 |
} |
| 121 |
return; |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
private function getMemoryBreakdown() { |
| 132 |
$memStats = []; |
| 133 |
|
| 134 |
foreach ( $GLOBALS as $name => $value ) { |
| 135 |
$memStats['$' . $name] = $this->guessVarSize( $value ); |
| 136 |
} |
| 137 |
|
| 138 |
$classes = get_declared_classes(); |
| 139 |
|
| 140 |
foreach ( $classes as $class ) { |
| 141 |
$rc = new ReflectionClass( $class ); |
| 142 |
$props = $rc->getStaticProperties(); |
| 143 |
$memStats[$class] = $this->guessVarSize( $props ); |
| 144 |
$methods = $rc->getMethods(); |
| 145 |
|
| 146 |
foreach ( $methods as $method ) { |
| 147 |
$memStats[$class] += $this->guessVarSize( $method->getStaticVariables() ); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
$functions = get_defined_functions(); |
| 152 |
|
| 153 |
foreach ( $functions['user'] as $function ) { |
| 154 |
$rf = new ReflectionFunction( $function ); |
| 155 |
$memStats["$function()"] = $this->guessVarSize( $rf->getStaticVariables() ); |
| 156 |
} |
| 157 |
|
| 158 |
asort( $memStats ); |
| 159 |
|
| 160 |
return $memStats; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
public function guessVarSize( $var ) { |
| 169 |
$length = 0; |
| 170 |
try { |
| 171 |
$length = strlen( @serialize( $var ) ); |
| 172 |
} catch ( Exception $e ) { |
| 173 |
} |
| 174 |
return $length; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public function getFuzzInput( $filenames ) { |
| 183 |
$dict = ''; |
| 184 |
|
| 185 |
foreach ( $filenames as $filename ) { |
| 186 |
$contents = file_get_contents( $filename ); |
| 187 |
preg_match_all( |
| 188 |
'/!!\s*(input|wikitext)\n(.*?)\n!!\s*(result|html|html\/\*|html\/php)/s', |
| 189 |
$contents, |
| 190 |
$matches |
| 191 |
); |
| 192 |
|
| 193 |
foreach ( $matches[1] as $match ) { |
| 194 |
$dict .= $match . "\n"; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
return $dict; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
$maintClass = ParserFuzzTest::class; |
| 203 |
require_once RUN_MAINTENANCE_IF_MAIN; |