-
Notifications
You must be signed in to change notification settings - Fork 0
/
xray.c
193 lines (149 loc) · 4.58 KB
/
xray.c
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
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "zend_types.h"
#include "zend_exceptions.h"
#include "php_xray.h"
ZEND_DECLARE_MODULE_GLOBALS(xray);
static ZEND_FUNCTION(set_compiler_hook);
ZEND_BEGIN_ARG_INFO_EX(arginfo_xray_set_compiler_hook, 0, 0, 1)
ZEND_ARG_INFO(0, callable)
ZEND_END_ARG_INFO()
ZEND_FUNCTION(set_compiler_hook)
{
zval *hook;
zend_string *hook_name = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &hook) == FAILURE) {
return;
}
if (!zend_is_callable(hook, 0, &hook_name)) {
zend_error(E_WARNING, "%s() expects the argument (%s) to be a valid callback",
get_active_function_name(), hook_name ? ZSTR_VAL(hook_name) : "unknown");
zend_string_release(hook_name);
RETURN_FALSE;
}
ZVAL_COPY_VALUE(XRAY_G(hook), hook);
ZVAL_COPY(XRAY_G(hook), hook);
RETURN_TRUE;
}
static ZEND_FUNCTION(restore_compiler_hook);
ZEND_BEGIN_ARG_INFO(arginfo_xray_restore_compiler_hook, 0)
ZEND_END_ARG_INFO()
ZEND_FUNCTION(restore_compiler_hook)
{
if (zend_parse_parameters_none() == FAILURE) {
return;
}
ZVAL_COPY(return_value, XRAY_G(hook));
ZVAL_NULL(XRAY_G(hook));
}
static const zend_function_entry xray_functions[] = { /* {{{ */
ZEND_NS_FE(XRAY_NS, set_compiler_hook, arginfo_xray_set_compiler_hook)
ZEND_NS_FE(XRAY_NS, restore_compiler_hook, arginfo_xray_restore_compiler_hook)
ZEND_FE_END
};
/* }}} */
ZEND_API zend_op_array *xray_compile_string(zval *src, char *filename) {
zval params[2], result;
zend_op_array *op_array;
XRAY_IF_HOOK_IS_AVAILABLE() {
ZVAL_NULL(&result);
params[0] = *src; // source string
ZVAL_NULL(¶ms[1]); // file name
if (XRAY_CALL_COMPILER_HOOK(result, params)) {
if (Z_TYPE(result) == IS_STRING) {
src = &result;
}
else {
XRAY_THROW_RESULT_MUST_BE_STRING();
}
}
zval_dtor(¶ms[1]);
}
op_array = orig_zend_compile_string(src, filename);
XRAY_IF_HOOK_IS_AVAILABLE() {
zval_dtor(&result);
}
return op_array;
}
ZEND_API zend_op_array* xray_compile_file(zend_file_handle* file, int type) {
char *buf;
size_t size;
zend_op_array *op_array;
zend_file_handle fake;
zval params[2], result;
int status = zend_stream_fixup(file, &buf, &size);
if (status == SUCCESS) {
XRAY_IF_HOOK_IS_AVAILABLE() {
ZVAL_NULL(&result);
char *filename = (char *)(file->opened_path ? ZSTR_VAL(file->opened_path) : file->filename);
ZVAL_STRINGL(¶ms[0], buf, size); // source string
ZVAL_STRINGL(¶ms[1], filename, strlen(filename)); // file name
if (XRAY_CALL_COMPILER_HOOK(result, params)) {
if (Z_TYPE(result) == IS_STRING) {
memset(&fake, 0, sizeof(fake));
fake.type = ZEND_HANDLE_MAPPED;
fake.free_filename = 0;
fake.filename = (char *)(file->opened_path ? ZSTR_VAL(file->opened_path) : file->filename);
fake.opened_path = file->opened_path;
fake.handle.stream.mmap.buf = ZSTR_VAL(Z_STR(result));
fake.handle.stream.mmap.len = ZSTR_LEN(Z_STR(result));
file = &fake;
}
else {
XRAY_THROW_RESULT_MUST_BE_STRING();
}
}
zval_dtor(¶ms[0]);
zval_dtor(¶ms[1]);
}
}
op_array = orig_zend_compile_file(file, type);
if (status == SUCCESS) {
XRAY_IF_HOOK_IS_AVAILABLE() {
zval_dtor(&result);
if (Z_TYPE(result) == IS_STRING) {
fake.opened_path = NULL;
zend_file_handle_dtor(&fake);
}
}
}
return op_array;
}
/* {{{ */
static void php_xray_init_globals(zend_xray_globals *xray_globals) {
ZVAL_NULL(&xray_globals->hook);
} /* }}} */
/* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(xray)
{
ZEND_INIT_MODULE_GLOBALS(xray, php_xray_init_globals, NULL);
XRAY_HOOK(compile_file);
XRAY_HOOK(compile_string);
return SUCCESS;
}
/* }}} */
/* {{{ xray_module_entry */
zend_module_entry xray_module_entry = {
STANDARD_MODULE_HEADER,
"xray",
xray_functions,
PHP_MINIT(xray),
NULL,
NULL,
NULL,
NULL,
"0.1.0",
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_XRAY
ZEND_GET_MODULE(xray)
#endif
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
*/