[982] | 1 | #include "xc_disassembler.h" |
---|
[1] | 2 | #include "xcache.h" |
---|
[1027] | 3 | #include "xcache/xc_ini.h" |
---|
[987] | 4 | #include "xcache/xc_utils.h" |
---|
| 5 | #include "xcache/xc_sandbox.h" |
---|
[1026] | 6 | #include "xcache/xc_compatibility.h" |
---|
[982] | 7 | #include "xc_processor.h" |
---|
[1] | 8 | |
---|
[1026] | 9 | #include "ext/standard/info.h" |
---|
| 10 | |
---|
[937] | 11 | static void xc_dasm(zval *output, zend_op_array *op_array TSRMLS_DC) /* {{{ */ |
---|
[1] | 12 | { |
---|
[937] | 13 | const Bucket *b; |
---|
[1] | 14 | zval *zv, *list; |
---|
| 15 | xc_compile_result_t cr; |
---|
| 16 | int bufsize = 2; |
---|
| 17 | char *buf; |
---|
[930] | 18 | xc_dasm_t dasm; |
---|
[1] | 19 | |
---|
| 20 | xc_compile_result_init_cur(&cr, op_array TSRMLS_CC); |
---|
| 21 | |
---|
| 22 | xc_apply_op_array(&cr, (apply_func_t) xc_undo_pass_two TSRMLS_CC); |
---|
| 23 | xc_apply_op_array(&cr, (apply_func_t) xc_fix_opcode TSRMLS_CC); |
---|
| 24 | |
---|
| 25 | /* go */ |
---|
[937] | 26 | array_init(output); |
---|
[1] | 27 | |
---|
| 28 | ALLOC_INIT_ZVAL(zv); |
---|
| 29 | array_init(zv); |
---|
[930] | 30 | xc_dasm_zend_op_array(&dasm, zv, op_array TSRMLS_CC); |
---|
[1313] | 31 | add_assoc_zval_ex(output, XCACHE_STRS("op_array"), zv); |
---|
[1] | 32 | |
---|
[749] | 33 | buf = emalloc(bufsize); |
---|
| 34 | |
---|
[1] | 35 | ALLOC_INIT_ZVAL(list); |
---|
| 36 | array_init(list); |
---|
[938] | 37 | for (b = xc_sandbox_user_function_begin(TSRMLS_C); b; b = b->pListNext) { |
---|
[749] | 38 | int keysize, keyLength; |
---|
| 39 | |
---|
[709] | 40 | ALLOC_INIT_ZVAL(zv); |
---|
| 41 | array_init(zv); |
---|
[930] | 42 | xc_dasm_zend_function(&dasm, zv, b->pData TSRMLS_CC); |
---|
[709] | 43 | |
---|
[749] | 44 | keysize = BUCKET_KEY_SIZE(b) + 2; |
---|
| 45 | if (keysize > bufsize) { |
---|
| 46 | do { |
---|
| 47 | bufsize *= 2; |
---|
| 48 | } while (keysize > bufsize); |
---|
| 49 | buf = erealloc(buf, bufsize); |
---|
| 50 | } |
---|
| 51 | memcpy(buf, BUCKET_KEY_S(b), keysize); |
---|
| 52 | buf[keysize - 2] = buf[keysize - 1] = ""[0]; |
---|
| 53 | keyLength = b->nKeyLength; |
---|
| 54 | #ifdef IS_UNICODE |
---|
| 55 | if (BUCKET_KEY_TYPE(b) == IS_UNICODE) { |
---|
| 56 | if (buf[0] == ""[0] && buf[1] == ""[0]) { |
---|
| 57 | keyLength ++; |
---|
| 58 | } |
---|
| 59 | } else |
---|
| 60 | #endif |
---|
| 61 | { |
---|
| 62 | if (buf[0] == ""[0]) { |
---|
| 63 | keyLength ++; |
---|
| 64 | } |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | add_u_assoc_zval_ex(list, BUCKET_KEY_TYPE(b), ZSTR(buf), keyLength, zv); |
---|
[709] | 68 | } |
---|
[1313] | 69 | add_assoc_zval_ex(output, XCACHE_STRS("function_table"), list); |
---|
[1] | 70 | |
---|
| 71 | ALLOC_INIT_ZVAL(list); |
---|
| 72 | array_init(list); |
---|
[938] | 73 | for (b = xc_sandbox_user_class_begin(TSRMLS_C); b; b = b->pListNext) { |
---|
[714] | 74 | int keysize, keyLength; |
---|
| 75 | |
---|
[1] | 76 | ALLOC_INIT_ZVAL(zv); |
---|
| 77 | array_init(zv); |
---|
[1321] | 78 | xc_dasm_zend_class_entry(&dasm, zv, CestToCePtr(*(xc_cest_t *)b->pData) TSRMLS_CC); |
---|
[1] | 79 | |
---|
| 80 | keysize = BUCKET_KEY_SIZE(b) + 2; |
---|
| 81 | if (keysize > bufsize) { |
---|
| 82 | do { |
---|
| 83 | bufsize *= 2; |
---|
| 84 | } while (keysize > bufsize); |
---|
| 85 | buf = erealloc(buf, bufsize); |
---|
| 86 | } |
---|
[200] | 87 | memcpy(buf, BUCKET_KEY_S(b), keysize); |
---|
[1] | 88 | buf[keysize - 2] = buf[keysize - 1] = ""[0]; |
---|
[714] | 89 | keyLength = b->nKeyLength; |
---|
[1] | 90 | #ifdef IS_UNICODE |
---|
| 91 | if (BUCKET_KEY_TYPE(b) == IS_UNICODE) { |
---|
| 92 | if (buf[0] == ""[0] && buf[1] == ""[0]) { |
---|
[714] | 93 | keyLength ++; |
---|
[1] | 94 | } |
---|
| 95 | } else |
---|
| 96 | #endif |
---|
| 97 | { |
---|
| 98 | if (buf[0] == ""[0]) { |
---|
[714] | 99 | keyLength ++; |
---|
[1] | 100 | } |
---|
| 101 | } |
---|
[714] | 102 | add_u_assoc_zval_ex(list, BUCKET_KEY_TYPE(b), ZSTR(buf), keyLength, zv); |
---|
[1] | 103 | } |
---|
| 104 | efree(buf); |
---|
[1313] | 105 | add_assoc_zval_ex(output, XCACHE_STRS("class_table"), list); |
---|
[1] | 106 | |
---|
| 107 | /*xc_apply_op_array(&cr, (apply_func_t) xc_redo_pass_two TSRMLS_CC);*/ |
---|
| 108 | xc_compile_result_free(&cr); |
---|
| 109 | } |
---|
| 110 | /* }}} */ |
---|
[937] | 111 | typedef struct xc_dasm_sandboxed_t { /* {{{ */ |
---|
| 112 | enum Type { |
---|
| 113 | xc_dasm_file_t |
---|
| 114 | , xc_dasm_string_t |
---|
| 115 | } type; |
---|
| 116 | union { |
---|
| 117 | zval *zfilename; |
---|
| 118 | struct { |
---|
| 119 | zval *source; |
---|
| 120 | char *eval_name; |
---|
| 121 | } compile_string; |
---|
| 122 | } input; |
---|
| 123 | |
---|
| 124 | zval *output; |
---|
[993] | 125 | } xc_dasm_sandboxed_t; /* }}} */ |
---|
| 126 | zend_op_array *xc_dasm_sandboxed(void *data TSRMLS_DC) /* {{{ */ |
---|
[1] | 127 | { |
---|
[937] | 128 | zend_bool catched = 0; |
---|
[1] | 129 | zend_op_array *op_array = NULL; |
---|
[937] | 130 | xc_dasm_sandboxed_t *sandboxed_dasm = (xc_dasm_sandboxed_t *) data; |
---|
[1] | 131 | |
---|
| 132 | zend_try { |
---|
[937] | 133 | if (sandboxed_dasm->type == xc_dasm_file_t) { |
---|
| 134 | op_array = compile_filename(ZEND_REQUIRE, sandboxed_dasm->input.zfilename TSRMLS_CC); |
---|
| 135 | } |
---|
| 136 | else { |
---|
| 137 | op_array = compile_string(sandboxed_dasm->input.compile_string.source, sandboxed_dasm->input.compile_string.eval_name TSRMLS_CC); |
---|
| 138 | } |
---|
[1] | 139 | } zend_catch { |
---|
| 140 | catched = 1; |
---|
| 141 | } zend_end_try(); |
---|
| 142 | |
---|
| 143 | if (catched || !op_array) { |
---|
[937] | 144 | #define return_value sandboxed_dasm->output |
---|
| 145 | RETVAL_FALSE; |
---|
| 146 | #undef return_value |
---|
| 147 | return NULL; |
---|
[1] | 148 | } |
---|
| 149 | |
---|
[937] | 150 | xc_dasm(sandboxed_dasm->output, op_array TSRMLS_CC); |
---|
[1] | 151 | |
---|
| 152 | /* free */ |
---|
[1321] | 153 | #ifdef ZEND_ENGINE_2 |
---|
[1] | 154 | destroy_op_array(op_array TSRMLS_CC); |
---|
[1321] | 155 | #else |
---|
| 156 | destroy_op_array(op_array); |
---|
| 157 | #endif |
---|
[1] | 158 | efree(op_array); |
---|
| 159 | |
---|
[937] | 160 | return NULL; |
---|
| 161 | } /* }}} */ |
---|
| 162 | void xc_dasm_string(zval *output, zval *source TSRMLS_DC) /* {{{ */ |
---|
| 163 | { |
---|
| 164 | xc_dasm_sandboxed_t sandboxed_dasm; |
---|
| 165 | char *eval_name = zend_make_compiled_string_description("runtime-created function" TSRMLS_CC); |
---|
| 166 | |
---|
| 167 | sandboxed_dasm.output = output; |
---|
| 168 | sandboxed_dasm.type = xc_dasm_string_t; |
---|
| 169 | sandboxed_dasm.input.compile_string.source = source; |
---|
| 170 | sandboxed_dasm.input.compile_string.eval_name = eval_name; |
---|
| 171 | xc_sandbox(&xc_dasm_sandboxed, (void *) &sandboxed_dasm, eval_name TSRMLS_CC); |
---|
[1] | 172 | efree(eval_name); |
---|
| 173 | } |
---|
| 174 | /* }}} */ |
---|
[937] | 175 | void xc_dasm_file(zval *output, const char *filename TSRMLS_DC) /* {{{ */ |
---|
[1] | 176 | { |
---|
| 177 | zval *zfilename; |
---|
[937] | 178 | xc_dasm_sandboxed_t sandboxed_dasm; |
---|
[1] | 179 | |
---|
| 180 | MAKE_STD_ZVAL(zfilename); |
---|
| 181 | zfilename->value.str.val = estrdup(filename); |
---|
| 182 | zfilename->value.str.len = strlen(filename); |
---|
| 183 | zfilename->type = IS_STRING; |
---|
| 184 | |
---|
[937] | 185 | sandboxed_dasm.output = output; |
---|
| 186 | sandboxed_dasm.type = xc_dasm_file_t; |
---|
| 187 | sandboxed_dasm.input.zfilename = zfilename; |
---|
| 188 | xc_sandbox(&xc_dasm_sandboxed, (void *) &sandboxed_dasm, zfilename->value.str.val TSRMLS_CC); |
---|
[1] | 189 | |
---|
| 190 | zval_dtor(zfilename); |
---|
| 191 | FREE_ZVAL(zfilename); |
---|
| 192 | } |
---|
| 193 | /* }}} */ |
---|
[993] | 194 | |
---|
| 195 | /* {{{ proto array xcache_dasm_file(string filename) |
---|
| 196 | Disassemble file into opcode array by filename */ |
---|
| 197 | PHP_FUNCTION(xcache_dasm_file) |
---|
| 198 | { |
---|
| 199 | char *filename; |
---|
| 200 | int filename_len; |
---|
| 201 | |
---|
| 202 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &filename, &filename_len) == FAILURE) { |
---|
| 203 | return; |
---|
| 204 | } |
---|
| 205 | if (!filename_len) RETURN_FALSE; |
---|
| 206 | |
---|
| 207 | xc_dasm_file(return_value, filename TSRMLS_CC); |
---|
| 208 | } |
---|
| 209 | /* }}} */ |
---|
| 210 | /* {{{ proto array xcache_dasm_string(string code) |
---|
| 211 | Disassemble php code into opcode array */ |
---|
| 212 | PHP_FUNCTION(xcache_dasm_string) |
---|
| 213 | { |
---|
| 214 | zval *code; |
---|
| 215 | |
---|
| 216 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &code) == FAILURE) { |
---|
| 217 | return; |
---|
| 218 | } |
---|
| 219 | xc_dasm_string(return_value, code TSRMLS_CC); |
---|
| 220 | } |
---|
| 221 | /* }}} */ |
---|
[1026] | 222 | |
---|
| 223 | /* {{{ PHP_MINFO_FUNCTION(xcache_disassembler) */ |
---|
| 224 | static PHP_MINFO_FUNCTION(xcache_disassembler) |
---|
| 225 | { |
---|
| 226 | php_info_print_table_start(); |
---|
[1043] | 227 | php_info_print_table_row(2, "XCache Disassembler Module", "enabled"); |
---|
[1026] | 228 | php_info_print_table_end(); |
---|
| 229 | |
---|
| 230 | DISPLAY_INI_ENTRIES(); |
---|
| 231 | } |
---|
| 232 | /* }}} */ |
---|
| 233 | static zend_function_entry xcache_disassembler_functions[] = /* {{{ */ |
---|
| 234 | { |
---|
| 235 | PHP_FE(xcache_dasm_file, NULL) |
---|
| 236 | PHP_FE(xcache_dasm_string, NULL) |
---|
| 237 | PHP_FE_END |
---|
| 238 | }; |
---|
| 239 | /* }}} */ |
---|
| 240 | static zend_module_entry xcache_disassembler_module_entry = { /* {{{ */ |
---|
| 241 | STANDARD_MODULE_HEADER, |
---|
[1043] | 242 | XCACHE_NAME " Disassembler", |
---|
[1026] | 243 | xcache_disassembler_functions, |
---|
| 244 | NULL, |
---|
| 245 | NULL, |
---|
| 246 | NULL, |
---|
| 247 | NULL, |
---|
| 248 | PHP_MINFO(xcache_disassembler), |
---|
| 249 | XCACHE_VERSION, |
---|
| 250 | #ifdef PHP_GINIT |
---|
| 251 | NO_MODULE_GLOBALS, |
---|
| 252 | #endif |
---|
[1321] | 253 | #ifdef ZEND_ENGINE_2 |
---|
[1026] | 254 | NULL, |
---|
[1321] | 255 | #else |
---|
| 256 | NULL, |
---|
| 257 | NULL, |
---|
| 258 | #endif |
---|
[1026] | 259 | STANDARD_MODULE_PROPERTIES_EX |
---|
| 260 | }; |
---|
| 261 | /* }}} */ |
---|
| 262 | int xc_disassembler_startup_module() /* {{{ */ |
---|
| 263 | { |
---|
| 264 | return zend_startup_module(&xcache_disassembler_module_entry); |
---|
| 265 | } |
---|
| 266 | /* }}} */ |
---|