https://www.mediawiki.org/wiki/Special:Code/MediaWiki/115085

Revision: 115085
Author:   tstarling
Date:     2012-04-30 03:39:51 +0000 (Mon, 30 Apr 2012)
Log Message:
-----------
* Fixed ZTS mode
* Fixed valgrind warnings
* Moved typedefs to a separate file to allow circular references to be more 
easily dealt with. Fixes a compile error.

Modified Paths:
--------------
    trunk/php/luasandbox/data_conversion.c
    trunk/php/luasandbox/library.c
    trunk/php/luasandbox/luasandbox.c
    trunk/php/luasandbox/luasandbox_timer.h
    trunk/php/luasandbox/php_luasandbox.h
    trunk/php/luasandbox/timer.c

Added Paths:
-----------
    trunk/php/luasandbox/luasandbox_types.h

Modified: trunk/php/luasandbox/data_conversion.c
===================================================================
--- trunk/php/luasandbox/data_conversion.c      2012-04-29 23:15:36 UTC (rev 
115084)
+++ trunk/php/luasandbox/data_conversion.c      2012-04-30 03:39:51 UTC (rev 
115085)
@@ -73,6 +73,7 @@
                        }
                        break;
                case IS_OBJECT: {
+                       TSRMLS_FETCH();
                        zend_class_entry * objce;
                        
                        objce = Z_OBJCE_P(z);
@@ -159,6 +160,7 @@
                return 1;
        }
        if (ht->nApplyCount) {
+               TSRMLS_FETCH();
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "recursion 
detected");
                return 0;
        }
@@ -278,7 +280,7 @@
                        int func_index;
                        php_luasandboxfunction_obj * func_obj;
                        php_luasandbox_obj * sandbox = (php_luasandbox_obj*)
-                               zend_object_store_get_object(sandbox_zval);
+                               zend_object_store_get_object(sandbox_zval 
TSRMLS_CC);
 
                        // Normalise the input index so that we can push 
without invalidating it.
                        if (index < 0) {
@@ -302,7 +304,7 @@
 
                        // Create a LuaSandboxFunction object to hold a 
reference to the function
                        object_init_ex(z, luasandboxfunction_ce);
-                       func_obj = 
(php_luasandboxfunction_obj*)zend_object_store_get_object(z);
+                       func_obj = 
(php_luasandboxfunction_obj*)zend_object_store_get_object(z TSRMLS_CC);
                        func_obj->index = func_index;
                        func_obj->sandbox = sandbox_zval;
                        Z_ADDREF_P(sandbox_zval);

Modified: trunk/php/luasandbox/library.c
===================================================================
--- trunk/php/luasandbox/library.c      2012-04-29 23:15:36 UTC (rev 115084)
+++ trunk/php/luasandbox/library.c      2012-04-30 03:39:51 UTC (rev 115085)
@@ -98,7 +98,7 @@
                        continue;
                }
                key = lua_tolstring(L, -1, &key_len);
-               if (zend_hash_find(luasandbox_lib_get_allowed_globals(), 
+               if 
(zend_hash_find(luasandbox_lib_get_allowed_globals(TSRMLS_C), 
                        (char*)key, key_len + 1, &data) == FAILURE) 
                {
                        // Not allowed, delete it
@@ -146,12 +146,12 @@
 }
 /* }}} */
 
-/** {{{ luasandbox_lib_shutdown */
-void luasandbox_lib_shutdown(TSRMLS_D)
+/** {{{ luasandbox_lib_destroy_globals */
+void luasandbox_lib_destroy_globals(zend_luasandbox_globals * g TSRMLS_DC)
 {
-       if (LUASANDBOX_G(allowed_globals)) {
-               zend_hash_destroy(LUASANDBOX_G(allowed_globals));
-               pefree(LUASANDBOX_G(allowed_globals), 1);
+       if (g->allowed_globals) {
+               zend_hash_destroy(g->allowed_globals);
+               pefree(g->allowed_globals, 1);
        }
 }
 /* }}} */

Modified: trunk/php/luasandbox/luasandbox.c
===================================================================
--- trunk/php/luasandbox/luasandbox.c   2012-04-29 23:15:36 UTC (rev 115084)
+++ trunk/php/luasandbox/luasandbox.c   2012-04-30 03:39:51 UTC (rev 115085)
@@ -22,10 +22,12 @@
                RETURN_FALSE; \
        }
 
+static PHP_GINIT_FUNCTION(luasandbox);
+static PHP_GSHUTDOWN_FUNCTION(luasandbox);
 static zend_object_value luasandbox_new(zend_class_entry *ce TSRMLS_DC);
-static lua_State * luasandbox_newstate(php_luasandbox_obj * intern);
+static lua_State * luasandbox_newstate(php_luasandbox_obj * intern TSRMLS_DC);
 static void luasandbox_free_storage(void *object TSRMLS_DC);
-static zend_object_value luasandboxfunction_new(zend_class_entry *ce 
TSRMLS_CC);
+static zend_object_value luasandboxfunction_new(zend_class_entry *ce 
TSRMLS_DC);
 static void luasandboxfunction_free_storage(void *object TSRMLS_DC);
 static void luasandboxfunction_destroy(void *object, zend_object_handle handle 
TSRMLS_DC);
 static int luasandbox_panic(lua_State * L);
@@ -39,8 +41,8 @@
 static void luasandbox_call_helper(lua_State * L, zval * sandbox_zval, 
        php_luasandbox_obj * sandbox,
        zval *** args, zend_uint numArgs, zval * return_value TSRMLS_DC);
-static void luasandbox_handle_error(php_luasandbox_obj * sandbox, int status);
-static void luasandbox_handle_emergency_timeout(php_luasandbox_obj * sandbox);
+static void luasandbox_handle_error(php_luasandbox_obj * sandbox, int status 
TSRMLS_DC);
+static void luasandbox_handle_emergency_timeout(php_luasandbox_obj * sandbox 
TSRMLS_DC);
 static int luasandbox_call_php(lua_State * L);
 static int luasandbox_dump_writer(lua_State * L, const void * p, size_t sz, 
void * ud);
 static zend_bool luasandbox_instanceof(
@@ -154,10 +156,12 @@
        NULL, /* RINIT */
        PHP_RSHUTDOWN(luasandbox), /* RSHUTDOWN */
        PHP_MINFO(luasandbox),
-#if ZEND_MODULE_API_NO >= 20010901
        "0.1",
-#endif
-       STANDARD_MODULE_PROPERTIES
+       PHP_MODULE_GLOBALS(luasandbox),
+       PHP_GINIT(luasandbox),
+       PHP_GSHUTDOWN(luasandbox),
+       NULL, /* post deactivate */
+       STANDARD_MODULE_PROPERTIES_EX
 };
 /* }}} */
 
@@ -211,13 +215,13 @@
                &ce, luasandboxfatalerror_ce, NULL TSRMLS_CC);
 
        zend_declare_class_constant_long(luasandboxerror_ce, 
-               "RUN", sizeof("RUN"), LUA_ERRRUN);
+               "RUN", sizeof("RUN"), LUA_ERRRUN TSRMLS_CC);
        zend_declare_class_constant_long(luasandboxerror_ce,
-               "SYNTAX", sizeof("SYNTAX"), LUA_ERRSYNTAX);
+               "SYNTAX", sizeof("SYNTAX"), LUA_ERRSYNTAX TSRMLS_CC);
        zend_declare_class_constant_long(luasandboxerror_ce,
-               "MEM", sizeof("MEM"), LUA_ERRMEM);
+               "MEM", sizeof("MEM"), LUA_ERRMEM TSRMLS_CC);
        zend_declare_class_constant_long(luasandboxerror_ce,
-               "ERR", sizeof("ERR"), LUA_ERRERR);
+               "ERR", sizeof("ERR"), LUA_ERRERR TSRMLS_CC);
 
        INIT_CLASS_ENTRY(ce, "LuaSandboxPlaceholder", luasandbox_empty_methods);
        luasandboxplaceholder_ce = zend_register_internal_class(&ce TSRMLS_CC);
@@ -225,17 +229,32 @@
        INIT_CLASS_ENTRY(ce, "LuaSandboxFunction", luasandboxfunction_methods);
        luasandboxfunction_ce = zend_register_internal_class(&ce TSRMLS_CC);
        luasandboxfunction_ce->create_object = luasandboxfunction_new;
-       
-       LUASANDBOX_G(allowed_globals) = NULL;
+
        return SUCCESS;
 }
 /* }}} */
 
+/** {{{ luasandbox_init_globals */
+static PHP_GINIT_FUNCTION(luasandbox)
+{
+       memset(luasandbox_globals, 0, sizeof(*luasandbox_globals));
+}
+/* }}} */
+
+/** {{{ luasandbox_destroy_globals */
+static PHP_GSHUTDOWN_FUNCTION(luasandbox)
+{
+       luasandbox_lib_destroy_globals(luasandbox_globals TSRMLS_CC);
+}
+/* }}} */
+
 /* {{{ PHP_MSHUTDOWN_FUNCTION
  */
 PHP_MSHUTDOWN_FUNCTION(luasandbox)
 {
-       luasandbox_lib_shutdown(TSRMLS_C);
+#ifndef ZTS
+       luasandbox_lib_destroy_globals(&luasandbox_globals TSRMLS_CC);
+#endif
        return SUCCESS;
 }
 /* }}} */
@@ -245,6 +264,7 @@
 {
        if (LUASANDBOX_G(signal_handler_installed)) {
                luasandbox_timer_remove_handler(&LUASANDBOX_G(old_handler));
+               LUASANDBOX_G(signal_handler_installed) = 0;
        }
        return SUCCESS;
 }
@@ -276,7 +296,7 @@
        sandbox->alloc.memory_limit = (size_t)-1;
 
        // Initialise the Lua state
-       sandbox->state = luasandbox_newstate(sandbox);
+       sandbox->state = luasandbox_newstate(sandbox TSRMLS_CC);
 
        // Initialise the timer
        luasandbox_timer_create(&sandbox->timer, sandbox);
@@ -297,7 +317,7 @@
  * Create a new lua_State which is suitable for running sandboxed scripts in.
  * Initialise libraries and any necessary registry entries.
  */
-static lua_State * luasandbox_newstate(php_luasandbox_obj * intern) 
+static lua_State * luasandbox_newstate(php_luasandbox_obj * intern TSRMLS_DC) 
 {
        lua_State * L = luasandbox_alloc_new_state(&intern->alloc, intern);
 
@@ -339,7 +359,7 @@
                luasandbox_alloc_delete_state(&sandbox->alloc, sandbox->state);
                sandbox->state = NULL;
        }
-       zend_object_std_dtor(&sandbox->std);
+       zend_object_std_dtor(&sandbox->std TSRMLS_CC);
        efree(object);
 }
 /* }}} */
@@ -348,7 +368,7 @@
  *
  * "new" handler for the LuaSandboxFunction class.
  */
-static zend_object_value luasandboxfunction_new(zend_class_entry *ce TSRMLS_CC)
+static zend_object_value luasandboxfunction_new(zend_class_entry *ce TSRMLS_DC)
 {
        php_luasandboxfunction_obj * intern;
        zend_object_value retval;
@@ -406,7 +426,7 @@
 static void luasandboxfunction_free_storage(void *object TSRMLS_DC)
 {
        php_luasandboxfunction_obj * func = (php_luasandboxfunction_obj*)object;
-       zend_object_std_dtor(&func->std);
+       zend_object_std_dtor(&func->std TSRMLS_CC);
        efree(object);
 }
 /* }}} */
@@ -429,6 +449,7 @@
  */
 static int luasandbox_panic(lua_State * L)
 {
+       TSRMLS_FETCH();
        php_error_docref(NULL TSRMLS_CC, E_ERROR,
                "PANIC: unprotected error in call to Lua API (%s)",
                luasandbox_error_to_string(L, -1));
@@ -504,7 +525,7 @@
        // Parse the string into a function on the stack
        status = luaL_loadbuffer(L, code, codeLength, chunkName);
        if (status != 0) {
-               luasandbox_handle_error(sandbox, status);
+               luasandbox_handle_error(sandbox, status TSRMLS_CC);
                return;
        }
 
@@ -553,7 +574,7 @@
  * Handle the situation where the emergency_timeout flag is set. Throws an 
  * appropriate exception and destroys the state.
  */
-static void luasandbox_handle_emergency_timeout(php_luasandbox_obj * sandbox)
+static void luasandbox_handle_emergency_timeout(php_luasandbox_obj * sandbox 
TSRMLS_DC)
 {
        lua_close(sandbox->state);
        sandbox->state = NULL;
@@ -561,7 +582,7 @@
        zend_throw_exception(luasandboxemergencytimeouterror_ce, 
                "The maximum execution time was exceeded "
                "and the current Lua statement failed to return, leading to "
-               "destruction of the Lua state", LUA_ERRRUN);
+               "destruction of the Lua state", LUA_ERRRUN TSRMLS_CC);
 }
 /* }}} */
 
@@ -571,7 +592,7 @@
  * status is returned and an error message pushed to the stack. Throws a 
suitable
  * exception.
  */
-static void luasandbox_handle_error(php_luasandbox_obj * sandbox, int status)
+static void luasandbox_handle_error(php_luasandbox_obj * sandbox, int status 
TSRMLS_DC)
 {
        lua_State * L = sandbox->state;
        const char * errorMsg = luasandbox_error_to_string(L, -1);
@@ -617,7 +638,7 @@
                lua_rawgeti(L, -1, 3);
                // Convert it to a zval
                MAKE_STD_ZVAL(ztrace);
-               luasandbox_lua_to_zval(ztrace, L, -1, sandbox->current_zval, 
NULL);
+               luasandbox_lua_to_zval(ztrace, L, -1, sandbox->current_zval, 
NULL TSRMLS_CC);
                // Put it in the exception object
                zend_update_property(ce, zex, "luaTrace", sizeof("luaTrace")-1, 
ztrace TSRMLS_CC);
                zval_ptr_dtor(&ztrace);
@@ -976,13 +997,15 @@
        // Initialise the CPU limit timer
        if (!sandbox->in_lua) {
                if (luasandbox_timer_is_expired(&sandbox->timer)) {
-                       zend_throw_exception(luasandboxtimeouterror_ce, 
luasandbox_timeout_message, LUA_ERRRUN);
+                       zend_throw_exception(luasandboxtimeouterror_ce, 
luasandbox_timeout_message, 
+                               LUA_ERRRUN TSRMLS_CC);
                        lua_settop(L, origTop - 1);
                        RETURN_FALSE;
                }
                timer_started = 1;
                if (!LUASANDBOX_G(signal_handler_installed)) {
                        
luasandbox_timer_install_handler(&LUASANDBOX_G(old_handler));
+                       LUASANDBOX_G(signal_handler_installed) = 1;
                }
                luasandbox_timer_start(&sandbox->timer);
        }
@@ -1003,13 +1026,13 @@
                luasandbox_timer_stop(&sandbox->timer);
        }
        if (sandbox->emergency_timed_out) {
-               luasandbox_handle_emergency_timeout(sandbox);
+               luasandbox_handle_emergency_timeout(sandbox TSRMLS_CC);
                return;
        }
 
        // Handle normal errors
        if (status) {
-               luasandbox_handle_error(sandbox, status);
+               luasandbox_handle_error(sandbox, status TSRMLS_CC);
                lua_settop(L, origTop - 1);
                RETURN_FALSE;
        }

Modified: trunk/php/luasandbox/luasandbox_timer.h
===================================================================
--- trunk/php/luasandbox/luasandbox_timer.h     2012-04-29 23:15:36 UTC (rev 
115084)
+++ trunk/php/luasandbox/luasandbox_timer.h     2012-04-30 03:39:51 UTC (rev 
115085)
@@ -1,6 +1,8 @@
 #ifndef LUASANDBOX_TIMER_H
 #define LUASANDBOX_TIMER_H
 
+#include "luasandbox_types.h"
+
 #ifdef CLOCK_REALTIME
 
 #ifdef CLOCK_PROCESS_CPUTIME_ID
@@ -10,43 +12,16 @@
 #endif
 
 #ifdef SIGRTMIN
-#define LUASANDBOX_SIGNAL (SIGRTMIN+1)
+#define LUASANDBOX_SIGNAL (SIGRTMIN+5)
 #else
 #define LUASANDBOX_SIGNAL SIGUSR2
 #endif
 
-struct _php_luasandbox_obj;
 
-typedef struct {
-       int emergency;
-       struct _php_luasandbox_obj * sandbox;
-} luasandbox_timer_callback_data;
-
-typedef struct {
-       timer_t timer;
-       luasandbox_timer_callback_data cbdata;
-} luasandbox_timer;
-
-typedef struct {
-       luasandbox_timer normal_timer;
-       luasandbox_timer emergency_timer;
-       struct timespec normal_limit, normal_remaining;
-       struct timespec emergency_limit, emergency_remaining;
-       struct timespec usage_start, usage;
-       struct _php_luasandbox_obj * sandbox;
-       int is_running;
-       int normal_running;
-       int emergency_running;
-} luasandbox_timer_set;
-
-
 #else /*CLOCK_REALTIME*/
 
 #define LUASANDBOX_NO_CLOCK
 
-typedef struct {} luasandbox_timer;
-typedef struct {} luasandbox_timer_set;
-
 #endif /*CLOCK_REALTIME*/
 
 void luasandbox_timer_install_handler(struct sigaction * oldact);

Added: trunk/php/luasandbox/luasandbox_types.h
===================================================================
--- trunk/php/luasandbox/luasandbox_types.h                             (rev 0)
+++ trunk/php/luasandbox/luasandbox_types.h     2012-04-30 03:39:51 UTC (rev 
115085)
@@ -0,0 +1,76 @@
+#ifndef LUASANDBOX_TYPES_H
+#define LUASANDBOX_TYPES_H
+
+#include "php.h"
+
+#ifdef CLOCK_REALTIME
+
+struct _php_luasandbox_obj;
+
+typedef struct {
+       int emergency;
+       struct _php_luasandbox_obj * sandbox;
+} luasandbox_timer_callback_data;
+
+typedef struct {
+       timer_t timer;
+       luasandbox_timer_callback_data cbdata;
+} luasandbox_timer;
+
+typedef struct {
+       luasandbox_timer normal_timer;
+       luasandbox_timer emergency_timer;
+       struct timespec normal_limit, normal_remaining;
+       struct timespec emergency_limit, emergency_remaining;
+       struct timespec usage_start, usage;
+       struct _php_luasandbox_obj * sandbox;
+       int is_running;
+       int normal_running;
+       int emergency_running;
+} luasandbox_timer_set;
+
+#else /*CLOCK_REALTIME*/
+
+typedef struct {} luasandbox_timer;
+typedef struct {} luasandbox_timer_set;
+
+#endif /*CLOCK_REALTIME*/
+
+ZEND_BEGIN_MODULE_GLOBALS(luasandbox)
+       int signal_handler_installed;
+       struct sigaction old_handler;
+       HashTable * allowed_globals;
+ZEND_END_MODULE_GLOBALS(luasandbox)
+
+typedef struct {
+       lua_Alloc old_alloc;
+       void * old_alloc_ud;
+       size_t memory_limit;
+       size_t memory_usage;
+} php_luasandbox_alloc;
+
+struct _php_luasandbox_obj {
+       zend_object std;
+       lua_State * state;
+       php_luasandbox_alloc alloc;
+       int in_php;
+       int in_lua;
+       zval * current_zval; /* The zval for the LuaSandbox which is currently 
executing Lua code */
+       volatile int timed_out;
+       volatile int emergency_timed_out;
+       int is_cpu_limited;
+       luasandbox_timer_set timer;
+       int function_index;
+       unsigned int random_seed;
+};
+typedef struct _php_luasandbox_obj php_luasandbox_obj;
+
+struct _php_luasandboxfunction_obj {
+       zend_object std;
+       zval * sandbox;
+       int index;
+};
+typedef struct _php_luasandboxfunction_obj php_luasandboxfunction_obj;
+
+#endif /*LUASANDBOX_TYPES_H*/
+


Property changes on: trunk/php/luasandbox/luasandbox_types.h
___________________________________________________________________
Added: svn:eol-style
   + native

Modified: trunk/php/luasandbox/php_luasandbox.h
===================================================================
--- trunk/php/luasandbox/php_luasandbox.h       2012-04-29 23:15:36 UTC (rev 
115084)
+++ trunk/php/luasandbox/php_luasandbox.h       2012-04-30 03:39:51 UTC (rev 
115085)
@@ -4,20 +4,14 @@
 
 #include <lua.h>
 #include <signal.h>
+
+#include "luasandbox_types.h"
 #include "luasandbox_timer.h"
 
 /* alloc.c */
 
-typedef struct {
-       lua_Alloc old_alloc;
-       void * old_alloc_ud;
-       size_t memory_limit;
-       size_t memory_usage;
-} php_luasandbox_alloc;
 
-struct _php_luasandbox_obj;
-
-lua_State * luasandbox_alloc_new_state(php_luasandbox_alloc * alloc, struct 
_php_luasandbox_obj * sandbox);
+lua_State * luasandbox_alloc_new_state(php_luasandbox_alloc * alloc, 
php_luasandbox_obj * sandbox);
 void luasandbox_alloc_delete_state(php_luasandbox_alloc * alloc, lua_State * 
L);
 
 /* luasandbox.c */
@@ -56,42 +50,13 @@
 PHP_METHOD(LuaSandboxFunction, call);
 PHP_METHOD(LuaSandboxFunction, dump);
 
-ZEND_BEGIN_MODULE_GLOBALS(luasandbox)
-       int signal_handler_installed;
-       struct sigaction old_handler;
-       HashTable * allowed_globals;
-ZEND_END_MODULE_GLOBALS(luasandbox)
-
 #ifdef ZTS
 #define LUASANDBOX_G(v) TSRMG(luasandbox_globals_id, zend_luasandbox_globals 
*, v)
 #else
 #define LUASANDBOX_G(v) (luasandbox_globals.v)
 #endif
 
-struct _php_luasandbox_obj {
-       zend_object std;
-       lua_State * state;
-       php_luasandbox_alloc alloc;
-       int in_php;
-       int in_lua;
-       zval * current_zval; /* The zval for the LuaSandbox which is currently 
executing Lua code */
-       volatile int timed_out;
-       volatile int emergency_timed_out;
-       int is_cpu_limited;
-       luasandbox_timer_set timer;
-       int function_index;
-       unsigned int random_seed;
-};
-typedef struct _php_luasandbox_obj php_luasandbox_obj;
 
-struct _php_luasandboxfunction_obj {
-       zend_object std;
-       zval * sandbox;
-       int index;
-};
-typedef struct _php_luasandboxfunction_obj php_luasandboxfunction_obj;
-
-
 php_luasandbox_obj * luasandbox_get_php_obj(lua_State * L);
 
 /** {{{ luasandbox_enter_php
@@ -128,7 +93,7 @@
 /* library.c */
 
 void luasandbox_lib_register(lua_State * L TSRMLS_DC);
-void luasandbox_lib_shutdown(TSRMLS_D);
+void luasandbox_lib_destroy_globals(zend_luasandbox_globals * g TSRMLS_DC);
 
 /* data_conversion.c */
 

Modified: trunk/php/luasandbox/timer.c
===================================================================
--- trunk/php/luasandbox/timer.c        2012-04-29 23:15:36 UTC (rev 115084)
+++ trunk/php/luasandbox/timer.c        2012-04-30 03:39:51 UTC (rev 115085)
@@ -206,6 +206,9 @@
 {
        struct sigevent ev;
 
+       // Make valgrind happy
+       memset(&ev, 0, sizeof(ev));
+
        lt->cbdata.emergency = emergency;
        lt->cbdata.sandbox = sandbox;
        


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to