Skip to content

std: allocate less memory in current_exe for OpenBSD#158183

Open
joboet wants to merge 2 commits into
rust-lang:mainfrom
joboet:openbsd_current_exe
Open

std: allocate less memory in current_exe for OpenBSD#158183
joboet wants to merge 2 commits into
rust-lang:mainfrom
joboet:openbsd_current_exe

Conversation

@joboet

@joboet joboet commented Jun 20, 2026

Copy link
Copy Markdown
Member

This bug was introduced back in 2f42ac4 when Alex ported the current_exe implementation from C to Rust. Vec::with_capacity measures capacity in the number of elements, but sysctl measures it in bytes, so we need to do some conversions.

CC @semarie

@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 Jun 20, 2026
@rustbot

rustbot commented Jun 20, 2026

Copy link
Copy Markdown
Collaborator

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
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 12 candidates
  • Random selection from Darksonn, Mark-Simulacrum, clarfonthey, jhpratt

@semarie

semarie commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

it seems a bit over engineered to me : sizeof(char) is 1 on all platforms that OpenBSD support.
it isn't a strong objection, but I would like to understand the rational.

@joboet

joboet commented Jun 20, 2026

Copy link
Copy Markdown
Member Author

Yes, but this isn't about an array of characters but of pointers, and size_of::<*mut c_char>() definitely isn't 1.

@@ -234,11 +234,17 @@ pub fn current_exe() -> io::Result<PathBuf> {
unsafe {
let mut mib = [libc::CTL_KERN, libc::KERN_PROC_ARGS, libc::getpid(), libc::KERN_PROC_ARGV];

@workingjubilee workingjubilee Jun 20, 2026

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.

Studying https://man.openbsd.org/sysctl.2#KERN_PROC_ARGS

...So uh is there a reason, since we have to make two sysctl calls, that we don't just ask for KERN_PROC_NARGV the first time?

...though "KERN_PROC_NARGV and KERN_PROC_NENV return the number of elements as an int in the argv or env array." has got to be the most curious phrasing possible for that.

View changes since the review

Comment thread library/std/src/sys/paths/unix.rs Outdated
let mut argv = Vec::<*const libc::c_char>::with_capacity(argv_len as usize);
// ... allocate a buffer for it ...
let mut argv =
Vec::<*const libc::c_char>::with_capacity(argv_len / size_of::<*const libc::c_char>());

@Mark-Simulacrum Mark-Simulacrum Jun 21, 2026

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.

Should we checked_div_exact and panic here just in case? I guess it seems a bit unlikely that this goes wrong (and would be a platform/libc bug anyway).

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, why not...

@Mark-Simulacrum

Copy link
Copy Markdown
Member

r=me if you're happy

Comment thread library/std/src/sys/paths/unix.rs Outdated

@joboet joboet Jun 21, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@semarie (since you added this in fcb30a0) do you remember why this checks for dots? Spawning ".my-executable" will still result in it being looked up in the PATH, which I'd have thought is the difference between the two branches here...

View changes since the review

@semarie semarie Jun 25, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think the original intent of the C code was to checking if argv0 was starting with "./"

        /* get realpath if possible */
        if ((argv[0] != NULL) && ((*argv[0] == '.') || (*argv[0] == '/')
				|| (strstr(argv[0], "/") != NULL)))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

but yeah, it doesn't seems right. checking if argv0 contains "/" should be enough

@joboet joboet force-pushed the openbsd_current_exe branch from 495d78c to db9b6a1 Compare June 25, 2026 12:49

@Mark-Simulacrum Mark-Simulacrum left a comment

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.

@rustbot label +I-libs-api-nominated

This changes the behavior of std::env::current_exe on OpenBSD to not attempt to canonicalize an argv[0] if it starts with .. Previously we would canonicalize both if it starts with . or if it contains / anywhere in the path, now only the latter is checked.

IMO this is eminently reasonable and we should make the change, but in theory it seems plausible that some program is broken by this. (I suspect it's more likely that some programs are fixed by it, but hard to say).

View changes since this review

}
let argv0 = CStr::from_ptr(argv[0]).to_bytes();
if argv0[0] == b'.' || argv0.iter().any(|b| *b == b'/') {
if argv0.iter().any(|b| *b == b'/') {

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.

I guess technically we should FCP this breakage, let me nominate for libs-api to decide.

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.

We discussed this in the @rust-lang/libs-api meeting and consider this a bug fix, not something that needs an FCP. There's no reason to check for a leading ., this doesn't determine whether a string represents a relative or absolute path.

@rustbot rustbot added the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label Jun 28, 2026
@Amanieu Amanieu removed the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label Jun 30, 2026
@Mark-Simulacrum

Copy link
Copy Markdown
Member

@bors r+ rollup

@rust-bors

rust-bors Bot commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

📌 Commit db9b6a1 has been approved by Mark-Simulacrum

It is now in the queue for this repository.

🌲 The tree is currently closed for pull requests below priority 1000. This pull request will be tested once the tree is reopened.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 4, 2026
jhpratt added a commit to jhpratt/rust that referenced this pull request Jul 5, 2026
…-Simulacrum

std: allocate less memory in `current_exe` for OpenBSD

This bug was introduced back in 2f42ac4 when Alex ported the `current_exe` implementation from C to Rust. `Vec::with_capacity` measures capacity in the number of elements, but `sysctl` measures it in bytes, so we need to do some conversions.

CC @semarie
jhpratt added a commit to jhpratt/rust that referenced this pull request Jul 5, 2026
…-Simulacrum

std: allocate less memory in `current_exe` for OpenBSD

This bug was introduced back in 2f42ac4 when Alex ported the `current_exe` implementation from C to Rust. `Vec::with_capacity` measures capacity in the number of elements, but `sysctl` measures it in bytes, so we need to do some conversions.

CC @semarie
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jul 5, 2026
…-Simulacrum

std: allocate less memory in `current_exe` for OpenBSD

This bug was introduced back in 2f42ac4 when Alex ported the `current_exe` implementation from C to Rust. `Vec::with_capacity` measures capacity in the number of elements, but `sysctl` measures it in bytes, so we need to do some conversions.

CC @semarie
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

6 participants