Skip to content

Commit 12fbabd

Browse files
committedAug 1, 2021
Do not call getpid wrapper after fork in tests
The test calls libc::getpid() in the pre_exec hook and asserts that the returned value is different from the PID of the parent. However, libc::getpid() returns the wrong value. Before version 2.25, glibc caches the PID of the current process with the goal of avoiding additional syscalls. The cached value is only updated when the wrapper functions for fork or clone are called. In PR rust-lang#81825 we switch to directly using the clone3 syscall. Thus, the cache is not updated and getpid returns the PID of the parent. source: https://man7.org/linux/man-pages/man2/getpid.2.html#NOTES
1 parent c3321d3 commit 12fbabd

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed
 

‎src/test/ui/command/command-pre-exec.rs

+19-6
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,30 @@
88
// ignore-sgx no processes
99
#![feature(process_exec, rustc_private)]
1010

11-
extern crate libc;
12-
1311
use std::env;
1412
use std::io::Error;
1513
use std::os::unix::process::CommandExt;
1614
use std::process::Command;
1715
use std::sync::atomic::{AtomicUsize, Ordering};
1816
use std::sync::Arc;
1917

18+
#[cfg(not(target_os = "linux"))]
19+
fn getpid() -> u32 {
20+
use std::process;
21+
process::id()
22+
}
23+
24+
/// We need to directly use the getpid syscall instead of using `process::id()`
25+
/// because the libc wrapper might return incorrect values after a process was
26+
/// forked.
27+
#[cfg(target_os = "linux")]
28+
fn getpid() -> u32 {
29+
extern crate libc;
30+
unsafe {
31+
libc::syscall(libc::SYS_getpid) as _
32+
}
33+
}
34+
2035
fn main() {
2136
if let Some(arg) = env::args().nth(1) {
2237
match &arg[..] {
@@ -68,14 +83,12 @@ fn main() {
6883
};
6984
assert_eq!(output.raw_os_error(), Some(102));
7085

71-
let pid = unsafe { libc::getpid() };
72-
assert!(pid >= 0);
86+
let pid = getpid();
7387
let output = unsafe {
7488
Command::new(&me)
7589
.arg("empty")
7690
.pre_exec(move || {
77-
let child = libc::getpid();
78-
assert!(child >= 0);
91+
let child = getpid();
7992
assert!(pid != child);
8093
Ok(())
8194
})

‎src/test/ui/process/process-panic-after-fork.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@ use std::sync::atomic::{AtomicU32, Ordering};
2323

2424
use libc::c_int;
2525

26+
#[cfg(not(target_os = "linux"))]
27+
fn getpid() -> u32 {
28+
process::id()
29+
}
30+
31+
/// We need to directly use the getpid syscall instead of using `process::id()`
32+
/// because the libc wrapper might return incorrect values after a process was
33+
/// forked.
34+
#[cfg(target_os = "linux")]
35+
fn getpid() -> u32 {
36+
unsafe {
37+
libc::syscall(libc::SYS_getpid) as _
38+
}
39+
}
40+
2641
/// This stunt allocator allows us to spot heap allocations in the child.
2742
struct PidChecking<A> {
2843
parent: A,
@@ -44,7 +59,7 @@ impl<A> PidChecking<A> {
4459
fn check(&self) {
4560
let require_pid = self.require_pid.load(Ordering::Acquire);
4661
if require_pid != 0 {
47-
let actual_pid = process::id();
62+
let actual_pid = getpid();
4863
if require_pid != actual_pid {
4964
unsafe {
5065
libc::raise(libc::SIGUSR1);

0 commit comments

Comments
 (0)
Please sign in to comment.