diff --git a/src/diagnostics.rs b/src/diagnostics.rs index 5189982b13..114f1d9be3 100644 --- a/src/diagnostics.rs +++ b/src/diagnostics.rs @@ -115,7 +115,19 @@ pub fn report_error<'tcx, 'mir>( e.print_backtrace(); let msg = e.to_string(); - report_msg(ecx, &format!("{}: {}", title, msg), msg, helps, true) + report_msg(ecx, &format!("{}: {}", title, msg), msg, helps, true); + + // Extra output to help debug specific issues. + if let UndefinedBehavior(UndefinedBehaviorInfo::InvalidUndefBytes(Some(ptr))) = e.kind { + eprintln!( + "Uninitialized read occurred at offset 0x{:x} into this allocation:", + ptr.offset.bytes(), + ); + ecx.memory.dump_alloc(ptr.alloc_id); + eprintln!(); + } + + None } /// Report an error or note (depending on the `error` argument) at the current frame's current statement. @@ -126,7 +138,7 @@ fn report_msg<'tcx, 'mir>( span_msg: String, mut helps: Vec, error: bool, -) -> Option { +) { let span = if let Some(frame) = ecx.machine.stack.last() { frame.current_source_info().unwrap().span } else { @@ -167,8 +179,6 @@ fn report_msg<'tcx, 'mir>( trace!(" local {}: {:?}", i, local.value); } } - // Let the reported error determine the return code. - return None; } thread_local! { diff --git a/tests/compile-fail/undefined_buffer.rs b/tests/compile-fail/undefined_buffer.rs new file mode 100644 index 0000000000..dac02a8690 --- /dev/null +++ b/tests/compile-fail/undefined_buffer.rs @@ -0,0 +1,20 @@ +// error-pattern: reading uninitialized memory + +use std::alloc::{alloc, dealloc, Layout}; +use std::slice::from_raw_parts; + +fn main() { + let layout = Layout::from_size_align(32, 8).unwrap(); + unsafe { + let ptr = alloc(layout); + *ptr = 0x41; + *ptr.add(1) = 0x42; + *ptr.add(2) = 0x43; + *ptr.add(3) = 0x44; + *ptr.add(16) = 0x00; + let slice1 = from_raw_parts(ptr, 16); + let slice2 = from_raw_parts(ptr.add(16), 16); + drop(slice1.cmp(slice2)); + dealloc(ptr, layout); + } +}