<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/56369>56369</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Please respect nakedness of functions for CFG/CFI/IBT/BTI/etc.
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          workingjubilee
      </td>
    </tr>
</table>

<pre>
    Hello, this is a follow up from https://github.com/rust-lang/rust/issues/98768

Currently, rustc will, if asked to use options like AArch64's BTI or Intel's CET, emit module-level attributes, following the overall "module, then function override" logic that has been used for these suites of security features. However, we also have people wanting to combine these with naked functions. The definition we have adopted for naked functions is pretty strict on having no prologue at all, and we do emit the `naked` function attribute, which the [LangRef](https://llvm.org/docs/LangRef.html) asserts is supposed to strip prologues and epilogues. The result looks like so in LLVM IR:

```llvm
target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
target triple = "aarch64-unknown-linux-gnu"

define void @_hlt() unnamed_addr #1 {
  call void asm sideeffect alignstack "hlt #1", "~{cc},~{memory}"(), !srcloc !6
  unreachable
}

define i64 @_start(i64 %_a, i8** %_b) unnamed_addr #2 {
  call void @_hlt() #3
  unreachable
}

attributes #0 = { noreturn nonlazybind uwtable "target-cpu"="generic" }
attributes #1 = { naked nocf_check noinline noreturn nonlazybind uwtable "target-cpu"="generic" }
attributes #2 = { nonlazybind uwtable "target-cpu"="generic" }
attributes #3 = { noreturn }

!llvm.module.flags = !{!0, !1, !2, !3, !4, !5}

!0 = !{i32 7, !"PIC Level", i32 2}
!1 = !{i32 2, !"RtLibUseGOT", i32 1}
!2 = !{i32 1, !"branch-target-enforcement", i32 1}
!3 = !{i32 1, !"sign-return-address", i32 0}
!4 = !{i32 1, !"sign-return-address-all", i32 0}
!5 = !{i32 1, !"sign-return-address-with-bkey", i32 0}
!6 = !{i32 249}
```

However, the generated assembly looks like:
```aarch64
_hlt:                                   // @_hlt
        hint    #34
        hlt     #0x1
_start:                                 // @_start
        hint    #34
        str     x30, [sp, #-16]!                // 8-byte Folded Spill
        bl      _hlt
```

That is very much a prologue right there!

Now, I think it's reasonable to ask whether this is "intended behavior" for naked functions... it is in Rust, but naked functions are fairly underspecified anyways. One could possibly even question what the definition of a "prologue" is anyways. But the `nocf_check` attribute is there because while we have a similar problem with the x86-64 equivalent for Intel CET (AKA IBT) coming with `endbr64` prologues, `nocf_check` strips them away. This is pretty convenient, and doing a bit of extra legwork to make sure they're gone is fine. However, AArch64's BTI doesn't respect this and instead requires `"branch-target-enforcement"=false` be set on the function to override it.

This seems like a logical bug. Naked functions should probably not have prologues, even these "security prologues". But if they do, then either `nocf_check` should strip all such branch control prologues or none of them. It would be nice if either or both consistently removed these instructions, so as to allow compilers to toggle them off in an architecture-neutral manner, rather than having to emit a distinct negation for each architecture.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy1V9ly4zYW_Rr5BUWVRGp90IOXOHGNJ0l1PPPaBZKXEmIQUADQsubrcy5ArXZPOlUzLpkLiLvg3AUHpa33q59IazvI70XYKC_wk6KxGNqJbisaZ1uxCWHrB8XtIH_Eb63CpiuHlW3x4jofMi3Nun_GTXnfkcfDcjGfLQajh8HoNl3vO-fIBL1nazy7EjulNb-pRkj_SrUIVnSehN0GZY0XWr2SuL111WY2GeRzL-5enoR14skE0nHg_ocXVkCtCqK1dacp0_RGWsgQnCq7wL7c90tSZo1lQv0bOam1GOR5kknrJyOazlRsOk5xqsaXXGi7VhW-yyA20ouSMBFe1tDqWAwO-07BkrCN8FR1ToW9aEiGzpEfip_sDi45NrIjIbW30PNGYkt2q0nspAnRMyuAaqkM9Up3gFoYybgc_IK2FyygpkYZFR2FxqhM1gCt9-lKhqO6dRTglAcoVRCQgxAbNRafLFbYQUWAczEe0tSsuLYJWMZsMBtFtbifUDqCHNe2UdUmzZ3ePSM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