File: luasandbox_types.h

package info (click to toggle)
php-luasandbox 4.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 620 kB
  • sloc: ansic: 3,700; xml: 2,152; php: 64; makefile: 13; sh: 3
file content (120 lines) | stat: -rw-r--r-- 3,243 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#ifndef LUASANDBOX_TYPES_H
#define LUASANDBOX_TYPES_H

#include "php.h"

#ifndef LUASANDBOX_NO_CLOCK
#include <semaphore.h>
#endif

#ifdef LUASANDBOX_NO_CLOCK

typedef struct {
	// structs must have at least one member
	int unused;
} luasandbox_timer;

typedef struct {
	struct timespec profiler_period;
	HashTable * function_counts;
	long total_count;
	int is_paused;
} luasandbox_timer_set;

#else /*LUASANDBOX_NO_CLOCK*/

struct _php_luasandbox_obj;

typedef struct _luasandbox_timer {
	struct _php_luasandbox_obj * sandbox;
	timer_t timer;
	clockid_t clock_id;
	int type;
	sem_t semaphore;
	int id;
} luasandbox_timer;

typedef struct {
	luasandbox_timer *limiter_timer;
	luasandbox_timer *profiler_timer;
	struct timespec limiter_limit, limiter_remaining;
	struct timespec usage_start, usage;
	struct timespec pause_start, pause_delta;
	struct timespec limiter_expired_at;
	struct timespec profiler_period;
	struct _php_luasandbox_obj * sandbox;
	int is_running;
	int limiter_running;
	int profiler_running;

	// A HashTable storing the number of times each function was hit by the
	// profiler. The data is a size_t because that hits a special case in
	// zend_hash which avoids the need to allocate separate space for the data
	// on the heap.
	HashTable * function_counts;

	// The total number of samples recorded in function_counts
	long total_count;

	// The number of timer expirations that have occurred since the profiler hook
	// was last run
	volatile long profiler_signal_count;

	volatile long overrun_count;
} luasandbox_timer_set;

#endif /*LUASANDBOX_NO_CLOCK*/

ZEND_BEGIN_MODULE_GLOBALS(luasandbox)
	HashTable * allowed_globals;
	long active_count;
ZEND_END_MODULE_GLOBALS(luasandbox)

typedef struct {
	lua_Alloc old_alloc;
	void * old_alloc_ud;
	size_t memory_limit;
	size_t memory_usage;
	size_t peak_memory_usage;
} php_luasandbox_alloc;

struct _php_luasandbox_obj {
	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;
	int is_cpu_limited;
	luasandbox_timer_set timer;
	int function_index;
	unsigned int random_seed;
	int allow_pause;
	zend_object std;
};
typedef struct _php_luasandbox_obj php_luasandbox_obj;

struct _php_luasandboxfunction_obj {
	zval sandbox;
	int index;
	zend_object std;
};
typedef struct _php_luasandboxfunction_obj php_luasandboxfunction_obj;

// Accessor macros
static inline php_luasandbox_obj *php_luasandbox_fetch_object(zend_object *obj) {
	return (php_luasandbox_obj *)((char*)(obj) - XtOffsetOf(php_luasandbox_obj, std));
}

static inline php_luasandboxfunction_obj *php_luasandboxfunction_fetch_object(zend_object *obj) {
	return (php_luasandboxfunction_obj *)((char*)(obj) - XtOffsetOf(php_luasandboxfunction_obj, std));
}

#define GET_LUASANDBOX_OBJ(z) php_luasandbox_fetch_object(Z_OBJ_P(z))
#define GET_LUASANDBOXFUNCTION_OBJ(z) php_luasandboxfunction_fetch_object(Z_OBJ_P(z))
#define LUASANDBOXFUNCTION_SANDBOX_IS_OK(pfunc) !Z_ISUNDEF((pfunc)->sandbox)
#define LUASANDBOXFUNCTION_GET_SANDBOX_ZVALPTR(pfunc) &((pfunc)->sandbox)
#define LUASANDBOX_GET_CURRENT_ZVAL_PTR(psandbox) &((psandbox)->current_zval)

#endif /*LUASANDBOX_TYPES_H*/