Skip to content

Commit 0fcb4e4

Browse files
add missing test
1 parent 3056f4c commit 0fcb4e4

File tree

4 files changed

+110
-1
lines changed

4 files changed

+110
-1
lines changed

ext/zend_test/test.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "zend_interfaces.h"
3131
#include "zend_weakrefs.h"
3232
#include "Zend/Optimizer/zend_optimizer.h"
33+
#include "Zend/zend_alloc.h"
3334
#include "test_arginfo.h"
3435

3536
#if defined(HAVE_LIBXML) && !defined(PHP_WIN32)
@@ -364,6 +365,67 @@ static ZEND_FUNCTION(zend_test_crash)
364365
php_printf("%s", invalid);
365366
}
366367

368+
zend_mm_heap* zend_test_heap;
369+
zend_mm_heap* zend_orig_heap;
370+
volatile uint32_t lineno = 0;
371+
372+
static bool has_opline(zend_execute_data *execute_data)
373+
{
374+
return execute_data
375+
&& execute_data->func
376+
&& ZEND_USER_CODE(execute_data->func->type)
377+
&& execute_data->opline
378+
;
379+
}
380+
381+
#pragma GCC push_options
382+
#pragma GCC optimize("O0")
383+
void * __attribute__((optnone)) zend_test_custom_malloc(size_t len)
384+
{
385+
if (has_opline(EG(current_execute_data))) {
386+
lineno = EG(current_execute_data)->opline->lineno;
387+
}
388+
return _zend_mm_alloc(zend_orig_heap, len);
389+
}
390+
391+
void __attribute__((optnone)) zend_test_custom_free(void *ptr)
392+
{
393+
if (has_opline(EG(current_execute_data))) {
394+
lineno = EG(current_execute_data)->opline->lineno;
395+
}
396+
_zend_mm_free(zend_orig_heap, ptr);
397+
}
398+
399+
void * __attribute__((optnone)) zend_test_custom_realloc(void * ptr, size_t len)
400+
{
401+
if (has_opline(EG(current_execute_data))) {
402+
lineno = EG(current_execute_data)->opline->lineno;
403+
}
404+
return _zend_mm_realloc(zend_orig_heap, ptr, len);
405+
}
406+
#pragma GCC pop_options
407+
408+
static ZEND_FUNCTION(zend_test_observe_opline_in_zendmm)
409+
{
410+
zend_test_heap = malloc(4096);
411+
memset(zend_test_heap, 0, 4096);
412+
zend_mm_set_custom_handlers(
413+
zend_test_heap,
414+
zend_test_custom_malloc,
415+
zend_test_custom_free,
416+
zend_test_custom_realloc
417+
);
418+
zend_orig_heap = zend_mm_get_heap();
419+
zend_mm_set_heap(zend_test_heap);
420+
}
421+
422+
static ZEND_FUNCTION(zend_test_unobserve_opline_in_zendmm)
423+
{
424+
free(zend_test_heap);
425+
zend_test_heap = NULL;
426+
zend_mm_set_heap(zend_orig_heap);
427+
}
428+
367429
static ZEND_FUNCTION(zend_test_is_pcre_bundled)
368430
{
369431
ZEND_PARSE_PARAMETERS_NONE();

ext/zend_test/test.stub.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ function zend_get_map_ptr_last(): int {}
122122

123123
function zend_test_crash(?string $message = null): void {}
124124

125+
function zend_test_observe_opline_in_zendmm(): void {}
126+
127+
function zend_test_unobserve_opline_in_zendmm(): void {}
128+
125129
#if defined(HAVE_LIBXML) && !defined(PHP_WIN32)
126130
function zend_test_override_libxml_global_state(): void {}
127131
#endif

ext/zend_test/test_arginfo.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: ae75eda2b4b40224858d680c3fcf3d7cd2056bb6 */
2+
* Stub hash: a57c9d1fc85dbea853f4cc910b9495a7a0c72eb3 */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_array_return, 0, 0, IS_ARRAY, 0)
55
ZEND_END_ARG_INFO()
@@ -86,6 +86,10 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_crash, 0, 0, IS_VOID,
8686
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, message, IS_STRING, 1, "null")
8787
ZEND_END_ARG_INFO()
8888

89+
#define arginfo_zend_test_observe_opline_in_zendmm arginfo_zend_test_void_return
90+
91+
#define arginfo_zend_test_unobserve_opline_in_zendmm arginfo_zend_test_void_return
92+
8993
#if defined(HAVE_LIBXML) && !defined(PHP_WIN32)
9094
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_override_libxml_global_state, 0, 0, IS_VOID, 0)
9195
ZEND_END_ARG_INFO()
@@ -155,6 +159,8 @@ static ZEND_FUNCTION(zend_get_current_func_name);
155159
static ZEND_FUNCTION(zend_call_method);
156160
static ZEND_FUNCTION(zend_get_map_ptr_last);
157161
static ZEND_FUNCTION(zend_test_crash);
162+
static ZEND_FUNCTION(zend_test_observe_opline_in_zendmm);
163+
static ZEND_FUNCTION(zend_test_unobserve_opline_in_zendmm);
158164
#if defined(HAVE_LIBXML) && !defined(PHP_WIN32)
159165
static ZEND_FUNCTION(zend_test_override_libxml_global_state);
160166
#endif
@@ -199,6 +205,8 @@ static const zend_function_entry ext_functions[] = {
199205
ZEND_FE(zend_call_method, arginfo_zend_call_method)
200206
ZEND_FE(zend_get_map_ptr_last, arginfo_zend_get_map_ptr_last)
201207
ZEND_FE(zend_test_crash, arginfo_zend_test_crash)
208+
ZEND_FE(zend_test_observe_opline_in_zendmm, arginfo_zend_test_observe_opline_in_zendmm)
209+
ZEND_FE(zend_test_unobserve_opline_in_zendmm, arginfo_zend_test_unobserve_opline_in_zendmm)
202210
#if defined(HAVE_LIBXML) && !defined(PHP_WIN32)
203211
ZEND_FE(zend_test_override_libxml_global_state, arginfo_zend_test_override_libxml_global_state)
204212
#endif
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
possible segfault in `ZEND_BIND_STATIC`
3+
--DESCRIPTION--
4+
https://github.com/php/php-src/pull/12758
5+
--EXTENSIONS--
6+
zend_test
7+
--FILE--
8+
<?php
9+
10+
function &ref() {
11+
static $a = 5;
12+
return $a;
13+
}
14+
15+
class Foo {
16+
public static int $i;
17+
public static string $s = "x";
18+
}
19+
20+
zend_test_observe_opline_in_zendmm();
21+
22+
var_dump(Foo::$i = "1");
23+
var_dump(Foo::$s, Foo::$i);
24+
var_dump(ref());
25+
26+
zend_test_unobserve_opline_in_zendmm();
27+
28+
echo 'Done.';
29+
?>
30+
--EXPECT--
31+
int(1)
32+
string(1) "x"
33+
int(1)
34+
int(5)
35+
Done.

0 commit comments

Comments
 (0)