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_accelerator_util_funcs.h"
25 #include "zend_persist.h"
26 #include "zend_shared_alloc.h"
27
28 typedef int (*id_function_t)(void *, void *);
29 typedef void (*unique_copy_ctor_func_t)(void *pElement);
30
create_persistent_script(void)31 zend_persistent_script* create_persistent_script(void)
32 {
33 zend_persistent_script *persistent_script = (zend_persistent_script *) emalloc(sizeof(zend_persistent_script));
34 memset(persistent_script, 0, sizeof(zend_persistent_script));
35
36 zend_hash_init(&persistent_script->script.function_table, 0, NULL, ZEND_FUNCTION_DTOR, 0);
37 /* class_table is usually destroyed by free_persistent_script() that
38 * overrides destructor. ZEND_CLASS_DTOR may be used by standard
39 * PHP compiler
40 */
41 zend_hash_init(&persistent_script->script.class_table, 0, NULL, ZEND_CLASS_DTOR, 0);
42
43 return persistent_script;
44 }
45
free_persistent_script(zend_persistent_script * persistent_script,int destroy_elements)46 void free_persistent_script(zend_persistent_script *persistent_script, int destroy_elements)
47 {
48 if (!destroy_elements) {
49 /* Both the keys and values have been transferred into the global tables.
50 * Set nNumUsed=0 to only deallocate the table, but not destroy any elements. */
51 persistent_script->script.function_table.nNumUsed = 0;
52 persistent_script->script.class_table.nNumUsed = 0;
53 } else {
54 destroy_op_array(&persistent_script->script.main_op_array);
55 }
56
57 zend_hash_destroy(&persistent_script->script.function_table);
58 zend_hash_destroy(&persistent_script->script.class_table);
59
60 if (persistent_script->script.filename) {
61 zend_string_release_ex(persistent_script->script.filename, 0);
62 }
63
64 efree(persistent_script);
65 }
66
zend_accel_move_user_functions(HashTable * src,uint32_t count,zend_script * script)67 void zend_accel_move_user_functions(HashTable *src, uint32_t count, zend_script *script)
68 {
69 Bucket *p, *end;
70 HashTable *dst;
71 zend_string *filename;
72 dtor_func_t orig_dtor;
73 zend_function *function;
74
75 if (!count) {
76 return;
77 }
78
79 dst = &script->function_table;
80 filename = script->main_op_array.filename;
81 orig_dtor = src->pDestructor;
82 src->pDestructor = NULL;
83 zend_hash_extend(dst, count, 0);
84 end = src->arData + src->nNumUsed;
85 p = end - count;
86 for (; p != end; p++) {
87 if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
88 function = Z_PTR(p->val);
89 if (EXPECTED(function->type == ZEND_USER_FUNCTION)
90 && EXPECTED(function->op_array.filename == filename)) {
91 _zend_hash_append_ptr(dst, p->key, function);
92 zend_hash_del_bucket(src, p);
93 }
94 }
95 src->pDestructor = orig_dtor;
96 }
97
zend_accel_move_user_classes(HashTable * src,uint32_t count,zend_script * script)98 void zend_accel_move_user_classes(HashTable *src, uint32_t count, zend_script *script)
99 {
100 Bucket *p, *end;
101 HashTable *dst;
102 zend_string *filename;
103 dtor_func_t orig_dtor;
104 zend_class_entry *ce;
105
106 if (!count) {
107 return;
108 }
109
110 dst = &script->class_table;
111 filename = script->main_op_array.filename;
112 orig_dtor = src->pDestructor;
113 src->pDestructor = NULL;
114 zend_hash_extend(dst, count, 0);
115 end = src->arData + src->nNumUsed;
116 p = end - count;
117 for (; p != end; p++) {
118 if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
119 ce = Z_PTR(p->val);
120 if (EXPECTED(ce->type == ZEND_USER_CLASS)
121 && EXPECTED(ce->info.user.filename == filename)) {
122 _zend_hash_append_ptr(dst, p->key, ce);
123 zend_hash_del_bucket(src, p);
124 }
125 }
126 src->pDestructor = orig_dtor;
127 }
128
zend_accel_function_hash_copy(HashTable * target,HashTable * source)129 static void zend_accel_function_hash_copy(HashTable *target, HashTable *source)
130 {
131 zend_function *function1, *function2;
132 Bucket *p, *end;
133 zval *t;
134
135 zend_hash_extend(target, target->nNumUsed + source->nNumUsed, 0);
136 p = source->arData;
137 end = p + source->nNumUsed;
138 for (; p != end; p++) {
139 ZEND_ASSERT(Z_TYPE(p->val) != IS_UNDEF);
140 ZEND_ASSERT(p->key);
141 t = zend_hash_find_known_hash(target, p->key);
142 if (UNEXPECTED(t != NULL)) {
143 goto failure;
144 }
145 _zend_hash_append_ptr_ex(target, p->key, Z_PTR(p->val), 1);
146 }
147 target->nInternalPointer = 0;
148 return;
149
150 failure:
151 function1 = Z_PTR(p->val);
152 function2 = Z_PTR_P(t);
153 CG(in_compilation) = 1;
154 zend_set_compiled_filename(function1->op_array.filename);
155 CG(zend_lineno) = function1->op_array.opcodes[0].lineno;
156 if (function2->type == ZEND_USER_FUNCTION
157 && function2->op_array.last > 0) {
158 zend_error(E_ERROR, "Cannot redeclare %s() (previously declared in %s:%d)",
159 ZSTR_VAL(function1->common.function_name),
160 ZSTR_VAL(function2->op_array.filename),
161 (int)function2->op_array.opcodes[0].lineno);
162 } else {
163 zend_error(E_ERROR, "Cannot redeclare %s()", ZSTR_VAL(function1->common.function_name));
164 }
165 }
166
zend_accel_class_hash_copy(HashTable * target,HashTable * source)167 static void zend_accel_class_hash_copy(HashTable *target, HashTable *source)
168 {
169 Bucket *p, *end;
170 zval *t;
171
172 zend_hash_extend(target, target->nNumUsed + source->nNumUsed, 0);
173 p = source->arData;
174 end = p + source->nNumUsed;
175 for (; p != end; p++) {
176 ZEND_ASSERT(Z_TYPE(p->val) != IS_UNDEF);
177 ZEND_ASSERT(p->key);
178 t = zend_hash_find_known_hash(target, p->key);
179 if (UNEXPECTED(t != NULL)) {
180 if (EXPECTED(ZSTR_LEN(p->key) > 0) && EXPECTED(ZSTR_VAL(p->key)[0] == 0)) {
181 /* Runtime definition key. There are two circumstances under which the key can
182 * already be defined:
183 * 1. The file has been re-included without being changed in the meantime. In
184 * this case we can keep the old value, because we know that the definition
185 * hasn't changed.
186 * 2. The file has been changed in the meantime, but the RTD key ends up colliding.
187 * This would be a bug.
188 * As we can't distinguish these cases, we assume that it is 1. and keep the old
189 * value. */
190 continue;
191 } else if (UNEXPECTED(!ZCG(accel_directives).ignore_dups)) {
192 zend_class_entry *ce1 = Z_PTR(p->val);
193 if (!(ce1->ce_flags & ZEND_ACC_ANON_CLASS)) {
194 CG(in_compilation) = 1;
195 zend_set_compiled_filename(ce1->info.user.filename);
196 CG(zend_lineno) = ce1->info.user.line_start;
197 zend_error(E_ERROR,
198 "Cannot declare %s %s, because the name is already in use",
199 zend_get_object_type(ce1), ZSTR_VAL(ce1->name));
200 return;
201 }
202 continue;
203 }
204 } else {
205 zend_class_entry *ce = Z_PTR(p->val);
206 t = _zend_hash_append_ptr_ex(target, p->key, Z_PTR(p->val), 1);
207 if ((ce->ce_flags & ZEND_ACC_LINKED)
208 && ZSTR_HAS_CE_CACHE(ce->name)
209 && ZSTR_VAL(p->key)[0]) {
210 ZSTR_SET_CE_CACHE_EX(ce->name, ce, 0);
211 }
212 }
213 }
214 target->nInternalPointer = 0;
215 return;
216 }
217
zend_accel_load_script(zend_persistent_script * persistent_script,int from_shared_memory)218 zend_op_array* zend_accel_load_script(zend_persistent_script *persistent_script, int from_shared_memory)
219 {
220 zend_op_array *op_array;
221
222 op_array = (zend_op_array *) emalloc(sizeof(zend_op_array));
223 *op_array = persistent_script->script.main_op_array;
224
225 if (EXPECTED(from_shared_memory)) {
226 if (ZCSG(map_ptr_last) > CG(map_ptr_last)) {
227 zend_map_ptr_extend(ZCSG(map_ptr_last));
228 }
229
230 /* Register __COMPILER_HALT_OFFSET__ constant */
231 if (persistent_script->compiler_halt_offset != 0 &&
232 persistent_script->script.filename) {
233 zend_string *name;
234 static const char haltoff[] = "__COMPILER_HALT_OFFSET__";
235
236 name = zend_mangle_property_name(haltoff, sizeof(haltoff) - 1, ZSTR_VAL(persistent_script->script.filename), ZSTR_LEN(persistent_script->script.filename), 0);
237 if (!zend_hash_exists(EG(zend_constants), name)) {
238 zend_register_long_constant(ZSTR_VAL(name), ZSTR_LEN(name), persistent_script->compiler_halt_offset, CONST_CS, 0);
239 }
240 zend_string_release_ex(name, 0);
241 }
242 }
243
244 if (zend_hash_num_elements(&persistent_script->script.function_table) > 0) {
245 zend_accel_function_hash_copy(CG(function_table), &persistent_script->script.function_table);
246 }
247
248 if (zend_hash_num_elements(&persistent_script->script.class_table) > 0) {
249 zend_accel_class_hash_copy(CG(class_table), &persistent_script->script.class_table);
250 }
251
252 if (persistent_script->script.first_early_binding_opline != (uint32_t)-1) {
253 zend_string *orig_compiled_filename = CG(compiled_filename);
254 CG(compiled_filename) = persistent_script->script.filename;
255 zend_do_delayed_early_binding(op_array, persistent_script->script.first_early_binding_opline);
256 CG(compiled_filename) = orig_compiled_filename;
257 }
258
259 if (UNEXPECTED(!from_shared_memory)) {
260 free_persistent_script(persistent_script, 0); /* free only hashes */
261 }
262
263 return op_array;
264 }
265
266 /*
267 * zend_adler32() is based on zlib implementation
268 * Computes the Adler-32 checksum of a data stream
269 *
270 * Copyright (C) 1995-2005 Mark Adler
271 * For conditions of distribution and use, see copyright notice in zlib.h
272 *
273 * Copyright (C) 1995-2005 Jean-loup Gailly and Mark Adler
274 *
275 * This software is provided 'as-is', without any express or implied
276 * warranty. In no event will the authors be held liable for any damages
277 * arising from the use of this software.
278 *
279 * Permission is granted to anyone to use this software for any purpose,
280 * including commercial applications, and to alter it and redistribute it
281 * freely, subject to the following restrictions:
282 *
283 * 1. The origin of this software must not be misrepresented; you must not
284 * claim that you wrote the original software. If you use this software
285 * in a product, an acknowledgment in the product documentation would be
286 * appreciated but is not required.
287 * 2. Altered source versions must be plainly marked as such, and must not be
288 * misrepresented as being the original software.
289 * 3. This notice may not be removed or altered from any source distribution.
290 *
291 */
292
293 #define ADLER32_BASE 65521 /* largest prime smaller than 65536 */
294 #define ADLER32_NMAX 5552
295 /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
296
297 #define ADLER32_DO1(buf) {s1 += *(buf); s2 += s1;}
298 #define ADLER32_DO2(buf, i) ADLER32_DO1(buf + i); ADLER32_DO1(buf + i + 1);
299 #define ADLER32_DO4(buf, i) ADLER32_DO2(buf, i); ADLER32_DO2(buf, i + 2);
300 #define ADLER32_DO8(buf, i) ADLER32_DO4(buf, i); ADLER32_DO4(buf, i + 4);
301 #define ADLER32_DO16(buf) ADLER32_DO8(buf, 0); ADLER32_DO8(buf, 8);
302
zend_adler32(unsigned int checksum,unsigned char * buf,uint32_t len)303 unsigned int zend_adler32(unsigned int checksum, unsigned char *buf, uint32_t len)
304 {
305 unsigned int s1 = checksum & 0xffff;
306 unsigned int s2 = (checksum >> 16) & 0xffff;
307 unsigned char *end;
308
309 while (len >= ADLER32_NMAX) {
310 len -= ADLER32_NMAX;
311 end = buf + ADLER32_NMAX;
312 do {
313 ADLER32_DO16(buf);
314 buf += 16;
315 } while (buf != end);
316 s1 %= ADLER32_BASE;
317 s2 %= ADLER32_BASE;
318 }
319
320 if (len) {
321 if (len >= 16) {
322 end = buf + (len & 0xfff0);
323 len &= 0xf;
324 do {
325 ADLER32_DO16(buf);
326 buf += 16;
327 } while (buf != end);
328 }
329 if (len) {
330 end = buf + len;
331 do {
332 ADLER32_DO1(buf);
333 buf++;
334 } while (buf != end);
335 }
336 s1 %= ADLER32_BASE;
337 s2 %= ADLER32_BASE;
338 }
339
340 return (s2 << 16) | s1;
341 }
342
zend_accel_script_checksum(zend_persistent_script * persistent_script)343 unsigned int zend_accel_script_checksum(zend_persistent_script *persistent_script)
344 {
345 unsigned char *mem = (unsigned char*)persistent_script->mem;
346 size_t size = persistent_script->size;
347 size_t persistent_script_check_block_size = ((char *)&(persistent_script->dynamic_members)) - (char *)persistent_script;
348 unsigned int checksum = ADLER32_INIT;
349
350 if (mem < (unsigned char*)persistent_script) {
351 checksum = zend_adler32(checksum, mem, (unsigned char*)persistent_script - mem);
352 size -= (unsigned char*)persistent_script - mem;
353 mem += (unsigned char*)persistent_script - mem;
354 }
355
356 zend_adler32(checksum, mem, persistent_script_check_block_size);
357 mem += sizeof(*persistent_script);
358 size -= sizeof(*persistent_script);
359
360 if (size > 0) {
361 checksum = zend_adler32(checksum, mem, size);
362 }
363 return checksum;
364 }
365