Skip to content

Commit 6fecfd9

Browse files
committed
adjust for error reform
1 parent 9893392 commit 6fecfd9

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
@@ -41,8 +41,7 @@ fn resolve_did<'mir, 'tcx>(tcx: TyCtxt<'tcx>, path: &[&str]) -> InterpResult<'tc
4141
None
4242
})
4343
.ok_or_else(|| {
44-
let path = path.iter().map(|&s| s.to_owned()).collect();
45-
err_unsup!(PathNotFound(path)).into()
44+
err_unsup_format!("failed to find required Rust item: {:?}", path).into()
4645
})
4746
}
4847

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
@@ -220,12 +220,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
220220
"__rust_alloc" => {
221221
let size = this.read_scalar(args[0])?.to_machine_usize(this)?;
222222
let align = this.read_scalar(args[1])?.to_machine_usize(this)?;
223-
if size == 0 {
224-
throw_unsup!(HeapAllocZeroBytes);
225-
}
226-
if !align.is_power_of_two() {
227-
throw_unsup!(HeapAllocNonPowerOfTwoAlignment(align));
228-
}
223+
Self::check_alloc_request(size, align)?;
229224
let ptr = this.memory.allocate(
230225
Size::from_bytes(size),
231226
Align::from_bytes(align).unwrap(),
@@ -236,12 +231,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
236231
"__rust_alloc_zeroed" => {
237232
let size = this.read_scalar(args[0])?.to_machine_usize(this)?;
238233
let align = this.read_scalar(args[1])?.to_machine_usize(this)?;
239-
if size == 0 {
240-
throw_unsup!(HeapAllocZeroBytes);
241-
}
242-
if !align.is_power_of_two() {
243-
throw_unsup!(HeapAllocNonPowerOfTwoAlignment(align));
244-
}
234+
Self::check_alloc_request(size, align)?;
245235
let ptr = this.memory.allocate(
246236
Size::from_bytes(size),
247237
Align::from_bytes(align).unwrap(),
@@ -255,12 +245,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
255245
let ptr = this.read_scalar(args[0])?.not_undef()?;
256246
let old_size = this.read_scalar(args[1])?.to_machine_usize(this)?;
257247
let align = this.read_scalar(args[2])?.to_machine_usize(this)?;
258-
if old_size == 0 {
259-
throw_unsup!(HeapAllocZeroBytes);
260-
}
261-
if !align.is_power_of_two() {
262-
throw_unsup!(HeapAllocNonPowerOfTwoAlignment(align));
263-
}
248+
// No need to check old_size/align; we anyway check that they match the allocation.
264249
let ptr = this.force_ptr(ptr)?;
265250
this.memory.deallocate(
266251
ptr,
@@ -272,12 +257,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
272257
let old_size = this.read_scalar(args[1])?.to_machine_usize(this)?;
273258
let align = this.read_scalar(args[2])?.to_machine_usize(this)?;
274259
let new_size = this.read_scalar(args[3])?.to_machine_usize(this)?;
275-
if old_size == 0 || new_size == 0 {
276-
throw_unsup!(HeapAllocZeroBytes);
277-
}
278-
if !align.is_power_of_two() {
279-
throw_unsup!(HeapAllocNonPowerOfTwoAlignment(align));
280-
}
260+
Self::check_alloc_request(new_size, align)?;
261+
// No need to check old_size; we anyway check that they match the allocation.
281262
let ptr = this.force_ptr(this.read_scalar(args[0])?.not_undef()?)?;
282263
let align = Align::from_bytes(align).unwrap();
283264
let new_ptr = this.memory.reallocate(
@@ -465,6 +446,18 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
465446
Ok(true)
466447
}
467448

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

src/shims/foreign_items/posix.rs

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

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

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

204199
// 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
}

0 commit comments

Comments
 (0)