add stack size limit

This commit is contained in:
Rokas Puzonas 2026-10-04 13:34:35 +03:00
parent 576df80400
commit dbae325cdb
3 changed files with 36 additions and 4 deletions

View File

@ -128,7 +128,11 @@ fn getPackageSuffix(target: Build.ResolvedTarget) []const u8 {
}; };
} }
const bytes_per_kib = 1024;
const bytes_per_mib = bytes_per_kib * 1024;
const ProjectOptions = struct { const ProjectOptions = struct {
stack_size: usize = 4 * bytes_per_mib,
imgui: ?bool = null, imgui: ?bool = null,
assets_from_filesystem: ?bool = null, assets_from_filesystem: ?bool = null,
tracy: ?bool = null, tracy: ?bool = null,
@ -188,6 +192,9 @@ fn buildProject(ctx: Context, target: Build.ResolvedTarget, optimize: std.builti
if (assets_from_filesystem) { if (assets_from_filesystem) {
build_options.addOptionPathDirectory("assets_dir", assets_dir); build_options.addOptionPathDirectory("assets_dir", assets_dir);
} }
if (is_debug and !is_wasm) {
build_options.addOption(usize, "stack_size_limit", opts.stack_size);
}
platform_mod.addOptions("build_options", build_options); platform_mod.addOptions("build_options", build_options);
platform_mod.addImport("embedded_assets", try createAssetsModule(b, assets_dir, &asset_files)); platform_mod.addImport("embedded_assets", try createAssetsModule(b, assets_dir, &asset_files));
@ -275,6 +282,18 @@ fn buildProject(ctx: Context, target: Build.ResolvedTarget, optimize: std.builti
&(dep_sokol.artifact("sokol_clib").step) &(dep_sokol.artifact("sokol_clib").step)
); );
var extra_args: std.ArrayList([]const u8) = .empty;
try extra_args.append(b.allocator, "-sALLOW_MEMORY_GROWTH");
try extra_args.append(b.allocator, "-sENVIRONMENT=web");
try extra_args.append(b.allocator, b.fmt("-sSTACK_SIZE={}", .{opts.stack_size}));
if (is_debug) {
try extra_args.append(b.allocator, "-sASSERTIONS=2");
try extra_args.append(b.allocator, "-sSTACK_OVERFLOW_CHECK=2");
try extra_args.append(b.allocator, "-gsource-map");
try extra_args.append(b.allocator, "-g2");
}
const link_step = try sokol.emLinkStep(b, .{ const link_step = try sokol.emLinkStep(b, .{
.lib_main = main_lib, .lib_main = main_lib,
.target = target, .target = target,
@ -284,7 +303,7 @@ fn buildProject(ctx: Context, target: Build.ResolvedTarget, optimize: std.builti
.use_emmalloc = true, .use_emmalloc = true,
.use_filesystem = true, .use_filesystem = true,
.shell_file_path = b.path("src/platform/shell.html"), .shell_file_path = b.path("src/platform/shell.html"),
.extra_args = &.{ "-sALLOW_MEMORY_GROWTH", "-sENVIRONMENT=web" } .extra_args = extra_args.items
}); });
return ProjectResult{ return ProjectResult{

View File

@ -356,7 +356,7 @@ footstep_sound: Audio.SoundId = .nil,
sfx_bus: Audio.BusId, sfx_bus: Audio.BusId,
game_scene: GameScene, game_scene: GameScene,
in_main_menu: bool = false, in_main_menu: bool = true,
main_menu_state: UI.State = .empty, main_menu_state: UI.State = .empty,
pub fn init(self: *App, plt: Platform.Init) !?u8 { pub fn init(self: *App, plt: Platform.Init) !?u8 {
@ -523,7 +523,7 @@ fn loadLevels(self: *App, gpa: Allocator, assets: *Platform.Assets) !std.ArrayLi
} }
}; };
std.mem.sort(World, levels.items, Context{}, Context.lessThan); std.sort.insertion(World, levels.items, Context{}, Context.lessThan);
return levels; return levels;
} }

View File

@ -623,7 +623,7 @@ pub const Frame = struct {
} }
}; };
pub fn run(App: type, opts: RunOptions) void { fn runThread(App: type, opts: RunOptions) void {
const gpa = opts.init.gpa; const gpa = opts.init.gpa;
const io = opts.init.io; const io = opts.init.io;
@ -706,6 +706,19 @@ pub fn run(App: type, opts: RunOptions) void {
}); });
} }
pub fn run(App: type, opts: RunOptions) void {
if (@hasDecl(build_options, "stack_size_limit")) {
var t = std.Thread.spawn(
.{ .stack_size = build_options.stack_size_limit },
runThread,
.{App, opts}
) catch @panic("Failed to launch thread for limiting stack size");
t.join();
} else {
runThread(App, opts);
}
}
fn posixSignalHandler(sig: std.posix.SIG) callconv(.c) void { fn posixSignalHandler(sig: std.posix.SIG) callconv(.c) void {
_ = sig; _ = sig;
sapp.requestQuit(); sapp.requestQuit();