Skip to content

Commit

Permalink
Re-enable stack depth checks under ASan (#161)
Browse files Browse the repository at this point in the history
The default 256 kb stack is too small to run some of the test262 tests
when ASAN is enabled.

Double it to 512 kb and ensure threads created by quickjs have big
enough stacks.
  • Loading branch information
bnoordhuis authored Nov 30, 2023
1 parent 0745c3a commit a5b9e54
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
3 changes: 3 additions & 0 deletions quickjs-libc.c
Original file line number Diff line number Diff line change
Expand Up @@ -3409,6 +3409,9 @@ static JSValue js_worker_ctor(JSContext *ctx, JSValueConst new_target,
pthread_attr_init(&attr);
/* no join at the end */
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
// musl libc gives threads 80 kb stacks, much smaller than
// JS_DEFAULT_STACK_SIZE (256 kb)
pthread_attr_setstacksize(&attr, 2 << 20); // 2 MB, glibc default
ret = pthread_create(&tid, &attr, worker_func, args);
pthread_attr_destroy(&attr);
if (ret != 0) {
Expand Down
5 changes: 4 additions & 1 deletion quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
#define CONFIG_PRINTF_RNDN
#endif

#if !defined(EMSCRIPTEN) && !defined(__ASAN__)
#if !defined(EMSCRIPTEN)
/* enable stack limitation */
#define CONFIG_STACK_CHECK
#endif
Expand Down Expand Up @@ -1635,6 +1635,9 @@ JSRuntime *JS_NewRuntime2(const JSMallocFunctions *mf, void *opaque)
rt->js_class_id_alloc = JS_CLASS_INIT_COUNT;

rt->stack_size = JS_DEFAULT_STACK_SIZE;
#ifdef __ASAN__
rt->stack_size *= 2; // stack frames are bigger under AddressSanitizer
#endif
JS_UpdateStackTop(rt);

rt->current_exception = JS_NULL;
Expand Down
8 changes: 7 additions & 1 deletion run-test262.c
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,7 @@ static JSValue js_agent_start(JSContext *ctx, JSValue this_val,
{
const char *script;
Test262Agent *agent;
pthread_attr_t attr;

if (JS_GetContextOpaque(ctx) != NULL)
return JS_ThrowTypeError(ctx, "cannot be called inside an agent");
Expand All @@ -545,7 +546,12 @@ static JSValue js_agent_start(JSContext *ctx, JSValue this_val,
agent->script = strdup(script);
JS_FreeCString(ctx, script);
list_add_tail(&agent->link, &agent_list);
pthread_create(&agent->tid, NULL, agent_start, agent);
pthread_attr_init(&attr);
// musl libc gives threads 80 kb stacks, much smaller than
// JS_DEFAULT_STACK_SIZE (256 kb)
pthread_attr_setstacksize(&attr, 2 << 20); // 2 MB, glibc default
pthread_create(&agent->tid, &attr, agent_start, agent);
pthread_attr_destroy(&attr);
return JS_UNDEFINED;
}

Expand Down

0 comments on commit a5b9e54

Please sign in to comment.