Issue 240315
Summary [LTO][CFI] LowerTypeTests Gates Non-Address-Taken Canonical Jump Tables on CrossDsoCfi, Silently Dropping __attribute__((cfi_canonical_jump_table)) Under -fno-sanitize-cfi-canonical-jump-tables
Labels new issue
Assignees
Reporter zjy-dev
    Clang's `__attribute__((cfi_canonical_jump_table))` (`"cfi-canonical-jump-table"` IR attribute) is specifically designed for `-fsanitize=cfi-icall -fno-sanitize-cfi-canonical-jump-tables` (non-Cross-DSO CFI) so that individual functions whose address is taken only in assembly or external non-LTO code still receive a canonical jump-table entry. However, in `LowerTypeTestsModule::lower` (llvm/lib/Transforms/IPO/LowerTypeTests.cpp:2374-2377), when `!F->hasAddressTaken()` in IR (because its address is only taken in inline/module assembly or external objects), the filter checks `if (!CrossDsoCfi || !IsJumpTableCanonical || F->hasLocalLinkage()) continue;`. Because `CrossDsoCfi` is `false` under `-fno-sanitize-cfi-canonical-jump-tables`, `!CrossDsoCfi` is unconditionally `true`, so `LowerTypeTests` skips `F` and never emits a jump-table entry for it.

### Reproducer (`trigger.c`)

```c
typedef int (*fn_t)(int);

/* Canonical-by-attribute function: its address is taken ONLY from the
 module-level asm below, so hasAddressTaken() is false in IR. */
__attribute__((cfi_canonical_jump_table))
int asm_target(int x) { return x + 1; }

int other_target(int x) { return x + 2; }

/* Keeps the type-id set for int(*)(int) non-empty and address-taken in C. */
fn_t keep_member = other_target;

/* Instrumented indirect call site: lowers to llvm.type.test + jump table. */
int call_through(fn_t p, int v) { return p(v); }

#if defined(__x86_64__)
__asm__(".globl asm_taken_addr\n"
        ".type asm_taken_addr,@function\n"
        "asm_taken_addr:\n\t"
 "lea asm_target(%rip), %rax\n\t"
        "ret\n");
extern fn_t asm_taken_addr(void);
#elif defined(__aarch64__)
__asm__(".globl asm_taken_addr\n"
        ".type asm_taken_addr,%function\n"
 "asm_taken_addr:\n\t"
        "adrp x0, asm_target\n\t"
 "add x0, x0, :lo12:asm_target\n\t"
        "ret\n");
extern fn_t asm_taken_addr(void);
#elif defined(__riscv) && __riscv_xlen == 64
__asm__(".globl asm_taken_addr\n"
        ".type asm_taken_addr,@function\n"
        "asm_taken_addr:\n\t"
 "la a0, asm_target\n\t"
        "ret\n");
extern fn_t asm_taken_addr(void);
#elif defined(__loongarch64)
__asm__(".globl asm_taken_addr\n"
        "asm_taken_addr:\n\t"
        "la.global $a0, asm_target\n\t"
        "ret\n");
extern fn_t asm_taken_addr(void);
#else
fn_t asm_taken_addr(void) { return keep_member; }
#endif

int main(void) {
  fn_t p = asm_taken_addr();
  /* exit 0 iff the asm-obtained address passes the CFI type check */
  return call_through(p, 1) == 2 ? 0 : 1;
}
```

Compile / reproduction command (`['--target=x86_64-unknown-linux-gnu', '-flto', '-fvisibility=hidden', '-fsanitize=cfi-icall', '-fno-sanitize-cfi-canonical-jump-tables', '-O2', '-S', '-emit-llvm']`):

```bash
podman exec -i -e TMPDIR=/dev/shm defuzz-toolchain-daemon bash -c '/opt/llvm23/bin/clang --target=x86_64-unknown-linux-gnu -flto -fvisibility=hidden -fsanitize=cfi-icall -fno-sanitize-cfi-canonical-jump-tables -O2 -x c - -S -emit-llvm -o - | /opt/llvm23/bin/opt -passes=lowertypetests -S'
```

Observed output on LLVM 23.1.2:

```text
define hidden range(i32 -2147483647, -2147483648) i32 @asm_target(i32 noundef %0) local_unnamed_addr #0 !type !13 !type !14 !guid !15 {
  %2 = add nsw i32 %0, 1
  ret i32 %2
}
...
define private void @.cfi.jumptable() #6 prefalign(8) !elf_section_properties !28 {
entry:
  call void asm sideeffect "jmp ${0:c}@plt\0Aint3\0Aint3\0Aint3\0A", "s"(ptr @other_target)
  unreachable
}
attributes #0 = { ... "cfi-canonical-jump-table" ... }
```

### Expected behavior

When `F->hasFnAttribute("cfi-canonical-jump-table")` (`IsJumpTableCanonical` is explicitly set via attribute under `-fno-sanitize-cfi-canonical-jump-tables`), `LowerTypeTestsModule::lower` must include `F` in the jump table even if `!F->hasAddressTaken()` in IR and `CrossDsoCfi` is false.

### Root cause & impact

- **Source location**: `llvm/lib/Transforms/IPO/LowerTypeTests.cpp:2237-2239, 2374-2377`
- **Impact**: Breaks the documented escape hatch `__attribute__((cfi_canonical_jump_table))` under `-fno-sanitize-cfi-canonical-jump-tables`, causing valid indirect calls to assembly-referenced or externally-referenced functions to fail the CFI check and abort with `ud2`.

### Suggested fix

In `LowerTypeTests.cpp:2374-2377` (and summary handling at lines 2237-2239), allow non-local functions with explicit `"cfi-canonical-jump-table"` attribute to retain their jump-table entry even when `CrossDsoCfi` is false and `!F->hasAddressTaken()`.

Environment: LLVM 23.1.2 (`llvmorg-23.1.2`), affected targets: `x86_64, aarch64, riscv64, loongarch64`.
clang trunk still attaches `"cfi-canonical-jump-table"` to the function (Compiler Explorer, 2026-11-09); the omission is in the `lowertypetests` pass.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to