Skip to content

Commit cac48dd

Browse files
committed
treat prctl like a variadic function
1 parent 59ee672 commit cac48dd

File tree

5 files changed

+30
-32
lines changed

5 files changed

+30
-32
lines changed

src/shims/posix/fs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
921921
dirfd_op: &OpTy<'tcx, Tag>, // Should be an `int`
922922
pathname_op: &OpTy<'tcx, Tag>, // Should be a `const char *`
923923
flags_op: &OpTy<'tcx, Tag>, // Should be an `int`
924-
mask_op: &OpTy<'tcx, Tag>, // Should be an `unsigned int`
924+
mask_op: &OpTy<'tcx, Tag>, // Should be an `unsigned int`
925925
statxbuf_op: &OpTy<'tcx, Tag>, // Should be a `struct statx *`
926926
) -> InterpResult<'tcx, i32> {
927927
let this = self.eval_context_mut();

src/shims/posix/linux/foreign_items.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
106106

107107
// Threading
108108
"prctl" => {
109-
let &[ref option, ref arg2, ref arg3, ref arg4, ref arg5] =
110-
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
111-
let result = this.prctl(option, arg2, arg3, arg4, arg5)?;
109+
// prctl is variadic. (It is not documented like that in the manpage, but defined like that in the libc crate.)
110+
this.check_abi_and_shim_symbol_clash(abi, Abi::C { unwind: false }, link_name)?;
111+
let result = this.prctl(args)?;
112112
this.write_scalar(Scalar::from_i32(result), dest)?;
113113
}
114114
"pthread_condattr_setclock" => {

src/shims/posix/thread.rs

+25-11
Original file line numberDiff line numberDiff line change
@@ -97,28 +97,42 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
9797
this.write_scalar(Scalar::from_uint(thread_id.to_u32(), dest.layout.size), dest)
9898
}
9999

100-
fn prctl(
101-
&mut self,
102-
option: &OpTy<'tcx, Tag>,
103-
arg2: &OpTy<'tcx, Tag>,
104-
_arg3: &OpTy<'tcx, Tag>,
105-
_arg4: &OpTy<'tcx, Tag>,
106-
_arg5: &OpTy<'tcx, Tag>,
107-
) -> InterpResult<'tcx, i32> {
100+
fn prctl(&mut self, args: &[OpTy<'tcx, Tag>]) -> InterpResult<'tcx, i32> {
108101
let this = self.eval_context_mut();
109102
this.assert_target_os("linux", "prctl");
110103

111-
let option = this.read_scalar(option)?.to_i32()?;
104+
if args.len() < 1 {
105+
throw_ub_format!(
106+
"incorrect number of arguments for `prctl`: got {}, expected at least 1",
107+
args.len()
108+
);
109+
}
110+
111+
let option = this.read_scalar(&args[0])?.to_i32()?;
112112
if option == this.eval_libc_i32("PR_SET_NAME")? {
113-
let address = this.read_pointer(arg2)?;
113+
if args.len() < 2 {
114+
throw_ub_format!(
115+
"incorrect number of arguments for `prctl` with `PR_SET_NAME`: got {}, expected at least 2",
116+
args.len()
117+
);
118+
}
119+
120+
let address = this.read_pointer(&args[1])?;
114121
let mut name = this.read_c_str(address)?.to_owned();
115122
// The name should be no more than 16 bytes, including the null
116123
// byte. Since `read_c_str` returns the string without the null
117124
// byte, we need to truncate to 15.
118125
name.truncate(15);
119126
this.set_active_thread_name(name);
120127
} else if option == this.eval_libc_i32("PR_GET_NAME")? {
121-
let address = this.read_pointer(arg2)?;
128+
if args.len() < 2 {
129+
throw_ub_format!(
130+
"incorrect number of arguments for `prctl` with `PR_SET_NAME`: got {}, expected at least 2",
131+
args.len()
132+
);
133+
}
134+
135+
let address = this.read_pointer(&args[1])?;
122136
let mut name = this.get_active_thread_name().to_vec();
123137
name.push(0u8);
124138
assert!(name.len() <= 16);

tests/compile-fail/fs/unix_open_missing_required_mode.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ fn main() {
1212
fn test_file_open_missing_needed_mode() {
1313
let name = b"missing_arg.txt\0";
1414
let name_ptr = name.as_ptr().cast::<libc::c_char>();
15-
let _fd = unsafe { libc::open(name_ptr, libc::O_CREAT) }; //~ ERROR Undefined Behavior: incorrect number of arguments for `open` with `O_CREAT`: got 2, expected 3
15+
let _fd = unsafe { libc::open(name_ptr, libc::O_CREAT) }; //~ ERROR Undefined Behavior: incorrect number of arguments for `open` with `O_CREAT`: got 2, expected at least 3
1616
}

tests/compile-fail/fs/unix_open_too_many_args.rs

-16
This file was deleted.

0 commit comments

Comments
 (0)