[PATCH] D148385: [RISCV] Implement KCFI operand bundle lowering

Fangrui Song via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 5 20:31:01 PDT 2023


MaskRay added a comment.

`KCFI_CHECK` lowering has some complexity to allocate a temporary register. This needs to following the calling convention which can be modified by many compiler options and function attributes.

I wonder whether we can move the if-condition part of the expanded code sequence (i.e. `if type-hashes mismatch; crash`) to ClangCodeGen (more like `-fsanitize=function`), and change the "kcfi" operand bundle to focus on expanding to a desired trap instruction (ud2 on x86-64).
On the plus side, this gives optimizers more opportunities to place trap basic blocks to cold regions.
On the downside, we cannot assume the code sequence is contiguous but that may be fine.

---

Some notice for other reviewers:

To emit `.kcfi_traps` and a preferred trap instruction, the instrumentation cannot be done purely in ClangCodeGen. Instead, the following scheme is used:

- ClangCodeGen emits "kcfi" operand bundles <https://llvm.org/docs/LangRef.html#kcfi-operand-bundles>.
- In a target-overridden `TargetPassConfig::addPreSched`, add a KCFI pass to emit `KCFI_CHECK` pseudo instructions.
- In a target AsmPrinter, lower `KCFI_CHECK` pseudo instructions to a code sequence that crash if the type hashes don't match.

If a target doesn't implement `KCFI_CHECK` lowering, `llvm/lib/Transforms/Instrumentation/KCFI.cpp` is used to replace "kcfi" operand bundles with if conditions, then no `TargetPassConfig::addPreSched` or `KCFI_CHECK` lowering will be needed.
In this case, `llvm.debugtrap` is used to generate trap instructions.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D148385/new/

https://reviews.llvm.org/D148385



More information about the cfe-commits mailing list