parser: LET foo = CALLBACK[(var_list spec) AS ] LET_CALLBACK
[m6w6/ext-psi] / src / module.c
1 /*******************************************************************************
2 Copyright (c) 2016, Michael Wallner <mike@php.net>.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
18 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *******************************************************************************/
25
26 #include "php_psi_stdinc.h"
27
28 #include "php.h"
29 #include "php_ini.h"
30 #include "ext/standard/info.h"
31 #include "zend_constants.h"
32 #include "zend_operators.h"
33
34 #include "php_psi.h"
35 #include "token.h"
36 #include "parser.h"
37
38 #define PSI_CPP_SEARCH
39 #include "php_psi_cpp.h"
40
41 #if HAVE_LIBJIT
42 # include "libjit.h"
43 # ifndef HAVE_LIBFFI
44 # define PSI_ENGINE "jit"
45 # endif
46 #endif
47 #if HAVE_LIBFFI
48 # include "libffi.h"
49 # define PSI_ENGINE "ffi"
50 #endif
51
52 ZEND_DECLARE_MODULE_GLOBALS(psi);
53
54 PHP_INI_BEGIN()
55 STD_PHP_INI_ENTRY("psi.engine", PSI_ENGINE, PHP_INI_SYSTEM, OnUpdateString, engine, zend_psi_globals, psi_globals)
56 STD_PHP_INI_ENTRY("psi.directory", "psi.d", PHP_INI_SYSTEM, OnUpdateString, directory, zend_psi_globals, psi_globals)
57 PHP_INI_END();
58
59 static zend_object_handlers psi_object_handlers;
60 static zend_class_entry *psi_class_entry;
61
62 zend_class_entry *psi_object_get_class_entry()
63 {
64 return psi_class_entry;
65 }
66
67 static void psi_object_free(zend_object *o)
68 {
69 psi_object *obj = PSI_OBJ(NULL, o);
70
71 if (obj->data) {
72 if (obj->dtor) {
73 obj->dtor(obj->data);
74 }
75 obj->data = NULL;
76 }
77 zend_object_std_dtor(o);
78 }
79
80 zend_object *psi_object_init_ex(zend_class_entry *ce, void *data, void (*dtor)(void *))
81 {
82 psi_object *o;
83
84 if (!ce) {
85 ce = psi_class_entry;
86 }
87
88 o = ecalloc(1, sizeof(*o) + zend_object_properties_size(ce));
89
90 o->data = data;
91 o->dtor = dtor;
92
93 zend_object_std_init(&o->std, ce);
94 object_properties_init(&o->std, ce);
95 o->std.handlers = &psi_object_handlers;
96 return &o->std;
97 }
98
99 zend_object *psi_object_init(zend_class_entry *ce)
100 {
101 return psi_object_init_ex(ce, NULL, NULL);
102 }
103
104 ZEND_BEGIN_ARG_INFO_EX(ai_psi_dump, 0, 0, 0)
105 ZEND_ARG_INFO(0, stream)
106 ZEND_END_ARG_INFO();
107 static PHP_FUNCTION(psi_dump)
108 {
109 php_stream *s;
110 zval *r = NULL;
111 int fd = STDOUT_FILENO;
112
113 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "|r!", &r)) {
114 return;
115 }
116 if (r) {
117 php_stream_from_zval(s, r);
118
119 if (SUCCESS != php_stream_cast(s, PHP_STREAM_AS_FD | PHP_STREAM_CAST_INTERNAL, (void **)&fd, 1)) {
120 RETURN_FALSE;
121 }
122 }
123 psi_context_dump(PSI_G(context), fd);
124 }
125
126 ZEND_BEGIN_ARG_INFO_EX(ai_psi_validate, 0, 0, 1)
127 ZEND_ARG_INFO(0, file)
128 ZEND_ARG_INFO(0, flags)
129 ZEND_END_ARG_INFO();
130 static PHP_FUNCTION(psi_validate)
131 {
132 zend_string *file;
133 struct psi_parser_input *I;
134 struct psi_parser P;
135 struct psi_data D = {0};
136 zend_long flags = 0;
137
138 #if PHP_DEBUG
139 if (psi_check_env("PSI_DEBUG")) {
140 flags |= PSI_DEBUG;
141 }
142 #endif
143
144 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "P|l", &file, &flags)) {
145 return;
146 }
147
148 if (!psi_parser_init(&P, psi_error_wrapper, flags)) {
149 RETURN_FALSE;
150 }
151 if (!(I = psi_parser_open_file(&P, file->val, true))) {
152 psi_parser_dtor(&P);
153 RETURN_FALSE;
154 }
155
156 psi_parser_parse(&P, I);
157 psi_data_ctor(&D, P.error, P.flags);
158 RETVAL_BOOL(psi_data_validate(&D, PSI_DATA(&P)) && !P.errors);
159 psi_data_dtor(&D);
160 psi_parser_dtor(&P);
161 free(I);
162 }
163
164 ZEND_BEGIN_ARG_INFO_EX(ai_psi_validate_string, 0, 0, 1)
165 ZEND_ARG_INFO(0, string)
166 ZEND_END_ARG_INFO();
167 static PHP_FUNCTION(psi_validate_string)
168 {
169 zend_string *string;
170 struct psi_parser_input *I;
171 struct psi_parser P;
172 struct psi_data D = {0};
173 zend_long flags = 0;
174
175 #if PHP_DEBUG
176 if (psi_check_env("PSI_DEBUG")) {
177 flags |= PSI_DEBUG;
178 }
179 #endif
180
181 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "S|l", &string, &flags)) {
182 return;
183 }
184
185 if (!psi_parser_init(&P, psi_error_wrapper, flags)) {
186 RETURN_FALSE;
187 }
188 if (!(I = psi_parser_open_string(&P, string->val, string->len))) {
189 psi_parser_dtor(&P);
190 RETURN_FALSE;
191 }
192
193 psi_parser_parse(&P, I);
194 psi_data_ctor(&D, P.error, P.flags);
195 RETVAL_BOOL(psi_data_validate(&D, PSI_DATA(&P)) && !P.errors);
196 psi_data_dtor(&D);
197 psi_parser_dtor(&P);
198 free(I);
199 }
200
201 static ZEND_RESULT_CODE psi_ops_load()
202 {
203 struct psi_context_ops *ops = NULL;
204 #ifdef HAVE_LIBJIT
205 if (!strcasecmp(PSI_G(engine), "jit")) {
206 ops = psi_libjit_ops();
207 } else
208 #endif
209 #ifdef HAVE_LIBFFI
210 ops = psi_libffi_ops();
211 #endif
212
213 if (!ops) {
214 php_error(E_WARNING, "No PSI engine found");
215 return FAILURE;
216 }
217
218 PSI_G(ops) = ops;
219 if (ops->load) {
220 return ops->load();
221 }
222 return SUCCESS;
223 }
224
225 static PHP_MINIT_FUNCTION(psi)
226 {
227 zend_class_entry ce = {0};
228 unsigned flags = 0;
229
230 REGISTER_INI_ENTRIES();
231
232 zend_register_long_constant(ZEND_STRL("PSI_DEBUG"), PSI_DEBUG, CONST_CS|CONST_PERSISTENT, module_number);
233 zend_register_long_constant(ZEND_STRL("PSI_SILENT"), PSI_SILENT, CONST_CS|CONST_PERSISTENT, module_number);
234
235 INIT_NS_CLASS_ENTRY(ce, "psi", "object", NULL);
236 psi_class_entry = zend_register_internal_class_ex(&ce, NULL);
237 psi_class_entry->create_object = psi_object_init;
238
239 memcpy(&psi_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
240 psi_object_handlers.offset = XtOffsetOf(psi_object, std);
241 psi_object_handlers.free_obj = psi_object_free;
242 psi_object_handlers.clone_obj = NULL;
243
244 if (SUCCESS != psi_ops_load()) {
245 return FAILURE;
246 }
247
248 if (psi_check_env("PSI_DEBUG")) {
249 flags |= PSI_DEBUG;
250 }
251 if (psi_check_env("PSI_SILENT")) {
252 flags |= PSI_SILENT;
253 }
254
255 PSI_G(search_path) = pemalloc(strlen(PSI_G(directory)) + strlen(psi_cpp_search) + 1 + 1, 1);
256 sprintf(PSI_G(search_path), "%s:%s", PSI_G(directory), psi_cpp_search);
257
258 PSI_G(context) = psi_context_init(NULL, PSI_G(ops), psi_error_wrapper, flags);
259 psi_context_build(PSI_G(context), PSI_G(directory));
260
261 return SUCCESS;
262 }
263
264 static PHP_MSHUTDOWN_FUNCTION(psi)
265 {
266 if (psi_check_env("PSI_DUMP")) {
267 psi_context_dump(PSI_G(context), STDOUT_FILENO);
268 }
269
270 psi_context_free(&PSI_G(context));
271
272 if (PSI_G(ops)->free) {
273 PSI_G(ops)->free();
274 }
275
276 UNREGISTER_INI_ENTRIES();
277
278 return SUCCESS;
279 }
280
281 #if defined(COMPILE_DL_PSI) && defined(ZTS)
282 static PHP_RINIT_FUNCTION(psi)
283 {
284 ZEND_TSRMLS_CACHE_UPDATE();
285 return SUCCESS;
286 }
287 #endif
288
289 static PHP_MINFO_FUNCTION(psi)
290 {
291 php_info_print_table_start();
292 php_info_print_table_header(2, "PSI Support", "enabled");
293 php_info_print_table_row(2, "Extension Version", PHP_PSI_VERSION);
294 php_info_print_table_row(2, "Search Path", PSI_G(search_path));
295 php_info_print_table_end();
296
297 php_info_print_table_start();
298 php_info_print_table_header(3, "Used Library", "Compiled", "Linked");
299 php_info_print_table_row(3, "libffi",
300 #ifndef PHP_PSI_LIBFFI_VERSION
301 # define PHP_PSI_LIBFFI_VERSION "unknown"
302 #endif
303 #ifdef HAVE_LIBFFI
304 PHP_PSI_LIBFFI_VERSION, "unknown"
305 #else
306 "disabled", "disabled"
307 #endif
308 );
309 php_info_print_table_row(3, "libjit",
310 #ifdef HAVE_LIBJIT
311 "unknown", "unknown"
312 #else
313 "disabled", "disabled"
314 #endif
315 );
316
317 DISPLAY_INI_ENTRIES();
318 }
319
320 static void ptr_free(void *ptr)
321 {
322 free(*(void **) ptr);
323 }
324
325 static PHP_GINIT_FUNCTION(psi)
326 {
327 char *tmp;
328 struct psi_plist **bl_decls = &psi_globals->blacklist.decls;
329
330 *bl_decls = psi_plist_init(ptr_free);
331
332 #define BL_DECL_ADD(d) \
333 tmp = strdup(d); \
334 *bl_decls = psi_plist_add(*bl_decls, &tmp)
335
336 BL_DECL_ADD("dlsym");
337 BL_DECL_ADD("alloca");
338 BL_DECL_ADD("atexit");
339 BL_DECL_ADD("at_quick_exit");
340
341 /* missing */
342 BL_DECL_ADD("_IO_cookie_init");
343 BL_DECL_ADD("bindresvport6");
344
345 /* va_list as arg */
346 BL_DECL_ADD("*v*printf");
347 BL_DECL_ADD("*v*scanf");
348
349 /* LFS/LFO for 32bit */
350 BL_DECL_ADD("*stat*64");
351 BL_DECL_ADD("*glob*64");
352 /* Hurd only */
353 BL_DECL_ADD("getumask");
354 }
355
356 static PHP_GSHUTDOWN_FUNCTION(psi)
357 {
358 psi_plist_free(psi_globals->blacklist.decls);
359 }
360
361 static const zend_function_entry psi_functions[] = {
362 PHP_FE(psi_dump, ai_psi_dump)
363 PHP_FE(psi_validate, ai_psi_validate)
364 PHP_FE(psi_validate_string, ai_psi_validate_string)
365 PHP_FE_END
366 };
367
368 zend_module_entry psi_module_entry = {
369 STANDARD_MODULE_HEADER,
370 "psi",
371 psi_functions,
372 PHP_MINIT(psi),
373 PHP_MSHUTDOWN(psi),
374 #if defined(COMPILE_DL_PSI) && defined(ZTS)
375 PHP_RINIT(psi), /* Replace with NULL if there's nothing to do at request start */
376 #else
377 NULL,
378 #endif
379 NULL,
380 PHP_MINFO(psi),
381 PHP_PSI_VERSION,
382 ZEND_MODULE_GLOBALS(psi),
383 PHP_GINIT(psi),
384 PHP_GSHUTDOWN(psi),
385 NULL, /* post-deactivate */
386 STANDARD_MODULE_PROPERTIES_EX
387 };
388
389 #ifdef COMPILE_DL_PSI
390 #ifdef ZTS
391 ZEND_TSRMLS_CACHE_DEFINE();
392 #endif
393 ZEND_GET_MODULE(psi)
394 #endif
395
396 /*
397 * Local variables:
398 * tab-width: 4
399 * c-basic-offset: 4
400 * End:
401 * vim600: noet sw=4 ts=4 fdm=marker
402 * vim<600: noet sw=4 ts=4
403 */