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