1 /*
2 +----------------------------------------------------------------------+
3 | Zend OPcache |
4 +----------------------------------------------------------------------+
5 | Copyright (c) The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | https://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Authors: Andi Gutmans <andi@php.net> |
16 | Zeev Suraski <zeev@php.net> |
17 | Stanislav Malyshev <stas@zend.com> |
18 | Dmitry Stogov <dmitry@php.net> |
19 +----------------------------------------------------------------------+
20 */
21
22 #include "zend_API.h"
23 #include "zend_constants.h"
24 #include "zend_inheritance.h"
25 #include "zend_accelerator_util_funcs.h"
26 #include "zend_persist.h"
27 #include "zend_shared_alloc.h"
28 #include "zend_observer.h"
29
30 typedef int (*id_function_t)(void *, void *);
31 typedef void (*unique_copy_ctor_func_t)(void *pElement);
32
create_persistent_script(void)33 zend_persistent_script* create_persistent_script(void)
34 {
35 zend_persistent_script *persistent_script = (zend_persistent_script *) emalloc(sizeof(zend_persistent_script));
36 memset(persistent_script, 0, sizeof(zend_persistent_script));
37
38 zend_hash_init(&persistent_script->script.function_table, 0, NULL, ZEND_FUNCTION_DTOR, 0);
39 /* class_table is usually destroyed by free_persistent_script() that
40 * overrides destructor. ZEND_CLASS_DTOR may be used by standard
41 * PHP compiler
42 */
43 zend_hash_init(&persistent_script->script.class_table, 0, NULL, ZEND_CLASS_DTOR, 0);
44
45 return persistent_script;
46 }
47
free_persistent_script(zend_persistent_script * persistent_script,int destroy_elements)48 void free_persistent_script(zend_persistent_script *persistent_script, int destroy_elements)
49 {
50 if (!destroy_elements) {
51 /* Both the keys and values have been transferred into the global tables.
52 * Set nNumUsed=0 to only deallocate the table, but not destroy any elements. */
53 persistent_script->script.function_table.nNumUsed = 0;
54 persistent_script->script.class_table.nNumUsed = 0;
55 } else {
56 destroy_op_array(&persistent_script->script.main_op_array);
57 }
58
59 zend_hash_destroy(&persistent_script->script.function_table);
60 zend_hash_destroy(&persistent_script->script.class_table);
61
62 if (persistent_script->script.filename) {
63 zend_string_release_ex(persistent_script->script.filename, 0);
64 }
65
66 if (persistent_script->warnings) {
67 for (uint32_t i = 0; i < persistent_script->num_warnings; i++) {
68 zend_error_info *info = persistent_script->warnings[i];
69 zend_string_release(info->filename);
70 zend_string_release(info->message);
71 efree(info);
72 }
73 efree(persistent_script->warnings);
74 }
75
76 zend_accel_free_delayed_early_binding_list(persistent_script);
77
78 efree(persistent_script);
79 }
80
zend_accel_move_user_functions(HashTable * src,uint32_t count,zend_script * script)81 void zend_accel_move_user_functions(HashTable *src, uint32_t count, zend_script *script)
82 {
83 Bucket *p, *end;
84 HashTable *dst;
85 zend_string *filename;
86 dtor_func_t orig_dtor;
87 zend_function *function;
88
89 if (!count) {
90 return;
91 }
92
93 dst = &script->function_table;
94 filename = script->main_op_array.filename;
95 orig_dtor = src->pDestructor;
96 src->pDestructor = NULL;
97 zend_hash_extend(dst, count, 0);
98 end = src->arData + src->nNumUsed;
99 p = end - count;
100 for (; p != end; p++) {
101 if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
102 function = Z_PTR(p->val);
103 if (EXPECTED(function->type == ZEND_USER_FUNCTION)
104 && EXPECTED(function->op_array.filename == filename)) {
105 _zend_hash_append_ptr(dst, p->key, function);
106 zend_hash_del_bucket(src, p);
107 }
108 }
109 src->pDestructor = orig_dtor;
110 }
111
zend_accel_move_user_classes(HashTable * src,uint32_t count,zend_script * script)112 void zend_accel_move_user_classes(HashTable *src, uint32_t count, zend_script *script)
113 {
114 Bucket *p, *end;
115 HashTable *dst;
116 zend_string *filename;
117 dtor_func_t orig_dtor;
118 zend_class_entry *ce;
119
120 if (!count) {
121 return;
122 }
123
124 dst = &script->class_table;
125 filename = script->main_op_array.filename;
126 orig_dtor = src->pDestructor;
127 src->pDestructor = NULL;
128 zend_hash_extend(dst, count, 0);
129 end = src->arData + src->nNumUsed;
130 p = end - count;
131 for (; p != end; p++) {
132 if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
133 ce = Z_PTR(p->val);
134 if (EXPECTED(ce->type == ZEND_USER_CLASS)
135 && EXPECTED(ce->info.user.filename == filename)) {
136 _zend_hash_append_ptr(dst, p->key, ce);
137 zend_hash_del_bucket(src, p);
138 }
139 }
140 src->pDestructor = orig_dtor;
141 }
142
_zend_accel_function_hash_copy(HashTable * target,HashTable * source,bool call_observers)143 static zend_always_inline void _zend_accel_function_hash_copy(HashTable *target, HashTable *source, bool call_observers)
144 {
145 zend_function *function1, *function2;
146 Bucket *p, *end;
147 zval *t;
148
149 zend_hash_extend(target, target->nNumUsed + source->nNumUsed, 0);
150 p = source->arData;
151 end = p + source->nNumUsed;
152 for (; p != end; p++) {
153 ZEND_ASSERT(Z_TYPE(p->val) != IS_UNDEF);
154 ZEND_ASSERT(p->key);
155 t = zend_hash_find_known_hash(target, p->key);
156 if (UNEXPECTED(t != NULL)) {
157 goto failure;
158 }
159 _zend_hash_append_ptr_ex(target, p->key, Z_PTR(p->val), 1);
160 if (UNEXPECTED(call_observers) && *ZSTR_VAL(p->key)) { // if not rtd key
161 _zend_observer_function_declared_notify(Z_PTR(p->val), p->key);
162 }
163 }
164 target->nInternalPointer = 0;
165
166 return;
167
168 failure:
169 function1 = Z_PTR(p->val);
170 function2 = Z_PTR_P(t);
171 CG(in_compilation) = 1;
172 zend_set_compiled_filename(function1->op_array.filename);
173 CG(zend_lineno) = function1->op_array.opcodes[0].lineno;
174 if (function2->type == ZEND_USER_FUNCTION
175 && function2->op_array.last > 0) {
176 zend_error(E_ERROR, "Cannot redeclare %s() (previously declared in %s:%d)",
177 ZSTR_VAL(function1->common.function_name),
178 ZSTR_VAL(function2->op_array.filename),
179 (int)function2->op_array.opcodes[0].lineno);
180 } else {
181 zend_error(E_ERROR, "Cannot redeclare %s()", ZSTR_VAL(function1->common.function_name));
182 }
183 }
184
zend_accel_function_hash_copy(HashTable * target,HashTable * source)185 static zend_always_inline void zend_accel_function_hash_copy(HashTable *target, HashTable *source)
186 {
187 _zend_accel_function_hash_copy(target, source, 0);
188 }
189
zend_accel_function_hash_copy_notify(HashTable * target,HashTable * source)190 static zend_never_inline void zend_accel_function_hash_copy_notify(HashTable *target, HashTable *source)
191 {
192 _zend_accel_function_hash_copy(target, source, 1);
193 }
194
_zend_accel_class_hash_copy(HashTable * target,HashTable * source,bool call_observers)195 static zend_always_inline void _zend_accel_class_hash_copy(HashTable *target, HashTable *source, bool call_observers)
196 {
197 Bucket *p, *end;
198 zval *t;
199
200 zend_hash_extend(target, target->nNumUsed + source->nNumUsed, 0);
201 p = source->arData;
202 end = p + source->nNumUsed;
203 for (; p != end; p++) {
204 ZEND_ASSERT(Z_TYPE(p->val) != IS_UNDEF);
205 ZEND_ASSERT(p->key);
206 t = zend_hash_find_known_hash(target, p->key);
207 if (UNEXPECTED(t != NULL)) {
208 if (EXPECTED(ZSTR_LEN(p->key) > 0) && EXPECTED(ZSTR_VAL(p->key)[0] == 0)) {
209 /* Runtime definition key. There are two circumstances under which the key can
210 * already be defined:
211 * 1. The file has been re-included without being changed in the meantime. In
212 * this case we can keep the old value, because we know that the definition
213 * hasn't changed.
214 * 2. The file has been changed in the meantime, but the RTD key ends up colliding.
215 * This would be a bug.
216 * As we can't distinguish these cases, we assume that it is 1. and keep the old
217 * value. */
218 continue;
219 } else if (UNEXPECTED(!ZCG(accel_directives).ignore_dups)) {
220 zend_class_entry *ce1 = Z_PTR(p->val);
221 if (!(ce1->ce_flags & ZEND_ACC_ANON_CLASS)) {
222 CG(in_compilation) = 1;
223 zend_set_compiled_filename(ce1->info.user.filename);
224 CG(zend_lineno) = ce1->info.user.line_start;
225 zend_error(E_ERROR,
226 "Cannot declare %s %s, because the name is already in use",
227 zend_get_object_type(ce1), ZSTR_VAL(ce1->name));
228 return;
229 }
230 continue;
231 }
232 } else {
233 zend_class_entry *ce = Z_PTR(p->val);
234 _zend_hash_append_ptr_ex(target, p->key, Z_PTR(p->val), 1);
235 if ((ce->ce_flags & ZEND_ACC_LINKED) && ZSTR_VAL(p->key)[0]) {
236 if (ZSTR_HAS_CE_CACHE(ce->name)) {
237 ZSTR_SET_CE_CACHE_EX(ce->name, ce, 0);
238 }
239 if (UNEXPECTED(call_observers)) {
240 _zend_observer_class_linked_notify(ce, p->key);
241 }
242 }
243 }
244 }
245 target->nInternalPointer = 0;
246 }
247
zend_accel_class_hash_copy(HashTable * target,HashTable * source)248 static zend_always_inline void zend_accel_class_hash_copy(HashTable *target, HashTable *source)
249 {
250 _zend_accel_class_hash_copy(target, source, 0);
251 }
252
zend_accel_class_hash_copy_notify(HashTable * target,HashTable * source)253 static zend_never_inline void zend_accel_class_hash_copy_notify(HashTable *target, HashTable *source)
254 {
255 _zend_accel_class_hash_copy(target, source, 1);
256 }
257
zend_accel_build_delayed_early_binding_list(zend_persistent_script * persistent_script)258 void zend_accel_build_delayed_early_binding_list(zend_persistent_script *persistent_script)
259 {
260 zend_op_array *op_array = &persistent_script->script.main_op_array;
261 if (!(op_array->fn_flags & ZEND_ACC_EARLY_BINDING)) {
262 return;
263 }
264
265 zend_op *end = op_array->opcodes + op_array->last;
266 for (zend_op *opline = op_array->opcodes; opline < end; opline++) {
267 if (opline->opcode == ZEND_DECLARE_CLASS_DELAYED) {
268 persistent_script->num_early_bindings++;
269 }
270 }
271
272 zend_early_binding *early_binding = persistent_script->early_bindings =
273 emalloc(sizeof(zend_early_binding) * persistent_script->num_early_bindings);
274
275 for (zend_op *opline = op_array->opcodes; opline < end; opline++) {
276 if (opline->opcode == ZEND_DECLARE_CLASS_DELAYED) {
277 zval *lcname = RT_CONSTANT(opline, opline->op1);
278 early_binding->lcname = zend_string_copy(Z_STR_P(lcname));
279 early_binding->rtd_key = zend_string_copy(Z_STR_P(lcname + 1));
280 early_binding->lc_parent_name =
281 zend_string_copy(Z_STR_P(RT_CONSTANT(opline, opline->op2)));
282 early_binding->cache_slot = (uint32_t) -1;
283 early_binding++;
284 }
285 }
286 }
287
zend_accel_finalize_delayed_early_binding_list(zend_persistent_script * persistent_script)288 void zend_accel_finalize_delayed_early_binding_list(zend_persistent_script *persistent_script)
289 {
290 if (!persistent_script->num_early_bindings) {
291 return;
292 }
293
294 zend_early_binding *early_binding = persistent_script->early_bindings;
295 zend_early_binding *early_binding_end = early_binding + persistent_script->num_early_bindings;
296 zend_op_array *op_array = &persistent_script->script.main_op_array;
297 zend_op *opline_end = op_array->opcodes + op_array->last;
298 for (zend_op *opline = op_array->opcodes; opline < opline_end; opline++) {
299 if (opline->opcode == ZEND_DECLARE_CLASS_DELAYED) {
300 zend_string *rtd_key = Z_STR_P(RT_CONSTANT(opline, opline->op1) + 1);
301 /* Skip early_binding entries that don't match, maybe their DECLARE_CLASS_DELAYED
302 * was optimized away. */
303 while (!zend_string_equals(early_binding->rtd_key, rtd_key)) {
304 early_binding++;
305 if (early_binding >= early_binding_end) {
306 return;
307 }
308 }
309
310 early_binding->cache_slot = opline->extended_value;
311 early_binding++;
312 if (early_binding >= early_binding_end) {
313 return;
314 }
315 }
316 }
317 }
318
zend_accel_free_delayed_early_binding_list(zend_persistent_script * persistent_script)319 void zend_accel_free_delayed_early_binding_list(zend_persistent_script *persistent_script)
320 {
321 if (persistent_script->num_early_bindings) {
322 for (uint32_t i = 0; i < persistent_script->num_early_bindings; i++) {
323 zend_early_binding *early_binding = &persistent_script->early_bindings[i];
324 zend_string_release(early_binding->lcname);
325 zend_string_release(early_binding->rtd_key);
326 zend_string_release(early_binding->lc_parent_name);
327 }
328 efree(persistent_script->early_bindings);
329 persistent_script->early_bindings = NULL;
330 persistent_script->num_early_bindings = 0;
331 }
332 }
333
zend_accel_do_delayed_early_binding(zend_persistent_script * persistent_script,zend_op_array * op_array)334 static void zend_accel_do_delayed_early_binding(
335 zend_persistent_script *persistent_script, zend_op_array *op_array)
336 {
337 ZEND_ASSERT(!ZEND_MAP_PTR(op_array->run_time_cache));
338 ZEND_ASSERT(op_array->fn_flags & ZEND_ACC_HEAP_RT_CACHE);
339 void *run_time_cache = emalloc(op_array->cache_size);
340
341 ZEND_MAP_PTR_INIT(op_array->run_time_cache, run_time_cache);
342 memset(run_time_cache, 0, op_array->cache_size);
343
344 zend_string *orig_compiled_filename = CG(compiled_filename);
345 bool orig_in_compilation = CG(in_compilation);
346 CG(compiled_filename) = persistent_script->script.filename;
347 CG(in_compilation) = 1;
348 for (uint32_t i = 0; i < persistent_script->num_early_bindings; i++) {
349 zend_early_binding *early_binding = &persistent_script->early_bindings[i];
350 zend_class_entry *ce = zend_hash_find_ex_ptr(EG(class_table), early_binding->lcname, 1);
351 if (!ce) {
352 zval *zv = zend_hash_find_known_hash(EG(class_table), early_binding->rtd_key);
353 if (zv) {
354 zend_class_entry *orig_ce = Z_CE_P(zv);
355 zend_class_entry *parent_ce =
356 zend_hash_find_ex_ptr(EG(class_table), early_binding->lc_parent_name, 1);
357 if (parent_ce) {
358 ce = zend_try_early_bind(orig_ce, parent_ce, early_binding->lcname, zv);
359 }
360 }
361 if (ce && early_binding->cache_slot != (uint32_t) -1) {
362 *(void**)((char*)run_time_cache + early_binding->cache_slot) = ce;
363 }
364 }
365 }
366 CG(compiled_filename) = orig_compiled_filename;
367 CG(in_compilation) = orig_in_compilation;
368 }
369
zend_accel_load_script(zend_persistent_script * persistent_script,int from_shared_memory)370 zend_op_array* zend_accel_load_script(zend_persistent_script *persistent_script, int from_shared_memory)
371 {
372 zend_op_array *op_array;
373
374 op_array = (zend_op_array *) emalloc(sizeof(zend_op_array));
375 *op_array = persistent_script->script.main_op_array;
376
377 if (EXPECTED(from_shared_memory)) {
378 if (ZCSG(map_ptr_last) > CG(map_ptr_last)) {
379 zend_map_ptr_extend(ZCSG(map_ptr_last));
380 }
381
382 /* Register __COMPILER_HALT_OFFSET__ constant */
383 if (persistent_script->compiler_halt_offset != 0 &&
384 persistent_script->script.filename) {
385 zend_string *name;
386 static const char haltoff[] = "__COMPILER_HALT_OFFSET__";
387
388 name = zend_mangle_property_name(haltoff, sizeof(haltoff) - 1, ZSTR_VAL(persistent_script->script.filename), ZSTR_LEN(persistent_script->script.filename), 0);
389 if (!zend_hash_exists(EG(zend_constants), name)) {
390 zend_register_long_constant(ZSTR_VAL(name), ZSTR_LEN(name), persistent_script->compiler_halt_offset, CONST_CS, 0);
391 }
392 zend_string_release_ex(name, 0);
393 }
394 }
395
396 if (zend_hash_num_elements(&persistent_script->script.function_table) > 0) {
397 if (EXPECTED(!zend_observer_function_declared_observed)) {
398 zend_accel_function_hash_copy(CG(function_table), &persistent_script->script.function_table);
399 } else {
400 zend_accel_function_hash_copy_notify(CG(function_table), &persistent_script->script.function_table);
401 }
402 }
403
404 if (zend_hash_num_elements(&persistent_script->script.class_table) > 0) {
405 if (EXPECTED(!zend_observer_class_linked_observed)) {
406 zend_accel_class_hash_copy(CG(class_table), &persistent_script->script.class_table);
407 } else {
408 zend_accel_class_hash_copy_notify(CG(class_table), &persistent_script->script.class_table);
409 }
410 }
411
412 if (persistent_script->num_early_bindings) {
413 zend_accel_do_delayed_early_binding(persistent_script, op_array);
414 }
415
416 if (UNEXPECTED(!from_shared_memory)) {
417 free_persistent_script(persistent_script, 0); /* free only hashes */
418 }
419
420 return op_array;
421 }
422
423 /*
424 * zend_adler32() is based on zlib implementation
425 * Computes the Adler-32 checksum of a data stream
426 *
427 * Copyright (C) 1995-2005 Mark Adler
428 * For conditions of distribution and use, see copyright notice in zlib.h
429 *
430 * Copyright (C) 1995-2005 Jean-loup Gailly and Mark Adler
431 *
432 * This software is provided 'as-is', without any express or implied
433 * warranty. In no event will the authors be held liable for any damages
434 * arising from the use of this software.
435 *
436 * Permission is granted to anyone to use this software for any purpose,
437 * including commercial applications, and to alter it and redistribute it
438 * freely, subject to the following restrictions:
439 *
440 * 1. The origin of this software must not be misrepresented; you must not
441 * claim that you wrote the original software. If you use this software
442 * in a product, an acknowledgment in the product documentation would be
443 * appreciated but is not required.
444 * 2. Altered source versions must be plainly marked as such, and must not be
445 * misrepresented as being the original software.
446 * 3. This notice may not be removed or altered from any source distribution.
447 *
448 */
449
450 #define ADLER32_BASE 65521 /* largest prime smaller than 65536 */
451 #define ADLER32_NMAX 5552
452 /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
453
454 #define ADLER32_DO1(buf) {s1 += *(buf); s2 += s1;}
455 #define ADLER32_DO2(buf, i) ADLER32_DO1(buf + i); ADLER32_DO1(buf + i + 1);
456 #define ADLER32_DO4(buf, i) ADLER32_DO2(buf, i); ADLER32_DO2(buf, i + 2);
457 #define ADLER32_DO8(buf, i) ADLER32_DO4(buf, i); ADLER32_DO4(buf, i + 4);
458 #define ADLER32_DO16(buf) ADLER32_DO8(buf, 0); ADLER32_DO8(buf, 8);
459
zend_adler32(unsigned int checksum,unsigned char * buf,uint32_t len)460 unsigned int zend_adler32(unsigned int checksum, unsigned char *buf, uint32_t len)
461 {
462 unsigned int s1 = checksum & 0xffff;
463 unsigned int s2 = (checksum >> 16) & 0xffff;
464 unsigned char *end;
465
466 while (len >= ADLER32_NMAX) {
467 len -= ADLER32_NMAX;
468 end = buf + ADLER32_NMAX;
469 do {
470 ADLER32_DO16(buf);
471 buf += 16;
472 } while (buf != end);
473 s1 %= ADLER32_BASE;
474 s2 %= ADLER32_BASE;
475 }
476
477 if (len) {
478 if (len >= 16) {
479 end = buf + (len & 0xfff0);
480 len &= 0xf;
481 do {
482 ADLER32_DO16(buf);
483 buf += 16;
484 } while (buf != end);
485 }
486 if (len) {
487 end = buf + len;
488 do {
489 ADLER32_DO1(buf);
490 buf++;
491 } while (buf != end);
492 }
493 s1 %= ADLER32_BASE;
494 s2 %= ADLER32_BASE;
495 }
496
497 return (s2 << 16) | s1;
498 }
499
zend_accel_script_checksum(zend_persistent_script * persistent_script)500 unsigned int zend_accel_script_checksum(zend_persistent_script *persistent_script)
501 {
502 unsigned char *mem = (unsigned char*)persistent_script->mem;
503 size_t size = persistent_script->size;
504 size_t persistent_script_check_block_size = ((char *)&(persistent_script->dynamic_members)) - (char *)persistent_script;
505 unsigned int checksum = ADLER32_INIT;
506
507 if (mem < (unsigned char*)persistent_script) {
508 checksum = zend_adler32(checksum, mem, (unsigned char*)persistent_script - mem);
509 size -= (unsigned char*)persistent_script - mem;
510 mem += (unsigned char*)persistent_script - mem;
511 }
512
513 zend_adler32(checksum, mem, persistent_script_check_block_size);
514 mem += sizeof(*persistent_script);
515 size -= sizeof(*persistent_script);
516
517 if (size > 0) {
518 checksum = zend_adler32(checksum, mem, size);
519 }
520 return checksum;
521 }
522