Live data from Hacker News

Ported my C game to WASM, here's every bug that I hit

ernesernesto.github.io

41–50 of 116 posts

Re: Ported my C game to WASM, here's every bug that I hit

#41
post #4

The memory64 proposal was merged into upstream last year, any reason to opt into 32 bit despite that?

You don't need 4GB and it wastes memory to make pointers twice as big? Even Linux supports running 64-bit code in a 32-bit address space ("x32 ABI") for this reason.

> Even Linux supports running 64-bit code in a 32-bit address space ("x32 ABI") for this reason.

I don't think that ever had much, if any, adoption and it looks like it will be removed in the next few releases.

Re: Ported my C game to WASM, here's every bug that I hit

#42

Why is a relatively new technology like WASM being limited to 32-bit pointers? Why repeat the same mistake again? > Web is 32-bit. Your 64-bit structs will break. This was the root cause of most of my bugs. WASM is 32-bit address space, pointers are 4 bytes not 8.

Because a web page shouldn’t use 4 GB of ram, and the win is that each pointer can be half the size (better for memory and cache).

The real mistake is requiring pointer to be 64 bit when most programs don’t use it.

Re: Ported my C game to WASM, here's every bug that I hit

#43

Earlier quoted context omitted.

1: Letting your code break on pointer size changes is a quite bad sign imho (it's a sign that many other things are probably done with aliasing,etc and has a high risk of breaking due to undefined behaviour once gcc/clang gets around to utilizing it for an optimization). 2: iirc WASM was initially designed to be shimmable via Asm.JS to force laggards(Apple, Google) to implement it, Asm.JS in turn relied on specific r…

Thanks! 1: True, although it also limits the addressable memory and the typical 4GB limit seems less these days. I’m thinking of large apps like Figma running in the browser. 2: Will existing 32-bit WASM binaries break on WASM64 engines or does the binary have a flag for compatibility?

1: Something like Figma could probably offload some of the memory pressure to GPU textures. (But they'd probably run into safety browser limits before that).

2: Most runtimes are 64bit already, A runtime detecting a wasm32 binary will just continue to generate code with the current JIT compiler whilst WASM64 will require another JIT (and perhaps memory system since WASM32 runtimes are often based on "hacks" where 4gb of address space is reserved but not given real memory so that the JIT compiler gets an easier job without security implications).

Re: Ported my C game to WASM, here's every bug that I hit

#45
post #8
post #4

The memory64 proposal was merged into upstream last year, any reason to opt into 32 bit despite that?

It's slower. Wasm32 can just reserve 8 GiB (32-bit pointer + 32-bit offset) of the virtual address space from the OS for each memory, so checking for out-of-bounds memory accesses imposes no performance penalty. Wasm64 can't do that, so each memory access is a bit slower.

Sometimes I wonder whether it's possible to run the wasm code in a separate sandboxed process to eliminate a lot of checks. I mean optionally, because normally JS calls wasm code synchronously in the same address space. The bridge will add more latency when there is a transition between JS and wasm. It's obviously complicated because some data structures can also be shared, such as SharedArrayBuffer.

Re: Ported my C game to WASM, here's every bug that I hit

#46

Why is a relatively new technology like WASM being limited to 32-bit pointers? Why repeat the same mistake again? > Web is 32-bit. Your 64-bit structs will break. This was the root cause of most of my bugs. WASM is 32-bit address space, pointers are 4 bytes not 8.

64 bit was added in WebAssembly 2.0 (finished in 2022 according to Wikipedia). I know what doesn't answer any it wasn't there in the first place.

Re: Ported my C game to WASM, here's every bug that I hit

#47
post #29

With regards to 1), do not write/read structs directly to/from files. Instead write a proper serializer/deserializer. Without it, you may encounter another breakage soon when a different compiler/compiler options insert different struct padding bytes, which will then once again make your data non-portable, and a maliciously crafted save file with no length/size field validation on the deserializer level can lead to a…

struct layout is well specified, it should be possible to avoid any padding issues by just aligning and by padding (with dummy members) correctly. The problem in practice is mostly integer representation (big-endian vs little-endian).

If you modify or even just move fields around the struct that also changes the way they are serialized...

You really need a serializer for this sort of thing because it can also include forwards compatibility of your data structures.

Re: Ported my C game to WASM, here's every bug that I hit

#48

Earlier quoted context omitted.

struct layout is well specified, it should be possible to avoid any padding issues by just aligning and by padding (with dummy members) correctly. The problem in practice is mostly integer representation (big-endian vs little-endian).

If you modify or even just move fields around the struct that also changes the way they are serialized... You really need a serializer for this sort of thing because it can also include forwards compatibility of your data structures.

sure, if you change the struct, it will now be different.

Re: Ported my C game to WASM, here's every bug that I hit

#49
post #38

Earlier quoted context omitted.

struct layout is well specified, it should be possible to avoid any padding issues by just aligning and by padding (with dummy members) correctly. The problem in practice is mostly integer representation (big-endian vs little-endian).

Specified by whom? Not the C standard for sure. It is indeed soecified by individual ABIs, and ABIs don't tend to do anything too weird, but that's another question.

looks like I was wrong, but here is the de-facto standard I was relying on over the years ;-). Not that I've memcpied many structs to file directly btw. http://www.catb.org/esr/structure-packing/

Re: Ported my C game to WASM, here's every bug that I hit

#50

Why is a relatively new technology like WASM being limited to 32-bit pointers? Why repeat the same mistake again? > Web is 32-bit. Your 64-bit structs will break. This was the root cause of most of my bugs. WASM is 32-bit address space, pointers are 4 bytes not 8.

Because a web page shouldn’t use 4 GB of ram, and the win is that each pointer can be half the size (better for memory and cache). The real mistake is requiring pointer to be 64 bit when most programs don’t use it.

You sounds like the misattributed Bill Gates of 2026.
Post reply on HN