Skip to content

Commit 63160c6

Browse files
committed
move pthread related functions
1 parent 8fe7543 commit 63160c6

File tree

2 files changed

+112
-112
lines changed

2 files changed

+112
-112
lines changed

src/shims/foreign_items.rs

Lines changed: 1 addition & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::{convert::TryInto, iter};
66
use rustc_hir::def_id::DefId;
77
use rustc::mir;
88
use rustc::ty;
9-
use rustc::ty::layout::{Align, LayoutOf, Size};
9+
use rustc::ty::layout::{Align, Size};
1010
use rustc_apfloat::Float;
1111
use rustc_span::symbol::sym;
1212
use syntax::attr;
@@ -192,7 +192,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
192192
dest: PlaceTy<'tcx, Tag>,
193193
) -> InterpResult<'tcx> {
194194
let this = self.eval_context_mut();
195-
let tcx = &{ this.tcx.tcx };
196195

197196
match link_name {
198197
"malloc" => {
@@ -487,115 +486,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
487486
this.write_null(dest)?;
488487
}
489488

490-
// Hook pthread calls that go to the thread-local storage memory subsystem.
491-
"pthread_key_create" => {
492-
let key_place = this.deref_operand(args[0])?;
493-
494-
// Extract the function type out of the signature (that seems easier than constructing it ourselves).
495-
let dtor = match this.test_null(this.read_scalar(args[1])?.not_undef()?)? {
496-
Some(dtor_ptr) => Some(this.memory.get_fn(dtor_ptr)?.as_instance()?),
497-
None => None,
498-
};
499-
500-
// Figure out how large a pthread TLS key actually is.
501-
// This is `libc::pthread_key_t`.
502-
let key_type = args[0].layout.ty
503-
.builtin_deref(true)
504-
.ok_or_else(|| err_ub_format!(
505-
"wrong signature used for `pthread_key_create`: first argument must be a raw pointer."
506-
))?
507-
.ty;
508-
let key_layout = this.layout_of(key_type)?;
509-
510-
// Create key and write it into the memory where `key_ptr` wants it.
511-
let key = this.machine.tls.create_tls_key(dtor) as u128;
512-
if key_layout.size.bits() < 128 && key >= (1u128 << key_layout.size.bits() as u128)
513-
{
514-
throw_unsup!(OutOfTls);
515-
}
516-
517-
this.write_scalar(Scalar::from_uint(key, key_layout.size), key_place.into())?;
518-
519-
// Return success (`0`).
520-
this.write_null(dest)?;
521-
}
522-
"pthread_key_delete" => {
523-
let key = this.read_scalar(args[0])?.to_bits(args[0].layout.size)?;
524-
this.machine.tls.delete_tls_key(key)?;
525-
// Return success (0)
526-
this.write_null(dest)?;
527-
}
528-
"pthread_getspecific" => {
529-
let key = this.read_scalar(args[0])?.to_bits(args[0].layout.size)?;
530-
let ptr = this.machine.tls.load_tls(key, tcx)?;
531-
this.write_scalar(ptr, dest)?;
532-
}
533-
"pthread_setspecific" => {
534-
let key = this.read_scalar(args[0])?.to_bits(args[0].layout.size)?;
535-
let new_ptr = this.read_scalar(args[1])?.not_undef()?;
536-
this.machine.tls.store_tls(key, this.test_null(new_ptr)?)?;
537-
538-
// Return success (`0`).
539-
this.write_null(dest)?;
540-
}
541-
542-
// Stack size/address stuff.
543-
| "pthread_attr_init"
544-
| "pthread_attr_destroy"
545-
| "pthread_self"
546-
| "pthread_attr_setstacksize" => {
547-
this.write_null(dest)?;
548-
}
549-
"pthread_attr_getstack" => {
550-
let addr_place = this.deref_operand(args[1])?;
551-
let size_place = this.deref_operand(args[2])?;
552-
553-
this.write_scalar(
554-
Scalar::from_uint(STACK_ADDR, addr_place.layout.size),
555-
addr_place.into(),
556-
)?;
557-
this.write_scalar(
558-
Scalar::from_uint(STACK_SIZE, size_place.layout.size),
559-
size_place.into(),
560-
)?;
561-
562-
// Return success (`0`).
563-
this.write_null(dest)?;
564-
}
565-
566-
// We don't support threading. (Also for Windows.)
567-
| "pthread_create"
568-
| "CreateThread"
569-
=> {
570-
throw_unsup_format!("Miri does not support threading");
571-
}
572-
573-
// Stub out calls for condvar, mutex and rwlock, to just return `0`.
574-
| "pthread_mutexattr_init"
575-
| "pthread_mutexattr_settype"
576-
| "pthread_mutex_init"
577-
| "pthread_mutexattr_destroy"
578-
| "pthread_mutex_lock"
579-
| "pthread_mutex_unlock"
580-
| "pthread_mutex_destroy"
581-
| "pthread_rwlock_rdlock"
582-
| "pthread_rwlock_unlock"
583-
| "pthread_rwlock_wrlock"
584-
| "pthread_rwlock_destroy"
585-
| "pthread_condattr_init"
586-
| "pthread_condattr_setclock"
587-
| "pthread_cond_init"
588-
| "pthread_condattr_destroy"
589-
| "pthread_cond_destroy"
590-
=> {
591-
this.write_null(dest)?;
592-
}
593-
594-
// We don't support fork so we don't have to do anything for atfork.
595-
"pthread_atfork" => {
596-
this.write_null(dest)?;
597-
}
598-
599489
"posix_fadvise" => {
600490
// fadvise is only informational, we can ignore it.
601491
this.write_null(dest)?;

src/shims/foreign_items/posix.rs

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ mod linux;
22
mod macos;
33

44
use crate::*;
5-
use rustc::ty::layout::{Align, Size};
5+
use rustc::ty::layout::{Align, LayoutOf, Size};
66

77
impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
88
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
@@ -157,6 +157,116 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
157157
}
158158
}
159159

160+
// Hook pthread calls that go to the thread-local storage memory subsystem.
161+
"pthread_key_create" => {
162+
let key_place = this.deref_operand(args[0])?;
163+
164+
// Extract the function type out of the signature (that seems easier than constructing it ourselves).
165+
let dtor = match this.test_null(this.read_scalar(args[1])?.not_undef()?)? {
166+
Some(dtor_ptr) => Some(this.memory.get_fn(dtor_ptr)?.as_instance()?),
167+
None => None,
168+
};
169+
170+
// Figure out how large a pthread TLS key actually is.
171+
// This is `libc::pthread_key_t`.
172+
let key_type = args[0].layout.ty
173+
.builtin_deref(true)
174+
.ok_or_else(|| err_ub_format!(
175+
"wrong signature used for `pthread_key_create`: first argument must be a raw pointer."
176+
))?
177+
.ty;
178+
let key_layout = this.layout_of(key_type)?;
179+
180+
// Create key and write it into the memory where `key_ptr` wants it.
181+
let key = this.machine.tls.create_tls_key(dtor) as u128;
182+
if key_layout.size.bits() < 128 && key >= (1u128 << key_layout.size.bits() as u128)
183+
{
184+
throw_unsup!(OutOfTls);
185+
}
186+
187+
this.write_scalar(Scalar::from_uint(key, key_layout.size), key_place.into())?;
188+
189+
// Return success (`0`).
190+
this.write_null(dest)?;
191+
}
192+
"pthread_key_delete" => {
193+
let key = this.read_scalar(args[0])?.to_bits(args[0].layout.size)?;
194+
this.machine.tls.delete_tls_key(key)?;
195+
// Return success (0)
196+
this.write_null(dest)?;
197+
}
198+
"pthread_getspecific" => {
199+
let key = this.read_scalar(args[0])?.to_bits(args[0].layout.size)?;
200+
let ptr = this.machine.tls.load_tls(key, tcx)?;
201+
this.write_scalar(ptr, dest)?;
202+
}
203+
"pthread_setspecific" => {
204+
let key = this.read_scalar(args[0])?.to_bits(args[0].layout.size)?;
205+
let new_ptr = this.read_scalar(args[1])?.not_undef()?;
206+
this.machine.tls.store_tls(key, this.test_null(new_ptr)?)?;
207+
208+
// Return success (`0`).
209+
this.write_null(dest)?;
210+
}
211+
212+
// Stack size/address stuff.
213+
| "pthread_attr_init"
214+
| "pthread_attr_destroy"
215+
| "pthread_self"
216+
| "pthread_attr_setstacksize" => {
217+
this.write_null(dest)?;
218+
}
219+
"pthread_attr_getstack" => {
220+
let addr_place = this.deref_operand(args[1])?;
221+
let size_place = this.deref_operand(args[2])?;
222+
223+
this.write_scalar(
224+
Scalar::from_uint(STACK_ADDR, addr_place.layout.size),
225+
addr_place.into(),
226+
)?;
227+
this.write_scalar(
228+
Scalar::from_uint(STACK_SIZE, size_place.layout.size),
229+
size_place.into(),
230+
)?;
231+
232+
// Return success (`0`).
233+
this.write_null(dest)?;
234+
}
235+
236+
// We don't support threading. (Also for Windows.)
237+
| "pthread_create"
238+
| "CreateThread"
239+
=> {
240+
throw_unsup_format!("Miri does not support threading");
241+
}
242+
243+
// Stub out calls for condvar, mutex and rwlock, to just return `0`.
244+
| "pthread_mutexattr_init"
245+
| "pthread_mutexattr_settype"
246+
| "pthread_mutex_init"
247+
| "pthread_mutexattr_destroy"
248+
| "pthread_mutex_lock"
249+
| "pthread_mutex_unlock"
250+
| "pthread_mutex_destroy"
251+
| "pthread_rwlock_rdlock"
252+
| "pthread_rwlock_unlock"
253+
| "pthread_rwlock_wrlock"
254+
| "pthread_rwlock_destroy"
255+
| "pthread_condattr_init"
256+
| "pthread_condattr_setclock"
257+
| "pthread_cond_init"
258+
| "pthread_condattr_destroy"
259+
| "pthread_cond_destroy"
260+
=> {
261+
this.write_null(dest)?;
262+
}
263+
264+
// We don't support fork so we don't have to do anything for atfork.
265+
"pthread_atfork" => {
266+
this.write_null(dest)?;
267+
}
268+
269+
160270
_ => {
161271
match this.tcx.sess.target.target.target_os.to_lowercase().as_str() {
162272
"linux" => linux::EvalContextExt::emulate_foreign_item_by_name(this, link_name, args, dest)?,

0 commit comments

Comments
 (0)