-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.php
More file actions
130 lines (122 loc) · 4.54 KB
/
main.php
File metadata and controls
130 lines (122 loc) · 4.54 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
#!/usr/bin/env php
<?php
#FIXME: when evaluating branch conditions for off-branching, it should be isolated, otherwise side-effects can apply!
ini_set("memory_limit","1000M");
require_once __DIR__ . "/phpanalyzer.php";
use malmax\PHPAnalyzer;
$usage="Usage: php main.php -f file.php [-v verbosity --output --strict --concolic --diehard --postprocessing]\n";
if (isset($argc))// and realpath($argv[0])==__FILE__)
{
$options=getopt("f:v:o",['strict','output','concolic','postprocessing','diehard','ppisolation'
,'combined','static']);
if (!isset($options['f']))
die($usage);
ini_set("memory_limit",-1);
$init_environ=[];
$superglobals=array_flip(explode(",$",'_GET,$_POST,$_FILES,$_COOKIE,$_SESSION,$_SERVER,$_REQUEST,$_ENV,$GLOBALS'));
foreach ($superglobals as $k=>$sg)
if (isset($GLOBALS[$k]))
$init_environ[$k]=&$GLOBALS[$k];
else
$init_environ[$k]=[];
$init_environ['GLOBALS']=&$init_environ;
// Read config from JSON
$config = 'config.json';
$config_json = file_get_contents($config);
$config_json = json_decode($config_json, true);
$init_environ['_SERVER']['SERVER_NAME'] = $config_json['server']['server_name'];
$init_environ['_SERVER']['SERVER_ADDR'] = $config_json['server']['server_addr'];
$init_environ['_SERVER']['GATEWAY_INTERFACE'] = $config_json['server']['gateway_interface'];
$init_environ['_SERVER']['SERVER_SOFTWARE'] = $config_json['server']['server_software'];
$init_environ['_SERVER']['SERVER_PROTOCOL'] = $config_json['server']['server_protocol'];
$init_environ['_SERVER']['SERVER_ADMIN'] = $config_json['server']['server_admin'];
$init_environ['_SERVER']['SERVER_PORT'] = $config_json['server']['server_port'];
$init_environ['_SERVER']['SERVER_SIGNATURE'] = $config_json['server']['server_signature'];
$x=new PHPAnalyzer($init_environ);
$x->static=isset($options['static']); //only postprocessing/static
$x->strict=isset($options['strict']); //die on errors
$x->direct_output=isset($options['output']);
if (isset($options['v'])) $x->verbose=$options['v'];
$entry_file=$options['f'];
$x->concolic=isset($options['concolic']); //counterfactual mode
$x->diehard=isset($options['diehard']); //dont die on error in isolation
$x->post_processing_isolation=isset($options['ppisolation']); //do post processing in isolation
$combined=isset($options['combined']); //run on all files (experimental)
timer(0);
if (!$x->static)
$x->start($entry_file);
else
$x->parse($entry_file);
$exec_time=timer();
function h($x)
{
return PHP_EOL."===== {$x} =====".PHP_EOL;
}
$stats="Request: {$entry_file}".PHP_EOL;
$stats.=h("Options");
$stats.=print_r($options,true);
$stats.=getcwd().PHP_EOL;
$stats.=h("General Statistics");
$stats.="Execution time: {$exec_time}".PHP_EOL;
$stats.=print_r($x->stats,true);
$stats.=h("Included Files");
$stats.=print_r($x->included_files,true);
if ($x->concolic)
{
$stats.=h("Concolic Files");
$stats.=print_r($x->all_files,true);
}
if ($combined)
{
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(dirname($entry_file),
RecursiveDirectoryIterator::SKIP_DOTS));
$x->Verbose("Starting combined mode...\n",1);
$combined_time=0;
$combined_count=0;
$combined_files=[];
foreach($it as $path)
{
echo $path,PHP_EOL;
if (substr($path,-4)==".php" and realpath($path)!==realpath($entry_file))
{
$x->Verbose("Combined mode file {$combined_count}...\n",2);
timer(0);
$x->off_branch_start();
$x->start($path);
$x->off_branch_end();
$time=timer();
$combined_time+=$time;
$combined_count++;
$combined_files[]=["file"=>$path,"time"=>$time];
}
}
$stats.=h("Combined Mode");
$stats.="Total time: {$combined_time}\n";
$stats.="Total files: {$combined_count}\n";
$stats.=print_r($combined_files,true);
}
if (!isset($options['output']))
file_put_contents("output.txt",$x->output);
if (isset($options['postprocessing']))
{
$firstinc=$x->all_files;
timer(0);
$x->postprocessing();
$pptime=timer();
$stats.=h("General Statistics after PostProcessing");
$stats.="Execution time: {$pptime}".PHP_EOL;
$stats.=print_r($x->stats,true);
$stats.=h("PostProcessing Included Files");
$stats.=print_r(array_diff(array_keys($x->postprocessing['included_files']),array_keys($firstinc)),true);
$stats.=h("PostProcessing Data");
$stats.=print_r($x->postprocessing,true);
}
$stats.=h("Constants").print_r($x->constants,true);
file_put_contents("stats.txt", $stats);
if (isset($x->termination_value))
exit($x->termination_value);
else
exit(0);
}
else
die($usage);