Debian Codegraph @

debian/missing-sources/rustix/src/backend/linux_raw/param/init.rs

raw
1 //! Linux auxv `init` function, for "use-explicitly-provided-auxv" mode.
2 //!
3 //! # Safety
4 //!
5 //! This uses raw pointers to locate and read the kernel-provided auxv array.
6 #![allow(unsafe_code)]
7
8 use crate::backend::c;
9 #[cfg(feature = "param")]
10 use crate::ffi::CStr;
11 use core::ffi::c_void;
12 use core::ptr::{null_mut, read, NonNull};
13 #[cfg(feature = "runtime")]
14 use core::sync::atomic::AtomicBool;
15 use core::sync::atomic::{AtomicPtr, AtomicUsize, Ordering};
16 use linux_raw_sys::auxvec::{
17 AT_CLKTCK, AT_EXECFN, AT_HWCAP, AT_HWCAP2, AT_MINSIGSTKSZ, AT_NULL, AT_PAGESZ, AT_SYSINFO_EHDR,
18 };
19 #[cfg(feature = "runtime")]
20 use linux_raw_sys::auxvec::{AT_ENTRY, AT_PHDR, AT_PHENT, AT_PHNUM, AT_RANDOM, AT_SECURE};
21 use linux_raw_sys::elf::*;
22
23 #[cfg(feature = "param")]
24 #[inline]
25 pub(crate) fn page_size() -> usize {
26 PAGE_SIZE.load(Ordering::Relaxed)
27 }
28
29 #[cfg(feature = "param")]
30 #[inline]
31 pub(crate) fn clock_ticks_per_second() -> u64 {
32 CLOCK_TICKS_PER_SECOND.load(Ordering::Relaxed) as u64
33 }
34
35 #[cfg(feature = "param")]
36 #[inline]
37 pub(crate) fn linux_hwcap() -> (usize, usize) {
38 (
39 HWCAP.load(Ordering::Relaxed),
40 HWCAP2.load(Ordering::Relaxed),
41 )
42 }
43
44 #[cfg(feature = "param")]
45 #[inline]
46 pub(crate) fn linux_minsigstksz() -> usize {
47 MINSIGSTKSZ.load(Ordering::Relaxed)
48 }
49
50 #[cfg(feature = "param")]
51 #[inline]
52 pub(crate) fn linux_execfn() -> &'static CStr {
53 let execfn = EXECFN.load(Ordering::Relaxed);
54
55 // SAFETY: We initialize `EXECFN` to a valid `CStr` pointer, and we assume
56 // the `AT_EXECFN` value provided by the kernel points to a valid C string.
57 unsafe { CStr::from_ptr(execfn.cast()) }
58 }
59
60 #[cfg(feature = "runtime")]
61 #[inline]
62 pub(crate) fn linux_secure() -> bool {
63 SECURE.load(Ordering::Relaxed)
64 }
65
66 #[cfg(feature = "runtime")]
67 #[inline]
68 pub(crate) fn exe_phdrs() -> (*const c_void, usize, usize) {
69 (
70 PHDR.load(Ordering::Relaxed).cast(),
71 PHENT.load(Ordering::Relaxed),
72 PHNUM.load(Ordering::Relaxed),
73 )
74 }
75
76 /// `AT_SYSINFO_EHDR` isn't present on all platforms in all configurations, so
77 /// if we don't see it, this function returns a null pointer.
78 #[inline]
79 pub(in super::super) fn sysinfo_ehdr() -> *const Elf_Ehdr {
80 SYSINFO_EHDR.load(Ordering::Relaxed)
81 }
82
83 #[cfg(feature = "runtime")]
84 #[inline]
85 pub(crate) fn entry() -> usize {
86 ENTRY.load(Ordering::Relaxed)
87 }
88
89 #[cfg(feature = "runtime")]
90 #[inline]
91 pub(crate) fn random() -> *const [u8; 16] {
92 RANDOM.load(Ordering::Relaxed)
93 }
94
95 static PAGE_SIZE: AtomicUsize = AtomicUsize::new(0);
96 static CLOCK_TICKS_PER_SECOND: AtomicUsize = AtomicUsize::new(0);
97 static HWCAP: AtomicUsize = AtomicUsize::new(0);
98 static HWCAP2: AtomicUsize = AtomicUsize::new(0);
99 static MINSIGSTKSZ: AtomicUsize = AtomicUsize::new(0);
100 static SYSINFO_EHDR: AtomicPtr<Elf_Ehdr> = AtomicPtr::new(null_mut());
101 // Initialize `EXECFN` to a valid `CStr` pointer so that we don't need to check
102 // for null on every `execfn` call.
103 static EXECFN: AtomicPtr<c::c_char> = AtomicPtr::new(b"\0".as_ptr() as _);
104 #[cfg(feature = "runtime")]
105 static SECURE: AtomicBool = AtomicBool::new(false);
106 // Use `dangling` so that we can always treat it like an empty slice.
107 #[cfg(feature = "runtime")]
108 static PHDR: AtomicPtr<Elf_Phdr> = AtomicPtr::new(NonNull::dangling().as_ptr());
109 #[cfg(feature = "runtime")]
110 static PHENT: AtomicUsize = AtomicUsize::new(0);
111 #[cfg(feature = "runtime")]
112 static PHNUM: AtomicUsize = AtomicUsize::new(0);
113 #[cfg(feature = "runtime")]
114 static ENTRY: AtomicUsize = AtomicUsize::new(0);
115 #[cfg(feature = "runtime")]
116 static RANDOM: AtomicPtr<[u8; 16]> = AtomicPtr::new(NonNull::dangling().as_ptr());
117
118 /// When "use-explicitly-provided-auxv" is enabled, we export a function to be
119 /// called during initialization, and passed a pointer to the original
120 /// environment variable block set up by the OS.
121 pub(crate) unsafe fn init(envp: *mut *mut u8) {
122 init_from_envp(envp);
123 }
124
125 /// # Safety
126 ///
127 /// This must be passed a pointer to the environment variable buffer
128 /// provided by the kernel, which is followed in memory by the auxv array.
129 unsafe fn init_from_envp(mut envp: *mut *mut u8) {
130 while !(*envp).is_null() {
131 envp = envp.add(1);
132 }
133 init_from_auxp(envp.add(1).cast())
134 }
135
136 /// Process auxv entries from the auxv array pointed to by `auxp`.
137 ///
138 /// # Safety
139 ///
140 /// This must be passed a pointer to an auxv array.
141 ///
142 /// The buffer contains `Elf_aux_t` elements, though it need not be aligned;
143 /// function uses `read_unaligned` to read from it.
144 unsafe fn init_from_auxp(mut auxp: *const Elf_auxv_t) {
145 loop {
146 let Elf_auxv_t { a_type, a_val } = read(auxp);
147
148 match a_type as _ {
149 AT_PAGESZ => PAGE_SIZE.store(a_val as usize, Ordering::Relaxed),
150 AT_CLKTCK => CLOCK_TICKS_PER_SECOND.store(a_val as usize, Ordering::Relaxed),
151 AT_HWCAP => HWCAP.store(a_val as usize, Ordering::Relaxed),
152 AT_HWCAP2 => HWCAP2.store(a_val as usize, Ordering::Relaxed),
153 AT_MINSIGSTKSZ => MINSIGSTKSZ.store(a_val as usize, Ordering::Relaxed),
154 AT_EXECFN => EXECFN.store(a_val.cast::<c::c_char>(), Ordering::Relaxed),
155 AT_SYSINFO_EHDR => SYSINFO_EHDR.store(a_val.cast::<Elf_Ehdr>(), Ordering::Relaxed),
156
157 #[cfg(feature = "runtime")]
158 AT_SECURE => SECURE.store(a_val as usize != 0, Ordering::Relaxed),
159 #[cfg(feature = "runtime")]
160 AT_PHDR => PHDR.store(a_val.cast::<Elf_Phdr>(), Ordering::Relaxed),
161 #[cfg(feature = "runtime")]
162 AT_PHNUM => PHNUM.store(a_val as usize, Ordering::Relaxed),
163 #[cfg(feature = "runtime")]
164 AT_PHENT => PHENT.store(a_val as usize, Ordering::Relaxed),
165 #[cfg(feature = "runtime")]
166 AT_ENTRY => ENTRY.store(a_val as usize, Ordering::Relaxed),
167 #[cfg(feature = "runtime")]
168 AT_RANDOM => RANDOM.store(a_val.cast::<[u8; 16]>(), Ordering::Relaxed),
169
170 AT_NULL => break,
171 _ => (),
172 }
173 auxp = auxp.add(1);
174 }
175 }