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
| <?php
define('ZEND_VM_OP_UNUSED', 1 << 0); define('ZEND_VM_OP_CONST', 1 << 1); define('ZEND_VM_OP_TMPVAR', 1 << 2); define('ZEND_VM_OP_VAR', 1 << 3); define('ZEND_VM_OP_CV', 1 << 4);
$op_types_map = array( "UNUSED" => ZEND_VM_OP_UNUSED, "CONST" => ZEND_VM_OP_CONST, "TMPVAR" => ZEND_VM_OP_TMPVAR, "VAR" => ZEND_VM_OP_VAR, "CV" => ZEND_VM_OP_CV, );
$op1_get = array( "UNUSED" => "nullptr", "CONST" => "opline->op1.num", "TMPVAR" => "op_array->literals[opline->op1.var]", "VAR" => "nullptr", "CV" => "nullptr", );
$op2_get = array( "UNUSED" => "nullptr", "CONST" => "opline->op2.num", "TMPVAR" => "op_array->literals[opline->op2.var]", "VAR" => "nullptr", "CV" => "nullptr", );
$opcodes = []; $max_opcode = 0; $spec_names = [];
function parse_operand_spec($def, $lineno, $str, &$flags) { global $op_types_map;
$flags = 0; $a = explode("|", $str); foreach ($a as $val) { if (isset($op_types_map[$val])) { $flags |= $op_types_map[$val]; } else { die("ERROR ($def:$lineno): Wrong operand type '$str'\n"); } }
return array_flip($a); }
function gen_handler($f, $opcode) { global $op1_get, $op2_get, $spec_names, $op_types_map;
$opTypes = array_keys($op_types_map);
foreach ($opTypes as $op1Type) { foreach ($opTypes as $op2Type) { if (isset($opcode['op1'][$op1Type]) && isset($opcode['op2'][$op2Type])) { $specialized_replacements = [ "/GET_OP1\(([^)]*)\)/" => $op1_get[$op1Type], "/GET_OP2\(([^)]*)\)/" => $op2_get[$op2Type], ];
$name = $opcode['op']; $templateCode = $opcode['code'];
$spec_name = $name."_SPEC"."_".$op1Type."_".$op2Type; $spec_names[] = $spec_name; fputs($f, "static int $spec_name(zend_op_array *op_array, zend_op *opline) "); $code = preg_replace(array_keys($specialized_replacements), array_values($specialized_replacements), $templateCode); fputs($f, $code); } else { $spec_names[] = 'nullptr'; } } } }
function gen_spec_handlers($f) { global $spec_names;
fputs($f, "\tstatic const void * const spec_handlers[] = {\n"); foreach ($spec_names as $spec_name) { fputs($f, "\t\t(void *) $spec_name,\n"); } fputs($f, "\t};\n");
fputs($f, "\tzend_spec_handlers = spec_handlers;\n"); }
function gen_vm_execute_code($f) { fputs($f, "void zend_execute(zend_op_array *op_array) {\n"); fputs($f, "\tfor (size_t i = 0; i < op_array->last; i++) {\n"); fputs($f, "\t\tzend_op *opline = &(op_array->opcodes[i]);\n"); fputs($f, "\t\t((opcode_handler_t)opline->handler)(op_array, opline);\n"); fputs($f, "\t}\n"); fputs($f, "}\n\n"); }
function gen_vm_init_code($f) { fputs($f, "void zend_vm_init() {\n");
gen_spec_handlers($f);
fputs($f, "}\n"); }
function gen_executor_code($f) { global $opcodes, $max_opcode;
fputs($f, "const void * const *zend_spec_handlers;\n"); fputs($f, "typedef int (*opcode_handler_t) (zend_op_array *op_array, const zend_op *opline);\n\n");
fputs($f, "static uint32_t zend_vm_get_opcode_handler_idx(const zend_op *opline)\n"); fputs($f, "{\n"); fputs($f, "\tstatic int zend_vm_decode[IS_CV + 1] = {0};\n\n"); fputs($f, "\t#define OP_TYPE_CODE_GEN(name, value) zend_vm_decode[name] = _##name##_CODE;\n"); fputs($f, "\t\tOP_TYPE_MAP(OP_TYPE_CODE_GEN)\n"); fputs($f, "\t#undef OP_TYPE_CODE_GEN\n\n"); fputs($f, "\tuint32_t offset = 0;\n"); fputs($f, "\toffset += opline->opcode * 5 * 5;\n"); fputs($f, "\toffset += zend_vm_decode[(int) opline->op1_type] * 5;\n"); fputs($f, "\toffset += zend_vm_decode[(int) opline->op2_type];\n"); fputs($f, "\treturn offset;\n"); fputs($f, "}\n\n");
fputs($f, "const void *zend_vm_get_opcode_handler(const zend_op *opline)\n"); fputs($f, "{\n"); fputs($f, "\tuint32_t offset = zend_vm_get_opcode_handler_idx(opline);\n"); fputs($f, "\treturn zend_spec_handlers[offset];\n"); fputs($f, "}\n\n");
fputs($f, "void zend_vm_set_opcode_handler(zend_op *opline)\n"); fputs($f, "{\n"); fputs($f, "\topline->handler = zend_vm_get_opcode_handler(opline);\n"); fputs($f, "}\n\n");
$num = 0;
for ($i = 0; $i <= $max_opcode; $i++) { if (isset($opcodes[$num])) { gen_handler($f, $opcodes[$num], $num); } else { gen_handler($f, [], $num); } $num++; }
gen_vm_execute_code($f);
gen_vm_init_code($f); }
function gen_vm(string $def) { global $opcodes, $max_opcode;
$in = file($def);
$lineno = 0; $handler = 0;
foreach ($in as $line) { if (strpos($line, "ZEND_VM_HANDLER(") === 0) { if (preg_match( "/^ZEND_VM_HANDLER\(\s*([0-9]+)\s*,\s*([A-Z_]+)\s*,\s*([A-Z_|]+)\s*,\s*([A-Z_|]+)\s*(,\s*([A-Z_|]+)\s*)?(,\s*SPEC\(([A-Z_|=,]+)\)\s*)?\)/", $line, $m ) == 0) { die("ERROR ($def:$lineno): Invalid ZEND_VM_HANDLER definition.\n"); }
$code = (int)$m[1]; $op = $m[2]; $op1 = parse_operand_spec($def, $lineno, $m[3], $flags1); $op2 = parse_operand_spec($def, $lineno, $m[4], $flags2); $flags = $flags1 | ($flags2 << 8);
if ($code > $max_opcode) { $max_opcode = $code; }
if (isset($opcodes[$code])) { die("ERROR ($def:$lineno): Opcode with code '$code' is already defined.\n"); } if (isset($opnames[$op])) { die("ERROR ($def:$lineno): Opcode with name '$op' is already defined.\n"); } $handler = $code;
$opcodes[$code] = array("op"=>$op,"op1"=>$op1,"op2"=>$op2,"code"=>"","flags"=>$flags); } else { $opcodes[$handler]['code'] .= $line; } }
ksort($opcodes);
$f = fopen(__DIR__ . "/zend_vm_opcodes.h", "w+") or die("ERROR: Cannot create zend_vm_opcodes.h\n"); fputs($f, "#pragma once\n\n");
foreach ($opcodes as $code => $dsc) { $op = str_pad($dsc["op"], 20); fputs($f, "#define $op $code\n"); } fclose($f); echo "zend_vm_opcodes.h generated successfully.\n";
$f = fopen(__DIR__ . "/zend_vm_opcodes.cc", "w+") or die("ERROR: Cannot create zend_vm_opcodes.c\n"); fputs($f, "#include \"zend_vm_opcodes.h\"\n\n");
fputs($f, "static const char *zend_vm_opcodes_names[".($max_opcode + 1)."] = {\n"); for ($i = 0; $i <= $max_opcode; $i++) { fputs($f, "\t".(isset($opcodes[$i]["op"])?'"'.$opcodes[$i]["op"].'"':"nullptr").",\n"); } fputs($f, "};\n\n");
fputs($f, "const char* zend_get_opcode_name(char opcode) {\n"); fputs($f, "\treturn zend_vm_opcodes_names[opcode];\n"); fputs($f, "}\n");
fclose($f); echo "zend_vm_opcodes.cc generated successfully.\n";
$f = fopen(__DIR__ . "/zend_vm_execute.h", "w+") or die("ERROR: Cannot create zend_vm_execute.h\n"); fputs($f, "#pragma once\n\n"); fputs($f, "#include <stdint.h>\n"); fputs($f, "#include <stddef.h>\n"); fputs($f, "#include \"zend_compile.h\"\n\n");
gen_executor_code($f); echo "zend_vm_execute.h generated successfully.\n"; }
gen_vm(__DIR__ . "/zend_vm_def.h");
|