[LFI][X86] Add memory access rewrites #227511
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-x86
Author: Zachary Yedidia (zyedidia)
<details>
<summary>Changes</summary>
This adds memory access rewriting for the x86-64 LFI target using %gs as the base register for rewrites. By using 32-bit register combined with the sandbox base in %gs, memory accesses can be rewritten to be confined to an LFI sandbox with little overhead. See the documentation in LFI.md for details on exact rewrite sequences.
---
Full diff: https://github.com/llvm/llvm-project/pull/227511.diff
10 Files Affected:
````````````The server is unavailable at this time. Please wait a few minutes before you try again.
</details>
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:white_check_mark: With the latest revision this PR passed the C/C++ code formatter.
Do not assert without an error message.
Is this equivalent?
return X86II::isPrefix(InstInfo.get(Inst.getOpcode()).TSFlags);
Please add tests for:
movq (%rax,%riz,1), %rbx
movq (,%riz,1), %rbx
movq foo(%eip), %rax
Please add tests for:
gs movq (%rsp), %rax
fs movq (%rsp), %rax
addr32 movq (%rsp), %rax
rex64 movq %rdi, %rsp
data16 addq $8, %rsp
lock jmpq *(%rdi)
movsq (%esi), %es:(%edi)
movsq %fs:(%rsi), %es:(%rdi)
xchgl %eax, %esp
shll $1, %esp
enter $16, $2
<!--PREMERGE ADVISOR COMMENT: Linux-->
:penguin: Linux x64 Test Results
181322 tests passed
4277 tests skipped
:white_check_mark: The build succeeded and all tests passed.
bikeshed on the naming; functions that return bool are less readable when framed in the negative when they get negated on the caller; the reader has to double negate them in their head.
i.e if you rename this to isSupportedPrefix, invert the return values as written, then negate in the callers I think it's more readable.
naming nit: isWriteSegmentBase
Consider returning a std::optional<unsigned> here instead.
Consider returning a std::optional<unsigned> here instead.
Please add tests for:
movq %fs:0, %rsp
addq %fs:0, %rsp
(I suspect perhaps moving getWrittenStackReg(Inst) ahead of isFSAccess(Inst) in doRewriteInst handles all of these).
Please add tests for:
xchgq %rsp, %fs:0
movl %fs:0, %esp
btq %rax, (%rdi)
btsq %rax, (%rdi)
lock btsq %rax, (%rdi)
Missing unit test coverage for this. Probably should have a test under llvm/test/CodeGen/X86/ with a RUN line like RUN: llc < %s -mtriple=x86_64_lfi | FileCheck %s.
I think we've returned early for all relevant cases already, could this be simply:
Out.emitBundleLock(/*AlignToEnd=*/false, STI);
Out.emitInstruction(Modified, Out, STI);
I've added lfi-atomic-bit-test.ll with the new unit test coverage.
Please add tests for:
btq %rax, %fs:0
btsq %rax, %fs:0
movdir64b %fs:0, %rbx
enqcmd %fs:0, %rbx
Maybe %rsp/%rip/%r14?
Is the %gs replacing %r14? Should remove addq now? Otherwise, I don't see where the overhead save from.
movl 16(%r15), %eD. The same for all the others.
LP64 mode.
Why no memory rewite instructions in the assemble?
Sounds like a worthwhile optimization for X86MCLFIRewriter::emitTPLoad.
I think the version in the document is consistent with the terminology section from the beginning.
This case is a jump to a target address that must be loaded from memory. The %gs covers the sandboxing of the memory load into %r11, but since segment registers don't apply to jump instructions, we still need to use %r14 to mask the target of the jump once it has been loaded into a register.
I think this should stay as the 64-bit version since LFI pointers are still 64-bit despite being masked to 32-bit offsets at the load/store time. For stores-only or jumps-only modes (MPK integration), we need it to be possible to directly access pointers without masks, so they need to be 64-bit. The two forms should also be the same performance so we aren't losing anything by keeping it 64-bit.
The rewrite happens in the MC layer when the code is assembled, and llc's textual output is deliberately left unrewritten so it can be assembled later. This test only checks the ISel choice, so plain assembly is sufficient. We could send it through emit objects to apply the rewrites, like lfi-rep-string.ll does, but I don't think that's necessary for this case.
ah, I assumed the 32b mov (which implicitly zeros the upper 32b) would have been a shorter encoding. Turns out, no! https://godbolt.org/z/5E3T7Wz9z
Approved these changes.
LGTM.
nickdesaulniers
zyedidia
This adds memory access rewriting for the x86-64 LFI target using
%gsas the base register for rewrites. By using 32-bit register combined with the sandbox base in%gs, memory accesses can be rewritten to be confined to an LFI sandbox with little overhead. See the documentation inLFI.mdfor details on exact rewrite sequences.