Skip to content

Commit 4282450

Browse files
committed
Auto merge of rust-lang#2306 - RalfJung:unix, r=RalfJung
make some things available for all Unixes
2 parents c17e68d + 5ba2c1e commit 4282450

File tree

4 files changed

+46
-41
lines changed

4 files changed

+46
-41
lines changed

src/shims/unix/foreign_items.rs

+27-2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
6262
let result = this.open(args)?;
6363
this.write_scalar(Scalar::from_i32(result), dest)?;
6464
}
65+
"close" => {
66+
let [fd] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
67+
let result = this.close(fd)?;
68+
this.write_scalar(Scalar::from_i32(result), dest)?;
69+
}
6570
"fcntl" => {
6671
// `fcntl` is variadic. The argument count is checked based on the first argument
6772
// in `this.fcntl()`, so we do not use `check_shim` here.
@@ -112,17 +117,27 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
112117
let result = this.rmdir(path)?;
113118
this.write_scalar(Scalar::from_i32(result), dest)?;
114119
}
120+
"opendir" => {
121+
let [name] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
122+
let result = this.opendir(name)?;
123+
this.write_scalar(result, dest)?;
124+
}
115125
"closedir" => {
116126
let [dirp] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
117127
let result = this.closedir(dirp)?;
118128
this.write_scalar(Scalar::from_i32(result), dest)?;
119129
}
120-
"lseek" | "lseek64" => {
130+
"lseek64" => {
121131
let [fd, offset, whence] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
122132
let result = this.lseek64(fd, offset, whence)?;
123-
// "lseek" is only used on macOS which is 64bit-only, so `i64` always works.
124133
this.write_scalar(Scalar::from_i64(result), dest)?;
125134
}
135+
"ftruncate64" => {
136+
let [fd, length] =
137+
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
138+
let result = this.ftruncate64(fd, length)?;
139+
this.write_scalar(Scalar::from_i32(result), dest)?;
140+
}
126141
"fsync" => {
127142
let [fd] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
128143
let result = this.fsync(fd)?;
@@ -138,6 +153,16 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
138153
let result = this.readlink(pathname, buf, bufsize)?;
139154
this.write_scalar(Scalar::from_machine_isize(result, this), dest)?;
140155
}
156+
"posix_fadvise" => {
157+
let [fd, offset, len, advice] =
158+
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
159+
this.read_scalar(fd)?.to_i32()?;
160+
this.read_scalar(offset)?.to_machine_isize(this)?;
161+
this.read_scalar(len)?.to_machine_isize(this)?;
162+
this.read_scalar(advice)?.to_i32()?;
163+
// fadvise is only informational, we can ignore it.
164+
this.write_null(dest)?;
165+
}
141166

142167
// Time related shims
143168
"gettimeofday" => {

src/shims/unix/linux/foreign_items.rs

-28
Original file line numberDiff line numberDiff line change
@@ -30,40 +30,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
3030
}
3131

3232
// File related shims (but also see "syscall" below for statx)
33-
// These symbols have different names on Linux and macOS, which is the only reason they are not
34-
// in the `posix` module.
35-
"close" => {
36-
let [fd] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
37-
let result = this.close(fd)?;
38-
this.write_scalar(Scalar::from_i32(result), dest)?;
39-
}
40-
"opendir" => {
41-
let [name] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
42-
let result = this.opendir(name)?;
43-
this.write_scalar(result, dest)?;
44-
}
4533
"readdir64" => {
4634
let [dirp] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
4735
let result = this.linux_readdir64(dirp)?;
4836
this.write_scalar(result, dest)?;
4937
}
50-
"ftruncate64" => {
51-
let [fd, length] =
52-
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
53-
let result = this.ftruncate64(fd, length)?;
54-
this.write_scalar(Scalar::from_i32(result), dest)?;
55-
}
5638
// Linux-only
57-
"posix_fadvise" => {
58-
let [fd, offset, len, advice] =
59-
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
60-
this.read_scalar(fd)?.to_i32()?;
61-
this.read_scalar(offset)?.to_machine_isize(this)?;
62-
this.read_scalar(len)?.to_machine_isize(this)?;
63-
this.read_scalar(advice)?.to_i32()?;
64-
// fadvise is only informational, we can ignore it.
65-
this.write_null(dest)?;
66-
}
6739
"sync_file_range" => {
6840
let [fd, offset, nbytes, flags] =
6941
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;

src/shims/unix/macos/foreign_items.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
2828
}
2929

3030
// File related shims
31-
"close" | "close$NOCANCEL" => {
31+
"close$NOCANCEL" => {
3232
let [result] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
3333
let result = this.close(result)?;
3434
this.write_scalar(Scalar::from_i32(result), dest)?;
@@ -50,7 +50,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
5050
let result = this.macos_fstat(fd, buf)?;
5151
this.write_scalar(Scalar::from_i32(result), dest)?;
5252
}
53-
"opendir" | "opendir$INODE64" => {
53+
"opendir$INODE64" => {
5454
let [name] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
5555
let result = this.opendir(name)?;
5656
this.write_scalar(result, dest)?;
@@ -61,9 +61,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
6161
let result = this.macos_readdir_r(dirp, entry, result)?;
6262
this.write_scalar(Scalar::from_i32(result), dest)?;
6363
}
64+
"lseek" => {
65+
let [fd, offset, whence] =
66+
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
67+
// macOS is 64bit-only, so this is lseek64
68+
let result = this.lseek64(fd, offset, whence)?;
69+
this.write_scalar(Scalar::from_i64(result), dest)?;
70+
}
6471
"ftruncate" => {
6572
let [fd, length] =
6673
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
74+
// macOS is 64bit-only, so this is ftruncate64
6775
let result = this.ftruncate64(fd, length)?;
6876
this.write_scalar(Scalar::from_i32(result), dest)?;
6977
}

tests/pass/libc.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fn test_posix_fadvise() {
4242
assert_eq!(result, 0);
4343
}
4444

45-
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
45+
#[cfg(any(target_os = "linux"))]
4646
fn test_sync_file_range() {
4747
use std::fs::{remove_file, File};
4848
use std::io::Write;
@@ -208,7 +208,7 @@ fn test_rwlock_libc_static_initializer() {
208208
/// Test whether the `prctl` shim correctly sets the thread name.
209209
///
210210
/// Note: `prctl` exists only on Linux.
211-
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
211+
#[cfg(any(target_os = "linux"))]
212212
fn test_prctl_thread_name() {
213213
use libc::c_long;
214214
use std::ffi::CString;
@@ -259,9 +259,9 @@ fn test_prctl_thread_name() {
259259

260260
/// Tests whether each thread has its own `__errno_location`.
261261
fn test_thread_local_errno() {
262-
#[cfg(not(target_os = "macos"))]
262+
#[cfg(target_os = "linux")]
263263
use libc::__errno_location;
264-
#[cfg(target_os = "macos")]
264+
#[cfg(any(target_os = "macos", target_os = "freebsd"))]
265265
use libc::__error as __errno_location;
266266

267267
unsafe {
@@ -278,7 +278,7 @@ fn test_thread_local_errno() {
278278
}
279279

280280
/// Tests whether clock support exists at all
281-
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
281+
#[cfg(any(target_os = "linux"))]
282282
fn test_clocks() {
283283
let mut tp = std::mem::MaybeUninit::<libc::timespec>::uninit();
284284
let is_error = unsafe { libc::clock_gettime(libc::CLOCK_REALTIME, tp.as_mut_ptr()) };
@@ -317,22 +317,22 @@ fn main() {
317317

318318
test_posix_gettimeofday();
319319

320-
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
320+
#[cfg(any(target_os = "linux"))]
321321
test_sync_file_range();
322322

323323
test_mutex_libc_init_recursive();
324324
test_mutex_libc_init_normal();
325325
test_mutex_libc_init_errorcheck();
326326
test_rwlock_libc_static_initializer();
327327

328-
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
328+
#[cfg(any(target_os = "linux"))]
329329
test_mutex_libc_static_initializer_recursive();
330330

331-
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
331+
#[cfg(any(target_os = "linux"))]
332332
test_prctl_thread_name();
333333

334334
test_thread_local_errno();
335335

336-
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
336+
#[cfg(any(target_os = "linux"))]
337337
test_clocks();
338338
}

0 commit comments

Comments
 (0)