From 1386c96d267d0658d765e83a12b5e04496d8c248 Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Thu, 11 Dec 2014 19:30:06 +0100 Subject: [PATCH] Catch serialization of V8Js object, closes #119 --- tests/serialize_basic.phpt | 35 +++++++++++++++++++++++++++++++++++ v8js.cc | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 tests/serialize_basic.phpt diff --git a/tests/serialize_basic.phpt b/tests/serialize_basic.phpt new file mode 100644 index 0000000..bdd2520 --- /dev/null +++ b/tests/serialize_basic.phpt @@ -0,0 +1,35 @@ +--TEST-- +Test serialize(V8Js) : __sleep and __wakeup throw +--SKIPIF-- + +--FILE-- +getMessage()); +} + +$stored = 'O:4:"V8Js":0:{}'; + +try { + $b = unserialize($stored); +} +catch(\V8JsException $e) { + var_dump(get_class($e)); + var_dump($e->getMessage()); +} + +?> +===EOF=== +--EXPECT-- +string(13) "V8JsException" +string(50) "You cannot serialize or unserialize V8Js instances" +string(13) "V8JsException" +string(50) "You cannot serialize or unserialize V8Js instances" +===EOF=== diff --git a/v8js.cc b/v8js.cc index 3caa8f0..ab77766 100644 --- a/v8js.cc +++ b/v8js.cc @@ -730,7 +730,11 @@ static void php_v8js_free_storage(void *object TSRMLS_DC) /* {{{ */ } c->modules_loaded.~map(); - c->isolate->Dispose(); + if(c->isolate) { + /* c->isolate is initialized by V8Js::__construct, but __wakeup calls + * are not fully constructed and hence this would cause a NPE. */ + c->isolate->Dispose(); + } if(c->tz != NULL) { free(c->tz); @@ -1048,6 +1052,26 @@ static PHP_METHOD(V8Js, __construct) } /* }}} */ +/* {{{ proto V8JS::__sleep() + */ +PHP_METHOD(V8Js, __sleep) +{ + zend_throw_exception(php_ce_v8js_exception, + "You cannot serialize or unserialize V8Js instances", 0 TSRMLS_CC); + RETURN_FALSE; +} +/* }}} */ + +/* {{{ proto V8JS::__wakeup() + */ +PHP_METHOD(V8Js, __wakeup) +{ + zend_throw_exception(php_ce_v8js_exception, + "You cannot serialize or unserialize V8Js instances", 0 TSRMLS_CC); + RETURN_FALSE; +} +/* }}} */ + #define V8JS_CTX_PROLOGUE(ctx) \ if (!V8JSG(v8_initialized)) { \ zend_error(E_ERROR, "V8 not initialized"); \ @@ -1816,6 +1840,12 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_construct, 0, 0, 0) ZEND_ARG_INFO(0, report_uncaught_exceptions) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_INFO(arginfo_v8js_sleep, 0) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO(arginfo_v8js_wakeup, 0) +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_executestring, 0, 0, 1) ZEND_ARG_INFO(0, script) ZEND_ARG_INFO(0, identifier) @@ -1897,6 +1927,8 @@ static const zend_function_entry v8_function_methods[] = { /* {{{ */ static const zend_function_entry v8js_methods[] = { /* {{{ */ PHP_ME(V8Js, __construct, arginfo_v8js_construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR) + PHP_ME(V8Js, __sleep, arginfo_v8js_sleep, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) + PHP_ME(V8Js, __wakeup, arginfo_v8js_sleep, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) PHP_ME(V8Js, executeString, arginfo_v8js_executestring, ZEND_ACC_PUBLIC) PHP_ME(V8Js, compileString, arginfo_v8js_compilestring, ZEND_ACC_PUBLIC) PHP_ME(V8Js, executeScript, arginfo_v8js_executescript, ZEND_ACC_PUBLIC)