Bug #22639
openZJIT: three byte order assumptions in code shared by all backends
Description
Three places in code that every ZJIT backend shares assume a little-endian host:
gen_string_copy(zjit/src/codegen.rs) reverses every 8 bytes of a short string it copies inline.rb_shape_id_offset()(shape.c) returns the offset of the flag bits, not of the shape ID, so every shape guard fails.optimize_load_store(zjit/src/hir.rs) cannot tell the shape ID from the flags word once they are at the same offset.
None of them can be hit on master today, because ZJIT has no backend for a big-endian target. I found them while working on a backend for 64-bit PowerPC, which also runs on big-endian ppc64 with the ELFv2 ABI
1. Inline string copies are reversed 8 bytes at a time¶
Revert f2ecf348b0.
Actual:
Expected, and what the interpreter prints:
make btest RUN_OPTS=--zjit-call-threshold=1 had 99 failures out of 2,148 because of this.
Cause: gen_string_copy packs the bytes of the string into u64 words with u64::from_le_bytes and then stores each word with a native 64-bit store. On big-endian the store writes the most significant byte first, which is the last byte of the chunk.
for (i, chunk) in string_bytes.chunks_exact(8).enumerate() {
let word = u64::from_le_bytes(chunk.try_into().unwrap());
let offset = RUBY_OFFSET_RSTRING_AS_ARY + (i as i32) * 8;
asm.store(Opnd::mem(64, obj, offset), Opnd::UImm(word));
}
2. Every shape guard fails¶
Revert f193823a09.
# ivar.rb
class Point
attr_reader :x
def initialize(x) = @x = x
end
def sum(points) = points.sum { |pt| pt.x }
points = Array.new(1000) { |i| Point.new(i) }
total = 0
1000.times { total += sum(points) }
puts total
$ ./miniruby --zjit-call-threshold=2 --zjit-stats ivar.rb 2>&1 | grep -E 'guard_shape|^side_exit_count'
| actual | expected | |
|---|---|---|
guard_shape_count |
33 | 1,000,998 |
guard_shape_exit_ratio |
100.0% | 0.0% |
side_exit_count |
33 | 0 |
| top side exit reason | guard_shape_failure (100%) |
none |
The script prints the right total, 499500000, either way. The result is correct because the guard always fails and execution goes back to the interpreter, which is also why btest, test-all and test-spec all pass with this bug present. It shows up in make zjit-test as 24 failures: tests in codegen_tests.rs that assert the program did not side-exit (test_attr_reader, test_attr_accessor_getivar, test_definedivar and others) and HIR snapshots that contain a shape.
Cause: the shape ID is the upper 32 bits of RBasic::flags.
rb_shape_id_offset() gives the JITs the byte offset to read those 32 bits from:
int32_t
rb_shape_id_offset(void)
{
return sizeof(uintptr_t) - SHAPE_ID_NUM_BITS / sizeof(uintptr_t);
}
That is 4, which is where the upper half of a 64-bit word is on little-endian only. On big-endian the upper half is at offset 0 and offset 4 is the flag bits. A guard therefore compares the flag bits with a shape ID. A shape store through the same offset, if a guard ever passed, would overwrite the flag bits.
YJIT calls this function as well, but has no big-endian target either.
3. Load-store elimination mixes up the shape ID and the flags¶
This one only appears once 2 is fixed. Revert 4a53742c98.
Actual: test_remove_two_stores and test_elide_load_store_extended fail. In the first, the shape is loaded and guarded again right after it was stored:
StoreField v18, :@a@0x1002, v13
v23:CShape[0x1003] = Const CShape(0x1003)
StoreField v18, :shape_id@0x1000, v23
PatchPoint NoEPEscape(initialize)
+ v44:CShape = LoadField v18, :shape_id@0x1000
+ v45:CShape[0x1003] = GuardBitEquals v44, CShape(0x1003) recompile
CheckInterrupts
Return v13
Expected: no reload, as on little-endian.
Cause: optimize_load_store keeps the known value of a field in a map keyed by (receiver, offset). On big-endian the 32-bit shape ID and the 64-bit flags word that contains it are both at offset 0, so they share an entry. This has two effects:
WriteBarrierandStringCoderangeinvalidate the entries atRUBY_OFFSET_RBASIC_FLAGS, since they may change flag bits. That now discards the known shape too. The code is correct but does more work than needed.- A flags load after a shape store on the same object would be given the stored shape as the value of the flags. That would be a miscompile. As far as I can tell nothing emits that sequence today: shape stores are only generated for
T_OBJECT, and flags loads only for arrays, strings and structs. I could not write a Ruby program that triggers it.
Files
No data to display