Line data Source code
1 : #ifndef LUASANDBOX_TYPES_H
2 : #define LUASANDBOX_TYPES_H
3 :
4 : #include "php.h"
5 :
6 : #ifndef LUASANDBOX_NO_CLOCK
7 : #include <semaphore.h>
8 : #endif
9 :
10 : #ifdef LUASANDBOX_NO_CLOCK
11 :
12 : typedef struct {
13 : // structs must have at least one member
14 : int unused;
15 : } luasandbox_timer;
16 :
17 : typedef struct {
18 : struct timespec profiler_period;
19 : HashTable * function_counts;
20 : long total_count;
21 : int is_paused;
22 : } luasandbox_timer_set;
23 :
24 : #else /*LUASANDBOX_NO_CLOCK*/
25 :
26 : struct _php_luasandbox_obj;
27 :
28 : typedef struct _luasandbox_timer {
29 : struct _php_luasandbox_obj * sandbox;
30 : timer_t timer;
31 : clockid_t clock_id;
32 : int type;
33 : sem_t semaphore;
34 : int id;
35 : } luasandbox_timer;
36 :
37 : typedef struct {
38 : luasandbox_timer *limiter_timer;
39 : luasandbox_timer *profiler_timer;
40 : struct timespec limiter_limit, limiter_remaining;
41 : struct timespec usage_start, usage;
42 : struct timespec pause_start, pause_delta;
43 : struct timespec limiter_expired_at;
44 : struct timespec profiler_period;
45 : struct _php_luasandbox_obj * sandbox;
46 : int is_running;
47 : int limiter_running;
48 : int profiler_running;
49 :
50 : // A HashTable storing the number of times each function was hit by the
51 : // profiler. The data is a size_t because that hits a special case in
52 : // zend_hash which avoids the need to allocate separate space for the data
53 : // on the heap.
54 : HashTable * function_counts;
55 :
56 : // The total number of samples recorded in function_counts
57 : long total_count;
58 :
59 : // The number of timer expirations that have occurred since the profiler hook
60 : // was last run
61 : volatile long profiler_signal_count;
62 :
63 : volatile long overrun_count;
64 : } luasandbox_timer_set;
65 :
66 : #endif /*LUASANDBOX_NO_CLOCK*/
67 :
68 : ZEND_BEGIN_MODULE_GLOBALS(luasandbox)
69 : HashTable * allowed_globals;
70 : long active_count;
71 : ZEND_END_MODULE_GLOBALS(luasandbox)
72 :
73 : typedef struct {
74 : lua_Alloc old_alloc;
75 : void * old_alloc_ud;
76 : size_t memory_limit;
77 : size_t memory_usage;
78 : size_t peak_memory_usage;
79 : } php_luasandbox_alloc;
80 :
81 : struct _php_luasandbox_obj {
82 : lua_State * state;
83 : php_luasandbox_alloc alloc;
84 : int in_php;
85 : int in_lua;
86 : zval current_zval; /* The zval for the LuaSandbox which is currently executing Lua code */
87 : volatile int timed_out;
88 : int is_cpu_limited;
89 : luasandbox_timer_set timer;
90 : int function_index;
91 : unsigned int random_seed;
92 : int allow_pause;
93 : zend_object std;
94 : };
95 : typedef struct _php_luasandbox_obj php_luasandbox_obj;
96 :
97 : struct _php_luasandboxfunction_obj {
98 : zval sandbox;
99 : int index;
100 : zend_object std;
101 : };
102 : typedef struct _php_luasandboxfunction_obj php_luasandboxfunction_obj;
103 :
104 : // Accessor macros
105 1236 : static inline php_luasandbox_obj *php_luasandbox_fetch_object(zend_object *obj) {
106 1236 : return (php_luasandbox_obj *)((char*)(obj) - XtOffsetOf(php_luasandbox_obj, std));
107 : }
108 :
109 702 : static inline php_luasandboxfunction_obj *php_luasandboxfunction_fetch_object(zend_object *obj) {
110 702 : return (php_luasandboxfunction_obj *)((char*)(obj) - XtOffsetOf(php_luasandboxfunction_obj, std));
111 : }
112 :
113 : #define GET_LUASANDBOX_OBJ(z) php_luasandbox_fetch_object(Z_OBJ_P(z))
114 : #define GET_LUASANDBOXFUNCTION_OBJ(z) php_luasandboxfunction_fetch_object(Z_OBJ_P(z))
115 : #define LUASANDBOXFUNCTION_SANDBOX_IS_OK(pfunc) !Z_ISUNDEF((pfunc)->sandbox)
116 : #define LUASANDBOXFUNCTION_GET_SANDBOX_ZVALPTR(pfunc) &((pfunc)->sandbox)
117 : #define LUASANDBOX_GET_CURRENT_ZVAL_PTR(psandbox) &((psandbox)->current_zval)
118 :
119 : #endif /*LUASANDBOX_TYPES_H*/
120 :
|