File: disassembler.c

package info (click to toggle)
xcache 2.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,724 kB
  • sloc: ansic: 8,175; php: 4,557; awk: 285; sh: 135; makefile: 75
file content (208 lines) | stat: -rw-r--r-- 4,729 bytes parent folder | download
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
#include "disassembler.h"
#include "xcache.h"
#include "utils.h"
#include "processor.h"

#define return_value dst

/* sandbox {{{ */
#undef TG
#undef OG
#define TG(x) (sandbox->tmp_##x)
#define OG(x) (sandbox->orig_##x)
/* }}} */

#ifndef HAVE_XCACHE_OPCODE_SPEC_DEF
#error disassembler cannot be built without xcache/opcode_spec_def.h
#endif
static void xc_dasm(xc_sandbox_t *sandbox, zval *dst, zend_op_array *op_array TSRMLS_DC) /* {{{ */
{
	Bucket *b;
	zval *zv, *list;
	xc_compile_result_t cr;
	int bufsize = 2;
	char *buf;
	xc_dasm_t dasm;

	xc_compile_result_init_cur(&cr, op_array TSRMLS_CC);

	xc_apply_op_array(&cr, (apply_func_t) xc_undo_pass_two TSRMLS_CC);
	xc_apply_op_array(&cr, (apply_func_t) xc_fix_opcode TSRMLS_CC);

	/* go */
	array_init(dst);

	ALLOC_INIT_ZVAL(zv);
	array_init(zv);
	xc_dasm_zend_op_array(&dasm, zv, op_array TSRMLS_CC);
	add_assoc_zval_ex(dst, ZEND_STRS("op_array"), zv);

	buf = emalloc(bufsize);

	ALLOC_INIT_ZVAL(list);
	array_init(list);
	b = TG(internal_function_tail) ? TG(internal_function_tail)->pListNext : TG(function_table).pListHead;
	for (; b; b = b->pListNext) {
		int keysize, keyLength;

		ALLOC_INIT_ZVAL(zv);
		array_init(zv);
		xc_dasm_zend_function(&dasm, zv, b->pData TSRMLS_CC);

		keysize = BUCKET_KEY_SIZE(b) + 2;
		if (keysize > bufsize) {
			do {
				bufsize *= 2;
			} while (keysize > bufsize);
			buf = erealloc(buf, bufsize);
		}
		memcpy(buf, BUCKET_KEY_S(b), keysize);
		buf[keysize - 2] = buf[keysize - 1] = ""[0];
		keyLength = b->nKeyLength;
#ifdef IS_UNICODE
		if (BUCKET_KEY_TYPE(b) == IS_UNICODE) {
			if (buf[0] == ""[0] && buf[1] == ""[0]) {
				keyLength ++;
			}
		} else
#endif
		{
			if (buf[0] == ""[0]) {
				keyLength ++;
			}
		}

		add_u_assoc_zval_ex(list, BUCKET_KEY_TYPE(b), ZSTR(buf), keyLength, zv);
	}
	add_assoc_zval_ex(dst, ZEND_STRS("function_table"), list);
	
	ALLOC_INIT_ZVAL(list);
	array_init(list);
	b = TG(internal_class_tail) ? TG(internal_class_tail)->pListNext : TG(class_table).pListHead;
	for (; b; b = b->pListNext) {
		int keysize, keyLength;

		ALLOC_INIT_ZVAL(zv);
		array_init(zv);
		xc_dasm_zend_class_entry(&dasm, zv, CestToCePtr(*(xc_cest_t *)b->pData) TSRMLS_CC);

		keysize = BUCKET_KEY_SIZE(b) + 2;
		if (keysize > bufsize) {
			do {
				bufsize *= 2;
			} while (keysize > bufsize);
			buf = erealloc(buf, bufsize);
		}
		memcpy(buf, BUCKET_KEY_S(b), keysize);
		buf[keysize - 2] = buf[keysize - 1] = ""[0];
		keyLength = b->nKeyLength;
#ifdef IS_UNICODE
		if (BUCKET_KEY_TYPE(b) == IS_UNICODE) {
			if (buf[0] == ""[0] && buf[1] == ""[0]) {
				keyLength ++;
			}
		} else
#endif
		{
			if (buf[0] == ""[0]) {
				keyLength ++;
			}
		}
		add_u_assoc_zval_ex(list, BUCKET_KEY_TYPE(b), ZSTR(buf), keyLength, zv);
	}
	efree(buf);
	add_assoc_zval_ex(dst, ZEND_STRS("class_table"), list);

	/*xc_apply_op_array(&cr, (apply_func_t) xc_redo_pass_two TSRMLS_CC);*/
	xc_compile_result_free(&cr);

	return;
}
/* }}} */
void xc_dasm_string(zval *dst, zval *source TSRMLS_DC) /* {{{ */
{
	int catched;
	zend_op_array *op_array = NULL;
	xc_sandbox_t sandbox;
	char *eval_name = zend_make_compiled_string_description("runtime-created function" TSRMLS_CC);

	xc_sandbox_init(&sandbox, eval_name TSRMLS_CC);

	catched = 0;
	zend_try {
		op_array = compile_string(source, eval_name TSRMLS_CC);
	} zend_catch {
		catched = 1;
	} zend_end_try();

	if (catched || !op_array) {
		goto err_compile;
	}

	xc_dasm(&sandbox, dst, op_array TSRMLS_CC);

	/* free */
	efree(eval_name);
#ifdef ZEND_ENGINE_2
	destroy_op_array(op_array TSRMLS_CC);
#else
	destroy_op_array(op_array);
#endif
	efree(op_array);
	xc_sandbox_free(&sandbox, 0 TSRMLS_CC);
	return;

err_compile:
	efree(eval_name);
	xc_sandbox_free(&sandbox, 0 TSRMLS_CC);

	RETURN_FALSE;
}
/* }}} */
void xc_dasm_file(zval *dst, const char *filename TSRMLS_DC) /* {{{ */
{
	int catched;
	zend_op_array *op_array = NULL;
	xc_sandbox_t sandbox;
	zval *zfilename;

	MAKE_STD_ZVAL(zfilename);
	zfilename->value.str.val = estrdup(filename);
	zfilename->value.str.len = strlen(filename);
	zfilename->type = IS_STRING;

	xc_sandbox_init(&sandbox, zfilename->value.str.val TSRMLS_CC);

	catched = 0;
	zend_try {
		op_array = compile_filename(ZEND_REQUIRE, zfilename TSRMLS_CC);
	} zend_catch {
		catched = 1;
	} zend_end_try();

	if (catched || !op_array) {
		goto err_compile;
	}

	xc_dasm(&sandbox, dst, op_array TSRMLS_CC);

	/* free */
#ifdef ZEND_ENGINE_2
	destroy_op_array(op_array TSRMLS_CC);
#else
	destroy_op_array(op_array);
#endif
	efree(op_array);
	xc_sandbox_free(&sandbox, 0 TSRMLS_CC);
	zval_dtor(zfilename);
	FREE_ZVAL(zfilename);
	return;

err_compile:
	xc_sandbox_free(&sandbox, 0 TSRMLS_CC);

	zval_dtor(zfilename);
	FREE_ZVAL(zfilename);
	RETURN_FALSE;
}
/* }}} */