Skip to content
This repository was archived by the owner on Mar 29, 2024. It is now read-only.

Commit e3342f2

Browse files
committed
Gracefully die on OOM unsing new API to report OOM to embedder
V8 commit: v8/v8@bc44b1c
1 parent 2d4d7da commit e3342f2

File tree

2 files changed

+59
-39
lines changed

2 files changed

+59
-39
lines changed

src/php_v8_isolate.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ static void php_v8_isolate_free(zend_object *object) {
170170
}
171171
}
172172

173+
static void php_v8_isolate_oom_error_callback(const char *location, bool is_heap_oom) {
174+
zend_error(E_ERROR, "V8 OOM hit: location=%s, is_heap_oom=%s\n", location, is_heap_oom ? "yes" : "no");
175+
}
173176

174177
static zend_object *php_v8_isolate_ctor(zend_class_entry *ce) {
175178
php_v8_isolate_t *php_v8_isolate;
@@ -242,6 +245,7 @@ static PHP_METHOD(V8Isolate, __construct) {
242245
php_v8_isolate->isolate_handle = Z_OBJ_HANDLE_P(getThis());
243246

244247
php_v8_isolate->isolate->SetFatalErrorHandler(php_v8_fatal_error_handler);
248+
php_v8_isolate->isolate->SetOOMErrorHandler(php_v8_isolate_oom_error_callback);
245249
}
246250

247251
static PHP_METHOD(V8Isolate, SetTimeLimit) {

tests/V8Script_Run_out_of_memory.phpt

Lines changed: 55 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
V8\Script::Run() - out of memory example
33
--SKIPIF--
44
<?php if (!extension_loaded("v8")) print "skip"; ?>
5-
<?php if (!getenv("DEV_TESTS")) print "skip"; ?>
5+
<?php //if (!getenv("DEV_TESTS")) print "skip"; ?>
66
--FILE--
77
<?php
88
/** @var \Phpv8Testsuite $helper */
@@ -24,10 +24,10 @@ $context = new V8\Context($isolate, $extensions, $global_template);
2424
// This causes segfault
2525
$source = '
2626
x = \'x\';
27-
var multiply = 27;
27+
var multiply = 25;
2828
2929
while (multiply-- > 0){
30-
x = ""+x+x;
30+
x = x+x;
3131
print(x.length, "\n");
3232
}
3333
@@ -78,39 +78,55 @@ $helper->header('Object');
7878
$helper->value_matches_with_no_output($res, $obj);
7979

8080
?>
81-
--EXPECT--
82-
Checks on V8\StringValue:
83-
--------------------
84-
V8\StringValue::IsUndefined(): false
85-
V8\StringValue::IsNull(): false
86-
V8\StringValue::IsTrue(): false
87-
V8\StringValue::IsFalse(): false
88-
V8\StringValue::IsString(): true
89-
V8\StringValue::IsFunction(): false
90-
V8\StringValue::IsArray(): false
91-
V8\StringValue::IsObject(): false
92-
V8\StringValue::IsBoolean(): false
93-
V8\StringValue::IsNumber(): false
94-
V8\StringValue::IsInt32(): false
95-
V8\StringValue::IsUint32(): false
96-
V8\StringValue::IsDate(): false
97-
V8\StringValue::IsArgumentsObject(): false
98-
V8\StringValue::IsBooleanObject(): false
99-
V8\StringValue::IsNumberObject(): false
100-
V8\StringValue::IsStringObject(): false
101-
V8\StringValue::IsNativeError(): false
102-
V8\StringValue::IsRegExp(): false
103-
104-
105-
string(25) "TEST VALUE 111, confirmed" refcount(1)
106-
107-
108-
Scalar:
109-
-------
110-
Expected 123 value is identical to actual value 123
111-
Expected value is not identical to actual value
112-
113-
114-
Object:
115-
-------
116-
Expected value is identical to actual value
81+
--EXPECTF--
82+
2
83+
4
84+
8
85+
16
86+
32
87+
64
88+
128
89+
256
90+
512
91+
1024
92+
2048
93+
4096
94+
8192
95+
16384
96+
32768
97+
65536
98+
131072
99+
262144
100+
524288
101+
1048576
102+
2097152
103+
4194304
104+
8388608
105+
16777216
106+
33554432
107+
108+
109+
110+
Fatal error: V8 OOM hit: location=invalid array length, is_heap_oom=yes
111+
in %s/V8Script_Run_out_of_memory.php on line 44
112+
113+
<--- Last few GCs --->
114+
115+
%s %d ms: Mark-sweep %f (%f) -> %f (%f) MB, %f / %f ms %s
116+
%s %d ms: Mark-sweep %f (%f) -> %f (%f) MB, %f / %f ms %s
117+
%s %d ms: Mark-sweep %f (%f) -> %f (%f) MB, %f / %f ms %s
118+
119+
120+
<--- JS stacktrace --->
121+
122+
==== JS stack trace =========================================
123+
124+
Security context: 0x%x <JS Object>
125+
2: /* anonymous */ [test.js:~1] [pc=0x%x](this=0x%x <JS Global Object>)
126+
127+
==== Details ================================================
128+
129+
[2]: /* anonymous */ [test.js:~1] [pc=0x%x](this=0x%x <JS Global Object>) {
130+
// optimized frame
131+
--------- s o u r c e c o d e ---------
132+
%s

0 commit comments

Comments
 (0)