3 ms·
An example of a recent compile target that breaks on unaligned pointer accesses was asm.js. There, a 32-bit read turns into a read from a JavaScript Int32Array
by azakai 3y ago
An example of a recent compile target that breaks on unaligned pointer accesses was asm.js. There, a 32-bit read turns into a read from a JavaScript Int32Array like this:
HEAP32[ptr >> 2]
The k-th index in the array contains 4 bytes of data, so the pointer to an address must be divided by 4, which is what the >> 2 does. And >> 2 will "break" unaligned pointers because it discards the low bits.
In practice we did run into codebases that broke because of this, but it was fairly rare. We built some tools (SAFE_HEAP) that helped find such issues. In the end it may have added some work to a small amount of ports, but very few I think.
asm.js has been superceded by WebAssembly, which allows unaligned accesses, so this is no longer a problem there.
- titzer 3y ago> WebAssembly, which allows unaligned accesses And I think we made the right call (other than the vestigial alignment bits in load/store immediates, which AFAIK, no engine is making use of).