Skip to content

Commit

Permalink
Switch from .init_array constructors to /proc/self/auxv.
Browse files Browse the repository at this point in the history
In the linux_raw backend, switch from using a `.init_array` constructor
for obtaining the aux values to reading them from /proc/self/auxv. This avoids
problems in situation where other Rust code can run before the constructor,
potentially distrupting the `__environ` value.

Also, for the linux_raw backend, introduce a new "use-libc-auxv" feature,
which enables use of libc to read the aux values, instead of reading
them from /proc/self/auxv.

The "use-libc-auxv" option is enabled by default, because it's more
efficient and doesn't depend on /proc, so it's likely better for most
users.

Mustang, for its part, continues to be able to use the incoming auxv on
the stack because it controls program startup. Since it doesn't have to
worry about the hazards of /proc or QEMU, it can trust the incoming
values, and do less checking.

Fixes #382.
  • Loading branch information
sunfishcode committed Jul 26, 2022
1 parent 7dd900a commit 3357f57
Show file tree
Hide file tree
Showing 10 changed files with 683 additions and 360 deletions.
13 changes: 12 additions & 1 deletion Cargo.toml
Expand Up @@ -100,7 +100,13 @@ targets = [
]

[features]
default = ["std"]

# By default, use `std` and use libc for aux values.
#
# It turns out to be bizarrely awkward to obtain the aux values reliably and
# efficiently on Linux from anywhere other than libc. We can do it, but most
# users are better served by just using libc for this.
default = ["std", "use-libc-auxv"]

# This enables use of std. Disabling this enables `#![no_std], and requires
# nightly Rust.
Expand Down Expand Up @@ -170,6 +176,11 @@ all-apis = [
"time",
]

# When using the linux_raw backend, and not using Mustang, should we use libc
# for reading the aux vectors, instead of reading them ourselves from
# /proc/self/auxv?
use-libc-auxv = ["libc"]

# Expose io-lifetimes' features for third-party crate impls.
async-std = ["io-lifetimes/async-std"]
tokio = ["io-lifetimes/tokio"]
Expand Down
4 changes: 4 additions & 0 deletions src/backend/linux_raw/elf.rs
@@ -1,6 +1,10 @@
//! The ELF ABI.

#![allow(non_snake_case)]
#![cfg_attr(
all(not(target_vendor = "mustang"), feature = "use-libc-auxv"),
allow(dead_code)
)]

pub(super) const SELFMAG: usize = 4;
pub(super) const ELFMAG: [u8; SELFMAG] = [0x7f, b'E', b'L', b'F'];
Expand Down

0 comments on commit 3357f57

Please sign in to comment.