Browse Source

Ajduk exec timeout example, ajdukd target

pull/113/head
Sami Vaarala 10 years ago
parent
commit
87fdd163b2
  1. 14
      Makefile
  2. 28
      examples/cmdline/duk_cmdline.c
  3. 43
      examples/cmdline/duk_cmdline_ajduk.c

14
Makefile

@ -863,7 +863,8 @@ CCOPTS_AJDUK += '-DDUK_OPT_EXTSTR_INTERN_CHECK(ud,ptr,len)=ajsheap_extstr_check_
CCOPTS_AJDUK += '-DDUK_OPT_EXTSTR_FREE(ud,ptr)=ajsheap_extstr_free_2((ptr))'
#CCOPTS_AJDUK += '-DDUK_OPT_EXTSTR_INTERN_CHECK(ud,ptr,len)=ajsheap_extstr_check_3((ptr),(len))'
#CCOPTS_AJDUK += '-DDUK_OPT_EXTSTR_FREE(ud,ptr)=ajsheap_extstr_free_3((ptr))'
CCOPTS_AJDUK += '-DDUK_OPT_DECLARE=extern uint8_t *ajsheap_ram; extern duk_uint16_t ajsheap_enc16(void *ud, void *p); extern void *ajsheap_dec16(void *ud, duk_uint16_t x); extern const void *ajsheap_extstr_check_1(const void *ptr, duk_size_t len); extern const void *ajsheap_extstr_check_2(const void *ptr, duk_size_t len); extern const void *ajsheap_extstr_check_3(const void *ptr, duk_size_t len); extern void ajsheap_extstr_free_1(const void *ptr); extern void ajsheap_extstr_free_2(const void *ptr); extern void ajsheap_extstr_free_3(const void *ptr);'
CCOPTS_AJDUK += '-DDUK_OPT_EXEC_TIMEOUT_CHECK(udata)=ajsheap_exec_timeout_check(udata)'
CCOPTS_AJDUK += '-DDUK_OPT_DECLARE=extern uint8_t *ajsheap_ram; extern duk_uint16_t ajsheap_enc16(void *ud, void *p); extern void *ajsheap_dec16(void *ud, duk_uint16_t x); extern const void *ajsheap_extstr_check_1(const void *ptr, duk_size_t len); extern const void *ajsheap_extstr_check_2(const void *ptr, duk_size_t len); extern const void *ajsheap_extstr_check_3(const void *ptr, duk_size_t len); extern void ajsheap_extstr_free_1(const void *ptr); extern void ajsheap_extstr_free_2(const void *ptr); extern void ajsheap_extstr_free_3(const void *ptr); extern duk_bool_t ajsheap_exec_timeout_check(void *udata);'
#CCOPTS_AJDUK += -DDUK_OPT_DEBUG -DDUK_OPT_DPRINT
#CCOPTS_AJDUK += -DDUK_OPT_DEBUG -DDUK_OPT_DPRINT -DDUK_OPT_DDPRINT -DDUK_OPT_DDDPRINT
@ -881,6 +882,17 @@ ajduk: alljoyn-js ajtcl dist
-lm -lpthread
@echo "*** SUCCESS:"
@ls -l ajduk
ajdukd: alljoyn-js ajtcl dist
$(CC) -o $@ \
-Ialljoyn-js/ -Iajtcl/inc/ -Iajtcl/target/linux/ \
$(CCOPTS_DEBUG) \
$(CCOPTS_AJDUK) \
$(DUKTAPE_SOURCES) $(DUKTAPE_CMDLINE_SOURCES) \
dist/examples/cmdline/duk_cmdline_ajduk.c \
alljoyn-js/ajs_heap.c ajtcl/src/aj_debug.c ajtcl/target/linux/aj_target_util.c \
-lm -lpthread
@echo "*** SUCCESS:"
@ls -l ajdukd
.PHONY: gccpredefs
gccpredefs:

28
examples/cmdline/duk_cmdline.c

@ -48,6 +48,8 @@
void ajsheap_init(void);
void ajsheap_dump(void);
void ajsheap_register(duk_context *ctx);
void ajsheap_start_exec_timeout(void);
void ajsheap_clear_exec_timeout(void);
void *AJS_Alloc(void *udata, duk_size_t size);
void *AJS_Realloc(void *udata, void *ptr, duk_size_t size);
void AJS_Free(void *udata, void *ptr);
@ -153,9 +155,17 @@ static int wrapped_compile_execute(duk_context *ctx) {
/* [ ... src_data src_len function ] */
#if defined(DUK_CMDLINE_AJSHEAP)
ajsheap_start_exec_timeout();
#endif
duk_push_global_object(ctx); /* 'this' binding */
duk_call_method(ctx, 0);
#if defined(DUK_CMDLINE_AJSHEAP)
ajsheap_clear_exec_timeout();
#endif
if (interactive_mode) {
/*
* In interactive mode, write to stdout so output won't
@ -219,6 +229,10 @@ static int handle_fh(duk_context *ctx, FILE *f, const char *filename) {
rc = duk_safe_call(ctx, wrapped_compile_execute, 3 /*nargs*/, 1 /*nret*/);
#if defined(DUK_CMDLINE_AJSHEAP)
ajsheap_clear_exec_timeout();
#endif
free(buf);
buf = NULL;
@ -274,6 +288,11 @@ static int handle_eval(duk_context *ctx, const char *code) {
interactive_mode = 0; /* global */
rc = duk_safe_call(ctx, wrapped_compile_execute, 3 /*nargs*/, 1 /*nret*/);
#if defined(DUK_CMDLINE_AJSHEAP)
ajsheap_clear_exec_timeout();
#endif
if (rc != DUK_EXEC_SUCCESS) {
print_pop_error(ctx, stderr);
} else {
@ -333,6 +352,11 @@ static int handle_interactive(duk_context *ctx) {
interactive_mode = 1; /* global */
rc = duk_safe_call(ctx, wrapped_compile_execute, 3 /*nargs*/, 1 /*nret*/);
#if defined(DUK_CMDLINE_AJSHEAP)
ajsheap_clear_exec_timeout();
#endif
if (rc != DUK_EXEC_SUCCESS) {
/* in interactive mode, write to stdout */
print_pop_error(ctx, stdout);
@ -391,6 +415,10 @@ static int handle_interactive(duk_context *ctx) {
rc = duk_safe_call(ctx, wrapped_compile_execute, 3 /*nargs*/, 1 /*nret*/);
#if defined(DUK_CMDLINE_AJSHEAP)
ajsheap_clear_exec_timeout();
#endif
if (buffer) {
free(buffer);
buffer = NULL;

43
examples/cmdline/duk_cmdline_ajduk.c

@ -7,6 +7,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "ajs.h"
#include "ajs_heap.h"
@ -68,7 +69,7 @@ static const AJS_HeapConfig ajsheap_config[] = {
{ 256, 16, AJS_POOL_BORROW, 0 },
{ 512, 16, AJS_POOL_BORROW, 0 },
{ 1024, 6, AJS_POOL_BORROW, 0 },
{ 1352, 1, AJS_POOL_BORROW, 0 }, /* duk_heap, with heap ptr compression */
{ 1360, 1, AJS_POOL_BORROW, 0 }, /* duk_heap, with heap ptr compression */
{ 2048, 5, AJS_POOL_BORROW, 0 },
{ 4096, 3, 0, 0 },
{ 8192, 3, 0, 0 },
@ -770,6 +771,46 @@ void ajsheap_extstr_free_3(const void *ptr) {
free((void *) ptr);
}
/*
* Execution timeout example
*/
#define AJSHEAP_EXEC_TIMEOUT 5 /* seconds */
static time_t curr_pcall_start = 0;
static long exec_timeout_check_counter = 0;
void ajsheap_start_exec_timeout(void) {
curr_pcall_start = time(NULL);
}
void ajsheap_clear_exec_timeout(void) {
curr_pcall_start = 0;
}
duk_bool_t ajsheap_exec_timeout_check(void *udata) {
time_t now = time(NULL);
time_t diff = now - curr_pcall_start;
(void) udata; /* not needed */
exec_timeout_check_counter++;
#if 0
printf("exec timeout check %ld: now=%ld, start=%ld, diff=%ld\n",
(long) exec_timeout_check_counter, (long) now, (long) curr_pcall_start, (long) diff);
fflush(stdout);
#endif
if (curr_pcall_start == 0) {
/* protected call not yet running */
return 0;
}
if (diff > AJSHEAP_EXEC_TIMEOUT) {
return 1;
}
return 0;
}
#else /* DUK_CMDLINE_AJSHEAP */
int ajs_dummy = 0; /* to avoid empty source file */

Loading…
Cancel
Save