Skip to content

Commit 23cd7b8

Browse files
committed
rustup for pthread_setname_np on Linux
1 parent 39ee574 commit 23cd7b8

File tree

6 files changed

+44
-33
lines changed

6 files changed

+44
-33
lines changed

rust-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1603a70f82240ba2d27f72f964e36614d7620ad3
1+
20ffea6938b5839c390252e07940b99e3b6a889a

src/shims/unix/foreign_items.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
437437
}
438438
"pthread_self" => {
439439
let [] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
440-
this.pthread_self(dest)?;
440+
let res = this.pthread_self()?;
441+
this.write_scalar(res, dest)?;
441442
}
442443
"sched_yield" => {
443444
let [] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;

src/shims/unix/linux/foreign_items.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
6969
let result = this.pthread_condattr_getclock(attr, clock_id)?;
7070
this.write_scalar(Scalar::from_i32(result), dest)?;
7171
}
72+
"pthread_setname_np" => {
73+
let [thread, name] =
74+
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
75+
let res = this.pthread_setname_np(
76+
this.read_scalar(thread)?.check_init()?,
77+
this.read_scalar(name)?.check_init()?,
78+
)?;
79+
this.write_scalar(res, dest)?;
80+
}
7281

7382
// Dynamically invoked syscalls
7483
"syscall" => {

src/shims/unix/macos/foreign_items.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
173173
// Threading
174174
"pthread_setname_np" => {
175175
let [name] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
176-
let name = this.read_pointer(name)?;
177-
this.pthread_setname_np(name)?;
176+
let thread = this.pthread_self()?;
177+
this.pthread_setname_np(thread, this.read_scalar(name)?.check_init()?)?;
178178
}
179179

180180
// Incomplete shims that we "stub out" just to get pre-main initialization code to work.

src/shims/unix/thread.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,27 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
8484
Ok(0)
8585
}
8686

87-
fn pthread_self(&mut self, dest: &PlaceTy<'tcx, Provenance>) -> InterpResult<'tcx> {
87+
fn pthread_self(&mut self) -> InterpResult<'tcx, Scalar<Provenance>> {
8888
let this = self.eval_context_mut();
8989

9090
let thread_id = this.get_active_thread();
91-
this.write_scalar(Scalar::from_uint(thread_id.to_u32(), dest.layout.size), dest)
91+
Ok(Scalar::from_machine_usize(thread_id.into(), this))
92+
}
93+
94+
fn pthread_setname_np(
95+
&mut self,
96+
thread: Scalar<Provenance>,
97+
name: Scalar<Provenance>,
98+
) -> InterpResult<'tcx, Scalar<Provenance>> {
99+
let this = self.eval_context_mut();
100+
101+
let thread = ThreadId::try_from(thread.to_machine_usize(this)?).unwrap();
102+
let name = name.to_pointer(this)?;
103+
104+
let name = this.read_c_str(name)?.to_owned();
105+
this.set_thread_name(thread, name);
106+
107+
Ok(Scalar::from_u32(0))
92108
}
93109

94110
fn prctl(&mut self, args: &[OpTy<'tcx, Provenance>]) -> InterpResult<'tcx, i32> {
@@ -117,7 +133,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
117133
// byte. Since `read_c_str` returns the string without the null
118134
// byte, we need to truncate to 15.
119135
name.truncate(15);
120-
this.set_active_thread_name(name);
136+
this.set_thread_name(this.get_active_thread(), name);
121137
} else if option == this.eval_libc_i32("PR_GET_NAME")? {
122138
if args.len() < 2 {
123139
throw_ub_format!(
@@ -127,7 +143,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
127143
}
128144

129145
let address = this.read_pointer(&args[1])?;
130-
let mut name = this.get_active_thread_name().to_vec();
146+
let mut name = this.get_thread_name(this.get_active_thread()).to_vec();
131147
name.push(0u8);
132148
assert!(name.len() <= 16);
133149
this.write_bytes_ptr(address, name)?;
@@ -138,16 +154,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
138154
Ok(0)
139155
}
140156

141-
fn pthread_setname_np(&mut self, name: Pointer<Option<Provenance>>) -> InterpResult<'tcx> {
142-
let this = self.eval_context_mut();
143-
this.assert_target_os("macos", "pthread_setname_np");
144-
145-
let name = this.read_c_str(name)?.to_owned();
146-
this.set_active_thread_name(name);
147-
148-
Ok(())
149-
}
150-
151157
fn sched_yield(&mut self) -> InterpResult<'tcx, i32> {
152158
let this = self.eval_context_mut();
153159

src/thread.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ impl From<u32> for ThreadId {
6969
}
7070
}
7171

72-
impl ThreadId {
73-
pub fn to_u32_scalar(&self) -> Scalar<Provenance> {
74-
Scalar::from_u32(self.0)
72+
impl From<ThreadId> for u64 {
73+
fn from(t: ThreadId) -> Self {
74+
t.0.into()
7575
}
7676
}
7777

@@ -394,14 +394,9 @@ impl<'mir, 'tcx: 'mir> ThreadManager<'mir, 'tcx> {
394394
Ok(())
395395
}
396396

397-
/// Set the name of the active thread.
398-
fn set_active_thread_name(&mut self, new_thread_name: Vec<u8>) {
399-
self.active_thread_mut().thread_name = Some(new_thread_name);
400-
}
401-
402-
/// Get the name of the active thread.
403-
pub fn get_active_thread_name(&self) -> &[u8] {
404-
self.active_thread_ref().thread_name()
397+
/// Set the name of the given thread.
398+
pub fn set_thread_name(&mut self, thread: ThreadId, new_thread_name: Vec<u8>) {
399+
self.threads[thread].thread_name = Some(new_thread_name);
405400
}
406401

407402
/// Get the name of the given thread.
@@ -704,18 +699,18 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
704699
}
705700

706701
#[inline]
707-
fn set_active_thread_name(&mut self, new_thread_name: Vec<u8>) {
702+
fn set_thread_name(&mut self, thread: ThreadId, new_thread_name: Vec<u8>) {
708703
let this = self.eval_context_mut();
709-
this.machine.threads.set_active_thread_name(new_thread_name);
704+
this.machine.threads.set_thread_name(thread, new_thread_name);
710705
}
711706

712707
#[inline]
713-
fn get_active_thread_name<'c>(&'c self) -> &'c [u8]
708+
fn get_thread_name<'c>(&'c self, thread: ThreadId) -> &'c [u8]
714709
where
715710
'mir: 'c,
716711
{
717712
let this = self.eval_context_ref();
718-
this.machine.threads.get_active_thread_name()
713+
this.machine.threads.get_thread_name(thread)
719714
}
720715

721716
#[inline]

0 commit comments

Comments
 (0)