Skip to content

Commit 0dcb137

Browse files
add tests for ZendMM Observer
1 parent baafc33 commit 0dcb137

10 files changed

+217
-2
lines changed

ext/zend_test/config.m4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ PHP_ARG_ENABLE([zend-test],
44
[Enable zend_test extension])])
55

66
if test "$PHP_ZEND_TEST" != "no"; then
7-
PHP_NEW_EXTENSION(zend_test, test.c observer.c fiber.c, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
7+
PHP_NEW_EXTENSION(zend_test, test.c observer.c fiber.c zendmm_observer.c, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
88
fi

ext/zend_test/config.w32

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
ARG_ENABLE("zend-test", "enable zend_test extension", "no");
44

55
if (PHP_ZEND_TEST != "no") {
6-
EXTENSION("zend_test", "test.c observer.c fiber.c", PHP_ZEND_TEST_SHARED, "/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1");
6+
EXTENSION("zend_test", "test.c observer.c fiber.c zendmm_observer.c", PHP_ZEND_TEST_SHARED, "/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1");
77
ADD_FLAG("CFLAGS_ZEND_TEST", "/D PHP_ZEND_TEST_EXPORTS ");
88
}

ext/zend_test/php_test.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ ZEND_BEGIN_MODULE_GLOBALS(zend_test)
5959
zend_long quantity_value;
6060
zend_string *str_test;
6161
zend_string *not_empty_str_test;
62+
int zendmm_observer_enabled;
6263
ZEND_END_MODULE_GLOBALS(zend_test)
6364

6465
extern ZEND_DECLARE_MODULE_GLOBALS(zend_test)

ext/zend_test/test.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "ext/standard/info.h"
2424
#include "php_test.h"
2525
#include "observer.h"
26+
#include "zendmm_observer.h"
2627
#include "fiber.h"
2728
#include "zend_attributes.h"
2829
#include "zend_enum.h"
@@ -1031,6 +1032,7 @@ PHP_MINIT_FUNCTION(zend_test)
10311032
}
10321033

10331034
zend_test_observer_init(INIT_FUNC_ARGS_PASSTHRU);
1035+
zend_test_mm_observer_minit(INIT_FUNC_ARGS_PASSTHRU);
10341036
zend_test_fiber_init();
10351037

10361038
le_throwing_resource = zend_register_list_destructors_ex(le_throwing_resource_dtor, NULL, "throwing resource", module_number);
@@ -1057,6 +1059,7 @@ PHP_RINIT_FUNCTION(zend_test)
10571059
{
10581060
zend_hash_init(&ZT_G(global_weakmap), 8, NULL, ZVAL_PTR_DTOR, 0);
10591061
ZT_G(observer_nesting_depth) = 0;
1062+
zend_test_mm_observer_rinit();
10601063
return SUCCESS;
10611064
}
10621065

@@ -1067,6 +1070,7 @@ PHP_RSHUTDOWN_FUNCTION(zend_test)
10671070
zend_weakrefs_hash_del(&ZT_G(global_weakmap), zend_weakref_key_to_object(obj_key));
10681071
} ZEND_HASH_FOREACH_END();
10691072
zend_hash_destroy(&ZT_G(global_weakmap));
1073+
zend_test_mm_observer_rshutdown();
10701074
return SUCCESS;
10711075
}
10721076

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
ZendMM Observer: Observe all
3+
--EXTENSIONS--
4+
zend_test
5+
--INI--
6+
zend_test.zendmm_observer.enabled=1
7+
opcache.enable=0
8+
--FILE--
9+
<?php
10+
?>
11+
--EXPECTREGEX--
12+
.*
13+
ZendMM Observer enabled
14+
.*
15+
.*
16+
ZendMM Observer disabled
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
ZendMM Observer: Observe all
3+
--EXTENSIONS--
4+
zend_test
5+
--INI--
6+
zend_test.zendmm_observer.enabled=0
7+
opcache.enable=0
8+
--FILE--
9+
<?php
10+
ini_set('zend_test.zendmm_observer.enabled', 'true');
11+
$str0 = str_repeat("ZendMM Observer", 100);
12+
ini_set('zend_test.zendmm_observer.enabled', 'false');
13+
14+
$str1 = str_repeat("ZendMM Observer", 100);
15+
16+
ini_set('zend_test.zendmm_observer.enabled', 'true');
17+
$str2 = str_repeat("ZendMM Observer", 100);
18+
unset($str2);
19+
ini_set('zend_test.zendmm_observer.enabled', 'false');
20+
?>
21+
--EXPECTF--
22+
malloc 0x%s of size %d (block: %d)
23+
malloc 0x%s of size %d (block: %d)
24+
freed 0x%s of size %d

ext/zend_test/zendmm_observer.c

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| Copyright (c) The PHP Group |
4+
+----------------------------------------------------------------------+
5+
| This source file is subject to version 3.01 of the PHP license, |
6+
| that is bundled with this package in the file LICENSE, and is |
7+
| available through the world-wide-web at the following url: |
8+
| https://www.php.net/license/3_01.txt |
9+
| If you did not receive a copy of the PHP license and are unable to |
10+
| obtain it through the world-wide-web, please send a note to |
11+
| license@php.net so we can mail you a copy immediately. |
12+
+----------------------------------------------------------------------+
13+
| Author: |
14+
+----------------------------------------------------------------------+
15+
*/
16+
17+
#include "php.h"
18+
#include "php_test.h"
19+
#include "Zend/zend_alloc.h"
20+
#include "ext/standard/info.h"
21+
#include "ext/standard/php_var.h"
22+
#include "zendmm_observer.h"
23+
24+
__thread zend_mm_observer *observer = NULL;
25+
26+
void observer_malloc(size_t len, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
27+
{
28+
size_t block_len = zend_mm_block_size(zend_mm_get_heap(), ptr);
29+
printf("malloc %p of size %zu (block: %zu)\n", ptr, len, block_len);
30+
}
31+
32+
void observer_free(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
33+
{
34+
size_t block_len = zend_mm_block_size(zend_mm_get_heap(), ptr);
35+
printf("freed %p of size %zu\n", ptr, block_len);
36+
}
37+
38+
void observer_realloc(void *ptr, size_t len, void *newptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
39+
{
40+
size_t block_len = zend_mm_block_size(zend_mm_get_heap(), newptr);
41+
printf("realloc %p of size %zu (block: %zu, former %p)\n", newptr, len, block_len, ptr);
42+
}
43+
44+
PHP_FUNCTION(memprof_enable)
45+
{
46+
ZEND_PARSE_PARAMETERS_NONE();
47+
if (observer) {
48+
RETURN_FALSE;
49+
}
50+
observer = zend_mm_observer_register(zend_mm_get_heap(), observer_malloc, observer_free, observer_realloc);
51+
RETURN_TRUE;
52+
}
53+
54+
PHP_FUNCTION(memprof_disable)
55+
{
56+
ZEND_PARSE_PARAMETERS_NONE();
57+
zend_mm_observer_unregister(zend_mm_get_heap(), observer);
58+
observer = NULL;
59+
RETURN_TRUE;
60+
}
61+
62+
static PHP_INI_MH(OnUpdate_zend_test_zendmm_observer_enabled)
63+
{
64+
int int_value;
65+
if (new_value == NULL) {
66+
return FAILURE;
67+
}
68+
69+
if (zend_string_equals_literal_ci(new_value, "true")) {
70+
int_value = 1;
71+
} else if (zend_string_equals_literal_ci(new_value, "false")) {
72+
int_value = 0;
73+
} else {
74+
int_value = (int) zend_ini_parse_quantity_warn(new_value, entry->name);
75+
}
76+
77+
if (int_value == 1) {
78+
if (observer == NULL) {
79+
observer = zend_mm_observer_register(zend_mm_get_heap(), observer_malloc, observer_free, observer_realloc);
80+
}
81+
} else {
82+
if (observer != NULL) {
83+
zend_mm_observer_unregister(zend_mm_get_heap(), observer);
84+
observer = NULL;
85+
}
86+
}
87+
return OnUpdateBool(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
88+
}
89+
90+
PHP_INI_BEGIN()
91+
STD_PHP_INI_BOOLEAN("zend_test.zendmm_observer.enabled", "0", PHP_INI_ALL, OnUpdate_zend_test_zendmm_observer_enabled, zendmm_observer_enabled, zend_zend_test_globals, zend_test_globals)
92+
PHP_INI_END()
93+
94+
void zend_test_mm_observer_minit(INIT_FUNC_ARGS)
95+
{
96+
if (type != MODULE_TEMPORARY) {
97+
REGISTER_INI_ENTRIES();
98+
} else {
99+
(void)ini_entries;
100+
}
101+
}
102+
103+
void zend_test_mm_observer_rinit(void)
104+
{
105+
if (ZT_G(zendmm_observer_enabled)) {
106+
printf("ZendMM Observer enabled\n");
107+
observer = zend_mm_observer_register(zend_mm_get_heap(), observer_malloc, observer_free, observer_realloc);
108+
}
109+
}
110+
111+
void zend_test_mm_observer_rshutdown(void)
112+
{
113+
if (observer) {
114+
printf("ZendMM Observer disabled\n");
115+
zend_mm_observer_unregister(zend_mm_get_heap(), observer);
116+
}
117+
observer = NULL;
118+
}
119+

ext/zend_test/zendmm_observer.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| Copyright (c) The PHP Group |
4+
+----------------------------------------------------------------------+
5+
| This source file is subject to version 3.01 of the PHP license, |
6+
| that is bundled with this package in the file LICENSE, and is |
7+
| available through the world-wide-web at the following url: |
8+
| https://www.php.net/license/3_01.txt |
9+
| If you did not receive a copy of the PHP license and are unable to |
10+
| obtain it through the world-wide-web, please send a note to |
11+
| license@php.net so we can mail you a copy immediately. |
12+
+----------------------------------------------------------------------+
13+
| Author: |
14+
+----------------------------------------------------------------------+
15+
*/
16+
17+
#ifndef ZEND_TEST_MM_OBSERVER_H
18+
#define ZEND_TEST_MM_OBSERVER_H
19+
20+
void zend_test_mm_observer_minit(INIT_FUNC_ARGS);
21+
void zend_test_mm_observer_rinit(void);
22+
void zend_test_mm_observer_rshutdown(void);
23+
24+
#endif
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
/**
4+
* @generate-class-entries static
5+
* @undocumentable
6+
*/
7+
8+
function memprof_enable(): bool {}
9+
function memprof_disable(): bool {}

ext/zend_test/zendmm_observer_arginfo.h

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)