Skip to content

Commit c921673

Browse files
authored
Merge pull request #5416 from RalfJung/pthread_key_create-pointee
pthread_key_create: fix using wrong pointee type
2 parents d1d4e34 + 14b21e3 commit c921673

3 files changed

Lines changed: 37 additions & 11 deletions

File tree

‎src/shims/unix/foreign_items.rs‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -894,18 +894,9 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
894894
None
895895
};
896896

897-
// Figure out how large a pthread TLS key actually is.
898-
// To this end, deref the argument type. This is `libc::pthread_key_t`.
899-
let key_type = key.layout.ty
900-
.builtin_deref(true)
901-
.ok_or_else(|| err_ub_format!(
902-
"wrong signature used for `pthread_key_create`: first argument must be a raw pointer."
903-
))?;
904-
let key_layout = this.layout_of(key_type)?;
905-
906897
// Create key and write it into the memory where `key_ptr` wants it.
907-
let key = this.machine.tls.create_tls_key(dtor, key_layout.size)?;
908-
this.write_scalar(Scalar::from_uint(key, key_layout.size), &key_place)?;
898+
let key = this.machine.tls.create_tls_key(dtor, key_place.layout.size)?;
899+
this.write_scalar(Scalar::from_uint(key, key_place.layout.size), &key_place)?;
909900

910901
// Return success (`0`).
911902
this.write_null(dest)?;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//@ignore-target: windows # No pthreads on Windows
2+
//@normalize-stderr-test: "attempting to access \d+ bytes" -> "attempting to access N bytes"
3+
use std::ffi::c_void;
4+
5+
unsafe extern "C" {
6+
fn pthread_key_create(k: *mut c_void, dtor: extern "C" fn(*mut c_void)) -> i32;
7+
}
8+
9+
extern "C" fn my_dtor(_: *mut c_void) {}
10+
11+
pub fn main() {
12+
unsafe {
13+
let mut i: u8 = 32;
14+
pthread_key_create(&raw mut i as *mut c_void, my_dtor);
15+
//~^ ERROR: memory access failed
16+
}
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: Undefined Behavior: memory access failed: attempting to access N bytes, but got ALLOC which is only 1 byte from the end of the allocation
2+
--> tests/fail/shims/pthread_key_create_wrong_pointee_type.rs:LL:CC
3+
|
4+
LL | pthread_key_create(&raw mut i as *mut c_void, my_dtor);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Undefined Behavior occurred here
6+
|
7+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
8+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9+
help: ALLOC was allocated here:
10+
--> tests/fail/shims/pthread_key_create_wrong_pointee_type.rs:LL:CC
11+
|
12+
LL | let mut i: u8 = 32;
13+
| ^^^^^
14+
15+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
16+
17+
error: aborting due to 1 previous error
18+

0 commit comments

Comments
 (0)