Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
sandbox.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include "duktape.h"

Go to the source code of this file.

Data Structures

struct  alloc_hdr
 

Functions

static void sandbox_dump_memstate (void)
 
static void * sandbox_alloc (void *udata, duk_size_t size)
 
static void * sandbox_realloc (void *udata, void *ptr, duk_size_t size)
 
static void sandbox_free (void *udata, void *ptr)
 
static duk_ret_t do_sandbox_test (duk_context *ctx)
 
static void sandbox_fatal (duk_context *ctx, duk_errcode_t code, const char *msg)
 
int main (int argc, char *argv[])
 

Variables

static size_t total_allocated = 0
 
static size_t max_allocated = 256 * 1024
 

Function Documentation

◆ do_sandbox_test()

static duk_ret_t do_sandbox_test ( duk_context * ctx)
static

Definition at line 147 of file duktape-1.8.0/examples/sandbox/sandbox.c.

147 {
148 FILE *f;
149 char buf[4096];
150 size_t ret;
151 const char *globobj;
152
153 /*
154 * Setup sandbox
155 */
156
157 globobj =
158 "({\n"
159 " print: print,\n"
160 " Math: {\n"
161 " max: Math.max\n"
162 " }\n"
163 "})\n";
164#if 1
165 fprintf(stderr, "Sandbox global object:\n----------------\n%s----------------\n", globobj);
166 fflush(stderr);
167#endif
168 duk_eval_string(ctx, globobj);
170
171 /*
172 * Execute code from specified file
173 */
174
175 f = fopen(duk_require_string(ctx, -1), "rb");
176 if (!f) {
177 duk_error(ctx, DUK_ERR_ERROR, "failed to open file");
178 }
179
180 for (;;) {
181 if (ferror(f)) {
182 fclose(f);
183 duk_error(ctx, DUK_ERR_ERROR, "ferror when reading file");
184 }
185 if (feof(f)) {
186 break;
187 }
188
189 ret = fread(buf, 1, sizeof(buf), f);
190 if (ret == 0) {
191 break;
192 }
193
194 duk_push_lstring(ctx, (const char *) buf, ret);
195 }
196
197 duk_concat(ctx, duk_get_top(ctx) - 1); /* -1 for filename */
198
199 /* -> [ ... filename source ] */
200
201 duk_insert(ctx, -2);
202
203 /* -> [ ... source filename ] */
204
205 duk_compile(ctx, 0 /*flags*/); /* Compile as program */
206 duk_call(ctx, 0 /*nargs*/);
207
208 return 0;
209}
DUK_EXTERNAL void duk_concat(duk_context *ctx, duk_idx_t count)
DUK_EXTERNAL const char * duk_push_lstring(duk_context *ctx, const char *str, duk_size_t len)
DUK_EXTERNAL void duk_insert(duk_context *ctx, duk_idx_t to_index)
DUK_EXTERNAL duk_idx_t duk_get_top(duk_context *ctx)
DUK_EXTERNAL void duk_set_global_object(duk_context *ctx)
DUK_EXTERNAL void duk_call(duk_context *ctx, duk_idx_t nargs)
DUK_EXTERNAL const char * duk_require_string(duk_context *ctx, duk_idx_t index)
#define duk_eval_string(ctx, src)
#define duk_compile(ctx, flags)
size_t fread(void *, size_t, size_t, FILE *)

References duk_call(), duk_compile, duk_concat(), DUK_ERR_ERROR, duk_error, duk_eval_string, duk_get_top(), duk_insert(), duk_push_lstring(), duk_require_string(), duk_set_global_object(), and fread().

Referenced by main().

◆ main()

int main ( int argc,
char * argv[] )

Definition at line 222 of file duktape-1.8.0/examples/sandbox/sandbox.c.

222 {
223 duk_context *ctx;
224 duk_int_t rc;
225
226 if (argc < 2) {
227 fprintf(stderr, "Usage: sandbox <test.js>\n");
228 fflush(stderr);
229 exit(1);
230 }
231
235 NULL,
237
238 duk_push_string(ctx, argv[1]);
239 rc = duk_safe_call(ctx, do_sandbox_test, 1 /*nargs*/, 1 /*nrets*/);
240 if (rc) {
241 fprintf(stderr, "ERROR: %s\n", duk_safe_to_string(ctx, -1));
242 fflush(stderr);
243 }
244
245 duk_destroy_heap(ctx);
246
247 /* Should be zero. */
248 fprintf(stderr, "Final allocation: %ld\n", (long) total_allocated);
249 fflush(stderr);
250
251 return 1;
252}
duk_int_fast32_t duk_int_t
DUK_EXTERNAL const char * duk_push_string(duk_context *ctx, const char *str)
DUK_EXTERNAL duk_int_t duk_safe_call(duk_context *ctx, duk_safe_call_function func, duk_idx_t nargs, duk_idx_t nrets)
DUK_EXTERNAL void duk_destroy_heap(duk_context *ctx)
DUK_EXTERNAL duk_context * duk_create_heap(duk_alloc_function alloc_func, duk_realloc_function realloc_func, duk_free_function free_func, void *heap_udata, duk_fatal_function fatal_handler)
#define duk_safe_to_string(ctx, index)
static void * sandbox_alloc(void *udata, duk_size_t size)
static size_t total_allocated
static void * sandbox_realloc(void *udata, void *ptr, duk_size_t size)
static void sandbox_fatal(duk_context *ctx, duk_errcode_t code, const char *msg)
static void sandbox_free(void *udata, void *ptr)
static duk_ret_t do_sandbox_test(duk_context *ctx)
#define NULL
Definition gmacros.h:924

References do_sandbox_test(), duk_create_heap(), duk_destroy_heap(), duk_push_string(), duk_safe_call(), duk_safe_to_string, NULL, sandbox_alloc(), sandbox_fatal(), sandbox_free(), sandbox_realloc(), and total_allocated.

◆ sandbox_alloc()

static void * sandbox_alloc ( void * udata,
duk_size_t size )
static

Definition at line 40 of file duktape-1.8.0/examples/sandbox/sandbox.c.

40 {
41 alloc_hdr *hdr;
42
43 (void) udata; /* Suppress warning. */
44
45 if (size == 0) {
46 return NULL;
47 }
48
49 if (total_allocated + size > max_allocated) {
50 fprintf(stderr, "Sandbox maximum allocation size reached, %ld requested in sandbox_alloc\n",
51 (long) size);
52 fflush(stderr);
53 return NULL;
54 }
55
56 hdr = (alloc_hdr *) malloc(size + sizeof(alloc_hdr));
57 if (!hdr) {
58 return NULL;
59 }
60 hdr->u.sz = size;
61 total_allocated += size;
63 return (void *) (hdr + 1);
64}
#define malloc
Definition civetweb.c:1539
static size_t max_allocated
static void sandbox_dump_memstate(void)
union alloc_hdr::@15 u

References malloc, max_allocated, NULL, sandbox_dump_memstate(), alloc_hdr::sz, total_allocated, and alloc_hdr::u.

Referenced by main().

◆ sandbox_dump_memstate()

static void sandbox_dump_memstate ( void )
static

Definition at line 33 of file duktape-1.8.0/examples/sandbox/sandbox.c.

33 {
34#if 0
35 fprintf(stderr, "Total allocated: %ld\n", (long) total_allocated);
36 fflush(stderr);
37#endif
38}

References total_allocated.

Referenced by sandbox_alloc(), sandbox_free(), and sandbox_realloc().

◆ sandbox_fatal()

static void sandbox_fatal ( duk_context * ctx,
duk_errcode_t code,
const char * msg )
static

Definition at line 215 of file duktape-1.8.0/examples/sandbox/sandbox.c.

215 {
216 (void) ctx; /* Suppress warning. */
217 fprintf(stderr, "FATAL %ld: %s\n", (long) code, (msg ? msg : "no message"));
218 fflush(stderr);
219 exit(1); /* must not return */
220}

Referenced by main().

◆ sandbox_free()

static void sandbox_free ( void * udata,
void * ptr )
static

Definition at line 129 of file duktape-1.8.0/examples/sandbox/sandbox.c.

129 {
130 alloc_hdr *hdr;
131
132 (void) udata; /* Suppress warning. */
133
134 if (!ptr) {
135 return;
136 }
137 hdr = (alloc_hdr *) (((char *) ptr) - sizeof(alloc_hdr));
138 total_allocated -= hdr->u.sz;
139 free((void *) hdr);
141}
#define free
Definition civetweb.c:1542

References free, sandbox_dump_memstate(), alloc_hdr::sz, total_allocated, and alloc_hdr::u.

Referenced by main().

◆ sandbox_realloc()

static void * sandbox_realloc ( void * udata,
void * ptr,
duk_size_t size )
static

Definition at line 66 of file duktape-1.8.0/examples/sandbox/sandbox.c.

66 {
67 alloc_hdr *hdr;
68 size_t old_size;
69 void *t;
70
71 (void) udata; /* Suppress warning. */
72
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.
76 */
77
78 if (ptr) {
79 hdr = (alloc_hdr *) (((char *) ptr) - sizeof(alloc_hdr));
80 old_size = hdr->u.sz;
81
82 if (size == 0) {
83 total_allocated -= old_size;
84 free((void *) hdr);
86 return NULL;
87 } else {
88 if (total_allocated - old_size + size > max_allocated) {
89 fprintf(stderr, "Sandbox maximum allocation size reached, %ld requested in sandbox_realloc\n",
90 (long) size);
91 fflush(stderr);
92 return NULL;
93 }
94
95 t = realloc((void *) hdr, size + sizeof(alloc_hdr));
96 if (!t) {
97 return NULL;
98 }
99 hdr = (alloc_hdr *) t;
100 total_allocated -= old_size;
101 total_allocated += size;
102 hdr->u.sz = size;
104 return (void *) (hdr + 1);
105 }
106 } else {
107 if (size == 0) {
108 return NULL;
109 } else {
110 if (total_allocated + size > max_allocated) {
111 fprintf(stderr, "Sandbox maximum allocation size reached, %ld requested in sandbox_realloc\n",
112 (long) size);
113 fflush(stderr);
114 return NULL;
115 }
116
117 hdr = (alloc_hdr *) malloc(size + sizeof(alloc_hdr));
118 if (!hdr) {
119 return NULL;
120 }
121 hdr->u.sz = size;
122 total_allocated += size;
124 return (void *) (hdr + 1);
125 }
126 }
127}
#define realloc
Definition civetweb.c:1541

References free, malloc, max_allocated, NULL, realloc, sandbox_dump_memstate(), alloc_hdr::sz, total_allocated, and alloc_hdr::u.

Referenced by main().

Variable Documentation

◆ max_allocated

size_t max_allocated = 256 * 1024
static

Definition at line 31 of file duktape-1.8.0/examples/sandbox/sandbox.c.

Referenced by sandbox_alloc(), and sandbox_realloc().

◆ total_allocated

size_t total_allocated = 0
static