]>
git.proxmox.com Git - ceph.git/blob - ceph/src/civetweb/src/third_party/duktape-1.8.0/examples/sandbox/sandbox.c
4 * Uses custom memory allocation functions which keep track of total amount
5 * of memory allocated, imposing a maximum total allocation size.
13 * Memory allocator which backs to standard library memory functions but
14 * keeps a small header to track current allocation size.
16 * Many other sandbox allocation models are useful, e.g. preallocated pools.
20 /* The double value in the union is there to ensure alignment is
21 * good for IEEE doubles too. In many 32-bit environments 4 bytes
22 * would be sufficiently aligned and the double value is unnecessary.
30 static size_t total_allocated
= 0;
31 static size_t max_allocated
= 256 * 1024; /* 256kB sandbox */
33 static void sandbox_dump_memstate(void) {
35 fprintf(stderr
, "Total allocated: %ld\n", (long) total_allocated
);
40 static void *sandbox_alloc(void *udata
, duk_size_t size
) {
43 (void) udata
; /* Suppress warning. */
49 if (total_allocated
+ size
> max_allocated
) {
50 fprintf(stderr
, "Sandbox maximum allocation size reached, %ld requested in sandbox_alloc\n",
56 hdr
= (alloc_hdr
*) malloc(size
+ sizeof(alloc_hdr
));
61 total_allocated
+= size
;
62 sandbox_dump_memstate();
63 return (void *) (hdr
+ 1);
66 static void *sandbox_realloc(void *udata
, void *ptr
, duk_size_t size
) {
71 (void) udata
; /* Suppress warning. */
73 /* Handle the ptr-NULL vs. size-zero cases explicitly to minimize
74 * platform assumptions. You can get away with much less in specific
75 * well-behaving environments.
79 hdr
= (alloc_hdr
*) (((char *) ptr
) - sizeof(alloc_hdr
));
83 total_allocated
-= old_size
;
85 sandbox_dump_memstate();
88 if (total_allocated
- old_size
+ size
> max_allocated
) {
89 fprintf(stderr
, "Sandbox maximum allocation size reached, %ld requested in sandbox_realloc\n",
95 t
= realloc((void *) hdr
, size
+ sizeof(alloc_hdr
));
99 hdr
= (alloc_hdr
*) t
;
100 total_allocated
-= old_size
;
101 total_allocated
+= size
;
103 sandbox_dump_memstate();
104 return (void *) (hdr
+ 1);
110 if (total_allocated
+ size
> max_allocated
) {
111 fprintf(stderr
, "Sandbox maximum allocation size reached, %ld requested in sandbox_realloc\n",
117 hdr
= (alloc_hdr
*) malloc(size
+ sizeof(alloc_hdr
));
122 total_allocated
+= size
;
123 sandbox_dump_memstate();
124 return (void *) (hdr
+ 1);
129 static void sandbox_free(void *udata
, void *ptr
) {
132 (void) udata
; /* Suppress warning. */
137 hdr
= (alloc_hdr
*) (((char *) ptr
) - sizeof(alloc_hdr
));
138 total_allocated
-= hdr
->u
.sz
;
140 sandbox_dump_memstate();
144 * Sandbox setup and test
147 static duk_ret_t
do_sandbox_test(duk_context
*ctx
) {
165 fprintf(stderr
, "Sandbox global object:\n----------------\n%s----------------\n", globobj
);
168 duk_eval_string(ctx
, globobj
);
169 duk_set_global_object(ctx
);
172 * Execute code from specified file
175 f
= fopen(duk_require_string(ctx
, -1), "rb");
177 duk_error(ctx
, DUK_ERR_ERROR
, "failed to open file");
183 duk_error(ctx
, DUK_ERR_ERROR
, "ferror when reading file");
189 ret
= fread(buf
, 1, sizeof(buf
), f
);
194 duk_push_lstring(ctx
, (const char *) buf
, ret
);
197 duk_concat(ctx
, duk_get_top(ctx
) - 1); /* -1 for filename */
199 /* -> [ ... filename source ] */
203 /* -> [ ... source filename ] */
205 duk_compile(ctx
, 0 /*flags*/); /* Compile as program */
206 duk_call(ctx
, 0 /*nargs*/);
215 static void sandbox_fatal(duk_context
*ctx
, duk_errcode_t code
, const char *msg
) {
216 (void) ctx
; /* Suppress warning. */
217 fprintf(stderr
, "FATAL %ld: %s\n", (long) code
, (msg
? msg
: "no message"));
219 exit(1); /* must not return */
222 int main(int argc
, char *argv
[]) {
227 fprintf(stderr
, "Usage: sandbox <test.js>\n");
232 ctx
= duk_create_heap(sandbox_alloc
,
238 duk_push_string(ctx
, argv
[1]);
239 rc
= duk_safe_call(ctx
, do_sandbox_test
, 1 /*nargs*/, 1 /*nrets*/);
241 fprintf(stderr
, "ERROR: %s\n", duk_safe_to_string(ctx
, -1));
245 duk_destroy_heap(ctx
);
247 /* Should be zero. */
248 fprintf(stderr
, "Final allocation: %ld\n", (long) total_allocated
);