-
Notifications
You must be signed in to change notification settings - Fork 9
/
php_bfr.c
285 lines (233 loc) · 8.77 KB
/
php_bfr.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
+----------------------------------------------------------------------+
| Better Function Replacer |
| based on APD Profiler & Debugger |
+----------------------------------------------------------------------+
| Copyright (c) 2001-2003 Community Connect Inc. |
+----------------------------------------------------------------------+
| This source file is subject to version 2.02 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| http://www.php.net/license/2_02.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Lukas Rist <glaslos@gmail.com> |
| Daniel Cowgill <dcowgill@communityconnect.com> |
| George Schlossnagle <george@lethargy.org> |
| Sterling Hughes <sterling@php.net> |
| Nikita Tokarchuk <nikita@tokarch.uk> |
+----------------------------------------------------------------------+
*/
#include "php_bfr.h"
#include "php_helpers.h"
ZEND_DECLARE_MODULE_GLOBALS(bfr);
/* --------------------------------------------------------------------------
List of exported functions
--------------------------------------------------------------------------- */
ZEND_BEGIN_ARG_INFO(arginfo_override_function, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_rename_function, 0)
ZEND_END_ARG_INFO()
zend_function_entry bfr_functions[] = {
PHP_FE(override_function, arginfo_override_function)
PHP_FE(rename_function, arginfo_rename_function){
NULL, NULL, NULL}};
/* --------------------------------------------------------------------------
Module Entry
--------------------------------------------------------------------------- */
zend_module_entry bfr_module_entry = {
STANDARD_MODULE_HEADER,
"bfr",
bfr_functions,
PHP_MINIT(bfr),
NULL,
PHP_RINIT(bfr),
PHP_RSHUTDOWN(bfr),
PHP_MINFO(bfr),
PHP_BFR_VERSION,
STANDARD_MODULE_PROPERTIES};
#if COMPILE_DL_BFR
ZEND_GET_MODULE(bfr)
#endif
/* ---------------------------------------------------------------------------
PHP Configuration Functions
--------------------------------------------------------------------------- */
PHP_INI_BEGIN()
PHP_INI_END()
/* ---------------------------------------------------------------------------
Module Startup and Shutdown Function Definitions
--------------------------------------------------------------------------- */
static void php_bfr_init_globals(zend_bfr_globals *bfr_globals)
{
memset(bfr_globals, 0, sizeof(zend_bfr_globals));
}
PHP_MINIT_FUNCTION(bfr)
{
ZEND_INIT_MODULE_GLOBALS(bfr, php_bfr_init_globals, NULL);
REGISTER_INI_ENTRIES();
return SUCCESS;
}
PHP_RINIT_FUNCTION(bfr)
{
return SUCCESS;
}
PHP_RSHUTDOWN_FUNCTION(bfr)
{
return SUCCESS;
}
PHP_MINFO_FUNCTION(bfr)
{
php_info_print_table_start();
php_info_print_table_header(2, "Better Function Replacer (BFR)", "Enabled");
php_info_print_table_row(2, "BFR Version", PHP_BFR_VERSION);
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
}
/* ---------------------------------------------------------------------------
PHP Extension Functions
--------------------------------------------------------------------------- */
#define TEMP_OVRD_FUNC_NAME "__overridden__"
#define TEMP_OVRD_FUNC_HEADER "function " TEMP_OVRD_FUNC_NAME
#define TEMP_OVRD_FUNC_PATTERN TEMP_OVRD_FUNC_HEADER "(%s){%s}"
#define TEMP_OVRD_FUNC_DESC "runtime-created override function"
PHP_FUNCTION(override_function)
{
char *eval_code, *eval_name;
int eval_code_length, retval;
char *z_function_name, *z_function_args, *z_function_code;
size_t function_name_len, function_args_len, function_code_len;
zend_function *func, *func_dup;
if (ZEND_NUM_ARGS() != 3 ||
zend_parse_parameters(ZEND_NUM_ARGS(), "sss",
&z_function_name, &function_name_len,
&z_function_args, &function_args_len,
&z_function_code, &function_code_len) == FAILURE)
{
ZEND_WRONG_PARAM_COUNT();
}
eval_code_length = sizeof(TEMP_OVRD_FUNC_HEADER) + function_args_len + 2 /* parentheses */
+ 2 /* curlies */
+ function_code_len;
eval_code = (char *)emalloc(eval_code_length);
sprintf(eval_code, TEMP_OVRD_FUNC_PATTERN, z_function_args, z_function_code);
eval_name = zend_make_compiled_string_description(TEMP_OVRD_FUNC_DESC);
retval = zend_eval_string(eval_code, NULL, eval_name);
efree(eval_code);
efree(eval_name);
if (retval != SUCCESS)
{
zend_error(E_ERROR, "%s() failed to eval temporary function",
get_active_function_name());
RETURN_FALSE;
}
if ((func = zend_hash_str_find_ptr(EG(function_table),
TEMP_OVRD_FUNC_NAME, sizeof(TEMP_OVRD_FUNC_NAME) - 1)) == NULL)
{
zend_error(E_ERROR, "%s() temporary function name not present in global function_table",
get_active_function_name());
RETURN_FALSE;
}
func_dup = duplicate_function(func);
if (zend_hash_str_exists(EG(function_table),
z_function_name, function_name_len))
{
zend_hash_str_del(EG(function_table),
z_function_name, function_name_len);
}
if (zend_hash_str_add_new_ptr(EG(function_table),
z_function_name, function_name_len,
func_dup) == NULL)
{
zend_error(E_ERROR, "%s() failed to add function",
get_active_function_name());
RETURN_FALSE;
}
if (zend_hash_str_del(EG(function_table), TEMP_OVRD_FUNC_NAME,
sizeof(TEMP_OVRD_FUNC_NAME) - 1) == FAILURE)
{
zend_error(E_ERROR, "%s() failed to delete temporary function",
get_active_function_name());
zend_hash_str_del(EG(function_table),
z_function_name, function_name_len);
RETURN_FALSE;
}
}
PHP_FUNCTION(rename_function)
{
char *z_orig_fname, *z_new_fname;
size_t orig_fname_len, new_fname_len;
zend_function *func, *func_dup;
if (ZEND_NUM_ARGS() != 2 ||
zend_parse_parameters(ZEND_NUM_ARGS(), "ss",
&z_orig_fname, &orig_fname_len,
&z_new_fname, &new_fname_len) == FAILURE)
{
ZEND_WRONG_PARAM_COUNT();
}
if ((func = zend_hash_str_find_ptr(EG(function_table),
z_orig_fname, orig_fname_len)) == NULL)
{
zend_error(E_WARNING, "%s(%s, %s) failed: %s does not exist!",
get_active_function_name(),
z_orig_fname, z_new_fname, z_orig_fname);
RETURN_FALSE;
}
if (zend_hash_str_exists(EG(function_table), z_new_fname, new_fname_len))
{
zend_error(E_WARNING, "%s(%s, %s) failed: %s already exists!",
get_active_function_name(),
z_orig_fname, z_new_fname, z_new_fname);
RETURN_FALSE;
}
func_dup = duplicate_function(func);
if (zend_hash_str_add_ptr(EG(function_table), z_new_fname, new_fname_len, func_dup) == NULL)
{
zend_error(E_WARNING, "%s() failed to insert %s into EG(function_table)",
get_active_function_name(), z_new_fname);
RETURN_FALSE;
}
if (zend_hash_str_del(EG(function_table), z_orig_fname, orig_fname_len) == FAILURE)
{
zend_error(E_WARNING, "%s() failed to remove %s from function table",
get_active_function_name(), z_orig_fname);
zend_hash_str_del(EG(function_table), z_new_fname, new_fname_len);
RETURN_FALSE;
}
RETURN_TRUE;
}
// ---------------------------------------------------------------------------
// Zend Extension Functions
// ---------------------------------------------------------------------------
int bfr_zend_startup(zend_extension *extension)
{
return zend_startup_module(&bfr_module_entry);
}
ZEND_DLEXPORT void bfr_zend_shutdown(zend_extension *extension)
{
/* Do nothing. */
}
#ifndef ZEND_EXT_API
#define ZEND_EXT_API ZEND_DLEXPORT
#endif
ZEND_EXTENSION();
ZEND_DLEXPORT zend_extension zend_extension_entry = {
"Better Function Replacer (BFR)",
PHP_BFR_VERSION,
"Lukas Rist",
"http://mushmush.org/",
"Copyright (C) 2023",
bfr_zend_startup,
bfr_zend_shutdown,
NULL, // activate_func_t
NULL, // deactivate_func_t
NULL, // message_handler_func_t
NULL, // op_array_handler_func_t
NULL, // statement_handler_func_t
NULL, // fcall_begin_handler_func_t
NULL, // fcall_end_handler_func_t
NULL, // op_array_ctor_func_t
NULL, // op_array_dtor_func_t
NULL, // api_no_check
COMPAT_ZEND_EXTENSION_PROPERTIES};