| 1 | #include "xc_disassembler.h" |
|---|
| 2 | #include "xcache.h" |
|---|
| 3 | #include "xcache/xc_ini.h" |
|---|
| 4 | #include "xcache/xc_utils.h" |
|---|
| 5 | #include "xcache/xc_sandbox.h" |
|---|
| 6 | #include "xcache/xc_compatibility.h" |
|---|
| 7 | #include "xc_processor.h" |
|---|
| 8 | |
|---|
| 9 | #include "ext/standard/info.h" |
|---|
| 10 | |
|---|
| 11 | static void xc_dasm(zval *output, zend_op_array *op_array TSRMLS_DC) /* {{{ */ |
|---|
| 12 | { |
|---|
| 13 | const Bucket *b; |
|---|
| 14 | zval *zv, *list; |
|---|
| 15 | xc_compile_result_t cr; |
|---|
| 16 | int bufsize = 2; |
|---|
| 17 | char *buf; |
|---|
| 18 | xc_dasm_t dasm; |
|---|
| 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 */ |
|---|
| 26 | array_init(output); |
|---|
| 27 | |
|---|
| 28 | ALLOC_INIT_ZVAL(zv); |
|---|
| 29 | array_init(zv); |
|---|
| 30 | xc_dasm_zend_op_array(&dasm, zv, op_array TSRMLS_CC); |
|---|
| 31 | add_assoc_zval_ex(output, ZEND_STRS("op_array"), zv); |
|---|
| 32 | |
|---|
| 33 | buf = emalloc(bufsize); |
|---|
| 34 | |
|---|
| 35 | ALLOC_INIT_ZVAL(list); |
|---|
| 36 | array_init(list); |
|---|
| 37 | for (b = xc_sandbox_user_function_begin(TSRMLS_C); b; b = b->pListNext) { |
|---|
| 38 | int keysize, keyLength; |
|---|
| 39 | |
|---|
| 40 | ALLOC_INIT_ZVAL(zv); |
|---|
| 41 | array_init(zv); |
|---|
| 42 | xc_dasm_zend_function(&dasm, zv, b->pData TSRMLS_CC); |
|---|
| 43 | |
|---|
| 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); |
|---|
| 68 | } |
|---|
| 69 | add_assoc_zval_ex(output, ZEND_STRS("function_table"), list); |
|---|
| 70 | |
|---|
| 71 | ALLOC_INIT_ZVAL(list); |
|---|
| 72 | array_init(list); |
|---|
| 73 | for (b = xc_sandbox_user_class_begin(TSRMLS_C); b; b = b->pListNext) { |
|---|
| 74 | int keysize, keyLength; |
|---|
| 75 | |
|---|
| 76 | ALLOC_INIT_ZVAL(zv); |
|---|
| 77 | array_init(zv); |
|---|
| 78 | xc_dasm_zend_class_entry(&dasm, zv, CestToCePtr(*(xc_cest_t *)b->pData) TSRMLS_CC); |
|---|
| 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 | } |
|---|
| 87 | memcpy(buf, BUCKET_KEY_S(b), keysize); |
|---|
| 88 | buf[keysize - 2] = buf[keysize - 1] = ""[0]; |
|---|
| 89 | keyLength = b->nKeyLength; |
|---|
| 90 | #ifdef IS_UNICODE |
|---|
| 91 | if (BUCKET_KEY_TYPE(b) == IS_UNICODE) { |
|---|
| 92 | if (buf[0] == ""[0] && buf[1] == ""[0]) { |
|---|
| 93 | keyLength ++; |
|---|
| 94 | } |
|---|
| 95 | } else |
|---|
| 96 | #endif |
|---|
| 97 | { |
|---|
| 98 | if (buf[0] == ""[0]) { |
|---|
| 99 | keyLength ++; |
|---|
| 100 | } |
|---|
| 101 | } |
|---|
| 102 | add_u_assoc_zval_ex(list, BUCKET_KEY_TYPE(b), ZSTR(buf), keyLength, zv); |
|---|
| 103 | } |
|---|
| 104 | efree(buf); |
|---|
| 105 | add_assoc_zval_ex(output, ZEND_STRS("class_table"), list); |
|---|
| 106 | |
|---|
| 107 | /*xc_apply_op_array(&cr, (apply_func_t) xc_redo_pass_two TSRMLS_CC);*/ |
|---|
| 108 | xc_compile_result_free(&cr); |
|---|
| 109 | } |
|---|
| 110 | /* }}} */ |
|---|
| 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; |
|---|
| 125 | } xc_dasm_sandboxed_t; /* }}} */ |
|---|
| 126 | zend_op_array *xc_dasm_sandboxed(void *data TSRMLS_DC) /* {{{ */ |
|---|
| 127 | { |
|---|
| 128 | zend_bool catched = 0; |
|---|
| 129 | zend_op_array *op_array = NULL; |
|---|
| 130 | xc_dasm_sandboxed_t *sandboxed_dasm = (xc_dasm_sandboxed_t *) data; |
|---|
| 131 | |
|---|
| 132 | zend_try { |
|---|
| 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 | } |
|---|
| 139 | } zend_catch { |
|---|
| 140 | catched = 1; |
|---|
| 141 | } zend_end_try(); |
|---|
| 142 | |
|---|
| 143 | if (catched || !op_array) { |
|---|
| 144 | #define return_value sandboxed_dasm->output |
|---|
| 145 | RETVAL_FALSE; |
|---|
| 146 | #undef return_value |
|---|
| 147 | return NULL; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | xc_dasm(sandboxed_dasm->output, op_array TSRMLS_CC); |
|---|
| 151 | |
|---|
| 152 | /* free */ |
|---|
| 153 | #ifdef ZEND_ENGINE_2 |
|---|
| 154 | destroy_op_array(op_array TSRMLS_CC); |
|---|
| 155 | #else |
|---|
| 156 | destroy_op_array(op_array); |
|---|
| 157 | #endif |
|---|
| 158 | efree(op_array); |
|---|
| 159 | |
|---|
| 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); |
|---|
| 172 | efree(eval_name); |
|---|
| 173 | } |
|---|
| 174 | /* }}} */ |
|---|
| 175 | void xc_dasm_file(zval *output, const char *filename TSRMLS_DC) /* {{{ */ |
|---|
| 176 | { |
|---|
| 177 | zval *zfilename; |
|---|
| 178 | xc_dasm_sandboxed_t sandboxed_dasm; |
|---|
| 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 | |
|---|
| 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); |
|---|
| 189 | |
|---|
| 190 | zval_dtor(zfilename); |
|---|
| 191 | FREE_ZVAL(zfilename); |
|---|
| 192 | } |
|---|
| 193 | /* }}} */ |
|---|
| 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 | /* }}} */ |
|---|
| 222 | |
|---|
| 223 | /* {{{ PHP_MINFO_FUNCTION(xcache_disassembler) */ |
|---|
| 224 | static PHP_MINFO_FUNCTION(xcache_disassembler) |
|---|
| 225 | { |
|---|
| 226 | php_info_print_table_start(); |
|---|
| 227 | php_info_print_table_row(2, "XCache Disassembler Version", XCACHE_VERSION); |
|---|
| 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, |
|---|
| 242 | XCACHE_NAME "_Disassembler", |
|---|
| 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 |
|---|
| 253 | #ifdef ZEND_ENGINE_2 |
|---|
| 254 | NULL, |
|---|
| 255 | #else |
|---|
| 256 | NULL, |
|---|
| 257 | NULL, |
|---|
| 258 | #endif |
|---|
| 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 | /* }}} */ |
|---|