[1] | 1 | #include "disassembler.h" |
---|
| 2 | #include "xcache.h" |
---|
| 3 | #include "utils.h" |
---|
| 4 | #include "processor.h" |
---|
| 5 | |
---|
| 6 | #define return_value dst |
---|
| 7 | |
---|
[709] | 8 | /* sandbox {{{ */ |
---|
| 9 | #undef TG |
---|
| 10 | #undef OG |
---|
| 11 | #define TG(x) (sandbox->tmp_##x) |
---|
| 12 | #define OG(x) (sandbox->orig_##x) |
---|
| 13 | /* }}} */ |
---|
| 14 | |
---|
[8] | 15 | #ifndef HAVE_XCACHE_OPCODE_SPEC_DEF |
---|
| 16 | #error disassembler cannot be built without xcache/opcode_spec_def.h |
---|
| 17 | #endif |
---|
[709] | 18 | static void xc_dasm(xc_sandbox_t *sandbox, zval *dst, zend_op_array *op_array TSRMLS_DC) /* {{{ */ |
---|
[1] | 19 | { |
---|
| 20 | Bucket *b; |
---|
| 21 | zval *zv, *list; |
---|
| 22 | xc_compile_result_t cr; |
---|
| 23 | int bufsize = 2; |
---|
| 24 | char *buf; |
---|
| 25 | |
---|
| 26 | xc_compile_result_init_cur(&cr, op_array TSRMLS_CC); |
---|
| 27 | |
---|
| 28 | xc_apply_op_array(&cr, (apply_func_t) xc_undo_pass_two TSRMLS_CC); |
---|
| 29 | xc_apply_op_array(&cr, (apply_func_t) xc_fix_opcode TSRMLS_CC); |
---|
| 30 | |
---|
| 31 | /* go */ |
---|
| 32 | array_init(dst); |
---|
| 33 | |
---|
| 34 | ALLOC_INIT_ZVAL(zv); |
---|
| 35 | array_init(zv); |
---|
| 36 | xc_dasm_zend_op_array(zv, op_array TSRMLS_CC); |
---|
| 37 | add_assoc_zval_ex(dst, ZEND_STRS("op_array"), zv); |
---|
| 38 | |
---|
| 39 | ALLOC_INIT_ZVAL(list); |
---|
| 40 | array_init(list); |
---|
[709] | 41 | b = TG(internal_function_tail) ? TG(internal_function_tail)->pListNext : TG(function_table).pListHead; |
---|
| 42 | for (; b; b = b->pListNext) { |
---|
| 43 | ALLOC_INIT_ZVAL(zv); |
---|
| 44 | array_init(zv); |
---|
| 45 | xc_dasm_zend_function(zv, b->pData TSRMLS_CC); |
---|
| 46 | |
---|
[714] | 47 | add_u_assoc_zval_ex(list, BUCKET_KEY_TYPE(b), ZSTR(BUCKET_KEY_S(b)), b->nKeyLength, zv); |
---|
[709] | 48 | } |
---|
[1] | 49 | add_assoc_zval_ex(dst, ZEND_STRS("function_table"), list); |
---|
| 50 | |
---|
| 51 | buf = emalloc(bufsize); |
---|
| 52 | ALLOC_INIT_ZVAL(list); |
---|
| 53 | array_init(list); |
---|
[709] | 54 | b = TG(internal_class_tail) ? TG(internal_class_tail)->pListNext : TG(class_table).pListHead; |
---|
| 55 | for (; b; b = b->pListNext) { |
---|
[714] | 56 | int keysize, keyLength; |
---|
| 57 | |
---|
[1] | 58 | ALLOC_INIT_ZVAL(zv); |
---|
| 59 | array_init(zv); |
---|
| 60 | xc_dasm_zend_class_entry(zv, CestToCePtr(*(xc_cest_t *)b->pData) TSRMLS_CC); |
---|
| 61 | |
---|
| 62 | keysize = BUCKET_KEY_SIZE(b) + 2; |
---|
| 63 | if (keysize > bufsize) { |
---|
| 64 | do { |
---|
| 65 | bufsize *= 2; |
---|
| 66 | } while (keysize > bufsize); |
---|
| 67 | buf = erealloc(buf, bufsize); |
---|
| 68 | } |
---|
[200] | 69 | memcpy(buf, BUCKET_KEY_S(b), keysize); |
---|
[1] | 70 | buf[keysize - 2] = buf[keysize - 1] = ""[0]; |
---|
[714] | 71 | keyLength = b->nKeyLength; |
---|
[1] | 72 | #ifdef IS_UNICODE |
---|
| 73 | if (BUCKET_KEY_TYPE(b) == IS_UNICODE) { |
---|
| 74 | if (buf[0] == ""[0] && buf[1] == ""[0]) { |
---|
[714] | 75 | keyLength ++; |
---|
[1] | 76 | } |
---|
| 77 | } else |
---|
| 78 | #endif |
---|
| 79 | { |
---|
| 80 | if (buf[0] == ""[0]) { |
---|
[714] | 81 | keyLength ++; |
---|
[1] | 82 | } |
---|
| 83 | } |
---|
[714] | 84 | add_u_assoc_zval_ex(list, BUCKET_KEY_TYPE(b), ZSTR(buf), keyLength, zv); |
---|
[1] | 85 | } |
---|
| 86 | efree(buf); |
---|
| 87 | add_assoc_zval_ex(dst, ZEND_STRS("class_table"), list); |
---|
| 88 | |
---|
| 89 | /*xc_apply_op_array(&cr, (apply_func_t) xc_redo_pass_two TSRMLS_CC);*/ |
---|
| 90 | xc_compile_result_free(&cr); |
---|
| 91 | |
---|
| 92 | return; |
---|
| 93 | } |
---|
| 94 | /* }}} */ |
---|
| 95 | void xc_dasm_string(zval *dst, zval *source TSRMLS_DC) /* {{{ */ |
---|
| 96 | { |
---|
| 97 | int catched; |
---|
| 98 | zend_op_array *op_array = NULL; |
---|
| 99 | xc_sandbox_t sandbox; |
---|
| 100 | char *eval_name = zend_make_compiled_string_description("runtime-created function" TSRMLS_CC); |
---|
| 101 | |
---|
| 102 | xc_sandbox_init(&sandbox, eval_name TSRMLS_CC); |
---|
| 103 | |
---|
| 104 | catched = 0; |
---|
| 105 | zend_try { |
---|
| 106 | op_array = compile_string(source, eval_name TSRMLS_CC); |
---|
| 107 | } zend_catch { |
---|
| 108 | catched = 1; |
---|
| 109 | } zend_end_try(); |
---|
| 110 | |
---|
| 111 | if (catched || !op_array) { |
---|
| 112 | goto err_compile; |
---|
| 113 | } |
---|
| 114 | |
---|
[709] | 115 | xc_dasm(&sandbox, dst, op_array TSRMLS_CC); |
---|
[1] | 116 | |
---|
| 117 | /* free */ |
---|
| 118 | efree(eval_name); |
---|
[289] | 119 | #ifdef ZEND_ENGINE_2 |
---|
[1] | 120 | destroy_op_array(op_array TSRMLS_CC); |
---|
[289] | 121 | #else |
---|
| 122 | destroy_op_array(op_array); |
---|
| 123 | #endif |
---|
[1] | 124 | efree(op_array); |
---|
| 125 | xc_sandbox_free(&sandbox, 0 TSRMLS_CC); |
---|
| 126 | return; |
---|
| 127 | |
---|
| 128 | err_compile: |
---|
| 129 | efree(eval_name); |
---|
| 130 | xc_sandbox_free(&sandbox, 0 TSRMLS_CC); |
---|
| 131 | |
---|
| 132 | RETURN_FALSE; |
---|
| 133 | } |
---|
| 134 | /* }}} */ |
---|
| 135 | void xc_dasm_file(zval *dst, const char *filename TSRMLS_DC) /* {{{ */ |
---|
| 136 | { |
---|
| 137 | int catched; |
---|
| 138 | zend_op_array *op_array = NULL; |
---|
| 139 | xc_sandbox_t sandbox; |
---|
| 140 | zval *zfilename; |
---|
| 141 | |
---|
| 142 | MAKE_STD_ZVAL(zfilename); |
---|
| 143 | zfilename->value.str.val = estrdup(filename); |
---|
| 144 | zfilename->value.str.len = strlen(filename); |
---|
| 145 | zfilename->type = IS_STRING; |
---|
| 146 | |
---|
| 147 | xc_sandbox_init(&sandbox, zfilename->value.str.val TSRMLS_CC); |
---|
| 148 | |
---|
| 149 | catched = 0; |
---|
| 150 | zend_try { |
---|
| 151 | op_array = compile_filename(ZEND_REQUIRE, zfilename TSRMLS_CC); |
---|
| 152 | } zend_catch { |
---|
| 153 | catched = 1; |
---|
| 154 | } zend_end_try(); |
---|
| 155 | |
---|
| 156 | if (catched || !op_array) { |
---|
| 157 | goto err_compile; |
---|
| 158 | } |
---|
| 159 | |
---|
[709] | 160 | xc_dasm(&sandbox, dst, op_array TSRMLS_CC); |
---|
[1] | 161 | |
---|
| 162 | /* free */ |
---|
[289] | 163 | #ifdef ZEND_ENGINE_2 |
---|
[1] | 164 | destroy_op_array(op_array TSRMLS_CC); |
---|
[289] | 165 | #else |
---|
| 166 | destroy_op_array(op_array); |
---|
| 167 | #endif |
---|
[1] | 168 | efree(op_array); |
---|
| 169 | xc_sandbox_free(&sandbox, 0 TSRMLS_CC); |
---|
| 170 | zval_dtor(zfilename); |
---|
| 171 | FREE_ZVAL(zfilename); |
---|
| 172 | return; |
---|
| 173 | |
---|
| 174 | err_compile: |
---|
| 175 | xc_sandbox_free(&sandbox, 0 TSRMLS_CC); |
---|
| 176 | |
---|
| 177 | zval_dtor(zfilename); |
---|
| 178 | FREE_ZVAL(zfilename); |
---|
| 179 | RETURN_FALSE; |
---|
| 180 | } |
---|
| 181 | /* }}} */ |
---|