llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

JIT stub offsets silently truncated to 32 bits in call instruction

Open llvmbot opened this issue 17 years ago • 26 comments

Bugzilla Link 5201
Resolution FIXED
Resolved on Nov 17, 2015 11:45
Version trunk
OS Linux
Attachments Add asserts, against trunk r84189, Add a test for the bug, against trunk r84531
Reporter LLVM Bugzilla Contributor
CC @comex,@echristo,@edwintorok,@rnk

Extended Description

On x86-64 systems, we're observing segfaults that we believe to have tracked down to a codegen problem in the JIT:

Disassembling the buggy function in gdb gives us this: ... asm ... 0x00002aaab33482b1 <_23_u_23___init__45+673>: incq (%r14) 0x00002aaab33482b4 <_23_u_23___init__45+676>: mov 0x18(%rsp),%rbx 0x00002aaab33482b9 <_23_u_23___init__45+681>: mov %r14,(%rbx) 0x00002aaab33482bc <_23_u_23___init__45+684>: mov $0x2dd8b48,%rbx 0x00002aaab33482c6 <_23_u_23___init__45+694>: mov 0x18(%rbx),%rsi 0x00002aaab33482ca <_23_u_23___init__45+698>: mov %r14,%rdi 0x00002aaab33482cd <_23_u_23___init__45+701>: callq 0x2aaaa5c810b0 0x00002aaab33482d2 <_23_u_23___init__45+706>: mov (%r14),%rbx 0x00002aaab33482d5 <_23_u_23___init__45+709>: dec %rbx ... asm ...

(gdb) x 0x2aaaa5c810b0 0x2aaaa5c810b0: Cannot access memory at address 0x2aaaa5c810b0

That callq 0x2aaaa5c810b0 instruction is the problem: it's calling to invalid memory. Looking at the other callq instructions in this function, gdb can't access any of them. callq addresses in other functions can be accessed by gdb.

What's happening is that now that the JIT memory manager can allocate multiple code slabs, there's no protection against having stub slabs and code slabs more than 32 bits distant from each other. If the memory manager allocates a new code slab far away from the stub, the offset will be truncated to 32 bits, which means that Bad Things Happen.

I'm working on a reduced test case, but in the meantime, I think the attached patch should be applied to trunk so that the offset is no longer silently truncated.

Applying this patch and passing -debug-only=jit will show this output when running part of Unladen Swallow's test suite:

[...tests...] test_importhooks test_enumerate test_getopt test_codecencodings_cn JIT: Allocating another slab of memory for function. python: /usr/local/google/collinwinter/us/trunk/Util/llvm/lib/ExecutionEngine/JIT/JITEmitter.cpp:652: void*::JITEmitter::getPointerToGlobal(llvm::GlobalValue*, void*, bool): Assertion `(Offset & 0xFFFFFFFF) == Offset && "Offset too big for 32 bits"' failed. Stack dump: 0. Running pass 'X86 Machine Code Emitter' on function '@"#u#readline241"

Without this, you'd see a segfault instead.

llvmbot avatar Oct 15 '09 20:10 llvmbot

mentioned in issue llvm/llvm-bugzilla-archive#6758

edwintorok avatar Nov 27 '21 05:11 edwintorok

The old JIT has been deleted, and MCJIT doesn't suffer from this problem. It sometimes has a different class of issues where it fatal errors during relocation resolution, but we already have issues open for that.

rnk avatar Nov 17 '15 19:11 rnk

Is this still affecting anyone?

echristo avatar Oct 16 '15 05:10 echristo

pr5201-ppcfix-llvm2.8-rc1-openjdk-fail.log

Török's PPC workaround got commited to: http://llvm.org/viewvc/llvm-project?view=rev&revision=110246

Can you test if it works with OpenJDK too?

Unfortunately: PPC JIT still asserts with reallocation out of range errors when I test using LLVM 2.8 rc1.

llvmbot avatar Sep 10 '10 15:09 llvmbot

Török's PPC workaround got commited to: http://llvm.org/viewvc/llvm-project?view=rev&revision=110246

Can you test if it works with OpenJDK too?

edwintorok avatar Aug 09 '10 11:08 edwintorok

Török's PPC workaround got commited to: http://llvm.org/viewvc/llvm-project?view=rev&revision=110246

ARM got a new -arm-long-calls option to force calls to be indirect that got commited to: http://llvm.org/viewvc/llvm-project?view=rev&revision=101303 A possible fix would be to enable this new option by default on ARM.

llvmbot avatar Aug 09 '10 11:08 llvmbot

new patch for PowerPC JIT workaround New patch, that passes llvm/llvm-bugzilla-archive#6902 too.

edwintorok avatar Aug 04 '10 22:08 edwintorok

On PowerPC a short jump can only go 5Mb in each direction, so trying to keep the slabs "close enough" might be a losing battle.

I have hit this bug using the ARM JIT. On ARM a short jump can only go 32Mb in each direction, while it happens less frequently on ARM than on PowerPC it still can happen. This bug looks plausible to fix for ARM by generating a long branch to any address within the 4Gb addresspace by emitting the following when outside the +-32Mb scope: mov lr, pc mov pc, #stubaddress

llvmbot avatar May 19 '10 22:05 llvmbot

Created an attachment (id=4631) [details] PowerPC JIT workaround

Workaround: always emit indirect calls in JIT mode for PowerPC.

With the patch applied on LLVM 2.7 makes the PowerPC JIT fail to resolve llvm.memset.* intrinsics: bug 6902 (contains testcase and additional info)

llvmbot avatar Apr 23 '10 14:04 llvmbot

Aha. For some reason I thought I'd see it set in the diff.

echristo avatar Apr 06 '10 19:04 echristo

Seems somewhat reasonable, I'm probably missing something but I'm not seeing SetJITMode ever called?

It is called in PPCTargetMachine.cpp: bool PPCTargetMachine::addCodeEmitter(PassManagerBase &PM, CodeGenOpt::Level OptLevel, JITCodeEmitter &JCE) { // The JIT should use the static relocation model in ppc32 mode, PIC in ppc64. // FIXME: This should be moved to TargetJITInfo!! if (Subtarget.isPPC64()) { // We use PIC codegen in ppc64 mode, because otherwise we'd have to use many // instructions to materialize arbitrary global variable + function + // constant pool addresses. setRelocationModel(Reloc::PIC_); // Temporary workaround for the inability of PPC64 JIT to handle jump // tables. DisableJumpTables = true;
} else { setRelocationModel(Reloc::Static); }

// Inform the subtarget that we are in JIT mode. FIXME: does this break macho // writing? Subtarget.SetJITMode();

edwintorok avatar Apr 06 '10 19:04 edwintorok

Seems somewhat reasonable, I'm probably missing something but I'm not seeing SetJITMode ever called?

echristo avatar Apr 06 '10 19:04 echristo

PowerPC JIT workaround Workaround: always emit indirect calls in JIT mode for PowerPC.

edwintorok avatar Apr 04 '10 13:04 edwintorok

r88984 fixes this problem for x86-64 by generating far calls for all calls in the large code model. Unfortunately, PPC and ARM don't have a large code model, so making the same change there affects their static compilation. There's a couple things we could do on PPC and ARM:

  1. Go back to 16MB code allocation by default, which would mask this problem in most cases, but wouldn't fix it.

It should also allocated stubs together with code, otherwise they will be too far apart for relocations (see llvm/llvm-bugzilla-archive#6758 ).

Problem is that stub emission can't be guaranteed to be "close enough" to the code, at least not for PPC where the possible relocation range is very small.

  1. Thread the actual target address into the code generator. This would be a pretty general win, but would take even more changes.

Sounds good, we could emit direct calls when constants are small enough, and indirect when not. It would need some interface between the code generator and the JIT to query the address.

  1. ????
  1. Always emit indirect calls for ALL calls in the JIT.

The disadvantage is that even for local functions we need to use this, since we don't know if they will be close enough to call or not (and the "far call stubs" are broken, they are often too far to call too on PowerPC. Even when the function would be close-enough to call).

Attached is a patch that implements this.

  1. Profit!

Since I'm not working on PPC or ARM systems, I favor option 1 to undo the regression from Reid's change. When someone else gets enthusiastic about those platforms, they can fix them more generally.

edwintorok avatar Apr 04 '10 13:04 edwintorok

*** Bug llvm/llvm-bugzilla-archive#6758 has been marked as a duplicate of this bug. ***

edwintorok avatar Apr 04 '10 13:04 edwintorok

r88984 fixes this problem for x86-64 by generating far calls for all calls in the large code model. Unfortunately, PPC and ARM don't have a large code model, so making the same change there affects their static compilation. There's a couple things we could do on PPC and ARM:

  1. Go back to 16MB code allocation by default, which would mask this problem in most cases, but wouldn't fix it.
  2. Invent separate code models for PPC and ARM. I'm skeptical of this because there are no external ABI docs to justify or drive any choices we make.
  3. Figure out more generally how to have multiple far-call-stubs for a single target. This probably requires distinguishing address-of and call uses of Functions in JITEmitter::getPointerToGlobal(), which will take more widespread changes.
  4. Thread the actual target address into the code generator. This would be a pretty general win, but would take even more changes.
  5. ????
  6. Profit!

Since I'm not working on PPC or ARM systems, I favor option 1 to undo the regression from Reid's change. When someone else gets enthusiastic about those platforms, they can fix them more generally.

llvmbot avatar Nov 20 '09 02:11 llvmbot

Using far-call stubs allocated with the calling function has the problem that the stub gets returned from address-of operations, and may outlive the call to the function entirely. The function is destroyed then, the call stub gets invalidated, and calling it may cause a crash. :( The real fix here is to distinguish between address-of operations and call operations, and only use far-call stubs for calls, but that appears to be a hairy yak.

As a fix for this problem, nlewycky proposed making all calls into far calls, even if they turn out to be nearby, eliminating the need for far calls entirely. This will waste some code space, but make the code correct. Are there any problems we haven't seen with solving the problem this way?

llvmbot avatar Nov 10 '09 07:11 llvmbot

Awesome :)

llvmbot avatar Nov 06 '09 12:11 llvmbot

I'm going to look into un-sharing far-call-stubs.

llvmbot avatar Nov 06 '09 05:11 llvmbot