MediaWiki  1.29.2
fuzzTest.php
Go to the documentation of this file.
1 <?php
2 
3 use Wikimedia\ScopedCallback;
4 
5 require __DIR__ . '/../../maintenance/Maintenance.php';
6 
7 // Make RequestContext::resetMain() happy
8 define( 'MW_PARSER_TEST', 1 );
9 
10 class ParserFuzzTest extends Maintenance {
11  private $parserTest;
12  private $maxFuzzTestLength = 300;
13  private $memoryLimit = 100;
14  private $seed;
15 
16  function __construct() {
17  parent::__construct();
18  $this->addDescription( 'Run a fuzz test on the parser, until it segfaults ' .
19  'or throws an exception' );
20  $this->addOption( 'file', 'Use the specified file as a dictionary, ' .
21  ' or leave blank to use parserTests.txt', false, true, true );
22 
23  $this->addOption( 'seed', 'Start the fuzz test from the specified seed', false, true );
24  }
25 
26  function finalSetup() {
29  }
30 
31  function execute() {
32  $files = $this->getOption( 'file', [ __DIR__ . '/parserTests.txt' ] );
33  $this->seed = intval( $this->getOption( 'seed', 1 ) ) - 1;
34  $this->parserTest = new ParserTestRunner(
36  [] );
37  $this->fuzzTest( $files );
38  }
39 
45  function fuzzTest( $filenames ) {
46  $dict = $this->getFuzzInput( $filenames );
47  $dictSize = strlen( $dict );
48  $logMaxLength = log( $this->maxFuzzTestLength );
49 
50  $teardown = $this->parserTest->staticSetup();
51  $teardown = $this->parserTest->setupDatabase( $teardown );
52  $teardown = $this->parserTest->setupUploads( $teardown );
53 
54  $fakeTest = [
55  'test' => '',
56  'desc' => '',
57  'input' => '',
58  'result' => '',
59  'options' => '',
60  'config' => ''
61  ];
62 
63  ini_set( 'memory_limit', $this->memoryLimit * 1048576 * 2 );
64 
65  $numTotal = 0;
66  $numSuccess = 0;
67  $user = new User;
69  $title = Title::makeTitle( NS_MAIN, 'Parser_test' );
70 
71  while ( true ) {
72  // Generate test input
73  mt_srand( ++$this->seed );
74  $totalLength = mt_rand( 1, $this->maxFuzzTestLength );
75  $input = '';
76 
77  while ( strlen( $input ) < $totalLength ) {
78  $logHairLength = mt_rand( 0, 1000000 ) / 1000000 * $logMaxLength;
79  $hairLength = min( intval( exp( $logHairLength ) ), $dictSize );
80  $offset = mt_rand( 0, $dictSize - $hairLength );
81  $input .= substr( $dict, $offset, $hairLength );
82  }
83 
84  $perTestTeardown = $this->parserTest->perTestSetup( $fakeTest );
85  $parser = $this->parserTest->getParser();
86 
87  // Run the test
88  try {
89  $parser->parse( $input, $title, $opts );
90  $fail = false;
91  } catch ( Exception $exception ) {
92  $fail = true;
93  }
94 
95  if ( $fail ) {
96  echo "Test failed with seed {$this->seed}\n";
97  echo "Input:\n";
98  printf( "string(%d) \"%s\"\n\n", strlen( $input ), $input );
99  echo "$exception\n";
100  } else {
101  $numSuccess++;
102  }
103 
104  $numTotal++;
105  ScopedCallback::consume( $perTestTeardown );
106 
107  if ( $numTotal % 100 == 0 ) {
108  $usage = intval( memory_get_usage( true ) / $this->memoryLimit / 1048576 * 100 );
109  echo "{$this->seed}: $numSuccess/$numTotal (mem: $usage%)\n";
110  if ( $usage >= 100 ) {
111  echo "Out of memory:\n";
112  $memStats = $this->getMemoryBreakdown();
113 
114  foreach ( $memStats as $name => $usage ) {
115  echo "$name: $usage\n";
116  }
117  if ( function_exists( 'hphpd_break' ) ) {
118  hphpd_break();
119  }
120  return;
121  }
122  }
123  }
124  }
125 
130  function getMemoryBreakdown() {
131  $memStats = [];
132 
133  foreach ( $GLOBALS as $name => $value ) {
134  $memStats['$' . $name] = $this->guessVarSize( $value );
135  }
136 
137  $classes = get_declared_classes();
138 
139  foreach ( $classes as $class ) {
140  $rc = new ReflectionClass( $class );
141  $props = $rc->getStaticProperties();
142  $memStats[$class] = $this->guessVarSize( $props );
143  $methods = $rc->getMethods();
144 
145  foreach ( $methods as $method ) {
146  $memStats[$class] += $this->guessVarSize( $method->getStaticVariables() );
147  }
148  }
149 
150  $functions = get_defined_functions();
151 
152  foreach ( $functions['user'] as $function ) {
153  $rf = new ReflectionFunction( $function );
154  $memStats["$function()"] = $this->guessVarSize( $rf->getStaticVariables() );
155  }
156 
157  asort( $memStats );
158 
159  return $memStats;
160  }
161 
165  function guessVarSize( $var ) {
166  $length = 0;
167  try {
168  MediaWiki\suppressWarnings();
169  $length = strlen( serialize( $var ) );
170  MediaWiki\restoreWarnings();
171  } catch ( Exception $e ) {
172  }
173  return $length;
174  }
175 
181  function getFuzzInput( $filenames ) {
182  $dict = '';
183 
184  foreach ( $filenames as $filename ) {
185  $contents = file_get_contents( $filename );
186  preg_match_all(
187  '/!!\s*(input|wikitext)\n(.*?)\n!!\s*(result|html|html\/\*|html\/php)/s',
188  $contents,
189  $matches
190  );
191 
192  foreach ( $matches[1] as $match ) {
193  $dict .= $match . "\n";
194  }
195  }
196 
197  return $dict;
198  }
199 }
200 
201 $maintClass = 'ParserFuzzTest';
Preprocessor_DOM
Definition: Preprocessor_DOM.php:28
ParserFuzzTest\$maxFuzzTestLength
$maxFuzzTestLength
Definition: fuzzTest.php:12
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:287
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
ParserFuzzTest\getMemoryBreakdown
getMemoryBreakdown()
Get a memory usage breakdown.
Definition: fuzzTest.php:130
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:246
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
serialize
serialize()
Definition: ApiMessage.php:177
link
usually copyright or history_copyright This message must be in HTML not wikitext if the section is included from a template to be included in the link
Definition: hooks.txt:2929
ParserFuzzTest\$seed
$seed
Definition: fuzzTest.php:14
ParserFuzzTest\execute
execute()
Do the actual work.
Definition: fuzzTest.php:31
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:304
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
Preprocessor_Hash
Differences from DOM schema:
Definition: Preprocessor_Hash.php:43
User
User
Definition: All_system_messages.txt:425
http
Apache License January http
Definition: APACHE-LICENSE-2.0.txt:3
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
NS_MAIN
const NS_MAIN
Definition: Defines.php:62
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:934
a
</source > ! result< div class="mw-highlight mw-content-ltr" dir="ltr">< pre >< span ></span >< span class="kd"> var</span >< span class="nx"> a</span >< span class="p"></span ></pre ></div > ! end ! test Multiline< source/> in lists !input *< source > a b</source > *foo< source > a b</source > ! html< ul >< li >< div class="mw-highlight mw-content-ltr" dir="ltr">< pre > a b</pre ></div ></li ></ul >< ul >< li > foo< div class="mw-highlight mw-content-ltr" dir="ltr">< pre > a b</pre ></div ></li ></ul > ! html tidy< ul >< li >< div class="mw-highlight mw-content-ltr" dir="ltr">< pre > a b</pre ></div ></li ></ul >< ul >< li > foo< div class="mw-highlight mw-content-ltr" dir="ltr">< pre > a b</pre ></div ></li ></ul > ! end ! test Custom attributes !input< source lang="javascript" id="foo" class="bar" dir="rtl" style="font-size: larger;"> var a
Definition: parserTests.txt:89
ParserFuzzTest\$parserTest
$parserTest
Definition: fuzzTest.php:11
$input
if(is_array( $mode)) switch( $mode) $input
Definition: postprocess-phan.php:141
$matches
$matches
Definition: NoLocalSettings.php:24
in
null for the wiki Added in
Definition: hooks.txt:1572
ParserFuzzTest\fuzzTest
fuzzTest( $filenames)
Run a fuzz test series Draw input from a set of test files.
Definition: fuzzTest.php:45
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:215
TestSetup\applyInitialConfig
static applyInitialConfig()
This should be called before Setup.php, e.g.
Definition: TestSetup.php:11
$maintClass
$maintClass
Definition: fuzzTest.php:201
$parser
do that in ParserLimitReportFormat instead $parser
Definition: hooks.txt:2536
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:514
$GLOBALS
$GLOBALS['wgAutoloadClasses']['LocalisationUpdate']
Definition: Autoload.php:10
links
This document describes the XML format used to represent information about external sites known to a MediaWiki installation This information about external sites is used to allow inter wiki links
Definition: sitelist.txt:3
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2122
$value
$value
Definition: styleTest.css.php:45
ParserFuzzTest
Definition: fuzzTest.php:10
Maintenance\requireTestsAutoloader
static requireTestsAutoloader()
Call this to set up the autoloader to allow classes to be used from the tests directory.
Definition: Maintenance.php:1542
ParserFuzzTest\getFuzzInput
getFuzzInput( $filenames)
Get an input dictionary from a set of parser test files.
Definition: fuzzTest.php:181
options
We ve cleaned up the code here by removing clumps of infrequently used code and moving them off somewhere else It s much easier for someone working with this code to see what s _really_ going and make changes or fix bugs In we can take all the code that deals with the little used title reversing options(say) and put it in one place. Instead of having little title-reversing if-blocks spread all over the codebase in showAnArticle
ParserFuzzTest\$memoryLimit
$memoryLimit
Definition: fuzzTest.php:13
ParserFuzzTest\finalSetup
finalSetup()
Handle some last-minute setup here.
Definition: fuzzTest.php:26
ParserFuzzTest\__construct
__construct()
Default constructor.
Definition: fuzzTest.php:16
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:250
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
MultiTestRecorder
This is a TestRecorder representing a collection of other TestRecorders.
Definition: MultiTestRecorder.php:7
href
shown</td >< td > a href
Definition: All_system_messages.txt:2667
ParserFuzzTest\guessVarSize
guessVarSize( $var)
Estimate the size of the input variable.
Definition: fuzzTest.php:165
ParserOptions\newFromUser
static newFromUser( $user)
Get a ParserOptions object from a given user.
Definition: ParserOptions.php:705
Maintenance\memoryLimit
memoryLimit()
Normally we disable the memory_limit when running admin scripts.
Definition: Maintenance.php:700