Skip to content

Commit 6a4aa9a

Browse files
authored
Merge pull request rust-lang#4052 from devnexen/gh4050
sysconf interception fix for solarish systems.
2 parents fa93f35 + 004edb2 commit 6a4aa9a

File tree

8 files changed

+90
-30
lines changed

8 files changed

+90
-30
lines changed

src/tools/miri/ci/ci.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ case $HOST_TARGET in
153153
TEST_TARGET=x86_64-unknown-freebsd run_tests_minimal $BASIC $UNIX time hashmap random threadname pthread fs libc-pipe
154154
TEST_TARGET=i686-unknown-freebsd run_tests_minimal $BASIC $UNIX time hashmap random threadname pthread fs libc-pipe
155155
TEST_TARGET=x86_64-unknown-illumos run_tests_minimal $BASIC $UNIX time hashmap random thread sync available-parallelism tls libc-pipe
156-
# TEST_TARGET=x86_64-pc-solaris run_tests_minimal $BASIC $UNIX time hashmap random thread sync available-parallelism tls libc-pipe
156+
TEST_TARGET=x86_64-pc-solaris run_tests_minimal $BASIC $UNIX time hashmap random thread sync available-parallelism tls libc-pipe
157157
TEST_TARGET=aarch64-linux-android run_tests_minimal $BASIC $UNIX time hashmap random sync threadname pthread
158158
TEST_TARGET=wasm32-wasip2 run_tests_minimal $BASIC wasm
159159
TEST_TARGET=wasm32-unknown-unknown run_tests_minimal no_std empty_main wasm # this target doesn't really have std

src/tools/miri/src/shims/unix/android/foreign_items.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use rustc_abi::ExternAbi;
22
use rustc_span::Symbol;
33

44
use crate::shims::unix::android::thread::prctl;
5+
use crate::shims::unix::foreign_items::EvalContextExt as _;
56
use crate::shims::unix::linux::syscall::syscall;
67
use crate::*;
78

@@ -20,6 +21,14 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
2021
) -> InterpResult<'tcx, EmulateItemResult> {
2122
let this = self.eval_context_mut();
2223
match link_name.as_str() {
24+
// Querying system information
25+
"sysconf" => {
26+
let [val] =
27+
this.check_shim(abi, ExternAbi::C { unwind: false }, link_name, args)?;
28+
let result = this.sysconf(val)?;
29+
this.write_scalar(result, dest)?;
30+
}
31+
2332
// Miscellaneous
2433
"__errno" => {
2534
let [] = this.check_shim(abi, ExternAbi::C { unwind: false }, link_name, args)?;

src/tools/miri/src/shims/unix/foreign_items.rs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,35 @@ pub fn is_dyn_sym(name: &str, target_os: &str) -> bool {
3939

4040
impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
4141
pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
42+
// Querying system information
43+
fn sysconf(&mut self, val: &OpTy<'tcx>) -> InterpResult<'tcx, Scalar> {
44+
let this = self.eval_context_mut();
45+
46+
let name = this.read_scalar(val)?.to_i32()?;
47+
// FIXME: Which of these are POSIX, and which are GNU/Linux?
48+
// At least the names seem to all also exist on macOS.
49+
let sysconfs: &[(&str, fn(&MiriInterpCx<'_>) -> Scalar)] = &[
50+
("_SC_PAGESIZE", |this| Scalar::from_int(this.machine.page_size, this.pointer_size())),
51+
("_SC_PAGE_SIZE", |this| Scalar::from_int(this.machine.page_size, this.pointer_size())),
52+
("_SC_NPROCESSORS_CONF", |this| {
53+
Scalar::from_int(this.machine.num_cpus, this.pointer_size())
54+
}),
55+
("_SC_NPROCESSORS_ONLN", |this| {
56+
Scalar::from_int(this.machine.num_cpus, this.pointer_size())
57+
}),
58+
// 512 seems to be a reasonable default. The value is not critical, in
59+
// the sense that getpwuid_r takes and checks the buffer length.
60+
("_SC_GETPW_R_SIZE_MAX", |this| Scalar::from_int(512, this.pointer_size())),
61+
];
62+
for &(sysconf_name, value) in sysconfs {
63+
let sysconf_name = this.eval_libc_i32(sysconf_name);
64+
if sysconf_name == name {
65+
return interp_ok(value(this));
66+
}
67+
}
68+
throw_unsup_format!("unimplemented sysconf name: {}", name)
69+
}
70+
4271
fn emulate_foreign_item_inner(
4372
&mut self,
4473
link_name: Symbol,
@@ -393,35 +422,6 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
393422
}
394423
}
395424

396-
// Querying system information
397-
"sysconf" => {
398-
let [name] = this.check_shim(abi, ExternAbi::C { unwind: false }, link_name, args)?;
399-
let name = this.read_scalar(name)?.to_i32()?;
400-
// FIXME: Which of these are POSIX, and which are GNU/Linux?
401-
// At least the names seem to all also exist on macOS.
402-
let sysconfs: &[(&str, fn(&MiriInterpCx<'_>) -> Scalar)] = &[
403-
("_SC_PAGESIZE", |this| Scalar::from_int(this.machine.page_size, this.pointer_size())),
404-
("_SC_NPROCESSORS_CONF", |this| Scalar::from_int(this.machine.num_cpus, this.pointer_size())),
405-
("_SC_NPROCESSORS_ONLN", |this| Scalar::from_int(this.machine.num_cpus, this.pointer_size())),
406-
// 512 seems to be a reasonable default. The value is not critical, in
407-
// the sense that getpwuid_r takes and checks the buffer length.
408-
("_SC_GETPW_R_SIZE_MAX", |this| Scalar::from_int(512, this.pointer_size()))
409-
];
410-
let mut result = None;
411-
for &(sysconf_name, value) in sysconfs {
412-
let sysconf_name = this.eval_libc_i32(sysconf_name);
413-
if sysconf_name == name {
414-
result = Some(value(this));
415-
break;
416-
}
417-
}
418-
if let Some(result) = result {
419-
this.write_scalar(result, dest)?;
420-
} else {
421-
throw_unsup_format!("unimplemented sysconf name: {}", name)
422-
}
423-
}
424-
425425
// Thread-local storage
426426
"pthread_key_create" => {
427427
let [key, dtor] = this.check_shim(abi, ExternAbi::C { unwind: false }, link_name, args)?;

src/tools/miri/src/shims/unix/freebsd/foreign_items.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use rustc_abi::ExternAbi;
22
use rustc_span::Symbol;
33

4+
use crate::shims::unix::foreign_items::EvalContextExt as _;
45
use crate::shims::unix::*;
56
use crate::*;
67

@@ -75,6 +76,14 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
7576
this.write_scalar(result, dest)?;
7677
}
7778

79+
// Querying system information
80+
"sysconf" => {
81+
let [val] =
82+
this.check_shim(abi, ExternAbi::C { unwind: false }, link_name, args)?;
83+
let result = this.sysconf(val)?;
84+
this.write_scalar(result, dest)?;
85+
}
86+
7887
// Miscellaneous
7988
"__error" => {
8089
let [] = this.check_shim(abi, ExternAbi::C { unwind: false }, link_name, args)?;

src/tools/miri/src/shims/unix/linux/foreign_items.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use self::shims::unix::linux::eventfd::EvalContextExt as _;
66
use self::shims::unix::linux::mem::EvalContextExt as _;
77
use self::shims::unix::linux::syscall::syscall;
88
use crate::machine::{SIGRTMAX, SIGRTMIN};
9+
use crate::shims::unix::foreign_items::EvalContextExt as _;
910
use crate::shims::unix::*;
1011
use crate::*;
1112

@@ -124,6 +125,14 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
124125
this.write_scalar(result, dest)?;
125126
}
126127

128+
// Querying system information
129+
"sysconf" => {
130+
let [val] =
131+
this.check_shim(abi, ExternAbi::C { unwind: false }, link_name, args)?;
132+
let result = this.sysconf(val)?;
133+
this.write_scalar(result, dest)?;
134+
}
135+
127136
// Dynamically invoked syscalls
128137
"syscall" => {
129138
syscall(this, link_name, abi, args, dest)?;

src/tools/miri/src/shims/unix/macos/foreign_items.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use rustc_abi::ExternAbi;
22
use rustc_span::Symbol;
33

44
use super::sync::EvalContextExt as _;
5+
use crate::shims::unix::foreign_items::EvalContextExt as _;
56
use crate::shims::unix::*;
67
use crate::*;
78

@@ -167,6 +168,13 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
167168
this.write_scalar(stack_size, dest)?;
168169
}
169170

171+
"sysconf" => {
172+
let [val] =
173+
this.check_shim(abi, ExternAbi::C { unwind: false }, link_name, args)?;
174+
let result = this.sysconf(val)?;
175+
this.write_scalar(result, dest)?;
176+
}
177+
170178
// Threading
171179
"pthread_setname_np" => {
172180
let [name] =

src/tools/miri/src/shims/unix/solarish/foreign_items.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use rustc_abi::ExternAbi;
22
use rustc_span::Symbol;
33

4+
use crate::shims::unix::foreign_items::EvalContextExt as _;
45
use crate::shims::unix::*;
56
use crate::*;
67

@@ -112,6 +113,13 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
112113
this.write_null(dest)?;
113114
}
114115

116+
"sysconf" | "__sysconf_xpg7" => {
117+
let [val] =
118+
this.check_shim(abi, ExternAbi::C { unwind: false }, link_name, args)?;
119+
let result = this.sysconf(val)?;
120+
this.write_scalar(result, dest)?;
121+
}
122+
115123
_ => return interp_ok(EmulateItemResult::NotSupported),
116124
}
117125
interp_ok(EmulateItemResult::NeedsReturn)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//@ignore-target: windows # Supported only on unixes
2+
3+
fn test_sysconfbasic() {
4+
unsafe {
5+
let ncpus = libc::sysconf(libc::_SC_NPROCESSORS_CONF);
6+
assert!(ncpus >= 1);
7+
let psz = libc::sysconf(libc::_SC_PAGESIZE);
8+
assert!(psz % 4096 == 0);
9+
// note that in reality it can return -1 (no hard limit) on some platforms.
10+
let gwmax = libc::sysconf(libc::_SC_GETPW_R_SIZE_MAX);
11+
assert!(gwmax >= 512);
12+
}
13+
}
14+
15+
fn main() {
16+
test_sysconfbasic();
17+
}

0 commit comments

Comments
 (0)