Skip to content

Commit c4574dd

Browse files
author
Vytautas Astrauskas
committed
Many small changes to clean up code.
1 parent 6842eb2 commit c4574dd

File tree

3 files changed

+23
-36
lines changed

3 files changed

+23
-36
lines changed

src/shims/thread.rs

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::*;
2-
use rustc_index::vec::Idx;
32
use rustc_target::abi::LayoutOf;
43

54
impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
@@ -19,33 +18,26 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
1918
);
2019

2120
let new_thread_id = this.create_thread()?;
21+
// Also switch to new thread so that we can push the first stackframe.
2222
let old_thread_id = this.set_active_thread(new_thread_id)?;
2323

2424
let thread_info_place = this.deref_operand(thread)?;
25-
let thread_info_type = thread.layout.ty
26-
.builtin_deref(true)
27-
.ok_or_else(|| err_ub_format!(
28-
"wrong signature used for `pthread_create`: first argument must be a raw pointer."
29-
))?
30-
.ty;
31-
let thread_info_layout = this.layout_of(thread_info_type)?;
3225
this.write_scalar(
33-
Scalar::from_uint(new_thread_id.index() as u128, thread_info_layout.size),
26+
Scalar::from_uint(new_thread_id.to_u128(), thread_info_place.layout.size),
3427
thread_info_place.into(),
3528
)?;
3629

3730
let fn_ptr = this.read_scalar(start_routine)?.not_undef()?;
3831
let instance = this.memory.get_fn(fn_ptr)?.as_instance()?;
3932

4033
let func_arg = this.read_immediate(arg)?;
41-
let func_args = [*func_arg];
4234

4335
let ret_place =
4436
this.allocate(this.layout_of(this.tcx.types.usize)?, MiriMemoryKind::Machine.into());
4537

4638
this.call_function(
4739
instance,
48-
&func_args[..],
40+
&[*func_arg],
4941
Some(ret_place.into()),
5042
StackPopCleanup::None { cleanup: true },
5143
)?;
@@ -66,7 +58,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
6658
throw_unsup_format!("Miri supports pthread_join only with retval==NULL");
6759
}
6860

69-
let thread_id = this.read_scalar(thread)?.not_undef()?.to_machine_usize(this)?;
61+
let thread_id = this.read_scalar(thread)?.to_machine_usize(this)?;
7062
this.join_thread(thread_id.into())?;
7163

7264
Ok(0)
@@ -75,7 +67,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
7567
fn pthread_detach(&mut self, thread: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
7668
let this = self.eval_context_mut();
7769

78-
let thread_id = this.read_scalar(thread)?.not_undef()?.to_machine_usize(this)?;
70+
let thread_id = this.read_scalar(thread)?.to_machine_usize(this)?;
7971
this.detach_thread(thread_id.into())?;
8072

8173
Ok(0)
@@ -85,34 +77,20 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
8577
let this = self.eval_context_mut();
8678

8779
let thread_id = this.get_active_thread()?;
88-
this.write_scalar(Scalar::from_uint(thread_id.index() as u128, dest.layout.size), dest)
80+
this.write_scalar(Scalar::from_uint(thread_id.to_u128(), dest.layout.size), dest)
8981
}
9082

9183
fn prctl(
9284
&mut self,
9385
option: OpTy<'tcx, Tag>,
9486
arg2: OpTy<'tcx, Tag>,
95-
arg3: OpTy<'tcx, Tag>,
96-
arg4: OpTy<'tcx, Tag>,
97-
arg5: OpTy<'tcx, Tag>,
87+
_arg3: OpTy<'tcx, Tag>,
88+
_arg4: OpTy<'tcx, Tag>,
89+
_arg5: OpTy<'tcx, Tag>,
9890
) -> InterpResult<'tcx, i32> {
9991
let this = self.eval_context_mut();
10092

101-
// prctl last 5 arguments are declared as variadic. Therefore, we need
102-
// to check their types manually.
103-
let c_long_size = this.libc_ty_layout("c_long")?.size.bytes();
104-
let check_arg = |arg: OpTy<'tcx, Tag>| -> InterpResult<'tcx> {
105-
match this.read_scalar(arg)?.not_undef()? {
106-
Scalar::Raw { size, .. } if u64::from(size) == c_long_size => Ok(()),
107-
_ => throw_ub_format!("an argument of unsupported type was passed to prctl"),
108-
}
109-
};
110-
check_arg(arg2)?;
111-
check_arg(arg3)?;
112-
check_arg(arg4)?;
113-
check_arg(arg5)?;
114-
115-
let option = this.read_scalar(option)?.not_undef()?.to_i32()?;
93+
let option = this.read_scalar(option)?.to_i32()?;
11694
if option == this.eval_libc_i32("PR_SET_NAME")? {
11795
let address = this.read_scalar(arg2)?.not_undef()?;
11896
let name = this.memory.read_c_str(address)?.to_owned();
@@ -122,7 +100,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
122100
let name = this.get_active_thread_name()?.to_vec();
123101
this.memory.write_bytes(address, name)?;
124102
} else {
125-
throw_unsup_format!("Unsupported prctl option.");
103+
throw_unsup_format!("unsupported prctl option {}", option);
126104
}
127105

128106
Ok(0)

src/shims/tls.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ pub type TlsKey = u128;
2020
pub struct TlsEntry<'tcx> {
2121
/// The data for this key. None is used to represent NULL.
2222
/// (We normalize this early to avoid having to do a NULL-ptr-test each time we access the data.)
23-
/// Will eventually become a map from thread IDs to `Scalar`s, if we ever support more than one thread.
2423
data: BTreeMap<ThreadId, Scalar<Tag>>,
2524
dtor: Option<ty::Instance<'tcx>>,
2625
}
@@ -89,7 +88,7 @@ impl<'tcx> TlsData<'tcx> {
8988
) -> InterpResult<'tcx, Scalar<Tag>> {
9089
match self.keys.get(&key) {
9190
Some(TlsEntry { data, .. }) => {
92-
let value = data.get(&thread_id).cloned();
91+
let value = data.get(&thread_id).copied();
9392
trace!("TLS key {} for thread {:?} loaded: {:?}", key, thread_id, value);
9493
Ok(value.unwrap_or_else(|| Scalar::null_ptr(cx).into()))
9594
}
@@ -99,7 +98,10 @@ impl<'tcx> TlsData<'tcx> {
9998

10099
pub fn store_tls(
101100
&mut self,
102-
key: TlsKey, thread_id: ThreadId, new_data: Option<Scalar<Tag>>) -> InterpResult<'tcx> {
101+
key: TlsKey,
102+
thread_id: ThreadId,
103+
new_data: Option<Scalar<Tag>>
104+
) -> InterpResult<'tcx> {
103105
match self.keys.get_mut(&key) {
104106
Some(TlsEntry { data, .. }) => {
105107
match new_data {

src/thread.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Implements threads.
22
3+
use std::convert::TryInto;
34
use std::cell::RefCell;
45
use std::convert::TryFrom;
56
use std::num::NonZeroU32;
@@ -34,6 +35,12 @@ pub struct ThreadId(usize);
3435
/// The main thread. When it terminates, the whole application terminates.
3536
const MAIN_THREAD: ThreadId = ThreadId(0);
3637

38+
impl ThreadId {
39+
pub fn to_u128(self) -> u128 {
40+
self.0.try_into().unwrap()
41+
}
42+
}
43+
3744
impl Idx for ThreadId {
3845
fn new(idx: usize) -> Self {
3946
ThreadId(idx)

0 commit comments

Comments
 (0)