summaryrefslogtreecommitdiff
path: root/coroutine/asyncify
diff options
context:
space:
mode:
authornagachika <nagachika@ruby-lang.org>2024-11-30 14:25:56 +0900
committernagachika <nagachika@ruby-lang.org>2024-11-30 14:25:56 +0900
commit1fc0895971812d5287b23d8cbb00529e425041c0 (patch)
tree07e4f9a6b29d749a5c9eb50acb9d6486f639e89d /coroutine/asyncify
parentd3098d2068770f8bdeca8fda06736cbebb8d3a5b (diff)
merge revision(s) 0d4de0f4b1b9ac90be437bf1bac6851dd1d96fd0: [Backport #20898]
wasm: align fiber stack pointer to 16 bytes In WebAssembly C ABI, the linear stack pointer must be always aligned to 16 bytes like other archs. The misaligned stack pointer causes some weird memory corruption since compiler assumes the aligned stack pointer.
Diffstat (limited to 'coroutine/asyncify')
-rw-r--r--coroutine/asyncify/Context.h8
1 files changed, 6 insertions, 2 deletions
diff --git a/coroutine/asyncify/Context.h b/coroutine/asyncify/Context.h
index 7dba829a1d..71791a4004 100644
--- a/coroutine/asyncify/Context.h
+++ b/coroutine/asyncify/Context.h
@@ -13,6 +13,7 @@
#include <stddef.h>
#include <stdio.h>
+#include <stdint.h>
#include "wasm/asyncify.h"
#include "wasm/machine.h"
#include "wasm/fiber.h"
@@ -47,10 +48,13 @@ static inline void coroutine_initialize_main(struct coroutine_context * context)
static inline void coroutine_initialize(struct coroutine_context *context, coroutine_start start, void *stack, size_t size)
{
- if (ASYNCIFY_CORO_DEBUG) fprintf(stderr, "[%s] entry (context = %p, stack = %p ... %p)\n", __func__, context, stack, (char *)stack + size);
+ // Linear stack pointer must be always aligned down to 16 bytes.
+ // https://github.com/WebAssembly/tool-conventions/blob/c74267a5897c1bdc9aa60adeaf41816387d3cd12/BasicCABI.md#the-linear-stack
+ uintptr_t sp = ((uintptr_t)stack + size) & ~0xF;
+ if (ASYNCIFY_CORO_DEBUG) fprintf(stderr, "[%s] entry (context = %p, stack = %p ... %p)\n", __func__, context, stack, (char *)sp);
rb_wasm_init_context(&context->fc, coroutine_trampoline, start, context);
// record the initial stack pointer position to restore it after resumption
- context->current_sp = (char *)stack + size;
+ context->current_sp = (char *)sp;
context->stack_base = stack;
context->size = size;
}