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
);
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
)
59 static zend_object_handlers psi_object_handlers
;
60 static zend_class_entry
*psi_class_entry
;
62 zend_class_entry
*psi_object_get_class_entry()
64 return psi_class_entry
;
67 static void psi_object_free(zend_object
*o
)
69 psi_object
*obj
= PSI_OBJ(NULL
, o
);
77 zend_object_std_dtor(o
);
80 zend_object
*psi_object_init_ex(zend_class_entry
*ce
, void *data
, void (*dtor
)(void *))
88 o
= ecalloc(1, sizeof(*o
) + zend_object_properties_size(ce
));
93 zend_object_std_init(&o
->std
, ce
);
94 object_properties_init(&o
->std
, ce
);
95 o
->std
.handlers
= &psi_object_handlers
;
99 zend_object
*psi_object_init(zend_class_entry
*ce
)
101 return psi_object_init_ex(ce
, NULL
, NULL
);
104 ZEND_BEGIN_ARG_INFO_EX(ai_psi_dump
, 0, 0, 0)
105 ZEND_ARG_INFO(0, stream
)
107 static PHP_FUNCTION(psi_dump
)
111 int fd
= STDOUT_FILENO
;
113 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS(), "|r!", &r
)) {
117 php_stream_from_zval(s
, r
);
119 if (SUCCESS
!= php_stream_cast(s
, PHP_STREAM_AS_FD
| PHP_STREAM_CAST_INTERNAL
, (void **)&fd
, 1)) {
123 psi_context_dump(PSI_G(context
), fd
);
126 ZEND_BEGIN_ARG_INFO_EX(ai_psi_validate
, 0, 0, 1)
127 ZEND_ARG_INFO(0, file
)
128 ZEND_ARG_INFO(0, flags
)
130 static PHP_FUNCTION(psi_validate
)
133 struct psi_parser_input
*I
;
135 struct psi_data D
= {0};
139 if (psi_check_env("PSI_DEBUG")) {
144 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS(), "P|l", &file
, &flags
)) {
148 if (!psi_parser_init(&P
, psi_error_wrapper
, flags
)) {
151 if (!(I
= psi_parser_open_file(&P
, file
->val
, true))) {
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
);
164 ZEND_BEGIN_ARG_INFO_EX(ai_psi_validate_string
, 0, 0, 1)
165 ZEND_ARG_INFO(0, string
)
167 static PHP_FUNCTION(psi_validate_string
)
170 struct psi_parser_input
*I
;
172 struct psi_data D
= {0};
176 if (psi_check_env("PSI_DEBUG")) {
181 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS(), "S|l", &string
, &flags
)) {
185 if (!psi_parser_init(&P
, psi_error_wrapper
, flags
)) {
188 if (!(I
= psi_parser_open_string(&P
, string
->val
, string
->len
))) {
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
);
201 static PHP_MINIT_FUNCTION(psi
)
203 struct psi_context_ops
*ops
= NULL
;
204 zend_class_entry ce
= {0};
207 REGISTER_INI_ENTRIES();
209 zend_register_long_constant(ZEND_STRL("PSI_DEBUG"), PSI_DEBUG
, CONST_CS
|CONST_PERSISTENT
, module_number
);
210 zend_register_long_constant(ZEND_STRL("PSI_SILENT"), PSI_SILENT
, CONST_CS
|CONST_PERSISTENT
, module_number
);
212 INIT_NS_CLASS_ENTRY(ce
, "psi", "object", NULL
);
213 psi_class_entry
= zend_register_internal_class_ex(&ce
, NULL
);
214 psi_class_entry
->create_object
= psi_object_init
;
216 memcpy(&psi_object_handlers
, zend_get_std_object_handlers(), sizeof(zend_object_handlers
));
217 psi_object_handlers
.offset
= XtOffsetOf(psi_object
, std
);
218 psi_object_handlers
.free_obj
= psi_object_free
;
219 psi_object_handlers
.clone_obj
= NULL
;
222 if (!strcasecmp(PSI_G(engine
), "jit")) {
223 ops
= psi_libjit_ops();
227 ops
= psi_libffi_ops();
231 php_error(E_WARNING
, "No PSI engine found");
235 if (psi_check_env("PSI_DEBUG")) {
238 if (psi_check_env("PSI_SILENT")) {
242 PSI_G(search_path
) = pemalloc(strlen(PSI_G(directory
)) + strlen(psi_cpp_search
) + 1 + 1, 1);
243 sprintf(PSI_G(search_path
), "%s:%s", PSI_G(directory
), psi_cpp_search
);
245 PSI_G(context
) = psi_context_init(NULL
, ops
, psi_error_wrapper
, flags
);
246 psi_context_build(PSI_G(context
), PSI_G(directory
));
251 static PHP_MSHUTDOWN_FUNCTION(psi
)
253 if (psi_check_env("PSI_DUMP")) {
254 psi_context_dump(PSI_G(context
), STDOUT_FILENO
);
257 psi_context_free(&PSI_G(context
));
259 UNREGISTER_INI_ENTRIES();
264 #if defined(COMPILE_DL_PSI) && defined(ZTS)
265 static PHP_RINIT_FUNCTION(psi
)
267 ZEND_TSRMLS_CACHE_UPDATE();
272 static PHP_MINFO_FUNCTION(psi
)
274 php_info_print_table_start();
275 php_info_print_table_header(2, "PSI Support", "enabled");
276 php_info_print_table_row(2, "Extension Version", PHP_PSI_VERSION
);
277 php_info_print_table_row(2, "Search Path", PSI_G(search_path
));
278 php_info_print_table_end();
280 php_info_print_table_start();
281 php_info_print_table_header(3, "Used Library", "Compiled", "Linked");
282 php_info_print_table_row(3, "libffi",
283 #ifndef PHP_PSI_LIBFFI_VERSION
284 # define PHP_PSI_LIBFFI_VERSION "unknown"
287 PHP_PSI_LIBFFI_VERSION
, "unknown"
289 "disabled", "disabled"
292 php_info_print_table_row(3, "libjit",
296 "disabled", "disabled"
300 DISPLAY_INI_ENTRIES();
303 static void ptr_free(void *ptr
)
305 free(*(void **) ptr
);
308 static PHP_GINIT_FUNCTION(psi
)
311 struct psi_plist
**bl_decls
= &psi_globals
->blacklist
.decls
;
313 *bl_decls
= psi_plist_init(ptr_free
);
315 #define BL_DECL_ADD(d) \
317 *bl_decls = psi_plist_add(*bl_decls, &tmp)
319 BL_DECL_ADD("dlsym");
320 BL_DECL_ADD("_IO_cookie_init");
323 static PHP_GSHUTDOWN_FUNCTION(psi
)
325 psi_plist_free(psi_globals
->blacklist
.decls
);
328 static const zend_function_entry psi_functions
[] = {
329 PHP_FE(psi_dump
, ai_psi_dump
)
330 PHP_FE(psi_validate
, ai_psi_validate
)
331 PHP_FE(psi_validate_string
, ai_psi_validate_string
)
335 zend_module_entry psi_module_entry
= {
336 STANDARD_MODULE_HEADER
,
341 #if defined(COMPILE_DL_PSI) && defined(ZTS)
342 PHP_RINIT(psi
), /* Replace with NULL if there's nothing to do at request start */
349 ZEND_MODULE_GLOBALS(psi
),
352 NULL
, /* post-deactivate */
353 STANDARD_MODULE_PROPERTIES_EX
356 #ifdef COMPILE_DL_PSI
358 ZEND_TSRMLS_CACHE_DEFINE();
368 * vim600: noet sw=4 ts=4 fdm=marker
369 * vim<600: noet sw=4 ts=4