Issue 237911
Summary [SafeStack] Pointer passed to asm goto (callbr) is not treated as an escape; the object stays on the safe stack
Labels new issue
Assignees
Reporter zjy-dev
    `SafeStack::IsSafeStackAlloca` checks call arguments only for `Instruction::Call` and `Instruction::Invoke`.
A `callbr` (from `asm goto`) falls into `default:`, which only follows uses of the callbr's *result*.
Its pointer operands are never checked. The same asm without a label is a plain `call`, and that case moves the
object to the unsafe stack.

```c
int asm_goto_escape(int c) {
  int local = c;
  asm goto("" : : "r"(&local) : "memory" : out);   /* callbr */
  return local;
out:
  return -1;
}
int asm_plain_escape(int c) { /* control: plain call */
  int local = c;
  asm volatile("" : : "r"(&local) : "memory");
  return local;
}
```

Compile flag: `clang --target=x86_64-unknown-linux-gnu -O1 -fsanitize=safe-stack -S`

```asm
asm_goto_escape:
  movl  %edi, -4(%rsp)        # local on the regular (safe) stack
  leaq  -4(%rsp), %rcx        # its address handed to the asm
  #APP / #NO_APP
asm_plain_escape:
  movq __safestack_unsafe_stack_ptr@GOTTPOFF(%rip), %rcx
  movq  %fs:(%rcx), %rdx
 leaq  -16(%rdx), %rax
  movq  %rax, %fs:(%rcx)      # local moved to the unsafe stack
  movl  %edi, -4(%rdx)
```

IR before the pass: `callbr void asm sideeffect "", "r,!i,~{memory},..."(ptr nonnull %2)` with `%2 = alloca i32`.
AArch64 behaves the same way (`str w0, [sp, #12]; add x8, sp, #12`). Compiler Explorer clang trunk gives identical output.

**Root cause.** llvm/lib/CodeGen/SafeStack.cpp:320-352 handles `case Instruction::Call: case Instruction::Invoke:`
only. `Instruction::CallBr` reaches `default:` (SafeStack.cpp:356-358). That branch pushes the callbr onto the
worklist but never calls `doesNotCapture`/`doesNotAccessMemory` on its arguments.

**Suggested fix.** Add `case Instruction::CallBr:` to the Call/Invoke case (it is a `CallBase`), or switch on
`isa<CallBase>(I)`.

Environment: clang 23.1.2 (85ac560262434c9ccfc0c183ec22d4138ed647fb); x86_64/aarch64-linux-gnu.

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

Reply via email to