1 /*******************************************************************************
2 Copyright (c) 2016, Michael Wallner <mike@php.net>.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
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.
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 *******************************************************************************/
26 #include "php_psi_stdinc.h"
30 #include "ext/standard/info.h"
31 #include "zend_constants.h"
32 #include "zend_operators.h"
38 #define PSI_CPP_SEARCH
39 #include "php_psi_cpp.h"
44 # define PSI_ENGINE "jit"
49 # define PSI_ENGINE "ffi"
52 ZEND_DECLARE_MODULE_GLOBALS(psi
);
54 //#define ZEND_INI_MH(name) int name(zend_ini_entry *entry, zend_string *new_value, void *mh_arg1, void *mh_arg2, void *mh_arg3, int stage)
56 static void OnUpdateBlacklist(const char *str
, void (*cb
)(const char*, size_t))
63 end
= strchr(str
, ',');
77 static void psi_blacklist_add_decl(const char *pattern
, size_t len
);
78 static ZEND_INI_MH(OnUpdateBlacklistedDecls
)
80 OnUpdateBlacklist(new_value
->val
, psi_blacklist_add_decl
);
84 static void psi_blacklist_add_var(const char *pattern
, size_t len
);
85 static ZEND_INI_MH(OnUpdateBlacklistedVars
)
87 OnUpdateBlacklist(new_value
->val
, psi_blacklist_add_var
);
91 static void OnDisplayBlacklist(struct psi_plist
*bl
)
96 while (psi_plist_get(bl
, i
++, &item
)) {
103 static ZEND_INI_DISP(OnDisplayBlacklistedDecls
)
105 OnDisplayBlacklist(PSI_G(blacklist
).decls
);
107 static ZEND_INI_DISP(OnDisplayBlacklistedVars
)
109 OnDisplayBlacklist(PSI_G(blacklist
).vars
);
113 STD_PHP_INI_ENTRY("psi.engine", PSI_ENGINE
, PHP_INI_SYSTEM
, OnUpdateString
, engine
, zend_psi_globals
, psi_globals
)
114 STD_PHP_INI_ENTRY("psi.directory", "psi.d", PHP_INI_SYSTEM
, OnUpdateString
, directory
, zend_psi_globals
, psi_globals
)
115 PHP_INI_ENTRY_EX("psi.blacklist.decls", "", PHP_INI_SYSTEM
, OnUpdateBlacklistedDecls
, OnDisplayBlacklistedDecls
)
116 PHP_INI_ENTRY_EX("psi.blacklist.vars", "", PHP_INI_SYSTEM
, OnUpdateBlacklistedVars
, OnDisplayBlacklistedVars
)
119 static zend_object_handlers psi_object_handlers
;
120 static zend_class_entry
*psi_class_entry
;
122 zend_class_entry
*psi_object_get_class_entry()
124 return psi_class_entry
;
127 static void psi_object_free(zend_object
*o
)
129 psi_object
*obj
= PSI_OBJ(NULL
, o
);
133 obj
->dtor(obj
->data
);
137 zend_object_std_dtor(o
);
140 zend_object
*psi_object_init_ex(zend_class_entry
*ce
, void *data
, void (*dtor
)(void *))
145 ce
= psi_class_entry
;
148 o
= ecalloc(1, sizeof(*o
) + zend_object_properties_size(ce
));
153 zend_object_std_init(&o
->std
, ce
);
154 object_properties_init(&o
->std
, ce
);
155 o
->std
.handlers
= &psi_object_handlers
;
159 zend_object
*psi_object_init(zend_class_entry
*ce
)
161 return psi_object_init_ex(ce
, NULL
, NULL
);
164 ZEND_BEGIN_ARG_INFO_EX(ai_psi_dump
, 0, 0, 0)
165 ZEND_ARG_INFO(0, stream
)
167 static PHP_FUNCTION(psi_dump
)
171 int fd
= STDOUT_FILENO
;
173 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS(), "|r!", &r
)) {
177 php_stream_from_zval(s
, r
);
179 if (SUCCESS
!= php_stream_cast(s
, PHP_STREAM_AS_FD
| PHP_STREAM_CAST_INTERNAL
, (void **)&fd
, 1)) {
183 psi_context_dump(PSI_G(context
), fd
);
186 ZEND_BEGIN_ARG_INFO_EX(ai_psi_validate
, 0, 0, 1)
187 ZEND_ARG_INFO(0, file
)
188 ZEND_ARG_INFO(0, flags
)
190 static PHP_FUNCTION(psi_validate
)
193 struct psi_parser_input
*I
;
195 struct psi_data D
= {0};
196 struct psi_validate_scope S
= {0};
200 if (psi_check_env("PSI_DEBUG")) {
205 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS(), "P|l", &file
, &flags
)) {
209 if (!psi_parser_init(&P
, psi_error_wrapper
, flags
)) {
212 if (!(I
= psi_parser_open_file(&P
, file
->val
, true))) {
217 psi_parser_parse(&P
, I
);
218 psi_data_ctor(&D
, P
.error
, P
.flags
);
219 psi_validate_scope_ctor(&S
);
220 S
.defs
= &P
.preproc
->defs
;
221 RETVAL_BOOL(psi_validate(&S
, &D
, PSI_DATA(&P
)) && !P
.errors
);
222 psi_validate_scope_dtor(&S
);
228 ZEND_BEGIN_ARG_INFO_EX(ai_psi_validate_string
, 0, 0, 1)
229 ZEND_ARG_INFO(0, string
)
231 static PHP_FUNCTION(psi_validate_string
)
234 struct psi_parser_input
*I
;
236 struct psi_data D
= {0};
237 struct psi_validate_scope S
= {0};
241 if (psi_check_env("PSI_DEBUG")) {
246 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS(), "S|l", &string
, &flags
)) {
250 if (!psi_parser_init(&P
, psi_error_wrapper
, flags
)) {
253 if (!(I
= psi_parser_open_string(&P
, string
->val
, string
->len
))) {
258 psi_parser_parse(&P
, I
);
259 psi_data_ctor(&D
, P
.error
, P
.flags
);
260 psi_validate_scope_ctor(&S
);
261 S
.defs
= &P
.preproc
->defs
;
262 RETVAL_BOOL(psi_validate(&S
, &D
, PSI_DATA(&P
)) && !P
.errors
);
263 psi_validate_scope_dtor(&S
);
269 static ZEND_RESULT_CODE
psi_ops_load()
271 struct psi_context_ops
*ops
= NULL
;
273 if (!strcasecmp(PSI_G(engine
), "jit")) {
274 ops
= psi_libjit_ops();
278 ops
= psi_libffi_ops();
282 php_error(E_WARNING
, "No PSI engine found");
293 static PHP_MINIT_FUNCTION(psi
)
295 zend_class_entry ce
= {0};
298 REGISTER_INI_ENTRIES();
300 zend_register_long_constant(ZEND_STRL("PSI_DEBUG"), PSI_DEBUG
, CONST_CS
|CONST_PERSISTENT
, module_number
);
301 zend_register_long_constant(ZEND_STRL("PSI_SILENT"), PSI_SILENT
, CONST_CS
|CONST_PERSISTENT
, module_number
);
303 INIT_NS_CLASS_ENTRY(ce
, "psi", "object", NULL
);
304 psi_class_entry
= zend_register_internal_class_ex(&ce
, NULL
);
305 psi_class_entry
->create_object
= psi_object_init
;
307 memcpy(&psi_object_handlers
, zend_get_std_object_handlers(), sizeof(zend_object_handlers
));
308 psi_object_handlers
.offset
= XtOffsetOf(psi_object
, std
);
309 psi_object_handlers
.free_obj
= psi_object_free
;
310 psi_object_handlers
.clone_obj
= NULL
;
312 if (SUCCESS
!= psi_ops_load()) {
316 if (psi_check_env("PSI_DEBUG")) {
319 if (psi_check_env("PSI_SILENT")) {
323 PSI_G(search_path
) = pemalloc(strlen(PSI_G(directory
)) + strlen(psi_cpp_search
) + 1 + 1, 1);
324 sprintf(PSI_G(search_path
), "%s:%s", PSI_G(directory
), psi_cpp_search
);
326 PSI_G(context
) = psi_context_init(NULL
, PSI_G(ops
), psi_error_wrapper
, flags
);
327 psi_context_build(PSI_G(context
), PSI_G(directory
));
332 static PHP_MSHUTDOWN_FUNCTION(psi
)
334 if (psi_check_env("PSI_DUMP")) {
335 psi_context_dump(PSI_G(context
), STDOUT_FILENO
);
338 psi_context_free(&PSI_G(context
));
340 if (PSI_G(ops
)->free
) {
344 UNREGISTER_INI_ENTRIES();
349 #if defined(COMPILE_DL_PSI) && defined(ZTS)
350 static PHP_RINIT_FUNCTION(psi
)
352 ZEND_TSRMLS_CACHE_UPDATE();
357 static PHP_MINFO_FUNCTION(psi
)
359 php_info_print_table_start();
360 php_info_print_table_header(2, "PSI Support", "enabled");
361 php_info_print_table_row(2, "Extension Version", PHP_PSI_VERSION
);
362 php_info_print_table_row(2, "Search Path", PSI_G(search_path
));
363 php_info_print_table_end();
365 php_info_print_table_start();
366 php_info_print_table_header(3, "Used Library", "Compiled", "Linked");
367 php_info_print_table_row(3, "libffi",
368 #ifndef PHP_PSI_LIBFFI_VERSION
369 # define PHP_PSI_LIBFFI_VERSION "unknown"
372 PHP_PSI_LIBFFI_VERSION
, "unknown"
374 "disabled", "disabled"
377 php_info_print_table_row(3, "libjit",
381 "disabled", "disabled"
385 DISPLAY_INI_ENTRIES();
388 static void ptr_free(void *ptr
)
390 free(*(void **) ptr
);
393 static void psi_blacklist_add_decl(const char *pattern
, size_t len
)
395 char *tmp
= strndup(pattern
, len
);
396 struct psi_plist
**decls
= &PSI_G(blacklist
).decls
;
398 *decls
= psi_plist_add(*decls
, &tmp
);
401 static void psi_blacklist_add_var(const char *pattern
, size_t len
)
403 char *tmp
= strndup(pattern
, len
);
404 struct psi_plist
**vars
= &PSI_G(blacklist
).vars
;
406 *vars
= psi_plist_add(*vars
, &tmp
);
409 static PHP_GINIT_FUNCTION(psi
)
412 struct psi_plist
**bl_decls
= &psi_globals
->blacklist
.decls
;
413 struct psi_plist
**bl_vars
= &psi_globals
->blacklist
.vars
;
415 *bl_decls
= psi_plist_init(ptr_free
);
416 *bl_vars
= psi_plist_init(ptr_free
);
418 #define BL_ADD(D, d) \
420 *D = psi_plist_add(*D, &tmp)
421 #define BL_DECL_ADD(d) BL_ADD(bl_decls, d)
422 #define BL_VAR_ADD(d) BL_ADD(bl_vars, d)
424 BL_DECL_ADD("dlsym");
425 BL_DECL_ADD("alloca");
426 BL_DECL_ADD("atexit");
427 BL_DECL_ADD("at_quick_exit");
430 BL_DECL_ADD("_IO_cookie_init");
431 BL_DECL_ADD("bindresvport6");
434 BL_DECL_ADD("*v*printf");
435 BL_DECL_ADD("*v*scanf");
436 BL_DECL_ADD("vsyslog");
438 /* LFS/LFO for 32bit */
439 BL_DECL_ADD("*stat*64");
440 BL_DECL_ADD("*glob*64");
442 BL_DECL_ADD("getumask");
444 /* using hidden structs */
445 BL_VAR_ADD("_IO_2_*");
448 static PHP_GSHUTDOWN_FUNCTION(psi
)
450 psi_plist_free(psi_globals
->blacklist
.decls
);
453 static const zend_function_entry psi_functions
[] = {
454 PHP_FE(psi_dump
, ai_psi_dump
)
455 PHP_FE(psi_validate
, ai_psi_validate
)
456 PHP_FE(psi_validate_string
, ai_psi_validate_string
)
460 zend_module_entry psi_module_entry
= {
461 STANDARD_MODULE_HEADER
,
466 #if defined(COMPILE_DL_PSI) && defined(ZTS)
467 PHP_RINIT(psi
), /* Replace with NULL if there's nothing to do at request start */
474 ZEND_MODULE_GLOBALS(psi
),
477 NULL
, /* post-deactivate */
478 STANDARD_MODULE_PROPERTIES_EX
481 #ifdef COMPILE_DL_PSI
483 ZEND_TSRMLS_CACHE_DEFINE();
493 * vim600: noet sw=4 ts=4 fdm=marker
494 * vim<600: noet sw=4 ts=4