Skip to content
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

Allow const fn pointers to arbitrary memory address #117250

Open
wprzytula opened this issue Oct 26, 2023 · 2 comments
Open

Allow const fn pointers to arbitrary memory address #117250

wprzytula opened this issue Oct 26, 2023 · 2 comments
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@wprzytula
Copy link

Code

const FOO: unsafe extern "C" fn() = unsafe { core::mem::transmute(0x2123210 as *const ())};

fn main() {}

Current output

error[E0080]: it is undefined behavior to use this value
 --> src/main.rs:1:1
  |
1 | const FOO: unsafe extern "C" fn() = unsafe { core::mem::transmute(0x2123210 as *const ())};
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered 0x2123210[noalloc], but expected a function pointer
  |
  = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
  = note: the raw bytes of the constant (size: 8, align: 8) {
              10 32 12 02 00 00 00 00                         │ .2......
          }

Desired output

successful build

Rationale and extra context

I'm developing a library for embedded environment. The microcontroller I'm working with features ROM filled with preflashed code.
I'd like to create a listing of the functions there, similarly to what the C library does, and put them as consts. This is arguably not UB at all, because it's absolutely certain that such functions exist at given addresses.

Other cases

No response

Anything else?

No response

@wprzytula wprzytula added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 26, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 26, 2023
@Jules-Bertholet
Copy link
Contributor

@rustbot label c-bug a-const-eval

@rustbot rustbot added A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) C-bug Category: This is a bug. labels Oct 26, 2023
@oli-obk
Copy link
Contributor

oli-obk commented Oct 26, 2023

This is basically the same as #63197, except that in contrast to references that can be dereferenced, const eval cannot call function pointers, so technically any nonzero integer could be fine without causing problems.

cc @rust-lang/wg-const-eval

@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants