check_parameters.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | https://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Nuno Lopes <nlopess@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. define('REPORT_LEVEL', 1); // 0 reports less false-positives. up to level 5.
  19. define('VERSION', '7.0'); // minimum is 7.0
  20. define('PHPDIR', realpath(dirname(__FILE__) . '/../..'));
  21. // be sure you have enough memory and stack for PHP. pcre will push the limits!
  22. ini_set('pcre.backtrack_limit', 10000000);
  23. // ------------------------ end of config ----------------------------
  24. $API_params = array(
  25. 'a' => array('zval**'), // array
  26. 'A' => array('zval**'), // array or object
  27. 'b' => array('bool*'), // boolean
  28. 'd' => array('double*'), // double
  29. 'f' => array('zend_fcall_info*', 'zend_fcall_info_cache*'), // function
  30. 'h' => array('HashTable**'), // array as an HashTable*
  31. 'H' => array('HashTable**'), // array or HASH_OF(object)
  32. 'l' => array('zend_long*'), // long
  33. //TODO 'L' => array('zend_long*, '), // long
  34. 'o' => array('zval**'), //object
  35. 'O' => array('zval**', 'zend_class_entry*'), // object of given type
  36. 'P' => array('zend_string**'), // valid path
  37. 'r' => array('zval**'), // resource
  38. 'S' => array('zend_string**'), // string
  39. 'z' => array('zval**'), // zval*
  40. 'Z' => array('zval***') // zval**
  41. // 's', 'p', 'C' handled separately
  42. );
  43. /** reports an error, according to its level */
  44. function error($str, $level = 0)
  45. {
  46. global $current_file, $current_function, $line;
  47. if ($level <= REPORT_LEVEL) {
  48. if (strpos($current_file,PHPDIR) === 0) {
  49. $filename = substr($current_file, strlen(PHPDIR)+1);
  50. } else {
  51. $filename = $current_file;
  52. }
  53. echo $filename , " [$line] $current_function : $str\n";
  54. }
  55. }
  56. /** this updates the global var $line (for error reporting) */
  57. function update_lineno($offset)
  58. {
  59. global $lines_offset, $line;
  60. $left = 0;
  61. $right = $count = count($lines_offset)-1;
  62. // a nice binary search :)
  63. do {
  64. $mid = intval(($left + $right)/2);
  65. $val = $lines_offset[$mid];
  66. if ($val < $offset) {
  67. if (++$mid > $count || $lines_offset[$mid] > $offset) {
  68. $line = $mid;
  69. return;
  70. } else {
  71. $left = $mid;
  72. }
  73. } else if ($val > $offset) {
  74. if ($lines_offset[--$mid] < $offset) {
  75. $line = $mid+1;
  76. return;
  77. } else {
  78. $right = $mid;
  79. }
  80. } else {
  81. $line = $mid+1;
  82. return;
  83. }
  84. } while (true);
  85. }
  86. /** parses the sources and fetches its vars name, type and if they are initialized or not */
  87. function get_vars($txt)
  88. {
  89. $ret = array();
  90. preg_match_all('/((?:(?:unsigned|struct)\s+)?\w+)(?:\s*(\*+)\s+|\s+(\**))(\w+(?:\[\s*\w*\s*\])?)\s*(?:(=)[^,;]+)?((?:\s*,\s*\**\s*\w+(?:\[\s*\w*\s*\])?\s*(?:=[^,;]+)?)*)\s*;/S', $txt, $m, PREG_SET_ORDER);
  91. foreach ($m as $x) {
  92. // the first parameter is special
  93. if (!in_array($x[1], array('else', 'endif', 'return'))) // hack to skip reserved words
  94. $ret[$x[4]] = array($x[1] . $x[2] . $x[3], $x[5]);
  95. // are there more vars?
  96. if ($x[6]) {
  97. preg_match_all('/(\**)\s*(\w+(?:\[\s*\w*\s*\])?)\s*(=?)/S', $x[6], $y, PREG_SET_ORDER);
  98. foreach ($y as $z) {
  99. $ret[$z[2]] = array($x[1] . $z[1], $z[3]);
  100. }
  101. }
  102. }
  103. // if ($GLOBALS['current_function'] == 'for_debugging') { print_r($m);print_r($ret); }
  104. return $ret;
  105. }
  106. /** run diagnostic checks against one var. */
  107. function check_param($db, $idx, $exp, $optional, $allow_uninit = false)
  108. {
  109. global $error_few_vars_given;
  110. if ($idx >= count($db)) {
  111. if (!$error_few_vars_given) {
  112. error("too few variables passed to function");
  113. $error_few_vars_given = true;
  114. }
  115. return;
  116. } elseif ($db[$idx][0] === '**dummy**') {
  117. return;
  118. }
  119. if ($db[$idx][1] != $exp) {
  120. error("{$db[$idx][0]}: expected '$exp' but got '{$db[$idx][1]}' [".($idx+1).']');
  121. }
  122. if (!$optional && $db[$idx][2]) {
  123. error("not optional var is initialized: {$db[$idx][0]} [".($idx+1).']', 2);
  124. }
  125. if (!$allow_uninit && $optional && !$db[$idx][2]) {
  126. error("optional var not initialized: {$db[$idx][0]} [".($idx+1).']', 1);
  127. }
  128. }
  129. /** fetch params passed to zend_parse_params*() */
  130. function get_params($vars, $str)
  131. {
  132. $ret = array();
  133. preg_match_all('/(?:\([^)]+\))?(&?)([\w>.()-]+(?:\[\w+\])?)\s*,?((?:\)*\s*=)?)/S', $str, $m, PREG_SET_ORDER);
  134. foreach ($m as $x) {
  135. $name = $x[2];
  136. // little hack for last parameter
  137. if (strpos($name, '(') === false) {
  138. $name = rtrim($name, ')');
  139. }
  140. if (empty($vars[$name][0])) {
  141. error("variable not found: '$name'", 3);
  142. $ret[][] = '**dummy**';
  143. } else {
  144. $ret[] = array($name, $vars[$name][0] . ($x[1] ? '*' : ''), $vars[$name][1]);
  145. }
  146. // the end (yes, this is a little hack :P)
  147. if ($x[3]) {
  148. break;
  149. }
  150. }
  151. // if ($GLOBALS['current_function'] == 'for_debugging') { var_dump($m); var_dump($ret); }
  152. return $ret;
  153. }
  154. /** run tests on a function. the code is passed in $txt */
  155. function check_function($name, $txt, $offset)
  156. {
  157. global $API_params;
  158. $regex = '/
  159. (?: zend_parse_parameters(?:_throw)? \s*\([^,]+
  160. | zend_parse_(?:parameters_ex|method_parameters) \s*\([^,]+,[^,]+
  161. | zend_parse_method_parameters_ex \s*\([^,]+,[^,]+,[^,+]
  162. )
  163. ,\s*"([^"]*)"\s*
  164. ,\s*([^{;]*)
  165. /Sx';
  166. if (preg_match_all($regex, $txt, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE)) {
  167. $GLOBALS['current_function'] = $name;
  168. foreach ($matches as $m) {
  169. $GLOBALS['error_few_vars_given'] = false;
  170. update_lineno($offset + $m[2][1]);
  171. $vars = get_vars(substr($txt, 0, $m[0][1])); // limit var search to current location
  172. $params = get_params($vars, $m[2][0]);
  173. $optional = $varargs = false;
  174. $last_char = '';
  175. $j = -1;
  176. $spec = $m[1][0];
  177. $len = strlen($spec);
  178. for ($i = 0; $i < $len; ++$i) {
  179. $char = $spec[$i];
  180. switch ($char = $spec[$i]) {
  181. // separator for optional parameters
  182. case '|':
  183. if ($optional) {
  184. error("more than one optional separator at char #$i");
  185. } else {
  186. $optional = true;
  187. if ($i == $len-1) {
  188. error("unnecessary optional separator");
  189. }
  190. }
  191. break;
  192. // separate_zval_if_not_ref
  193. case '/':
  194. if (in_array($last_char, array('l', 'L', 'd', 'b'))) {
  195. error("the '/' specifier should not be applied to '$last_char'");
  196. }
  197. break;
  198. // nullable arguments
  199. case '!':
  200. if (in_array($last_char, array('l', 'L', 'd', 'b'))) {
  201. check_param($params, ++$j, 'bool*', $optional);
  202. }
  203. break;
  204. // variadic arguments
  205. case '+':
  206. case '*':
  207. if ($varargs) {
  208. error("A varargs specifier can only be used once. repeated char at column $i");
  209. } else {
  210. check_param($params, ++$j, 'zval**', $optional);
  211. check_param($params, ++$j, 'int*', $optional);
  212. $varargs = true;
  213. }
  214. break;
  215. case 's':
  216. case 'p':
  217. check_param($params, ++$j, 'char**', $optional, $allow_uninit=true);
  218. check_param($params, ++$j, 'size_t*', $optional, $allow_uninit=true);
  219. if ($optional && !$params[$j-1][2] && !$params[$j][2]
  220. && $params[$j-1][0] !== '**dummy**' && $params[$j][0] !== '**dummy**') {
  221. error("one of optional vars {$params[$j-1][0]} or {$params[$j][0]} must be initialized", 1);
  222. }
  223. break;
  224. case 'C':
  225. // C must always be initialized, independently of whether it's optional
  226. check_param($params, ++$j, 'zend_class_entry**', false);
  227. break;
  228. default:
  229. if (!isset($API_params[$char])) {
  230. error("unknown char ('$char') at column $i");
  231. }
  232. // If an is_null flag is in use, only that flag is required to be
  233. // initialized
  234. $allow_uninit = $i+1 < $len && $spec[$i+1] === '!'
  235. && in_array($char, array('l', 'L', 'd', 'b'));
  236. foreach ($API_params[$char] as $exp) {
  237. check_param($params, ++$j, $exp, $optional, $allow_uninit);
  238. }
  239. }
  240. $last_char = $char;
  241. }
  242. }
  243. }
  244. }
  245. /** the main recursion function. splits files in functions and calls the other functions */
  246. function recurse($path)
  247. {
  248. foreach (scandir($path) as $file) {
  249. if ($file == '.' || $file == '..' || $file == 'CVS') continue;
  250. $file = "$path/$file";
  251. if (is_dir($file)) {
  252. recurse($file);
  253. continue;
  254. }
  255. // parse only .c and .cpp files
  256. if (substr_compare($file, '.c', -2) && substr_compare($file, '.cpp', -4)) continue;
  257. $txt = file_get_contents($file);
  258. // remove comments (but preserve the number of lines)
  259. $txt = preg_replace('@//.*@S', '', $txt);
  260. $txt = preg_replace_callback('@/\*.*\*/@SsU', function($matches) {
  261. return preg_replace("/[^\r\n]+/S", "", $matches[0]);
  262. }, $txt);
  263. $split = preg_split('/PHP_(?:NAMED_)?(?:FUNCTION|METHOD)\s*\((\w+(?:,\s*\w+)?)\)/S', $txt, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE);
  264. if (count($split) < 2) continue; // no functions defined on this file
  265. array_shift($split); // the first part isn't relevant
  266. // generate the line offsets array
  267. $j = 0;
  268. $lines = preg_split("/(\r\n?|\n)/S", $txt, -1, PREG_SPLIT_DELIM_CAPTURE);
  269. $lines_offset = array();
  270. for ($i = 0; $i < count($lines); ++$i) {
  271. $j += strlen($lines[$i]) + strlen(@$lines[++$i]);
  272. $lines_offset[] = $j;
  273. }
  274. $GLOBALS['lines_offset'] = $lines_offset;
  275. $GLOBALS['current_file'] = $file;
  276. for ($i = 0; $i < count($split); $i+=2) {
  277. // if the /* }}} */ comment is found use it to reduce false positives
  278. // TODO: check the other indexes
  279. list($f) = preg_split('@/\*\s*}}}\s*\*/@S', $split[$i+1][0]);
  280. check_function(preg_replace('/\s*,\s*/S', '::', $split[$i][0]), $f, $split[$i][1]);
  281. }
  282. }
  283. }
  284. $dirs = array();
  285. if (isset($argc) && $argc > 1) {
  286. if ($argv[1] == '-h' || $argv[1] == '-help' || $argv[1] == '--help') {
  287. echo <<<HELP
  288. Synopsis:
  289. php check_parameters.php [directories]
  290. HELP;
  291. exit(0);
  292. }
  293. for ($i = 1; $i < $argc; $i++) {
  294. $dirs[] = $argv[$i];
  295. }
  296. } else {
  297. $dirs[] = PHPDIR;
  298. }
  299. foreach($dirs as $dir) {
  300. if (is_dir($dir)) {
  301. if (!is_readable($dir)) {
  302. echo "ERROR: directory '", $dir ,"' is not readable\n";
  303. exit(1);
  304. }
  305. } else {
  306. echo "ERROR: bogus directory '", $dir ,"'\n";
  307. exit(1);
  308. }
  309. }
  310. foreach ($dirs as $dir) {
  311. recurse(realpath($dir));
  312. }