Skip to content

Tracking Issue for abi_custom #140829

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
3 tasks
folkertdev opened this issue May 8, 2025 · 0 comments
Open
3 tasks

Tracking Issue for abi_custom #140829

folkertdev opened this issue May 8, 2025 · 0 comments
Labels
A-inline-assembly Area: Inline assembly (`asm!(…)`) C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@folkertdev
Copy link
Contributor

The feature gate for the issue is #![feature(abi_custom)].

The goal of the "custom" ABI is to define functions that use a custom calling convention that rust does not natively know about.

Background

One of the uses of naked functions is to implement custom calling conventions. We have some code in compiler-builtins like this:

// NOTE This function and the ones below are implemented using assembly because they are using a
// custom calling convention which can't be implemented using a normal Rust function.
#[unsafe(naked)]
pub unsafe extern "C" fn __aeabi_uidivmod() {
    core::arch::naked_asm!(
        "push {{lr}}",
        "sub sp, sp, #4",
        "mov r2, sp",
        "bl {trampoline}",
        "ldr r1, [sp]",
        "add sp, sp, #4",
        "pop {{pc}}",
        trampoline = sym crate::arm::__udivmodsi4
    );
}

The ABI needs to be specified, so extern "C" is used. However, this is misleading as the function does not actually use the C calling convention.

Correct ABI would be considered part of the preconditions for this function and it would only be callable inside an unsafe block, but Rust has no way to call the function correctly so it seems like we should prevent this.

Design

The "custom" ABI can only be used with #[unsafe(naked)] function declarations, and functions with this ABI cannot be called with a standard rust call expression.

/// # Safety
///
/// This function implements a custom calling convention that requires the
/// following inputs:
/// * `r8` contains a pointer
/// * `r9` contains a length
/// The pointer in `r8` must be valid for reads up to `r9` bytes.
///
/// `r8` and `r9` are clobbered but no other registers are.
#[unsafe(naked)]
pub unsafe extern "custom" fn foo() {
    core::arch::naked_asm!(
        /* ... */
    );
}

It is also possible to link to an extern "custom" function:

// SAFETY: `bar` is provided by `libbar.a` which we link.
unsafe extern "custom" {
    fn bar();
}

The only way to execute an extern "custom" is to use inline assembly for the call:

fn call_foo(buf: &[u8]) {
    // SAFETY: I didn't read the docs
    unsafe {
        foo();
        //~^ ERROR: `foo` has an unspecified ABI and cannot be called directly
        bar();
        //~^ ERROR: `bar` has an unspecified ABI and cannot be called directly
    }

    // SAFETY: call `foo` with its specified ABI, account for r8 & r9 clobbers
    unsafe {
        core::arch::asm!(
            "mov r8 {ptr}",
            "mov r9 {len}",
            "call {foo}",
            ptr = in(reg) buf.as_ptr(),
            len = in(reg) buf.len(),
            out("r8") _,
            out("r9") _,
            foo = sym foo,
        )
    }
}

An extern "custom¨ function cannot have any parameters or a return type. The types would be meaningless, and can easily go out of date. The function's documentation should describe how inline assembly can call the function: how arguments are passed and how a value is returned.

An extern "custom" functions is always unsafe, mostly as a nudge for writing a # Safety comment on their definition:

  • extern "custom" function declarations must use the unsafe keyword
  • in an unsafe extern "custom" { /* ... */ } block, the safe keyword cannot be used

About tracking issues

Tracking issues are used to record the overall progress of implementation.
They are also used as hubs connecting to other relevant issues, e.g., bugs or open design questions.
A tracking issue is however not meant for large scale discussion, questions, or bug reports about a feature.
Instead, open a dedicated issue for the specific matter and add the relevant feature gate label.
Discussion comments will get marked as off-topic or deleted.
Repeated discussions on the tracking issue may lead to the tracking issue getting locked.

Steps

Unresolved Questions

None

History

@rustbot label +T-lang +A-inline-assembly

@folkertdev folkertdev added the C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC label May 8, 2025
@rustbot rustbot added A-inline-assembly Area: Inline assembly (`asm!(…)`) T-lang Relevant to the language team, which will review and decide on the PR/issue. labels May 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-inline-assembly Area: Inline assembly (`asm!(…)`) C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants