Skip to content

RUSTFLAGS="-Z cf-protection=full" gives prologue in naked functions for x86_64-unknown-none #98768

Closed
@haraldh

Description

@haraldh

I tried this code:

#![feature(naked_functions)]
#![feature(start)]
#![no_std]
#![no_main]

use core::arch::asm;

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo<'_>) -> ! {
    loop {}
}

#[no_mangle]
#[naked]
pub unsafe extern "sysv64" fn _hlt() -> ! {
    asm!("hlt", options(noreturn))
}

#[start]
#[no_mangle]
pub fn _start(_a: isize, _b: *const *const u8) -> isize {
    unsafe { _hlt() }
}

I expected to see this happen:

no endbr64 prologue in _hlt naked function.

❯ RUSTFLAGS="-Z cf-protection=full" cargo +nightly build --target x86_64-unknown-none 
   Compiling cet-naked v0.1.0 (/home/harald/CLionProjects/cet-naked)
    Finished dev [unoptimized + debuginfo] target(s) in 0.44s

❯ objdump --disassembler-options=intel --disassemble target/x86_64-unknown-none/debug/cet-naked

target/x86_64-unknown-none/debug/cet-naked:     file format elf64-x86-64


Disassembly of section .text:

0000000000001210 <_hlt>:
    1210:	f4                   	hlt    
    1211:	0f 0b                	ud2    
    1213:	cc                   	int3   
    1214:	cc                   	int3   
    1215:	cc                   	int3   
    1216:	cc                   	int3   
    1217:	cc                   	int3   
    1218:	cc                   	int3   
    1219:	cc                   	int3   
    121a:	cc                   	int3   
    121b:	cc                   	int3   
    121c:	cc                   	int3   
    121d:	cc                   	int3   
    121e:	cc                   	int3   
    121f:	cc                   	int3   

0000000000001220 <_start>:
    1220:	f3 0f 1e fa          	endbr64 
    1224:	48 83 ec 18          	sub    rsp,0x18
    1228:	48 89 7c 24 08       	mov    QWORD PTR [rsp+0x8],rdi
    122d:	48 89 74 24 10       	mov    QWORD PTR [rsp+0x10],rsi
    1232:	e8 d9 ff ff ff       	call   1210 <_hlt>
    1237:	0f 0b                	ud2    

Instead, this happened: an unwanted endbr64 prologue in a naked function

❯ RUSTFLAGS="-Z cf-protection=full" cargo +nightly build --target x86_64-unknown-none 
   Compiling cet-naked v0.1.0 (/home/harald/CLionProjects/cet-naked)
    Finished dev [unoptimized + debuginfo] target(s) in 0.44s

❯ objdump --disassembler-options=intel --disassemble target/x86_64-unknown-none/debug/cet-naked

target/x86_64-unknown-none/debug/cet-naked:     file format elf64-x86-64


Disassembly of section .text:

0000000000001210 <_hlt>:
    1210:	f3 0f 1e fa          	endbr64 
    1214:	f4                   	hlt    
    1215:	0f 0b                	ud2    
    1217:	cc                   	int3   
    1218:	cc                   	int3   
    1219:	cc                   	int3   
    121a:	cc                   	int3   
    121b:	cc                   	int3   
    121c:	cc                   	int3   
    121d:	cc                   	int3   
    121e:	cc                   	int3   
    121f:	cc                   	int3   

0000000000001220 <_start>:
    1220:	f3 0f 1e fa          	endbr64 
    1224:	48 83 ec 18          	sub    rsp,0x18
    1228:	48 89 7c 24 08       	mov    QWORD PTR [rsp+0x8],rdi
    122d:	48 89 74 24 10       	mov    QWORD PTR [rsp+0x10],rsi
    1232:	e8 d9 ff ff ff       	call   1210 <_hlt>
    1237:	0f 0b                	ud2    

Meta

rustc --version --verbose:

rustc 1.64.0-nightly (7425fb293 2022-06-30)
binary: rustc
commit-hash: 7425fb293f510a6f138e82a963a3bc599a5b9e1c
commit-date: 2022-06-30
host: x86_64-unknown-linux-gnu
release: 1.64.0-nightly
LLVM version: 14.0.6
Backtrace

<backtrace>

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.A-codegenArea: Code generationA-nakedArea: `#[naked]`, prologue and epilogue-free, functions, https://git.io/vAzzSC-bugCategory: This is a bug.O-x86_64Target: x86-64 processors (like x86_64-*) (also known as amd64 and x64)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions