Skip to content

std::process: fix UEFI ExitStatus::code() silent truncation of error …#153973

Open
devnexen wants to merge 2 commits intorust-lang:mainfrom
devnexen:uefi_code_fix
Open

std::process: fix UEFI ExitStatus::code() silent truncation of error …#153973
devnexen wants to merge 2 commits intorust-lang:mainfrom
devnexen:uefi_code_fix

Conversation

@devnexen
Copy link
Copy Markdown
Contributor

…codes

UEFI status codes are usize-wide with the high bit set for errors (e.g. DEVICE_ERROR is 0x8000000000000007 on 64-bit). the previous as i32 cast silently dropped the upper bits, losing all error information. stripping the error high bit to extract the error number and negating it, so errors (negative) are distinguishable from success/warning codes (non-negative).

…codes

UEFI status codes are usize-wide with the high bit set for errors
(e.g. DEVICE_ERROR is 0x8000000000000007 on 64-bit). the previous
`as i32` cast silently dropped the upper bits, losing all error
information. stripping the error high bit to extract the error number
and negating it, so errors (negative) are distinguishable from
success/warning codes (non-negative).
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Mar 16, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Mar 16, 2026

r? @joboet

rustbot has assigned @joboet.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @ChrisDenton, libs
  • @ChrisDenton, libs expanded to 8 candidates
  • Random selection from Mark-Simulacrum, joboet

@joboet
Copy link
Copy Markdown
Member

joboet commented Mar 19, 2026

Wouldn't it make more sense to convert the error to the equivalent UEFI error on 32-bit platforms (by re-setting the highest bit) instead of negating?

@joboet
Copy link
Copy Markdown
Member

joboet commented Mar 28, 2026

Looks fine to me, though the ExitStatusError::code implementation seems like unnecessary duplication. I'm also nominating this for T-libs-api discussion, just in case...
@rustbot label +I-libs-api-nominated

@rustbot rustbot added the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label Mar 28, 2026
// (e.g. DEVICE_ERROR is 0x8000000000000007 on 64-bit).
// Strip the platform-width high bit and re-set bit 31 to
// produce the equivalent 32-bit UEFI error representation.
let err_num = code & !(0x80usize << (usize::BITS - 8));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let err_num = code & !(0x80usize << (usize::BITS - 8));
let err_num = code & !(1usize << (usize::BITS - 1));

if self.0.is_error() {
// same as ExitStatus::code(), stripping the platform-width
// high bit and re-setting bit 31 for the 32-bit equivalent.
let err_num = code & !(0x80usize << (usize::BITS - 8));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let err_num = code & !(0x80usize << (usize::BITS - 8));
let err_num = code & !(1usize << (usize::BITS - 1));

@joshtriplett
Copy link
Copy Markdown
Member

We talked about this in today's @rust-lang/libs-api meeting.

This seems like a reasonable change to make the truncated value somewhat more accurate. This may help make portable programs slightly more capable.

However, in a separate PR, we'd love to see an extension trait for ExitCodeExt and ExitStatusExt (the latter implemented for both ExitStatus and ExitStatusErr).

@nia-e nia-e removed the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label Apr 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants