Skip to content

Commit 75e6549

Browse files
author
Vytautas Astrauskas
committed
Improve prctl, add a test.
1 parent eab38df commit 75e6549

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

src/shims/thread.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
102102
let this = self.eval_context_mut();
103103

104104
let option = this.read_scalar(option)?.not_undef()?.to_i32()?;
105-
if option != this.eval_libc_i32("PR_SET_NAME")? {
106-
throw_unsup_format!("Miri supports only PR_SET_NAME");
105+
if option == this.eval_libc_i32("PR_SET_NAME")? {
106+
let address = this.read_scalar(arg2)?.not_undef()?;
107+
let name = this.memory.read_c_str(address)?.to_owned();
108+
this.set_active_thread_name(name)?;
109+
} else if option == this.eval_libc_i32("PR_GET_NAME")? {
110+
let address = this.read_scalar(arg2)?.not_undef()?;
111+
let name = this.get_active_thread_name()?;
112+
this.memory.write_bytes(address, name)?;
113+
} else {
114+
throw_unsup_format!("Unsupported prctl option.");
107115
}
108-
let address = this.read_scalar(arg2)?.not_undef()?;
109-
let name = this.memory.read_c_str(address)?.to_owned();
110-
this.set_active_thread_name(name)?;
111116

112117
Ok(0)
113118
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// ignore-windows: No libc on Windows
2+
3+
#![feature(rustc_private)]
4+
5+
extern crate libc;
6+
7+
use std::ffi::CString;
8+
9+
fn main() {
10+
unsafe {
11+
let thread_name = CString::new("hello").expect("CString::new failed");
12+
assert_eq!(libc::prctl(libc::PR_SET_NAME, thread_name.as_ptr() as u64, 0, 0, 0), 0);
13+
let mut buf = [0; 6];
14+
assert_eq!(libc::prctl(libc::PR_GET_NAME, buf.as_mut_ptr() as u64, 0, 0, 0), 0);
15+
assert_eq!(thread_name.as_bytes_with_nul(), buf);
16+
}
17+
}

0 commit comments

Comments
 (0)