瀏覽代碼

Catch serialization of V8Js object, closes #119

Stefan Siegl 10 年之前
父節點
當前提交
1386c96d26
共有 2 個文件被更改,包括 68 次插入1 次删除
  1. 35 0
      tests/serialize_basic.phpt
  2. 33 1
      v8js.cc

+ 35 - 0
tests/serialize_basic.phpt

@@ -0,0 +1,35 @@
+--TEST--
+Test serialize(V8Js) : __sleep and __wakeup throw
+--SKIPIF--
+<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
+--FILE--
+<?php
+
+$v8 = new V8Js();
+
+try {
+    $stored = serialize($v8);
+}
+catch(\V8JsException $e) {
+    var_dump(get_class($e));
+    var_dump($e->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===

+ 33 - 1
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)