ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
34.5k stars 2.52k forks source link

emscripten: OutOfMemory bug when using default page_allocator #19072

Open silbinarywolf opened 8 months ago

silbinarywolf commented 8 months ago

Zig Version

0.12.0-dev.3043+00ff123b1

Steps to Reproduce and Observed Behavior

  1. Pull down this repository: https://github.com/silbinarywolf/sdl-zig-demo-emscripten
  2. Comment out this workaround here: https://github.com/silbinarywolf/sdl-zig-demo-emscripten/blob/main/src/main.zig#L13-L17
  3. Run it with zig build run -Doptimize=Debug -Dtarget=wasm32-emscripten
  4. You should get a crash like this: image

The OutOfMemory error occurs here: https://github.com/silbinarywolf/sdl-zig-demo-emscripten/blob/14623becdcda293db03127ed92aa2acf345e608e/src/main.zig#L80

I know this because I've tested by changing that line to this:

const text_file_contents = asset_file.readToEndAlloc(gp.allocator(), @intCast(stat.size)) catch |err| {
  std.debug.print("failed to read file contents: {}", .{err});
  return;
};

When debugging in Chrome, I then get this: image

Expected Behavior

We should not get an OutOfMemory error when using the default page_allocator with Emscripten.

ssteinbach commented 1 month ago

We ran into a similar issue in zig 0.13.0 with an emscripten build crashing whenever our arena (backed by the std.heap.page_allocator) attempted to allocate.

As suggested in this pr: https://github.com/ziglang/zig/pull/19057, We switched the backing allocator to std.heap.c_allocator and our allocations are working.