hiphop-php /hphp/tools/check_native_signatures.php

Language PHP Lines 376
MD5 Hash a44d5cac205fc3aa6bdbbb4c83829481 Estimated Cost $8,140 (why?)
Repository git://github.com/facebook/hiphop-php.git View Raw File View Project SPDX
  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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
<?hh
/*
   +----------------------------------------------------------------------+
   | HipHop for PHP                                                       |
   +----------------------------------------------------------------------+
   | Copyright (c) 2014      Facebook, Inc. (http://www.facebook.com)     |
   +----------------------------------------------------------------------+
   | This source file is subject to version 3.01 of the PHP license,      |
   | that is bundled with this package in the file LICENSE, and is        |
   | available through the world-wide-web at the following url:           |
   | http://www.php.net/license/3_01.txt                                  |
   | If you did not receive a copy of the PHP license and are unable to   |
   | obtain it through the world-wide-web, please send a note to          |
   | license@php.net so we can mail you a copy immediately.               |
   +----------------------------------------------------------------------+
*/
/*
A helper script to check that HNI signatures in a PHP file match how the
functions are defined in the C++ file.

Currently only matches functions, so won't match classes/methods.
*/
// expects argv[1] to be the C++ file, argv[2] to be the PHP file
if (empty($_SERVER['argv'][2])) {
  fwrite(STDERR, "Usage: {$_SERVER['argv'][0]} <extfile.cpp> <extfile.php>\n");
  exit(1);
}

function parse_php_functions(string $file):
                           ConstMap<string, Pair<string, ConstVector<string>>> {
  $source = file_get_contents($file);
  if (!$source) {
    return ImmMap {};
  }

  // Don't handle methods yet, so function can't be indented
  static $function_regex =
    "#<<[^>]*__Native([^>]*)>>\nfunction +([^(]*)\(([^)]*)\) *: *(.+?);#m";

  $functions = Map {};
  $matches = [];
  if (preg_match_all($function_regex, $source, $matches, PREG_SET_ORDER)) {
    foreach($matches as $match) {
      $nativeArgs = $match[1];
      $name = $match[2];
      if (strpos($nativeArgs, '"ActRec"') !== false) {
        // ActRec functions have a specific structure
        $retType = 'actrec';
        $argTypes = Vector {'actrec'};
      } else {
        $argList = $match[3];
        $retType = explode('<', $match[4], 2)[0];
        $argTypes = Vector {};
        if ($argList) {
          $args = preg_split('/\s*,\s*/', $argList);
          if (count($args) > 7 && (in_array('float', $args)
              || in_array('double', $args))) {
            $retType = 'actrec';
            $argTypes = Vector {'actrec'};
          } else if (count($args) > 15) {
            $retType = 'actrec';
            $argTypes = Vector {'actrec'};
          } else {
            foreach($args as $arg) {
              $type = preg_split('/\s*\$/', $arg)[0];
              $type = explode('<', $type, 2)[0];
              if ($type == '...') {
                // Special case varargs
                $vargTypes = Vector {'int'};
                $vargTypes->addAll($argTypes);
                $vargTypes[] = 'array';
                $argTypes = $vargTypes;
              } else {
                $argTypes[] = $type;
              }
            }
          }
        }
      }
      $functions[strtolower($name)] = Pair { $retType, $argTypes };
    }
  }
  return $functions;
}

function parse_cpp_functions(string $file):
                           ConstMap<string, Pair<string, ConstVector<string>>> {
  $source = file_get_contents($file);
  if (!$source) {
    return ImmMap {};
  }

  // Don't handle methods yet, so function can't be indented
  static $function_regex =
            "#^(?:static )?(\S+) +HHVM_FUNCTION\(([^,)]+)(?:, *)?([^)]*)\)#m";

  $functions = Map {};
  $matches = [];

  if (preg_match_all($function_regex, $source, $matches, PREG_SET_ORDER)) {
    foreach($matches as $match) {
      $name = $match[2];
      $argList = $match[3];
      $retType = $match[1];
      $argTypes = Vector {};
      if ($argList) {
        $args = preg_split('/\s*,\s*/', $argList);
        foreach($args as $arg) {
          $type = preg_split('# */ *#', $arg)[0];
          $type = implode(' ', explode(' ', $type, -1));
          $argTypes[] = $type;
        }
      }
      $functions[strtolower($name)] = Pair { $retType, $argTypes };
    }
  }
  return $functions;
}

function parse_php_methods(string $file):
                           ConstMap<string, Pair<string, ConstVector<string>>> {
  $source = file_get_contents($file);
  if (!$source) {
    return ImmMap {};
  }

  static $class_regex = "#class ([^\\s{/]+)[^{/\\)]*\\{(.*?)\n\\}#ms";
  static $method_regex =
    "#<<[^>]*__Native([^>]*)>>\n\\s*.*?function +([^(]*)\(([^)]*)\) *: *(.+?);#m";

  $methods = Map {};
  $classes = [];

  if (preg_match_all($class_regex, $source, $classes, PREG_SET_ORDER)) {
    foreach ($classes as $class) {
      $cname = $class[1];
      $source = $class[2];
      $matches = [];
      if (preg_match_all($method_regex, $source, $matches, PREG_SET_ORDER)) {
        foreach($matches as $match) {
          $nativeArgs = $match[1];
          $mname = $match[2];
          if (strpos($nativeArgs, '"ActRec"') !== false) {
            // ActRec functions have a specific structure
            $retType = 'actrec';
            $argTypes = Vector {'actrec'};
          } else {
            $argList = $match[3];
            $retType = explode('<', $match[4], 2)[0];
            $argTypes = Vector {};
            if ($argList) {
              $args = preg_split('/\s*,\s*/', $argList);
              if (count($args) > 7 && (in_array('float', $args)
                  || in_array('double', $args))) {
                $retType = 'actrec';
                $argTypes = Vector {'actrec'};
              } else if (count($args) > 15) {
                $retType = 'actrec';
                $argTypes = Vector {'actrec'};
              } else {
                foreach($args as $arg) {
                  $type = preg_split('/\s*\$/', $arg)[0];
                  $type = explode('<', $type, 2)[0];
                  if ($type == '...') {
                    // Special case varargs
                    $vargTypes = Vector {'int'};
                    $vargTypes->addAll($argTypes);
                    $vargTypes[] = 'array';
                    $argTypes = $vargTypes;
                  } else {
                    $argTypes[] = $type;
                  }
                }
              }
            }
          }
          $methods[strtolower("$cname::$mname")] = Pair { $retType, $argTypes };
        }
      }
    }
  }

  return $methods;
}

function parse_cpp_methods(string $file):
                           ConstMap<string, Pair<string, ConstVector<string>>> {
  $source = file_get_contents($file);
  if (!$source) {
    return ImmMap {};
  }

  // Don't handle methods yet, so function can't be indented
  static $method_regex =
    "#^(?:static )?(\S+) +HHVM_(?:STATIC_)?METHOD\(([^,)]+),\s+([^,)]+)(?:, *)?([^)]*)\)#m";

  $methods = Map {};
  $matches = [];

  if (preg_match_all($method_regex, $source, $matches, PREG_SET_ORDER)) {
    foreach($matches as $match) {
      $cname = $match[2];
      $mname = $match[3];
      $argList = $match[4];
      $retType = $match[1];
      $argTypes = Vector {};
      if ($argList) {
        $args = preg_split('/\s*,\s*/', $argList);
        foreach($args as $arg) {
          $type = preg_split('# */ *#', $arg)[0];
          $type = implode(' ', explode(' ', $type, -1));
          $argTypes[] = $type;
        }
      }
      $methods[strtolower("$cname::$mname")] = Pair { $retType, $argTypes };
    }
  }
  return $methods;
}

function match_return_type(string $php, string $cpp): bool {
  if ($php[0] == '?') {
    $expected = 'Variant';
  } else {
    switch (strtolower($php)) {
      case 'bool':
      case 'boolean':
        $expected = 'bool';
        break;
      case 'int':
      case 'long':
        $expected = 'int64_t';
        break;
      case 'float':
      case 'double':
        $expected = 'double';
        break;
      case 'void':
        $expected = 'void';
        break;
      case 'string':
        $expected = 'String';
        break;
      case 'array':
        $expected = 'Array';
        break;
      case 'resource':
        $expected = 'Resource';
        break;
      case 'mixed':
      case 'callable':
        $expected = 'Variant';
        break;
      case 'actrec':
        $expected = 'TypedValue*';
        break;
      case 'object':
      default:
        $expected = 'Object';
        break;
    }
  }
  // Special case for ints
  if ($cpp == 'int') {
    $cpp = 'int64_t';
  }
  return $cpp == $expected;
}

function match_arg_type(string $php, string $cpp): bool {
  if ($php[0] == '@') {
     $php = substr($php, 1);
  }
  if ($php[0] == '?') {
    $expected = 'const Variant&';
  } else {
    switch (strtolower(strtok($php, ' &'))) {
      case 'bool':
      case 'boolean':
        $expected = 'bool';
        break;
      case 'int':
      case 'long':
        $expected = 'int64_t';
        break;
      case 'float':
      case 'double':
        $expected = 'double';
        break;
      case 'void':
        // Shouldn't have void as an argument type
        return false;
      case 'string':
        $expected = 'const String&';
        break;
      case 'array':
        $expected = 'const Array&';
        break;
      case 'resource':
        $expected = 'const Resource&';
        break;
      case 'mixed':
      case 'callable':
        $expected = 'const Variant&';
        break;
      case 'actrec':
        $expected = 'ActRec*';
        break;
      case 'object':
      default:
        $expected = 'const Object&';
        break;
    }
  }
  // References must be a variant type
  if ($php[strlen($php)-1] == '&') {
    if ($expected != 'const Variant&') {
      return false;
    } else {
      $expected = 'VRefParam';
    }
  }
  $cpp = trim($cpp);
  // Special case for ints
  if ($cpp == 'int') {
    $cpp = 'int64_t';
  }
  return $cpp == $expected;
}

function check_types(ConstMap<string, Pair<string, ConstVector<string>>> $php,
                     ConstMap<string, Pair<string, ConstVector<string>>> $cpp):
                                                                          bool {
  $errored = false;
  foreach($php as $name => $types) {
    if (!isset($cpp[$name])) {
      $errored = true;
      printf("Unimplemented native function '%s'\n", $name);
      continue;
    }
    $cppTypes = $cpp[$name];
    if (!match_return_type($types[0], $cppTypes[0])) {
      $errored = true;
      printf("Mismatched return type for function '%s'. PHP: %s C++: %s\n",
              $name, $types[0], $cppTypes[0]);
    }
    if ($types[1]->count() != $cppTypes[1]->count()) {
      $errored = true;
      printf("Unequal number of arguments for function '%s'\n", $name);
      continue;
    }
    foreach($types[1] as $idx => $t) {
      if (!match_arg_type($t, $cppTypes[1][$idx])) {
        $errored = true;
        printf("Mismatched argument type for function '%s' at index '%d'."
                . " PHP: %s C++: %s\n", $name, $idx, $t, $cppTypes[1][$idx]);
      }
    }
  }
  return $errored;
}

$phpFuncs = parse_php_functions($_SERVER['argv'][2]);
$cppFuncs = parse_cpp_functions($_SERVER['argv'][1]);

$cppMeths = parse_cpp_methods($_SERVER['argv'][1]);
$phpMeths = parse_php_methods($_SERVER['argv'][2]);

$funcs = check_types($phpFuncs, $cppFuncs);
$meths = check_types($phpMeths, $cppMeths);

if ($funcs || $meths) {
  echo "See https://github.com/facebook/hhvm/wiki/Extension-API for what types",
        " map to what\n";
}
Back to Top