2 ms·
For for curiosity, how does C handles this? Is that at the OS level? Because if it is, you can't really compare WASM to C, since WASM is not designed to be ran
by jokoon 4y ago
For for curiosity, how does C handles this? Is that at the OS level?
Because if it is, you can't really compare WASM to C, since WASM is not designed to be ran by an OS, so there are other protections.
WASM comes at some cost I guess, and I'm not sure if it makes it so much more difficult to debug.
- mananaysiempre 4y agoIn a typical protected memory situation, yes, this is done by the OS setting up a faulting mapping of OS-dependent size at low addresses, though the C runtime startup code could do it if the OS did not. (Not sure how Win9x handled it, by the way—wasn’t it supposed to map the DOS VM in the first 1M?..) In bare metal environments, there is usually something at address zero—if you’re lucky, it’s ROM, and you get a bus fault if you try to write there; if you’re not (as on the 8086), it is part of an architecturally significant part of RAM such as the interrupt vector table, and spectacular amounts of chaos can ensue if you touch it at the wrong time. Technically, while the C standard does require that void *p = 0; makes p be a special pointer value equal to any other one obtained that way and unequal to any normal pointer value, it does not require the memory backing the variable p to actually store all zero bits. Neither does it require that p be equal to q or r in int n = 0; void *q = (void *)n; void *r; memset(r, 0, sizeof r); nor, of course, that actually dereferencing p cause any particular behaviour like a crash. (POSIX may be stricter, I’m not sure.) Therefore the designers of the WASM C ABI could have made (void *)0 an all-ones pointer instead, for example, or better yet that minus some headroom, and guarantee a crash on dereference that way. Of course, this would be a DeathStation-level perversity, and I doubt Clang would be capable of dealing with such a platform out of the box. ... Now that I’m thinking about it, a more realistic solution would be to have (void *)n be stored as n but actually refer to WASM memory address n - 64K or something like that during codegen. Perfectly ordinary platform as far as C is concerned, possibly a bit of binary bloat(?). Don’t know why they didn’t do it that way.