From 378383ea3bf04ab6e083c597298db63ff665837e Mon Sep 17 00:00:00 2001 From: Daniel Silverstone Date: Mon, 6 May 2019 15:09:39 +0100 Subject: [PATCH] Support context dump safely in duktape Signed-off-by: Daniel Silverstone --- content/handlers/javascript/duktape/dukky.c | 23 +++++++++++++++++++-- content/handlers/javascript/duktape/dukky.h | 3 +++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/content/handlers/javascript/duktape/dukky.c b/content/handlers/javascript/duktape/dukky.c index e9e579d4d..c3b956902 100644 --- a/content/handlers/javascript/duktape/dukky.c +++ b/content/handlers/javascript/duktape/dukky.c @@ -669,6 +669,8 @@ jsobject *js_newcompartment(jscontext *ctx, void *win_priv, void *doc_priv) duk_pop(CTX); /* ... */ + dukky_log_stack_frame(CTX, "New compartment created"); + return (jsobject *)ctx; } @@ -698,8 +700,8 @@ static void dukky_dump_error(duk_context *ctx) { /* stack is ..., errobj */ duk_idx_t stacktop = duk_get_top(ctx); - if (!duk_is_error(ctx, stacktop - 1)) { - NSLOG(dukky, INFO, "Uncaught non-Error derived error in JS: %s", duk_safe_to_string(ctx, stacktop - 1)); + if (!duk_is_error(ctx, -1)) { + NSLOG(dukky, INFO, "Uncaught non-Error derived error in JS: %s", duk_safe_to_string(ctx, -1)); } else { #define GETTER(what) \ if (duk_has_prop_string(ctx, stacktop - 1, what)) { \ @@ -761,6 +763,23 @@ void dukky_push_generics(duk_context *ctx, const char *generic) /* ..., generic */ } +static duk_int_t dukky_push_context_dump(duk_context *ctx, void *udata) +{ + duk_push_context_dump(ctx); + return 1; +} + +void dukky_log_stack_frame(duk_context *ctx, const char * reason) +{ + if (duk_safe_call(ctx, dukky_push_context_dump, NULL, 0, 1) != 0) { + duk_pop(ctx); + duk_push_string(ctx, "[???]"); + } + NSLOG(dukky, DEEPDEBUG, "%s, stack is: %s", reason, duk_safe_to_string(ctx, -1)); + duk_pop(ctx); +} + + /* exported interface documented in js.h */ bool js_exec(jscontext *ctx, const uint8_t *txt, size_t txtlen, const char *name) diff --git a/content/handlers/javascript/duktape/dukky.h b/content/handlers/javascript/duktape/dukky.h index 93d416983..5a67951dc 100644 --- a/content/handlers/javascript/duktape/dukky.h +++ b/content/handlers/javascript/duktape/dukky.h @@ -54,4 +54,7 @@ duk_int_t dukky_pcall(duk_context *ctx, duk_size_t argc, bool reset_timeout); /* Push a generics function onto the stack */ void dukky_push_generics(duk_context *ctx, const char *generic); +/* Log the current stack frame if possible */ +void dukky_log_stack_frame(duk_context *ctx, const char * reason); + #endif