+/*******************************************************************************
+ Copyright (c) 2016, Michael Wallner <mike@php.net>.
+ All rights reserved.
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*******************************************************************************/
-#include <jit/jit.h>
-#include <dirent.h>
-#include <fnmatch.h>
+#include "php_psi_stdinc.h"
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
-#include "php_scandir.h"
+#include "zend_constants.h"
+#include "zend_operators.h"
#include "php_psi.h"
-
+#include "token.h"
#include "parser.h"
-#include "validator.h"
-#include "compiler.h"
+
+#if HAVE_LIBJIT
+# include "libjit.h"
+# ifndef HAVE_LIBFFI
+# define PSI_ENGINE "jit"
+# endif
+#endif
+#if HAVE_LIBFFI
+# include "libffi.h"
+# define PSI_ENGINE "ffi"
+#endif
ZEND_DECLARE_MODULE_GLOBALS(psi);
PHP_INI_BEGIN()
- STD_PHP_INI_ENTRY("psi.directory", "psis", PHP_INI_ALL, OnUpdateString, directory, zend_psi_globals, psi_globals)
+ STD_PHP_INI_ENTRY("psi.engine", PSI_ENGINE, PHP_INI_SYSTEM, OnUpdateString, engine, zend_psi_globals, psi_globals)
+ STD_PHP_INI_ENTRY("psi.directory", "psi.d", PHP_INI_SYSTEM, OnUpdateString, directory, zend_psi_globals, psi_globals)
PHP_INI_END();
-static int psi_select_dirent(const struct dirent *entry)
+static zend_object_handlers psi_object_handlers;
+static zend_class_entry *psi_class_entry;
+
+zend_class_entry *psi_object_get_class_entry()
{
-#ifndef FNM_CASEFOLD
-#define FNM_CASEFOLD 0
-#endif
- return 0 == fnmatch("*.psi", entry->d_name, FNM_CASEFOLD);
+ return psi_class_entry;
}
-PHP_MINIT_FUNCTION(psi)
+static void psi_object_free(zend_object *o)
{
- jit_context_t ctx;
- int i, n;
- struct dirent **entries = NULL;
+ psi_object *obj = PSI_OBJ(NULL, o);
- REGISTER_INI_ENTRIES();
+ if (obj->data) {
+ if (obj->dtor) {
+ obj->dtor(obj->data);
+ }
+ obj->data = NULL;
+ }
+ zend_object_std_dtor(o);
+}
- jit_init();
+zend_object *psi_object_init_ex(zend_class_entry *ce, void *data, void (*dtor)(void *))
+{
+ psi_object *o;
- if (!(ctx = jit_context_create())) {
- zend_error(E_WARNING, "Could not initialize libjit!");
- return FAILURE;
+ if (!ce) {
+ ce = psi_class_entry;
}
- PSI_G(context) = ctx;
+ o = ecalloc(1, sizeof(*o) + zend_object_properties_size(ce));
- n = php_scandir(PSI_G(directory), &entries, psi_select_dirent, alphasort);
- if (n < 0) {
- php_error(E_WARNING, "Failed to scan PSI directory '%s'", PSI_G(directory));
- } else for (i = 0; i < n; ++i) {
- char psi[MAXPATHLEN];
- PSI_Parser P;
- PSI_Validator V;
+ o->data = data;
+ o->dtor = dtor;
- if (MAXPATHLEN <= slprintf(psi, MAXPATHLEN, "%s/%s", PSI_G(directory), entries[i]->d_name)) {
- php_error(E_WARNING, "Path to PSI file too long: %s/%s",
- PSI_G(directory), entries[i]->d_name);
- }
- if (!PSI_ParserInit(&P, psi, 0)) {
- php_error(E_WARNING, "Failed to init PSI parser (%s): %s",
- psi, strerror(errno));
- continue;
+ zend_object_std_init(&o->std, ce);
+ object_properties_init(&o->std, ce);
+ o->std.handlers = &psi_object_handlers;
+ return &o->std;
+}
+
+zend_object *psi_object_init(zend_class_entry *ce)
+{
+ return psi_object_init_ex(ce, NULL, NULL);
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_psi_dump, 0, 0, 0)
+ ZEND_ARG_INFO(0, stream)
+ZEND_END_ARG_INFO();
+static PHP_FUNCTION(psi_dump)
+{
+ php_stream *s;
+ zval *r = NULL;
+ int fd = STDOUT_FILENO;
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "|r!", &r)) {
+ return;
+ }
+ if (r) {
+ php_stream_from_zval(s, r);
+
+ if (SUCCESS != php_stream_cast(s, PHP_STREAM_AS_FD | PHP_STREAM_CAST_INTERNAL, (void **)&fd, 1)) {
+ RETURN_FALSE;
}
+ }
+ psi_context_dump(PSI_G(context), fd);
+}
- while (-1 != PSI_ParserScan(&P)) {
- PSI_ParserParse(&P, PSI_TokenAlloc(&P));
- };
- PSI_ParserParse(&P, NULL);
+ZEND_BEGIN_ARG_INFO_EX(ai_psi_validate, 0, 0, 1)
+ ZEND_ARG_INFO(0, file)
+ZEND_END_ARG_INFO();
+static PHP_FUNCTION(psi_validate)
+{
+ zend_string *file;
+ struct psi_parser P;
+ struct psi_data D = {0};
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "P", &file)) {
+ return;
+ }
- if (!PSI_ValidatorInit(&V, &P)) {
- php_error(E_WARNING, "Failed to init PSI validator");
+ if (!psi_parser_init(&P, psi_error_wrapper, 0)) {
+ RETURN_FALSE;
+ }
+ if (!psi_parser_open_file(&P, file->val)) {
+ psi_parser_dtor(&P);
+ RETURN_FALSE;
+ }
+
+ while (0 < psi_parser_scan(&P)) {
+ psi_parser_parse(&P, psi_token_alloc(&P));
+ if (P.num == PSI_T_EOF) {
break;
}
- PSI_ParserDtor(&P);
-
- if (PSI_ValidatorValidate(&V)) {
- PSI_Compiler C;
-
- jit_context_build_start(ctx);
- if (PSI_CompilerInit(&C, &V, ctx)) {
- zend_function_entry *closures = PSI_CompilerCompile(&C);
-
- if (closures) {
- if (SUCCESS != zend_register_functions(NULL, closures, NULL, MODULE_PERSISTENT)) {
- fprintf(stderr, "Failed to register functions!\n");
- }
- }
- //PSI_CompilerDtor(&C);
- }
- jit_context_build_end(ctx);
- }
- PSI_ValidatorDtor(&V);
}
- if (entries) {
- for (i = 0; i < n; ++i) {
- free(entries[i]);
+ psi_parser_parse(&P, NULL);
+
+ psi_data_ctor(&D, P.error, P.flags);
+ RETVAL_BOOL(psi_data_validate(&D, PSI_DATA(&P)) && !P.errors);
+ psi_data_dtor(&D);
+
+ psi_parser_dtor(&P);
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_psi_validate_string, 0, 0, 1)
+ ZEND_ARG_INFO(0, string)
+ZEND_END_ARG_INFO();
+static PHP_FUNCTION(psi_validate_string)
+{
+ zend_string *string;
+ struct psi_parser P;
+ struct psi_data D = {0};
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "S", &string)) {
+ return;
+ }
+
+ if (!psi_parser_init(&P, psi_error_wrapper, 0)) {
+ RETURN_FALSE;
+ }
+ if (!psi_parser_open_string(&P, string->val, string->len)) {
+ psi_parser_dtor(&P);
+ RETURN_FALSE;
+ }
+
+ while (0 < psi_parser_scan(&P)) {
+ psi_parser_parse(&P, psi_token_alloc(&P));
+ if (P.num == PSI_T_EOF) {
+ break;
}
- free(entries);
}
- return SUCCESS;
+ psi_parser_parse(&P, NULL);
+
+ psi_data_ctor(&D, P.error, P.flags);
+ RETVAL_BOOL(psi_data_validate(&D, PSI_DATA(&P)) && !P.errors);
+ psi_data_dtor(&D);
+
+ psi_parser_dtor(&P);
}
-PHP_MSHUTDOWN_FUNCTION(psi)
+
+static PHP_MINIT_FUNCTION(psi)
{
- jit_context_t ctx = PSI_G(context);
- jit_context_destroy(ctx);
+ struct psi_context_ops *ops = NULL;
+ zend_class_entry ce = {0};
+ unsigned flags = 0;
- UNREGISTER_INI_ENTRIES();
+ if (psi_check_env("PSI_DEBUG")) {
+ flags |= PSI_DEBUG;
+ }
+ if (psi_check_env("PSI_SILENT")) {
+ flags |= PSI_SILENT;
+ }
+
+ REGISTER_INI_ENTRIES();
+
+ INIT_NS_CLASS_ENTRY(ce, "psi", "object", NULL);
+ psi_class_entry = zend_register_internal_class_ex(&ce, NULL);
+ psi_class_entry->create_object = psi_object_init;
+
+ memcpy(&psi_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
+ psi_object_handlers.offset = XtOffsetOf(psi_object, std);
+ psi_object_handlers.free_obj = psi_object_free;
+ psi_object_handlers.clone_obj = NULL;
+
+#ifdef HAVE_LIBJIT
+ if (!strcasecmp(PSI_G(engine), "jit")) {
+ ops = psi_libjit_ops();
+ } else
+#endif
+#ifdef HAVE_LIBFFI
+ ops = psi_libffi_ops();
+#endif
+
+ if (!ops) {
+ php_error(E_WARNING, "No PSI engine found");
+ return FAILURE;
+ }
+
+ PSI_G(context) = psi_context_init(NULL, ops, psi_error_wrapper, flags);
+ psi_context_build(PSI_G(context), PSI_G(directory));
+
+ if (psi_check_env("PSI_DUMP")) {
+ psi_context_dump(PSI_G(context), STDOUT_FILENO);
+ }
return SUCCESS;
}
-/* Remove if there's nothing to do at request start */
-/* {{{ PHP_RINIT_FUNCTION
- */
-PHP_RINIT_FUNCTION(psi)
+static PHP_MSHUTDOWN_FUNCTION(psi)
{
-#if defined(COMPILE_DL_PSI) && defined(ZTS)
- ZEND_TSRMLS_CACHE_UPDATE();
-#endif
+ psi_context_free(&PSI_G(context));
+
+ UNREGISTER_INI_ENTRIES();
+
return SUCCESS;
}
-/* }}} */
-/* Remove if there's nothing to do at request end */
-/* {{{ PHP_RSHUTDOWN_FUNCTION
- */
-PHP_RSHUTDOWN_FUNCTION(psi)
+#if defined(COMPILE_DL_PSI) && defined(ZTS)
+static PHP_RINIT_FUNCTION(psi)
{
+ ZEND_TSRMLS_CACHE_UPDATE();
return SUCCESS;
}
-/* }}} */
+#endif
-PHP_MINFO_FUNCTION(psi)
+static PHP_MINFO_FUNCTION(psi)
{
php_info_print_table_start();
- php_info_print_table_header(2, "psi support", "enabled");
+ php_info_print_table_header(2, "PSI Support", "enabled");
+ php_info_print_table_row(2, "Extension Version", PHP_PSI_VERSION);
php_info_print_table_end();
+ php_info_print_table_start();
+ php_info_print_table_header(3, "Used Library", "Compiled", "Linked");
+ php_info_print_table_row(3, "libffi",
+#ifndef PHP_PSI_LIBFFI_VERSION
+# define PHP_PSI_LIBFFI_VERSION "unknown"
+#endif
+#ifdef HAVE_LIBFFI
+ PHP_PSI_LIBFFI_VERSION, "unknown"
+#else
+ "disabled", "disabled"
+#endif
+ );
+ php_info_print_table_row(3, "libjit",
+#ifdef HAVE_LIBJIT
+ "unknown", "unknown"
+#else
+ "disabled", "disabled"
+#endif
+ );
DISPLAY_INI_ENTRIES();
}
-const zend_function_entry psi_functions[] = {
+
+static const zend_function_entry psi_functions[] = {
+ PHP_FE(psi_dump, ai_psi_dump)
+ PHP_FE(psi_validate, ai_psi_validate)
+ PHP_FE(psi_validate_string, ai_psi_validate_string)
PHP_FE_END
};
psi_functions,
PHP_MINIT(psi),
PHP_MSHUTDOWN(psi),
+#if defined(COMPILE_DL_PSI) && defined(ZTS)
PHP_RINIT(psi), /* Replace with NULL if there's nothing to do at request start */
- PHP_RSHUTDOWN(psi), /* Replace with NULL if there's nothing to do at request end */
+#else
+ NULL,
+#endif
+ NULL,
PHP_MINFO(psi),
PHP_PSI_VERSION,
STANDARD_MODULE_PROPERTIES