Repository navigation
Conversation
Fix two byte-swapping bugs that caused segfaults on big-endian platforms (PowerPC, s390x) when loading bytecode compiled on little-endian systems. This affected the REPL (which uses pre-compiled LE bytecode from gen/repl.c) and any use of qjsc-generated bytecode across endianness boundaries. Bug 1: lre_byte_swap swapped wrong variable for REOP_range/REOP_range32 In lre_byte_swap(), the code for variable-length REOP_range and REOP_range32 instructions was swapping 'n' (the base instruction size, always 3) instead of 'nw' (the number of range pairs read from the bytecode). bswap16(3) = 0x0300 = 768, so the bytecode walk position jumped 768 bytes forward, skipping all remaining instructions and leaving them un-swapped. Any regex with a character class (e.g. [a-z]) would have corrupted bytecode on big-endian. Bug 2: regex bytecodes in JS constant pool not byte-swapped When a JS source containing regex literals (e.g. /[a-z]+/) is compiled to bytecode by qjsc, the compiled regex bytecodes are stored as string constants in the function's constant pool. The OP_regexp opcode later retrieves these strings and passes them to js_regexp_constructor_internal to create RegExp objects. The existing JS_ReadRegExp path (for serialized RegExp objects) correctly called lre_byte_swap to convert regex bytecodes from LE to native format. However, the OP_regexp path (inline regex literals from the constant pool) did not -- the regex bytecodes were read as plain 8-bit strings with no byte-swapping applied, leaving them in LE format on a BE machine. The fix adds byte-swap logic to both the reader and writer: - JS_ReadFunctionBytecode: after reading JS bytecodes, scans for OP_regexp opcodes and byte-swaps the corresponding regex bytecode constants (identified by the preceding OP_push_const/OP_push_const8). - JS_WriteFunctionTag: before writing the constant pool, converts regex bytecodes from native to LE format, then restores them after writing so the in-memory state is not affected. Closes: #1122
|
What do you think of simply removing explicit support for big endian targets? Removing the byteswap logic makes the code a lot easier. It's not worth sinking time in trying to make big endian targets work, IMO. At this point there really are no big endian architectures left. Debian dropped support for powerpc (the 32-bits BE kind) 6 years ago. z/os is maybe the sole remnant and if IBM wants quickjs to run on that, hey, they can pay us. |
SGTM, axe it out! |
|
For posterity: #1412 |
DISCLAIMER: Created with Claude Code. Unreviewed yet.
Fix two byte-swapping bugs that caused segfaults on big-endian platforms
(PowerPC, s390x) when loading bytecode compiled on little-endian systems.
This affected the REPL (which uses pre-compiled LE bytecode from gen/repl.c)
and any use of qjsc-generated bytecode across endianness boundaries.
Bug 1: lre_byte_swap swapped wrong variable for REOP_range/REOP_range32
In lre_byte_swap(), the code for variable-length REOP_range and
REOP_range32 instructions was swapping 'n' (the base instruction size,
always 3) instead of 'nw' (the number of range pairs read from the
bytecode). bswap16(3) = 0x0300 = 768, so the bytecode walk position
jumped 768 bytes forward, skipping all remaining instructions and
leaving them un-swapped. Any regex with a character class (e.g. [a-z])
would have corrupted bytecode on big-endian.
Bug 2: regex bytecodes in JS constant pool not byte-swapped
When a JS source containing regex literals (e.g. /[a-z]+/) is compiled
to bytecode by qjsc, the compiled regex bytecodes are stored as string
constants in the function's constant pool. The OP_regexp opcode later
retrieves these strings and passes them to js_regexp_constructor_internal
to create RegExp objects.
The existing JS_ReadRegExp path (for serialized RegExp objects) correctly
called lre_byte_swap to convert regex bytecodes from LE to native format.
However, the OP_regexp path (inline regex literals from the constant pool)
did not -- the regex bytecodes were read as plain 8-bit strings with no
byte-swapping applied, leaving them in LE format on a BE machine.
The fix adds byte-swap logic to both the reader and writer:
JS_ReadFunctionBytecode: after reading JS bytecodes, scans for
OP_regexp opcodes and byte-swaps the corresponding regex bytecode
constants (identified by the preceding OP_push_const/OP_push_const8).
JS_WriteFunctionTag: before writing the constant pool, converts
regex bytecodes from native to LE format, then restores them after
writing so the in-memory state is not affected.
Closes: #1122