1323a9f5b5f616d83ac752a2af17a368a708ebee
[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 struct psi_validate_scope S = {0};
137 zend_long flags = 0;
138
139 #if PHP_DEBUG
140 if (psi_check_env("PSI_DEBUG")) {
141 flags |= PSI_DEBUG;
142 }
143 #endif
144
145 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "P|l", &file, &flags)) {
146 return;
147 }
148
149 if (!psi_parser_init(&P, psi_error_wrapper, flags)) {
150 RETURN_FALSE;
151 }
152 if (!(I = psi_parser_open_file(&P, file->val, true))) {
153 psi_parser_dtor(&P);
154 RETURN_FALSE;
155 }
156
157 psi_parser_parse(&P, I);
158 psi_data_ctor(&D, P.error, P.flags);
159 psi_validate_scope_ctor(&S);
160 S.defs = &P.preproc->defs;
161 RETVAL_BOOL(psi_validate(&S, &D, PSI_DATA(&P)) && !P.errors);
162 psi_validate_scope_dtor(&S);
163 psi_data_dtor(&D);
164 psi_parser_dtor(&P);
165 free(I);
166 }
167
168 ZEND_BEGIN_ARG_INFO_EX(ai_psi_validate_string, 0, 0, 1)
169 ZEND_ARG_INFO(0, string)
170 ZEND_END_ARG_INFO();
171 static PHP_FUNCTION(psi_validate_string)
172 {
173 zend_string *string;
174 struct psi_parser_input *I;
175 struct psi_parser P;
176 struct psi_data D = {0};
177 struct psi_validate_scope S = {0};
178 zend_long flags = 0;
179
180 #if PHP_DEBUG
181 if (psi_check_env("PSI_DEBUG")) {
182 flags |= PSI_DEBUG;
183 }
184 #endif
185
186 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "S|l", &string, &flags)) {
187 return;
188 }
189
190 if (!psi_parser_init(&P, psi_error_wrapper, flags)) {
191 RETURN_FALSE;
192 }
193 if (!(I = psi_parser_open_string(&P, string->val, string->len))) {
194 psi_parser_dtor(&P);
195 RETURN_FALSE;
196 }
197
198 psi_parser_parse(&P, I);
199 psi_data_ctor(&D, P.error, P.flags);
200 psi_validate_scope_ctor(&S);
201 S.defs = &P.preproc->defs;
202 RETVAL_BOOL(psi_validate(&S, &D, PSI_DATA(&P)) && !P.errors);
203 psi_validate_scope_dtor(&S);
204 psi_data_dtor(&D);
205 psi_parser_dtor(&P);
206 free(I);
207 }
208
209 static ZEND_RESULT_CODE psi_ops_load()
210 {
211 struct psi_context_ops *ops = NULL;
212 #ifdef HAVE_LIBJIT
213 if (!strcasecmp(PSI_G(engine), "jit")) {
214 ops = psi_libjit_ops();
215 } else
216 #endif
217 #ifdef HAVE_LIBFFI
218 ops = psi_libffi_ops();
219 #endif
220
221 if (!ops) {
222 php_error(E_WARNING, "No PSI engine found");
223 return FAILURE;
224 }
225
226 PSI_G(ops) = ops;
227 if (ops->load) {
228 return ops->load();
229 }
230 return SUCCESS;
231 }
232
233 static PHP_MINIT_FUNCTION(psi)
234 {
235 zend_class_entry ce = {0};
236 unsigned flags = 0;
237
238 REGISTER_INI_ENTRIES();
239
240 zend_register_long_constant(ZEND_STRL("PSI_DEBUG"), PSI_DEBUG, CONST_CS|CONST_PERSISTENT, module_number);
241 zend_register_long_constant(ZEND_STRL("PSI_SILENT"), PSI_SILENT, CONST_CS|CONST_PERSISTENT, module_number);
242
243 INIT_NS_CLASS_ENTRY(ce, "psi", "object", NULL);
244 psi_class_entry = zend_register_internal_class_ex(&ce, NULL);
245 psi_class_entry->create_object = psi_object_init;
246
247 memcpy(&psi_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
248 psi_object_handlers.offset = XtOffsetOf(psi_object, std);
249 psi_object_handlers.free_obj = psi_object_free;
250 psi_object_handlers.clone_obj = NULL;
251
252 if (SUCCESS != psi_ops_load()) {
253 return FAILURE;
254 }
255
256 if (psi_check_env("PSI_DEBUG")) {
257 flags |= PSI_DEBUG;
258 }
259 if (psi_check_env("PSI_SILENT")) {
260 flags |= PSI_SILENT;
261 }
262
263 PSI_G(search_path) = pemalloc(strlen(PSI_G(directory)) + strlen(psi_cpp_search) + 1 + 1, 1);
264 sprintf(PSI_G(search_path), "%s:%s", PSI_G(directory), psi_cpp_search);
265
266 PSI_G(context) = psi_context_init(NULL, PSI_G(ops), psi_error_wrapper, flags);
267 psi_context_build(PSI_G(context), PSI_G(directory));
268
269 return SUCCESS;
270 }
271
272 static PHP_MSHUTDOWN_FUNCTION(psi)
273 {
274 if (psi_check_env("PSI_DUMP")) {
275 psi_context_dump(PSI_G(context), STDOUT_FILENO);
276 }
277
278 psi_context_free(&PSI_G(context));
279
280 if (PSI_G(ops)->free) {
281 PSI_G(ops)->free();
282 }
283
284 UNREGISTER_INI_ENTRIES();
285
286 return SUCCESS;
287 }
288
289 #if defined(COMPILE_DL_PSI) && defined(ZTS)
290 static PHP_RINIT_FUNCTION(psi)
291 {
292 ZEND_TSRMLS_CACHE_UPDATE();
293 return SUCCESS;
294 }
295 #endif
296
297 static PHP_MINFO_FUNCTION(psi)
298 {
299 php_info_print_table_start();
300 php_info_print_table_header(2, "PSI Support", "enabled");
301 php_info_print_table_row(2, "Extension Version", PHP_PSI_VERSION);
302 php_info_print_table_row(2, "Search Path", PSI_G(search_path));
303 php_info_print_table_end();
304
305 php_info_print_table_start();
306 php_info_print_table_header(3, "Used Library", "Compiled", "Linked");
307 php_info_print_table_row(3, "libffi",
308 #ifndef PHP_PSI_LIBFFI_VERSION
309 # define PHP_PSI_LIBFFI_VERSION "unknown"
310 #endif
311 #ifdef HAVE_LIBFFI
312 PHP_PSI_LIBFFI_VERSION, "unknown"
313 #else
314 "disabled", "disabled"
315 #endif
316 );
317 php_info_print_table_row(3, "libjit",
318 #ifdef HAVE_LIBJIT
319 "unknown", "unknown"
320 #else
321 "disabled", "disabled"
322 #endif
323 );
324
325 DISPLAY_INI_ENTRIES();
326 }
327
328 static void ptr_free(void *ptr)
329 {
330 free(*(void **) ptr);
331 }
332
333 static PHP_GINIT_FUNCTION(psi)
334 {
335 char *tmp;
336 struct psi_plist **bl_decls = &psi_globals->blacklist.decls;
337 struct psi_plist **bl_vars = &psi_globals->blacklist.vars;
338
339 *bl_decls = psi_plist_init(ptr_free);
340 *bl_vars = psi_plist_init(ptr_free);
341
342 #define BL_ADD(D, d) \
343 tmp = strdup(d); \
344 *D = psi_plist_add(*D, &tmp)
345 #define BL_DECL_ADD(d) BL_ADD(bl_decls, d)
346 #define BL_VAR_ADD(d) BL_ADD(bl_vars, d)
347
348 BL_DECL_ADD("dlsym");
349 BL_DECL_ADD("alloca");
350 BL_DECL_ADD("atexit");
351 BL_DECL_ADD("at_quick_exit");
352
353 /* missing */
354 BL_DECL_ADD("_IO_cookie_init");
355 BL_DECL_ADD("bindresvport6");
356
357 /* va_list as arg */
358 BL_DECL_ADD("*v*printf");
359 BL_DECL_ADD("*v*scanf");
360 BL_DECL_ADD("vsyslog");
361
362 /* LFS/LFO for 32bit */
363 BL_DECL_ADD("*stat*64");
364 BL_DECL_ADD("*glob*64");
365 /* Hurd only */
366 BL_DECL_ADD("getumask");
367
368 /* using hidden structs */
369 BL_VAR_ADD("_IO_2_*");
370 }
371
372 static PHP_GSHUTDOWN_FUNCTION(psi)
373 {
374 psi_plist_free(psi_globals->blacklist.decls);
375 }
376
377 static const zend_function_entry psi_functions[] = {
378 PHP_FE(psi_dump, ai_psi_dump)
379 PHP_FE(psi_validate, ai_psi_validate)
380 PHP_FE(psi_validate_string, ai_psi_validate_string)
381 PHP_FE_END
382 };
383
384 zend_module_entry psi_module_entry = {
385 STANDARD_MODULE_HEADER,
386 "psi",
387 psi_functions,
388 PHP_MINIT(psi),
389 PHP_MSHUTDOWN(psi),
390 #if defined(COMPILE_DL_PSI) && defined(ZTS)
391 PHP_RINIT(psi), /* Replace with NULL if there's nothing to do at request start */
392 #else
393 NULL,
394 #endif
395 NULL,
396 PHP_MINFO(psi),
397 PHP_PSI_VERSION,
398 ZEND_MODULE_GLOBALS(psi),
399 PHP_GINIT(psi),
400 PHP_GSHUTDOWN(psi),
401 NULL, /* post-deactivate */
402 STANDARD_MODULE_PROPERTIES_EX
403 };
404
405 #ifdef COMPILE_DL_PSI
406 #ifdef ZTS
407 ZEND_TSRMLS_CACHE_DEFINE();
408 #endif
409 ZEND_GET_MODULE(psi)
410 #endif
411
412 /*
413 * Local variables:
414 * tab-width: 4
415 * c-basic-offset: 4
416 * End:
417 * vim600: noet sw=4 ts=4 fdm=marker
418 * vim<600: noet sw=4 ts=4
419 */