Skip to content

ptr::fn_code_ptr for explicitly getting an opaque pointer of a function #589

Open
@pitaj

Description

@pitaj

Proposal

Problem statement

Currently, the only way to get the address of a function pointer is using the as operator. But because as is heavily overloaded, this can result in unfortunate mistakes that are silently accepted.

Motivating examples or use cases

There was recently a real bug caused by the following code:

len < u16::max as usize

This was meant to refer to u16::MAX, the associated constant with value 0xFFFF, but instead returns the address of the function pointer to u16::max.

We would like to add a general lint for this form of as cast, but it can't be enabled by default without an alternative way of getting the address of a function pointer.

Solution sketch

// core::ptr

extern type CodeInner;

// Is there a way to prevent people from implementing traits for this?
// Would be nice if we could reserve the ability to just make this an alias to unit.
#[repr(transparent)]
pub struct Code(CodeInner);

pub fn fn_code_ptr<T: FnPtr>(f: T) -> *const Code { ... }

Then the lint can suggest doing a two-step conversion:

warning: casting directly to function address is error-prone
   |
11 | if x <= u16::max as usize {
             ^^^^^^^^^^^^^^^^^
cast to a code pointer first, then get the address
   |
11 | if x <= core::ptr::fn_code_ptr(u16::max).expose_provenance() {

Alternatives

We could just stabilize FnPtr::addr directly, but that trait is meant to be for internal use only.

We could provide a function that casts directly from a function pointer to usize, but there are concerns around code pointer vs data pointer sizing. This way, the programmer can explicitly choose what address operation to use: .addr() or .expose_provenance().

Links and related work

What happens now?

This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

Possible responses

The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):

  • We think this problem seems worth solving, and the standard library might be the right place to solve it.
  • We think that this probably doesn't belong in the standard library.

Second, if there's a concrete solution:

  • We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
  • We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-libs-apiapi-change-proposalA proposal to add or alter unstable APIs in the standard libraries

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions