Skip to content

Commit 1103a10

Browse files
committed
adjust for error reform
1 parent 6b56aef commit 1103a10

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+93
-111
lines changed

src/helpers.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ fn resolve_did<'mir, 'tcx>(tcx: TyCtxt<'tcx>, path: &[&str]) -> InterpResult<'tc
4242
None
4343
})
4444
.ok_or_else(|| {
45-
let path = path.iter().map(|&s| s.to_owned()).collect();
46-
err_unsup!(PathNotFound(path)).into()
45+
err_unsup_format!("failed to find required Rust item: {:?}", path).into()
4746
})
4847
}
4948

src/intptrcast.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ impl<'mir, 'tcx> GlobalState {
4343
int: u64,
4444
memory: &Memory<'mir, 'tcx, Evaluator<'tcx>>,
4545
) -> InterpResult<'tcx, Pointer<Tag>> {
46-
if int == 0 {
47-
throw_unsup!(InvalidNullPointerUsage);
48-
}
49-
5046
let global_state = memory.extra.intptrcast.borrow();
5147
let pos = global_state.int_to_ptr_map.binary_search_by_key(&int, |(addr, _)| *addr);
5248

@@ -57,7 +53,7 @@ impl<'mir, 'tcx> GlobalState {
5753
// zero. The pointer is untagged because it was created from a cast
5854
Pointer::new_with_tag(alloc_id, Size::from_bytes(0), Tag::Untagged)
5955
}
60-
Err(0) => throw_unsup!(DanglingPointerDeref),
56+
Err(0) => throw_ub!(InvalidIntPointerUsage(int)),
6157
Err(pos) => {
6258
// This is the largest of the adresses smaller than `int`,
6359
// i.e. the greatest lower bound (glb)
@@ -69,7 +65,7 @@ impl<'mir, 'tcx> GlobalState {
6965
// This pointer is untagged because it was created from a cast
7066
Pointer::new_with_tag(alloc_id, Size::from_bytes(offset), Tag::Untagged)
7167
} else {
72-
throw_unsup!(DanglingPointerDeref)
68+
throw_ub!(InvalidIntPointerUsage(int))
7369
}
7470
}
7571
})

src/shims/foreign_items.rs

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
222222
"__rust_alloc" => {
223223
let size = this.read_scalar(args[0])?.to_machine_usize(this)?;
224224
let align = this.read_scalar(args[1])?.to_machine_usize(this)?;
225-
if size == 0 {
226-
throw_unsup!(HeapAllocZeroBytes);
227-
}
228-
if !align.is_power_of_two() {
229-
throw_unsup!(HeapAllocNonPowerOfTwoAlignment(align));
230-
}
225+
Self::check_alloc_request(size, align)?;
231226
let ptr = this.memory.allocate(
232227
Size::from_bytes(size),
233228
Align::from_bytes(align).unwrap(),
@@ -238,12 +233,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
238233
"__rust_alloc_zeroed" => {
239234
let size = this.read_scalar(args[0])?.to_machine_usize(this)?;
240235
let align = this.read_scalar(args[1])?.to_machine_usize(this)?;
241-
if size == 0 {
242-
throw_unsup!(HeapAllocZeroBytes);
243-
}
244-
if !align.is_power_of_two() {
245-
throw_unsup!(HeapAllocNonPowerOfTwoAlignment(align));
246-
}
236+
Self::check_alloc_request(size, align)?;
247237
let ptr = this.memory.allocate(
248238
Size::from_bytes(size),
249239
Align::from_bytes(align).unwrap(),
@@ -257,12 +247,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
257247
let ptr = this.read_scalar(args[0])?.not_undef()?;
258248
let old_size = this.read_scalar(args[1])?.to_machine_usize(this)?;
259249
let align = this.read_scalar(args[2])?.to_machine_usize(this)?;
260-
if old_size == 0 {
261-
throw_unsup!(HeapAllocZeroBytes);
262-
}
263-
if !align.is_power_of_two() {
264-
throw_unsup!(HeapAllocNonPowerOfTwoAlignment(align));
265-
}
250+
// No need to check old_size/align; we anyway check that they match the allocation.
266251
let ptr = this.force_ptr(ptr)?;
267252
this.memory.deallocate(
268253
ptr,
@@ -274,12 +259,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
274259
let old_size = this.read_scalar(args[1])?.to_machine_usize(this)?;
275260
let align = this.read_scalar(args[2])?.to_machine_usize(this)?;
276261
let new_size = this.read_scalar(args[3])?.to_machine_usize(this)?;
277-
if old_size == 0 || new_size == 0 {
278-
throw_unsup!(HeapAllocZeroBytes);
279-
}
280-
if !align.is_power_of_two() {
281-
throw_unsup!(HeapAllocNonPowerOfTwoAlignment(align));
282-
}
262+
Self::check_alloc_request(new_size, align)?;
263+
// No need to check old_size; we anyway check that they match the allocation.
283264
let ptr = this.force_ptr(this.read_scalar(args[0])?.not_undef()?)?;
284265
let align = Align::from_bytes(align).unwrap();
285266
let new_ptr = this.memory.reallocate(
@@ -462,6 +443,18 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
462443
Ok(true)
463444
}
464445

446+
/// Check some basic requirements for this allocation request:
447+
/// non-zero size, power-of-two alignment.
448+
fn check_alloc_request(size: u64, align: u64) -> InterpResult<'tcx> {
449+
if size == 0 {
450+
throw_ub_format!("creating allocation with size 0");
451+
}
452+
if !align.is_power_of_two() {
453+
throw_ub_format!("creating allocation with non-power-of-two alignment {}", align);
454+
}
455+
Ok(())
456+
}
457+
465458
/// Evaluates the scalar at the specified path. Returns Some(val)
466459
/// if the path could be resolved, and None otherwise
467460
fn eval_path_scalar(

src/shims/foreign_items/posix.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
138138
let size = this.read_scalar(args[2])?.to_machine_usize(this)?;
139139
// Align must be power of 2, and also at least ptr-sized (POSIX rules).
140140
if !align.is_power_of_two() {
141-
throw_unsup!(HeapAllocNonPowerOfTwoAlignment(align));
141+
throw_ub_format!("posix_memalign: alignment must be a power of two, but is {}", align);
142142
}
143143
if align < this.pointer_size().bytes() {
144144
throw_ub_format!(
@@ -185,7 +185,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
185185
};
186186

187187
// Figure out how large a pthread TLS key actually is.
188-
// This is `libc::pthread_key_t`.
188+
// To this end, deref the argument type. This is `libc::pthread_key_t`.
189189
let key_type = args[0].layout.ty
190190
.builtin_deref(true)
191191
.ok_or_else(|| err_ub_format!(
@@ -195,12 +195,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
195195
let key_layout = this.layout_of(key_type)?;
196196

197197
// Create key and write it into the memory where `key_ptr` wants it.
198-
let key = this.machine.tls.create_tls_key(dtor) as u128;
199-
if key_layout.size.bits() < 128 && key >= (1u128 << key_layout.size.bits() as u128)
200-
{
201-
throw_unsup!(OutOfTls);
202-
}
203-
198+
let key = this.machine.tls.create_tls_key(dtor, key_layout.size)?;
204199
this.write_scalar(Scalar::from_uint(key, key_layout.size), key_place.into())?;
205200

206201
// Return success (`0`).

src/shims/foreign_items/windows.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
154154
// This just creates a key; Windows does not natively support TLS destructors.
155155

156156
// Create key and return it.
157-
let key = this.machine.tls.create_tls_key(None) as u128;
158-
159-
// Figure out how large a TLS key actually is. This is `c::DWORD`.
160-
if dest.layout.size.bits() < 128
161-
&& key >= (1u128 << dest.layout.size.bits() as u128)
162-
{
163-
throw_unsup!(OutOfTls);
164-
}
157+
let key = this.machine.tls.create_tls_key(None, dest.layout.size)?;
165158
this.write_scalar(Scalar::from_uint(key, dest.layout.size), dest)?;
166159
}
167160
"TlsGetValue" => {

src/shims/tls.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::collections::BTreeMap;
44

5-
use rustc::{ty, ty::layout::HasDataLayout};
5+
use rustc::{ty, ty::layout::{Size, HasDataLayout}};
66
use rustc_target::abi::LayoutOf;
77

88
use crate::{HelpersEvalContextExt, InterpResult, MPlaceTy, Scalar, StackPopCleanup, Tag};
@@ -37,12 +37,18 @@ impl<'tcx> Default for TlsData<'tcx> {
3737
}
3838

3939
impl<'tcx> TlsData<'tcx> {
40-
pub fn create_tls_key(&mut self, dtor: Option<ty::Instance<'tcx>>) -> TlsKey {
40+
/// Generate a new TLS key with the given destructor.
41+
/// `max_size` determines the integer size the key has to fit in.
42+
pub fn create_tls_key(&mut self, dtor: Option<ty::Instance<'tcx>>, max_size: Size) -> InterpResult<'tcx, TlsKey> {
4143
let new_key = self.next_key;
4244
self.next_key += 1;
4345
self.keys.insert(new_key, TlsEntry { data: None, dtor }).unwrap_none();
4446
trace!("New TLS key allocated: {} with dtor {:?}", new_key, dtor);
45-
new_key
47+
48+
if max_size.bits() < 128 && new_key >= (1u128 << max_size.bits() as u128) {
49+
throw_unsup_format!("we ran out of TLS key space");
50+
}
51+
Ok(new_key)
4652
}
4753

4854
pub fn delete_tls_key(&mut self, key: TlsKey) -> InterpResult<'tcx> {
@@ -51,7 +57,7 @@ impl<'tcx> TlsData<'tcx> {
5157
trace!("TLS key {} removed", key);
5258
Ok(())
5359
}
54-
None => throw_unsup!(TlsOutOfBounds),
60+
None => throw_ub_format!("removing a non-existig TLS key: {}", key),
5561
}
5662
}
5763

@@ -65,7 +71,7 @@ impl<'tcx> TlsData<'tcx> {
6571
trace!("TLS key {} loaded: {:?}", key, data);
6672
Ok(data.unwrap_or_else(|| Scalar::ptr_null(cx).into()))
6773
}
68-
None => throw_unsup!(TlsOutOfBounds),
74+
None => throw_ub_format!("loading from a non-existing TLS key: {}", key),
6975
}
7076
}
7177

@@ -76,7 +82,7 @@ impl<'tcx> TlsData<'tcx> {
7682
*data = new_data;
7783
Ok(())
7884
}
79-
None => throw_unsup!(TlsOutOfBounds),
85+
None => throw_ub_format!("storing to a non-existing TLS key: {}", key),
8086
}
8187
}
8288

tests/compile-fail/alignment.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ fn main() {
55
let x_ptr: *mut u8 = &mut x[0];
66
let y_ptr = x_ptr as *mut u64;
77
unsafe {
8-
*y_ptr = 42; //~ ERROR tried to access memory with alignment 1, but alignment
8+
*y_ptr = 42; //~ ERROR accessing memory with alignment 1, but alignment
99
}
1010
panic!("unreachable in miri");
1111
}

tests/compile-fail/atomic_unaligned.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ fn main() {
77
let zptr = &z as *const _ as *const u64;
88
unsafe {
99
::std::intrinsics::atomic_load(zptr);
10-
//~^ ERROR tried to access memory with alignment 4, but alignment 8 is required
10+
//~^ ERROR accessing memory with alignment 4, but alignment 8 is required
1111
}
1212
}

tests/compile-fail/cast_box_int_to_fn_ptr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ fn main() {
77
std::mem::transmute::<&Box<usize>, &fn(i32)>(&b)
88
};
99

10-
(*g)(42) //~ ERROR tried to treat a memory pointer as a function pointer
10+
(*g)(42) //~ ERROR it does not point to a function
1111
}

tests/compile-fail/cast_fn_ptr1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ fn main() {
55
std::mem::transmute::<fn(), fn(i32)>(f)
66
};
77

8-
g(42) //~ ERROR tried to call a function with incorrect number of arguments
8+
g(42) //~ ERROR calling a function with more arguments than it expected
99
}

tests/compile-fail/cast_fn_ptr2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ fn main() {
55
std::mem::transmute::<fn((i32,i32)), fn(i32)>(f)
66
};
77

8-
g(42) //~ ERROR tried to call a function with argument of type (i32, i32) passing data of type i32
8+
g(42) //~ ERROR calling a function with argument of type (i32, i32) passing data of type i32
99
}

tests/compile-fail/cast_fn_ptr3.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ fn main() {
55
std::mem::transmute::<fn((i32,i32)), fn()>(f)
66
};
77

8-
g() //~ ERROR tried to call a function with incorrect number of arguments
8+
g() //~ ERROR calling a function with fewer arguments than it requires
99
}
1010

tests/compile-fail/cast_fn_ptr4.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ fn main() {
55
std::mem::transmute::<fn(*const [i32]), fn(*const i32)>(f)
66
};
77

8-
g(&42 as *const i32) //~ ERROR tried to call a function with argument of type *const [i32] passing data of type *const i32
8+
g(&42 as *const i32) //~ ERROR calling a function with argument of type *const [i32] passing data of type *const i32
99
}

tests/compile-fail/cast_fn_ptr5.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ fn main() {
55
std::mem::transmute::<fn() -> u32, fn()>(f)
66
};
77

8-
g() //~ ERROR tried to call a function with return type u32 passing return place of type ()
8+
g() //~ ERROR calling a function with return type u32 passing return place of type ()
99
}

tests/compile-fail/cast_int_to_fn_ptr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ fn main() {
66
std::mem::transmute::<usize, fn(i32)>(42)
77
};
88

9-
g(42) //~ ERROR dangling pointer was dereferenced
9+
g(42) //~ ERROR invalid use of 42 as a pointer
1010
}

tests/compile-fail/copy_unaligned.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//error-pattern: tried to access memory with alignment 1, but alignment 2 is required
1+
//error-pattern: accessing memory with alignment 1, but alignment 2 is required
22
#![feature(intrinsics)]
33

44
// Directly call intrinsic to avoid debug assertions in libstd

tests/compile-fail/dangling_pointer_deref.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ fn main() {
33
let b = Box::new(42);
44
&*b as *const i32
55
};
6-
let x = unsafe { *p }; //~ ERROR dangling pointer was dereferenced
6+
let x = unsafe { *p }; //~ ERROR dereferenced after this allocation got freed
77
panic!("this should never print: {}", x);
88
}

tests/compile-fail/dangling_zst_deref.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ fn main() {
33
let b = Box::new(42);
44
&*b as *const i32 as *const ()
55
};
6-
let _x = unsafe { *p }; //~ ERROR dangling pointer was dereferenced
6+
let _x = unsafe { *p }; //~ ERROR dereferenced after this allocation got freed
77
}

tests/compile-fail/deallocate-bad-alignment.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ extern crate alloc;
55
use alloc::alloc::Global;
66
use std::alloc::{AllocRef, Layout};
77

8-
// error-pattern: incorrect alloc info: expected size 1 and align 2, got size 1 and align 1
8+
// error-pattern: allocation has size 1 and alignment 1, but gave size 1 and alignment 2
99

1010
fn main() {
1111
unsafe {

tests/compile-fail/deallocate-bad-size.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ extern crate alloc;
55
use alloc::alloc::Global;
66
use std::alloc::{AllocRef, Layout};
77

8-
// error-pattern: incorrect alloc info: expected size 2 and align 1, got size 1 and align 1
8+
// error-pattern: allocation has size 1 and alignment 1, but gave size 2 and alignment 1
99

1010
fn main() {
1111
unsafe {

tests/compile-fail/deallocate-twice.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ extern crate alloc;
55
use alloc::alloc::Global;
66
use std::alloc::{AllocRef, Layout};
77

8-
// error-pattern: tried to deallocate dangling pointer
8+
// error-pattern: dereferenced after this allocation got freed
99

1010
fn main() {
1111
unsafe {

tests/compile-fail/deref-invalid-ptr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33

44
fn main() {
55
let x = 2usize as *const u32;
6-
let _y = unsafe { &*x as *const u32 }; //~ ERROR dangling pointer was dereferenced
6+
let _y = unsafe { &*x as *const u32 }; //~ ERROR invalid use of 2 as a pointer
77
}

tests/compile-fail/deref-partially-dangling.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
fn main() {
44
let x = (1, 13);
55
let xptr = &x as *const _ as *const (i32, i32, i32);
6-
let val = unsafe { (*xptr).1 }; //~ ERROR pointer must be in-bounds at offset 12, but is outside bounds of allocation
6+
let val = unsafe { (*xptr).1 }; //~ ERROR pointer must be in-bounds at offset 12, but is outside bounds of alloc
77
assert_eq!(val, 13);
88
}

tests/compile-fail/deref_fn_ptr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ fn f() {}
22

33
fn main() {
44
let x: u8 = unsafe {
5-
*std::mem::transmute::<fn(), *const u8>(f) //~ ERROR tried to dereference a function pointer
5+
*std::mem::transmute::<fn(), *const u8>(f) //~ ERROR contains a function
66
};
77
panic!("this should never print: {}", x);
88
}

tests/compile-fail/environ-gets-deallocated.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ fn main() {
2020
let pointer = get_environ();
2121
let _x = unsafe { *pointer };
2222
std::env::set_var("FOO", "BAR");
23-
let _y = unsafe { *pointer }; //~ ERROR dangling pointer was dereferenced
23+
let _y = unsafe { *pointer }; //~ ERROR dereferenced after this allocation got freed
2424
}

tests/compile-fail/execute_memory.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ fn main() {
77
let x = box 42;
88
unsafe {
99
let f = std::mem::transmute::<Box<i32>, fn()>(x);
10-
f() //~ ERROR tried to treat a memory pointer as a function pointer
10+
f() //~ ERROR function pointer but it does not point to a function
1111
}
1212
}

tests/compile-fail/fn_ptr_offset.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ fn main() {
1010
let y : *mut u8 = unsafe { mem::transmute(x) };
1111
let y = y.wrapping_offset(1);
1212
let x : fn() = unsafe { mem::transmute(y) };
13-
x(); //~ ERROR tried to use a function pointer after offsetting it
13+
x(); //~ ERROR function pointer but it does not point to a function
1414
}

tests/compile-fail/generator-pinned-moved.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn firstn() -> impl Generator<Yield = u64, Return = ()> {
1111
let num = &mut num;
1212

1313
yield *num;
14-
*num += 1; //~ ERROR dangling pointer was dereferenced
14+
*num += 1; //~ ERROR dereferenced after this allocation got freed
1515
}
1616
}
1717

tests/compile-fail/intptrcast_alignment_check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ fn main() {
77
let base_addr = x as *mut _ as usize;
88
let base_addr_aligned = if base_addr % 2 == 0 { base_addr } else { base_addr+1 };
99
let u16_ptr = base_addr_aligned as *mut u16;
10-
unsafe { *u16_ptr = 2; } //~ ERROR tried to access memory with alignment 1, but alignment 2 is required
10+
unsafe { *u16_ptr = 2; } //~ ERROR memory with alignment 1, but alignment 2 is required
1111
println!("{:?}", x);
1212
}

0 commit comments

Comments
 (0)