Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wasm2c: Implement PT_GNU_STACK-based stack size expansion #22237

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -805,19 +805,24 @@ if(MSVC)
set(ZIG_WASM2C_COMPILE_FLAGS "")
set(ZIG1_COMPILE_FLAGS "/Os")
set(ZIG2_COMPILE_FLAGS "/Od")
set(ZIG1_LINK_FLAGS "/STACK:16777216")
set(ZIG2_LINK_FLAGS "/STACK:16777216 /FORCE:MULTIPLE")
else()
set(ZIG_WASM2C_COMPILE_FLAGS "-std=c99 -O2")
set(ZIG1_COMPILE_FLAGS "-std=c99 -Os")
set(ZIG2_COMPILE_FLAGS "-std=c99 -O0 -fno-stack-protector")
if(APPLE)
set(ZIG1_LINK_FLAGS "-Wl,-stack_size,0x1000000")
set(ZIG2_LINK_FLAGS "-Wl,-stack_size,0x10000000")
elseif(MINGW)
set(ZIG1_LINK_FLAGS "-Wl,--stack,0x1000000")
set(ZIG2_LINK_FLAGS "-Wl,--stack,0x10000000")
# Solaris/illumos ld(1) does not provide a --stack-size option.
elseif(CMAKE_HOST_SOLARIS)
unset(ZIG1_LINK_FLAGS)
unset(ZIG2_LINK_FLAGS)
else()
set(ZIG1_LINK_FLAGS "-Wl,-z,stack-size=0x1000000")
set(ZIG2_LINK_FLAGS "-Wl,-z,stack-size=0x10000000")
endif()
endif()
Expand All @@ -839,7 +844,10 @@ add_custom_command(
)

add_executable(zig1 ${ZIG1_C_SOURCE} stage1/wasi.c)
set_target_properties(zig1 PROPERTIES COMPILE_FLAGS ${ZIG1_COMPILE_FLAGS})
set_target_properties(zig1 PROPERTIES
COMPILE_FLAGS ${ZIG1_COMPILE_FLAGS}
LINK_FLAGS "${ZIG1_LINK_FLAGS}"
)

if(MSVC)
target_link_options(zig1 PRIVATE /STACK:0x10000000)
Expand Down
9 changes: 8 additions & 1 deletion bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,14 @@ int main(int argc, char **argv) {
}
{
const char *child_argv[] = {
cc, "-o", "zig1", "zig1.c", "stage1/wasi.c", "-std=c99", "-Os", "-lm", NULL,
cc, "-o", "zig1", "zig1.c", "stage1/wasi.c",
"-std=c99", "-Os", "-lm",
#if defined(__APPLE__)
"-Wl,-stack_size,0x1000000",
#else
"-Wl,-z,stack-size=0x1000000",
#endif
NULL,
};
print_and_run(child_argv);
}
Expand Down
1 change: 1 addition & 0 deletions lib/std/start.zig
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ fn posixCallMainAndExit(argc_argv_ptr: [*]usize) callconv(.c) noreturn {
std.posix.exit(callMainWithArgs(argc, argv, envp));
}

// This code should be kept in sync with stage1/wasi.c.
fn expandStackSize(phdrs: []elf.Phdr) void {
for (phdrs) |*phdr| {
switch (phdr.p_type) {
Expand Down
34 changes: 34 additions & 0 deletions stage1/wasi.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
#include <string.h>
#include <time.h>

#if defined(__linux__)
#include <elf.h>
#include <sys/auxv.h>
#include <sys/resource.h>

#if ULONG_MAX == 0xffffffff
typedef Elf32_Phdr Elf_Phdr;
#else
typedef Elf64_Phdr Elf_Phdr;
#endif
#endif

Comment on lines +8 to +19
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code was intentionally written to not depend on posix or os-specific things. This patch breaks that. Please stick to only the C language and standard libc in wasm2c.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I noted on #22111 (comment) and on Zulip, the kernel's indifference to PT_GNU_STACK leaves us with not a lot of options.

If you'd prefer, we could setrlimit(RLIMIT_STACK, ...) just before fork()ing in bootstrap.c, which already has -- and will grow more -- platform-specific code. We already hardcode the stack size values in that file anyway, so perhaps that's more fitting.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's the right way to go 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although one downside of that approach is that it won't help if you're using CMake.

Do we care? Are we getting rid of CMake once we get rid of the LLVM dependency?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes and it will be a glorious day

#include "panic.h"

#define LOG_TRACE 0
Expand Down Expand Up @@ -236,6 +248,28 @@ static void *dupe(const void *data, size_t len) {
}

int main(int argc, char **argv) {
// This stack size code should be kept in sync with `expandStackSize` in lib/std/start.zig.
#if defined(__linux__)
Elf_Phdr *at_phdr = (Elf_Phdr *)getauxval(AT_PHDR);
unsigned long at_phnum = getauxval(AT_PHNUM);

for (Elf_Phdr *phdr = at_phdr; phdr < at_phdr + at_phnum; phdr++) {
if (phdr->p_type != PT_GNU_STACK) continue;
if (phdr->p_memsz == 0) break;

struct rlimit limit;
if (getrlimit(RLIMIT_STACK, &limit) != 0) break;

rlim_t stack_size = phdr->p_memsz < limit.rlim_max ? phdr->p_memsz : limit.rlim_max;
if (stack_size > limit.rlim_cur) {
limit.rlim_cur = stack_size;
setrlimit(RLIMIT_STACK, &limit);
}

break;
}
#endif

if (argc < 2) {
fprintf(stderr, "usage: %s <zig-lib-path> <args...>\n", argv[0]);
return 1;
Expand Down