PPC soft-float: fix ipairs() returning corrupted values and pairs() yielding a spurious nil - #270
PPC soft-float: fix ipairs() returning corrupted values and pairs() yielding a spurious nil#270BKPepe wants to merge 2 commits into
Conversation
|
@zhuizhuhaomeng Can you take a look, please? |
|
[P1] PPC32LE soft-float ipairs() values are corrupted — src/vm_ppc.dasc (line 1836) [P2] pairs() checks the payload instead of the type on PPC32LE soft-float — src/vm_ppc.dasc (line 5813) Would you please check the review issues from AI? |
|
Thanks. Both of these come from 2763a42, and looking again the right fix in each case is to restore what that commit replaced. For ipairs, the line was For BC_ITERN, 2763a42 removed the shared That makes both hunks correct on either endianness. I will respin the PR accordingly. I can still only test on big-endian e500v2. |
The soft-float branch of the ipairs_aux fast function loads the second word of the array slot from WORD_HI. On big-endian targets WORD_HI is 0, which is the itype the branch has already loaded into TMP2, so the value word at offset 4 is never read. Every element therefore comes back carrying the itype in its payload. Numbers surface as -14, the LJ_TNUMX tag read as an int32. For GC types the payload is the GCref, so the tag becomes a fabricated pointer that tostring() then dereferences. Restore the hardcoded 4(TMP1) that commit 2763a42 ("Patch for PPC64 support") rewrote as WORD_HI. The two words are stored straight back to 8(RA) and 12(RA), so a raw offset preserves the slot layout and is correct on either endianness. Upstream LuaJIT is unaffected. Reproducer on Turris 1.x (e500v2, 32-bit big-endian, soft-float): $ luajit -e 'local s=0 for i,v in ipairs({10,20,30}) do s=s+v end print(s)' -42 -- expected 60 Signed-off-by: Josef Schlehofer <pepe.schlehofer@gmail.com>
BC_ITERN checks the itype of the node value in RB to skip empty slots in the hash part. Upstream loads RB unconditionally before the FPU split. Commit 2763a42 ("Patch for PPC64 support") moved that load into the FPU branch only. On soft-float builds, RB still contains RC*8 from the hash-part setup, so the nil check never succeeds and iterating a table whose hash part is empty yields one extra (nil, nil) pair. Load RB in the soft-float branch as well, mirroring the FPU branch. Taking it from WORD_HI keeps the check correct on either endianness and leaves checknil untouched. Reproducer on Turris 1.x (e500v2, 32-bit big-endian, soft-float): $ luajit -e 'for k,v in pairs({7,8,9}) do print(k,v) end' 1 7 2 8 3 9 nil nil -- spurious Signed-off-by: Josef Schlehofer <pepe.schlehofer@gmail.com>
ae896c3 to
60c3d8d
Compare
Two independent bugs in
src/vm_ppc.dasc, both confined to the.if not FPUbranch and both introduced by 2763a42 ("Patch for PPC64 support"). Upstream LuaJIT is not affected — on the same board, the distro's upstream LuaJIT returns the correct results.Reproducers on Turris 1.x (e500v2, 32-bit big-endian, musl, soft-float):
Before the fixes, the
ipairs()test yields-42instead of60and thepairs()test sees an extra(nil, nil)iteration. With the fixes, both pass, with and without the JIT.ipairs()also has a memory-safety angle: only the payload half of the TValue is wrong while the itype stays correct, so for GC-typed elements the type tag ends up in the GCref andtostring()dereferences a fabricated pointer.Verified on hardware, and through the OpenWrt package build in openwrt/packages#30280, where the resulting
powerpc_8548package was installed on the device and behaves correctly.