Skip to content

Commit

Permalink
moved from function call to INI setting, so we can use this in other …
Browse files Browse the repository at this point in the history
…places as well
  • Loading branch information
realFlowControl committed Nov 24, 2023
1 parent 0e40a22 commit 2449f9e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 41 deletions.
3 changes: 3 additions & 0 deletions ext/zend_test/php_test.h
Expand Up @@ -52,6 +52,9 @@ ZEND_BEGIN_MODULE_GLOBALS(zend_test)
HashTable global_weakmap;
int replace_zend_execute_ex;
int register_passes;
int observe_opline_in_zendmm;
zend_mm_heap* zend_orig_heap;
zend_mm_heap* zend_test_heap;
zend_test_fiber *active_fiber;
ZEND_END_MODULE_GLOBALS(zend_test)

Expand Down
58 changes: 34 additions & 24 deletions ext/zend_test/test.c
Expand Up @@ -14,7 +14,6 @@
+----------------------------------------------------------------------+
*/

#include <stdint.h>
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
Expand Down Expand Up @@ -366,9 +365,6 @@ static ZEND_FUNCTION(zend_test_crash)
php_printf("%s", invalid);
}

zend_mm_heap* zend_test_heap;
zend_mm_heap* zend_orig_heap;

static bool has_opline(zend_execute_data *execute_data)
{
return execute_data
Expand All @@ -383,44 +379,50 @@ void * zend_test_custom_malloc(size_t len)
if (has_opline(EG(current_execute_data))) {
assert(EG(current_execute_data)->opline->lineno != (uint32_t)-1);
}
return _zend_mm_alloc(zend_orig_heap, len);
return _zend_mm_alloc(ZT_G(zend_orig_heap), len ZEND_FILE_LINE_EMPTY_CC ZEND_FILE_LINE_EMPTY_CC);
}

void zend_test_custom_free(void *ptr)
{
if (has_opline(EG(current_execute_data))) {
assert(EG(current_execute_data)->opline->lineno != (uint32_t)-1);
}
_zend_mm_free(zend_orig_heap, ptr);
_zend_mm_free(ZT_G(zend_orig_heap), ptr ZEND_FILE_LINE_EMPTY_CC ZEND_FILE_LINE_EMPTY_CC);
}

void * zend_test_custom_realloc(void * ptr, size_t len)
{
if (has_opline(EG(current_execute_data))) {
assert(EG(current_execute_data)->opline->lineno != (uint32_t)-1);
}
return _zend_mm_realloc(zend_orig_heap, ptr, len);
return _zend_mm_realloc(ZT_G(zend_orig_heap), ptr, len ZEND_FILE_LINE_EMPTY_CC ZEND_FILE_LINE_EMPTY_CC);
}

static ZEND_FUNCTION(zend_test_observe_opline_in_zendmm)
static PHP_INI_MH(OnUpdateZendTestObserveOplineInZendMM)
{
zend_test_heap = malloc(4096);
memset(zend_test_heap, 0, 4096);
zend_mm_set_custom_handlers(
zend_test_heap,
zend_test_custom_malloc,
zend_test_custom_free,
zend_test_custom_realloc
);
zend_orig_heap = zend_mm_get_heap();
zend_mm_set_heap(zend_test_heap);
}
if (new_value == NULL) {
return FAILURE;
}

static ZEND_FUNCTION(zend_test_unobserve_opline_in_zendmm)
{
free(zend_test_heap);
zend_test_heap = NULL;
zend_mm_set_heap(zend_orig_heap);
int int_value = zend_ini_parse_bool(new_value);

if (int_value == 1) {
ZT_G(zend_test_heap) = malloc(4096);
memset(ZT_G(zend_test_heap), 0, 4096);
zend_mm_set_custom_handlers(
ZT_G(zend_test_heap),
zend_test_custom_malloc,
zend_test_custom_free,
zend_test_custom_realloc
);
ZT_G(zend_orig_heap) = zend_mm_get_heap();
zend_mm_set_heap(ZT_G(zend_test_heap));
} else if (ZT_G(zend_test_heap)) {
free(ZT_G(zend_test_heap));
ZT_G(zend_test_heap) = NULL;
zend_mm_set_heap(ZT_G(zend_orig_heap));
}
return OnUpdateBool(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
}

static ZEND_FUNCTION(zend_test_is_pcre_bundled)
Expand Down Expand Up @@ -617,6 +619,7 @@ static ZEND_METHOD(ZendTestChildClassWithMethodWithParameterAttribute, override)
PHP_INI_BEGIN()
STD_PHP_INI_BOOLEAN("zend_test.replace_zend_execute_ex", "0", PHP_INI_SYSTEM, OnUpdateBool, replace_zend_execute_ex, zend_zend_test_globals, zend_test_globals)
STD_PHP_INI_BOOLEAN("zend_test.register_passes", "0", PHP_INI_SYSTEM, OnUpdateBool, register_passes, zend_zend_test_globals, zend_test_globals)
STD_PHP_INI_BOOLEAN("zend_test.observe_opline_in_zendmm", "0", PHP_INI_ALL, OnUpdateZendTestObserveOplineInZendMM, observe_opline_in_zendmm, zend_zend_test_globals, zend_test_globals)
PHP_INI_END()

void (*old_zend_execute_ex)(zend_execute_data *execute_data);
Expand Down Expand Up @@ -759,6 +762,13 @@ PHP_RSHUTDOWN_FUNCTION(zend_test)
zend_weakrefs_hash_del(&ZT_G(global_weakmap), (zend_object *)(uintptr_t)objptr);
} ZEND_HASH_FOREACH_END();
zend_hash_destroy(&ZT_G(global_weakmap));

if (ZT_G(zend_test_heap)) {
free(ZT_G(zend_test_heap));
ZT_G(zend_test_heap) = NULL;
zend_mm_set_heap(ZT_G(zend_orig_heap));
}

return SUCCESS;
}

Expand Down
4 changes: 0 additions & 4 deletions ext/zend_test/test.stub.php
Expand Up @@ -122,10 +122,6 @@ function zend_get_map_ptr_last(): int {}

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

function zend_test_observe_opline_in_zendmm(): void {}

function zend_test_unobserve_opline_in_zendmm(): void {}

#if defined(HAVE_LIBXML) && !defined(PHP_WIN32)
function zend_test_override_libxml_global_state(): void {}
#endif
Expand Down
10 changes: 1 addition & 9 deletions ext/zend_test/test_arginfo.h
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: a57c9d1fc85dbea853f4cc910b9495a7a0c72eb3 */
* Stub hash: ae75eda2b4b40224858d680c3fcf3d7cd2056bb6 */

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_array_return, 0, 0, IS_ARRAY, 0)
ZEND_END_ARG_INFO()
Expand Down Expand Up @@ -86,10 +86,6 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_crash, 0, 0, IS_VOID,
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, message, IS_STRING, 1, "null")
ZEND_END_ARG_INFO()

#define arginfo_zend_test_observe_opline_in_zendmm arginfo_zend_test_void_return

#define arginfo_zend_test_unobserve_opline_in_zendmm arginfo_zend_test_void_return

#if defined(HAVE_LIBXML) && !defined(PHP_WIN32)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_override_libxml_global_state, 0, 0, IS_VOID, 0)
ZEND_END_ARG_INFO()
Expand Down Expand Up @@ -159,8 +155,6 @@ static ZEND_FUNCTION(zend_get_current_func_name);
static ZEND_FUNCTION(zend_call_method);
static ZEND_FUNCTION(zend_get_map_ptr_last);
static ZEND_FUNCTION(zend_test_crash);
static ZEND_FUNCTION(zend_test_observe_opline_in_zendmm);
static ZEND_FUNCTION(zend_test_unobserve_opline_in_zendmm);
#if defined(HAVE_LIBXML) && !defined(PHP_WIN32)
static ZEND_FUNCTION(zend_test_override_libxml_global_state);
#endif
Expand Down Expand Up @@ -205,8 +199,6 @@ static const zend_function_entry ext_functions[] = {
ZEND_FE(zend_call_method, arginfo_zend_call_method)
ZEND_FE(zend_get_map_ptr_last, arginfo_zend_get_map_ptr_last)
ZEND_FE(zend_test_crash, arginfo_zend_test_crash)
ZEND_FE(zend_test_observe_opline_in_zendmm, arginfo_zend_test_observe_opline_in_zendmm)
ZEND_FE(zend_test_unobserve_opline_in_zendmm, arginfo_zend_test_unobserve_opline_in_zendmm)
#if defined(HAVE_LIBXML) && !defined(PHP_WIN32)
ZEND_FE(zend_test_override_libxml_global_state, arginfo_zend_test_override_libxml_global_state)
#endif
Expand Down
6 changes: 2 additions & 4 deletions ext/zend_test/tests/opline_dangling.phpt
Expand Up @@ -4,6 +4,8 @@ possible segfault in `ZEND_BIND_STATIC`
https://github.com/php/php-src/pull/12758
--EXTENSIONS--
zend_test
--INI--
zend_test.observe_opline_in_zendmm=1
--FILE--
<?php

Expand All @@ -17,14 +19,10 @@ class Foo {
public static string $s = "x";
}

zend_test_observe_opline_in_zendmm();

var_dump(Foo::$i = "1");
var_dump(Foo::$s, Foo::$i);
var_dump(ref());

zend_test_unobserve_opline_in_zendmm();

echo 'Done.';
?>
--EXPECT--
Expand Down

0 comments on commit 2449f9e

Please sign in to comment.