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.opcodes[0].lineno;
179 	if (function2->type == ZEND_USER_FUNCTION
180 		&& function2->op_array.last > 0) {
181 		zend_error_noreturn(E_ERROR, "Cannot redeclare function %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.opcodes[0].lineno);
185 	} else {
186 		zend_error_noreturn(E_ERROR, "Cannot redeclare function %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_class_redeclaration_error(E_ERROR, Z_PTR_P(t));
231 					return;
232 				}
233 				continue;
234 			}
235 		} else {
236 			zend_class_entry *ce = Z_PTR(p->val);
237 			_zend_hash_append_ptr_ex(target, p->key, Z_PTR(p->val), 1);
238 			if ((ce->ce_flags & ZEND_ACC_LINKED) && ZSTR_VAL(p->key)[0]) {
239 				if (ZSTR_HAS_CE_CACHE(ce->name)) {
240 					ZSTR_SET_CE_CACHE_EX(ce->name, ce, 0);
241 				}
242 				if (UNEXPECTED(call_observers)) {
243 					_zend_observer_class_linked_notify(ce, p->key);
244 				}
245 			}
246 		}
247 	}
248 	target->nInternalPointer = 0;
249 }
250 
zend_accel_class_hash_copy(HashTable * target,HashTable * source)251 static zend_always_inline void zend_accel_class_hash_copy(HashTable *target, HashTable *source)
252 {
253 	_zend_accel_class_hash_copy(target, source, 0);
254 }
255 
zend_accel_class_hash_copy_notify(HashTable * target,HashTable * source)256 static zend_never_inline void zend_accel_class_hash_copy_notify(HashTable *target, HashTable *source)
257 {
258 	_zend_accel_class_hash_copy(target, source, 1);
259 }
260 
zend_accel_build_delayed_early_binding_list(zend_persistent_script * persistent_script)261 void zend_accel_build_delayed_early_binding_list(zend_persistent_script *persistent_script)
262 {
263 	zend_op_array *op_array = &persistent_script->script.main_op_array;
264 	if (!(op_array->fn_flags & ZEND_ACC_EARLY_BINDING)) {
265 		return;
266 	}
267 
268 	zend_op *end = op_array->opcodes + op_array->last;
269 	for (zend_op *opline = op_array->opcodes; opline < end; opline++) {
270 		if (opline->opcode == ZEND_DECLARE_CLASS_DELAYED) {
271 			persistent_script->num_early_bindings++;
272 		}
273 	}
274 
275 	zend_early_binding *early_binding = persistent_script->early_bindings =
276 		emalloc(sizeof(zend_early_binding) * persistent_script->num_early_bindings);
277 
278 	for (zend_op *opline = op_array->opcodes; opline < end; opline++) {
279 		if (opline->opcode == ZEND_DECLARE_CLASS_DELAYED) {
280 			zval *lcname = RT_CONSTANT(opline, opline->op1);
281 			early_binding->lcname = zend_string_copy(Z_STR_P(lcname));
282 			early_binding->rtd_key = zend_string_copy(Z_STR_P(lcname + 1));
283 			early_binding->lc_parent_name =
284 				zend_string_copy(Z_STR_P(RT_CONSTANT(opline, opline->op2)));
285 			early_binding->cache_slot = (uint32_t) -1;
286 			early_binding++;
287 		}
288 	}
289 }
290 
zend_accel_finalize_delayed_early_binding_list(zend_persistent_script * persistent_script)291 void zend_accel_finalize_delayed_early_binding_list(zend_persistent_script *persistent_script)
292 {
293 	if (!persistent_script->num_early_bindings) {
294 		return;
295 	}
296 
297 	zend_early_binding *early_binding = persistent_script->early_bindings;
298 	zend_early_binding *early_binding_end = early_binding + persistent_script->num_early_bindings;
299 	zend_op_array *op_array = &persistent_script->script.main_op_array;
300 	zend_op *opline_end = op_array->opcodes + op_array->last;
301 	for (zend_op *opline = op_array->opcodes; opline < opline_end; opline++) {
302 		if (opline->opcode == ZEND_DECLARE_CLASS_DELAYED) {
303 			zend_string *rtd_key = Z_STR_P(RT_CONSTANT(opline, opline->op1) + 1);
304 			/* Skip early_binding entries that don't match, maybe their DECLARE_CLASS_DELAYED
305 			 * was optimized away. */
306 			while (!zend_string_equals(early_binding->rtd_key, rtd_key)) {
307 				early_binding++;
308 				if (early_binding >= early_binding_end) {
309 					return;
310 				}
311 			}
312 
313 			early_binding->cache_slot = opline->extended_value;
314 			early_binding++;
315 			if (early_binding >= early_binding_end) {
316 				return;
317 			}
318 		}
319 	}
320 }
321 
zend_accel_free_delayed_early_binding_list(zend_persistent_script * persistent_script)322 void zend_accel_free_delayed_early_binding_list(zend_persistent_script *persistent_script)
323 {
324 	if (persistent_script->num_early_bindings) {
325 		for (uint32_t i = 0; i < persistent_script->num_early_bindings; i++) {
326 			zend_early_binding *early_binding = &persistent_script->early_bindings[i];
327 			zend_string_release(early_binding->lcname);
328 			zend_string_release(early_binding->rtd_key);
329 			zend_string_release(early_binding->lc_parent_name);
330 		}
331 		efree(persistent_script->early_bindings);
332 		persistent_script->early_bindings = NULL;
333 		persistent_script->num_early_bindings = 0;
334 	}
335 }
336 
zend_accel_do_delayed_early_binding(zend_persistent_script * persistent_script,zend_op_array * op_array)337 static void zend_accel_do_delayed_early_binding(
338 		zend_persistent_script *persistent_script, zend_op_array *op_array)
339 {
340 	ZEND_ASSERT(!ZEND_MAP_PTR(op_array->run_time_cache));
341 	ZEND_ASSERT(op_array->fn_flags & ZEND_ACC_HEAP_RT_CACHE);
342 	void *run_time_cache = emalloc(op_array->cache_size);
343 
344 	ZEND_MAP_PTR_INIT(op_array->run_time_cache, run_time_cache);
345 	memset(run_time_cache, 0, op_array->cache_size);
346 
347 	zend_string *orig_compiled_filename = CG(compiled_filename);
348 	bool orig_in_compilation = CG(in_compilation);
349 	CG(compiled_filename) = persistent_script->script.filename;
350 	CG(in_compilation) = 1;
351 	for (uint32_t i = 0; i < persistent_script->num_early_bindings; i++) {
352 		zend_early_binding *early_binding = &persistent_script->early_bindings[i];
353 		zend_class_entry *ce = zend_hash_find_ex_ptr(EG(class_table), early_binding->lcname, 1);
354 		if (!ce) {
355 			zval *zv = zend_hash_find_known_hash(EG(class_table), early_binding->rtd_key);
356 			if (zv) {
357 				zend_class_entry *orig_ce = Z_CE_P(zv);
358 				zend_class_entry *parent_ce = !(orig_ce->ce_flags & ZEND_ACC_LINKED)
359 					? zend_hash_find_ex_ptr(EG(class_table), early_binding->lc_parent_name, 1)
360 					: NULL;
361 				if (parent_ce || (orig_ce->ce_flags & ZEND_ACC_LINKED)) {
362 					ce = zend_try_early_bind(orig_ce, parent_ce, early_binding->lcname, zv);
363 				}
364 			}
365 			if (ce && early_binding->cache_slot != (uint32_t) -1) {
366 				*(void**)((char*)run_time_cache + early_binding->cache_slot) = ce;
367 			}
368 		}
369 	}
370 	CG(compiled_filename) = orig_compiled_filename;
371 	CG(in_compilation) = orig_in_compilation;
372 }
373 
zend_accel_load_script(zend_persistent_script * persistent_script,int from_shared_memory)374 zend_op_array* zend_accel_load_script(zend_persistent_script *persistent_script, int from_shared_memory)
375 {
376 	zend_op_array *op_array;
377 
378 	op_array = (zend_op_array *) emalloc(sizeof(zend_op_array));
379 	*op_array = persistent_script->script.main_op_array;
380 
381 	if (EXPECTED(from_shared_memory)) {
382 		if (ZCSG(map_ptr_last) > CG(map_ptr_last)) {
383 			zend_map_ptr_extend(ZCSG(map_ptr_last));
384 		}
385 
386 		/* Register __COMPILER_HALT_OFFSET__ constant */
387 		if (persistent_script->compiler_halt_offset != 0 &&
388 		    persistent_script->script.filename) {
389 			zend_string *name;
390 			static const char haltoff[] = "__COMPILER_HALT_OFFSET__";
391 
392 			name = zend_mangle_property_name(haltoff, sizeof(haltoff) - 1, ZSTR_VAL(persistent_script->script.filename), ZSTR_LEN(persistent_script->script.filename), 0);
393 			if (!zend_hash_exists(EG(zend_constants), name)) {
394 				zend_register_long_constant(ZSTR_VAL(name), ZSTR_LEN(name), persistent_script->compiler_halt_offset, 0, 0);
395 			}
396 			zend_string_release_ex(name, 0);
397 		}
398 	}
399 
400 	if (zend_hash_num_elements(&persistent_script->script.function_table) > 0) {
401 		if (EXPECTED(!zend_observer_function_declared_observed)) {
402 			zend_accel_function_hash_copy(CG(function_table), &persistent_script->script.function_table);
403 		} else {
404 			zend_accel_function_hash_copy_notify(CG(function_table), &persistent_script->script.function_table);
405 		}
406 	}
407 
408 	if (zend_hash_num_elements(&persistent_script->script.class_table) > 0) {
409 		if (EXPECTED(!zend_observer_class_linked_observed)) {
410 			zend_accel_class_hash_copy(CG(class_table), &persistent_script->script.class_table);
411 		} else {
412 			zend_accel_class_hash_copy_notify(CG(class_table), &persistent_script->script.class_table);
413 		}
414 	}
415 
416 	if (persistent_script->num_early_bindings) {
417 		zend_accel_do_delayed_early_binding(persistent_script, op_array);
418 	}
419 
420 	if (UNEXPECTED(!from_shared_memory)) {
421 		free_persistent_script(persistent_script, 0); /* free only hashes */
422 	}
423 
424 	return op_array;
425 }
426 
427 /*
428  * zend_adler32() is based on zlib implementation
429  * Computes the Adler-32 checksum of a data stream
430  *
431  * Copyright (C) 1995-2005 Mark Adler
432  * For conditions of distribution and use, see copyright notice in zlib.h
433  *
434  * Copyright (C) 1995-2005 Jean-loup Gailly and Mark Adler
435  *
436  *  This software is provided 'as-is', without any express or implied
437  *  warranty.  In no event will the authors be held liable for any damages
438  *  arising from the use of this software.
439  *
440  *  Permission is granted to anyone to use this software for any purpose,
441  *  including commercial applications, and to alter it and redistribute it
442  *  freely, subject to the following restrictions:
443  *
444  *  1. The origin of this software must not be misrepresented; you must not
445  *     claim that you wrote the original software. If you use this software
446  *     in a product, an acknowledgment in the product documentation would be
447  *     appreciated but is not required.
448  *  2. Altered source versions must be plainly marked as such, and must not be
449  *     misrepresented as being the original software.
450  *  3. This notice may not be removed or altered from any source distribution.
451  *
452  */
453 
454 #define ADLER32_BASE 65521 /* largest prime smaller than 65536 */
455 #define ADLER32_NMAX 5552
456 /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
457 
458 #define ADLER32_SCALAR_DO1(buf)        {s1 += *(buf); s2 += s1;}
459 #define ADLER32_SCALAR_DO2(buf, i)     ADLER32_SCALAR_DO1(buf + i); ADLER32_SCALAR_DO1(buf + i + 1);
460 #define ADLER32_SCALAR_DO4(buf, i)     ADLER32_SCALAR_DO2(buf, i); ADLER32_SCALAR_DO2(buf, i + 2);
461 #define ADLER32_SCALAR_DO8(buf, i)     ADLER32_SCALAR_DO4(buf, i); ADLER32_SCALAR_DO4(buf, i + 4);
462 #define ADLER32_SCALAR_DO16(buf)       ADLER32_SCALAR_DO8(buf, 0); ADLER32_SCALAR_DO8(buf, 8);
463 
adler32_do16_loop(unsigned char * buf,unsigned char * end,unsigned int * s1_out,unsigned int * s2_out)464 static zend_always_inline void adler32_do16_loop(unsigned char *buf, unsigned char *end, unsigned int *s1_out, unsigned int *s2_out)
465 {
466 	unsigned int s1 = *s1_out;
467 	unsigned int s2 = *s2_out;
468 
469 #ifdef __SSE2__
470 	const __m128i zero = _mm_setzero_si128();
471 
472 	__m128i accumulate_s2 = zero;
473 	unsigned int accumulate_s1 = 0;
474 
475 	do {
476 		__m128i read = _mm_loadu_si128((__m128i *) buf); /* [A:P] */
477 
478 		/* Split the 8-bit-element vector into two 16-bit-element vectors where each element gets zero-extended from 8-bits to 16-bits */
479 		__m128i lower = _mm_unpacklo_epi8(read, zero);									/* [A:H] zero-extended to 16-bits */
480 		__m128i higher = _mm_unpackhi_epi8(read, zero);									/* [I:P] zero-extended to 16-bits */
481 		lower = _mm_madd_epi16(lower, _mm_set_epi16(9, 10, 11, 12, 13, 14, 15, 16));	/* [A * 16:H * 9] */
482 		higher = _mm_madd_epi16(higher, _mm_set_epi16(1, 2, 3, 4, 5, 6, 7, 8)); 		/* [I * 8:P * 1] */
483 
484 		/* We'll cheat here: it's difficult to add 16-bit elementwise, but we can do 32-bit additions.
485 			* The highest value the sum of two elements of the vectors can take is 0xff * 16 + 0xff * 8 < 0xffff.
486 			* That means there is no carry possible from 16->17 bits so the 32-bit addition is safe. */
487 		__m128i sum = _mm_add_epi32(lower, higher); /* [A * 16 + I * 8:H * 9 + P * 1] */
488 		accumulate_s2 = _mm_add_epi32(accumulate_s2, sum);
489 		accumulate_s1 += s1;
490 
491 		/* Computes 8-bit element-wise abs(buf - zero) and then sums the elements into two 16 bit parts */
492 		sum = _mm_sad_epu8(read, zero);
493 		s1 += _mm_cvtsi128_si32(sum) + _mm_extract_epi16(sum, 4);
494 
495 		buf += 16;
496 	} while (buf != end);
497 
498 	/* For convenience, let's do a rename of variables and let accumulate_s2 = [X, Y, Z, W] */
499 	__m128i shuffled = _mm_shuffle_epi32(accumulate_s2, _MM_SHUFFLE(1, 0, 0, 2));	/* [Y, X, X, Z] */
500 	accumulate_s2 = _mm_add_epi32(accumulate_s2, shuffled);							/* [X + Y, Y + X, Z + X, W + Z] */
501 	shuffled = _mm_shuffle_epi32(accumulate_s2, _MM_SHUFFLE(3, 3, 3, 3));			/* [X + Y, X + Y, X + Y, X + Y] */
502 	accumulate_s2 = _mm_add_epi32(accumulate_s2, shuffled);							/* [/, /, /, W + Z + X + Y] */
503 	s2 += accumulate_s1 * 16 + _mm_cvtsi128_si32(accumulate_s2);
504 #else
505 	do {
506 		ADLER32_SCALAR_DO16(buf);
507 		buf += 16;
508 	} while (buf != end);
509 #endif
510 
511 	*s1_out = s1;
512 	*s2_out = s2;
513 }
514 
zend_adler32(unsigned int checksum,unsigned char * buf,uint32_t len)515 unsigned int zend_adler32(unsigned int checksum, unsigned char *buf, uint32_t len)
516 {
517 	unsigned int s1 = checksum & 0xffff;
518 	unsigned int s2 = (checksum >> 16) & 0xffff;
519 	unsigned char *end;
520 
521 	while (len >= ADLER32_NMAX) {
522 		len -= ADLER32_NMAX;
523 		end = buf + ADLER32_NMAX;
524 		adler32_do16_loop(buf, end, &s1, &s2);
525 		buf = end;
526 		s1 %= ADLER32_BASE;
527 		s2 %= ADLER32_BASE;
528 	}
529 
530 	if (len) {
531 		if (len >= 16) {
532 			end = buf + (len & 0xfff0);
533 			len &= 0xf;
534 			adler32_do16_loop(buf, end, &s1, &s2);
535 			buf = end;
536 		}
537 		if (len) {
538 			end = buf + len;
539 			do {
540 				ADLER32_SCALAR_DO1(buf);
541 				buf++;
542 			} while (buf != end);
543 		}
544 		s1 %= ADLER32_BASE;
545 		s2 %= ADLER32_BASE;
546 	}
547 
548 	return (s2 << 16) | s1;
549 }
550 
zend_accel_script_checksum(zend_persistent_script * persistent_script)551 unsigned int zend_accel_script_checksum(zend_persistent_script *persistent_script)
552 {
553 	unsigned char *mem = (unsigned char*)persistent_script->mem;
554 	size_t size = persistent_script->size;
555 	size_t persistent_script_check_block_size = ((char *)&(persistent_script->dynamic_members)) - (char *)persistent_script;
556 	unsigned int checksum = ADLER32_INIT;
557 
558 	if (mem < (unsigned char*)persistent_script) {
559 		checksum = zend_adler32(checksum, mem, (unsigned char*)persistent_script - mem);
560 		size -= (unsigned char*)persistent_script - mem;
561 		mem  += (unsigned char*)persistent_script - mem;
562 	}
563 
564 	zend_adler32(checksum, mem, persistent_script_check_block_size);
565 	mem  += sizeof(*persistent_script);
566 	size -= sizeof(*persistent_script);
567 
568 	if (size > 0) {
569 		checksum = zend_adler32(checksum, mem, size);
570 	}
571 	return checksum;
572 }
573