improved type validation
[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 #if HAVE_LIBJIT
39 # include "libjit.h"
40 # ifndef HAVE_LIBFFI
41 # define PSI_ENGINE "jit"
42 # endif
43 #endif
44 #if HAVE_LIBFFI
45 # include "libffi.h"
46 # define PSI_ENGINE "ffi"
47 #endif
48
49 ZEND_DECLARE_MODULE_GLOBALS(psi);
50
51 PHP_INI_BEGIN()
52 STD_PHP_INI_ENTRY("psi.engine", PSI_ENGINE, PHP_INI_SYSTEM, OnUpdateString, engine, zend_psi_globals, psi_globals)
53 STD_PHP_INI_ENTRY("psi.directory", "psi.d", PHP_INI_SYSTEM, OnUpdateString, directory, zend_psi_globals, psi_globals)
54 PHP_INI_END();
55
56 static zend_object_handlers psi_object_handlers;
57 static zend_class_entry *psi_class_entry;
58
59 zend_class_entry *psi_object_get_class_entry()
60 {
61 return psi_class_entry;
62 }
63
64 static void psi_object_free(zend_object *o)
65 {
66 psi_object *obj = PSI_OBJ(NULL, o);
67
68 if (obj->data) {
69 if (obj->dtor) {
70 obj->dtor(obj->data);
71 }
72 obj->data = NULL;
73 }
74 zend_object_std_dtor(o);
75 }
76
77 zend_object *psi_object_init_ex(zend_class_entry *ce, void *data, void (*dtor)(void *))
78 {
79 psi_object *o;
80
81 if (!ce) {
82 ce = psi_class_entry;
83 }
84
85 o = ecalloc(1, sizeof(*o) + zend_object_properties_size(ce));
86
87 o->data = data;
88 o->dtor = dtor;
89
90 zend_object_std_init(&o->std, ce);
91 object_properties_init(&o->std, ce);
92 o->std.handlers = &psi_object_handlers;
93 return &o->std;
94 }
95
96 zend_object *psi_object_init(zend_class_entry *ce)
97 {
98 return psi_object_init_ex(ce, NULL, NULL);
99 }
100
101 ZEND_BEGIN_ARG_INFO_EX(ai_psi_dump, 0, 0, 0)
102 ZEND_ARG_INFO(0, stream)
103 ZEND_END_ARG_INFO();
104 static PHP_FUNCTION(psi_dump)
105 {
106 php_stream *s;
107 zval *r = NULL;
108 int fd = STDOUT_FILENO;
109
110 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "|r!", &r)) {
111 return;
112 }
113 if (r) {
114 php_stream_from_zval(s, r);
115
116 if (SUCCESS != php_stream_cast(s, PHP_STREAM_AS_FD | PHP_STREAM_CAST_INTERNAL, (void **)&fd, 1)) {
117 RETURN_FALSE;
118 }
119 }
120 psi_context_dump(PSI_G(context), fd);
121 }
122
123 ZEND_BEGIN_ARG_INFO_EX(ai_psi_validate, 0, 0, 1)
124 ZEND_ARG_INFO(0, file)
125 ZEND_ARG_INFO(0, flags)
126 ZEND_END_ARG_INFO();
127 static PHP_FUNCTION(psi_validate)
128 {
129 zend_string *file;
130 struct psi_parser_input *I;
131 struct psi_parser P;
132 struct psi_data D = {0};
133 zend_long flags = 0;
134
135 #if PHP_DEBUG
136 if (psi_check_env("PSI_DEBUG")) {
137 flags |= PSI_DEBUG;
138 }
139 #endif
140
141 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "P|l", &file, &flags)) {
142 return;
143 }
144
145 if (!psi_parser_init(&P, psi_error_wrapper, flags)) {
146 RETURN_FALSE;
147 }
148 if (!(I = psi_parser_open_file(&P, file->val, true))) {
149 psi_parser_dtor(&P);
150 RETURN_FALSE;
151 }
152
153 psi_parser_parse(&P, I);
154 psi_data_ctor(&D, P.error, P.flags);
155 RETVAL_BOOL(psi_data_validate(&D, PSI_DATA(&P)) && !P.errors);
156 psi_data_dtor(&D);
157 psi_parser_dtor(&P);
158 free(I);
159 }
160
161 ZEND_BEGIN_ARG_INFO_EX(ai_psi_validate_string, 0, 0, 1)
162 ZEND_ARG_INFO(0, string)
163 ZEND_END_ARG_INFO();
164 static PHP_FUNCTION(psi_validate_string)
165 {
166 zend_string *string;
167 struct psi_parser_input *I;
168 struct psi_parser P;
169 struct psi_data D = {0};
170 zend_long flags = 0;
171
172 #if PHP_DEBUG
173 if (psi_check_env("PSI_DEBUG")) {
174 flags |= PSI_DEBUG;
175 }
176 #endif
177
178 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "S|l", &string, &flags)) {
179 return;
180 }
181
182 if (!psi_parser_init(&P, psi_error_wrapper, flags)) {
183 RETURN_FALSE;
184 }
185 if (!(I = psi_parser_open_string(&P, string->val, string->len))) {
186 psi_parser_dtor(&P);
187 RETURN_FALSE;
188 }
189
190 psi_parser_parse(&P, I);
191 psi_data_ctor(&D, P.error, P.flags);
192 RETVAL_BOOL(psi_data_validate(&D, PSI_DATA(&P)) && !P.errors);
193 psi_data_dtor(&D);
194 psi_parser_dtor(&P);
195 free(I);
196 }
197
198 static PHP_MINIT_FUNCTION(psi)
199 {
200 struct psi_context_ops *ops = NULL;
201 zend_class_entry ce = {0};
202 unsigned flags = 0;
203
204 REGISTER_INI_ENTRIES();
205
206 zend_register_long_constant(ZEND_STRL("PSI_DEBUG"), PSI_DEBUG, CONST_CS|CONST_PERSISTENT, module_number);
207 zend_register_long_constant(ZEND_STRL("PSI_SILENT"), PSI_SILENT, CONST_CS|CONST_PERSISTENT, module_number);
208
209 INIT_NS_CLASS_ENTRY(ce, "psi", "object", NULL);
210 psi_class_entry = zend_register_internal_class_ex(&ce, NULL);
211 psi_class_entry->create_object = psi_object_init;
212
213 memcpy(&psi_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
214 psi_object_handlers.offset = XtOffsetOf(psi_object, std);
215 psi_object_handlers.free_obj = psi_object_free;
216 psi_object_handlers.clone_obj = NULL;
217
218 #ifdef HAVE_LIBJIT
219 if (!strcasecmp(PSI_G(engine), "jit")) {
220 ops = psi_libjit_ops();
221 } else
222 #endif
223 #ifdef HAVE_LIBFFI
224 ops = psi_libffi_ops();
225 #endif
226
227 if (!ops) {
228 php_error(E_WARNING, "No PSI engine found");
229 return FAILURE;
230 }
231
232 if (psi_check_env("PSI_DEBUG")) {
233 flags |= PSI_DEBUG;
234 }
235 if (psi_check_env("PSI_SILENT")) {
236 flags |= PSI_SILENT;
237 }
238
239 PSI_G(context) = psi_context_init(NULL, ops, psi_error_wrapper, flags);
240 psi_context_build(PSI_G(context), PSI_G(directory));
241
242 return SUCCESS;
243 }
244
245 static PHP_MSHUTDOWN_FUNCTION(psi)
246 {
247 if (psi_check_env("PSI_DUMP")) {
248 psi_context_dump(PSI_G(context), STDOUT_FILENO);
249 }
250
251 psi_context_free(&PSI_G(context));
252
253 UNREGISTER_INI_ENTRIES();
254
255 return SUCCESS;
256 }
257
258 #if defined(COMPILE_DL_PSI) && defined(ZTS)
259 static PHP_RINIT_FUNCTION(psi)
260 {
261 ZEND_TSRMLS_CACHE_UPDATE();
262 return SUCCESS;
263 }
264 #endif
265
266 static PHP_MINFO_FUNCTION(psi)
267 {
268 php_info_print_table_start();
269 php_info_print_table_header(2, "PSI Support", "enabled");
270 php_info_print_table_row(2, "Extension Version", PHP_PSI_VERSION);
271 php_info_print_table_end();
272 php_info_print_table_start();
273 php_info_print_table_header(3, "Used Library", "Compiled", "Linked");
274 php_info_print_table_row(3, "libffi",
275 #ifndef PHP_PSI_LIBFFI_VERSION
276 # define PHP_PSI_LIBFFI_VERSION "unknown"
277 #endif
278 #ifdef HAVE_LIBFFI
279 PHP_PSI_LIBFFI_VERSION, "unknown"
280 #else
281 "disabled", "disabled"
282 #endif
283 );
284 php_info_print_table_row(3, "libjit",
285 #ifdef HAVE_LIBJIT
286 "unknown", "unknown"
287 #else
288 "disabled", "disabled"
289 #endif
290 );
291
292 DISPLAY_INI_ENTRIES();
293 }
294
295 static const zend_function_entry psi_functions[] = {
296 PHP_FE(psi_dump, ai_psi_dump)
297 PHP_FE(psi_validate, ai_psi_validate)
298 PHP_FE(psi_validate_string, ai_psi_validate_string)
299 PHP_FE_END
300 };
301
302 zend_module_entry psi_module_entry = {
303 STANDARD_MODULE_HEADER,
304 "psi",
305 psi_functions,
306 PHP_MINIT(psi),
307 PHP_MSHUTDOWN(psi),
308 #if defined(COMPILE_DL_PSI) && defined(ZTS)
309 PHP_RINIT(psi), /* Replace with NULL if there's nothing to do at request start */
310 #else
311 NULL,
312 #endif
313 NULL,
314 PHP_MINFO(psi),
315 PHP_PSI_VERSION,
316 STANDARD_MODULE_PROPERTIES
317 };
318
319 #ifdef COMPILE_DL_PSI
320 #ifdef ZTS
321 ZEND_TSRMLS_CACHE_DEFINE();
322 #endif
323 ZEND_GET_MODULE(psi)
324 #endif
325
326 /*
327 * Local variables:
328 * tab-width: 4
329 * c-basic-offset: 4
330 * End:
331 * vim600: noet sw=4 ts=4 fdm=marker
332 * vim<600: noet sw=4 ts=4
333 */