From e37d0e312596c06659b881c240a5afdd58395665 Mon Sep 17 00:00:00 2001 From: David Cook Date: Tue, 21 Apr 2020 21:28:22 -0500 Subject: [PATCH 1/5] Print hex dump of alloc on reading undef bytes --- src/diagnostics.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/diagnostics.rs b/src/diagnostics.rs index 5189982b13..77aaacea02 100644 --- a/src/diagnostics.rs +++ b/src/diagnostics.rs @@ -114,6 +114,9 @@ pub fn report_error<'tcx, 'mir>( }; e.print_backtrace(); + if let UndefinedBehavior(UndefinedBehaviorInfo::InvalidUndefBytes(Some(ptr))) = e.kind { + ecx.memory.dump_alloc(ptr.alloc_id); + } let msg = e.to_string(); report_msg(ecx, &format!("{}: {}", title, msg), msg, helps, true) } From 9057dae235df4d893b3051991e9794af15b32902 Mon Sep 17 00:00:00 2001 From: David Cook Date: Wed, 22 Apr 2020 17:41:06 -0500 Subject: [PATCH 2/5] Reorder output --- src/diagnostics.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/diagnostics.rs b/src/diagnostics.rs index 77aaacea02..565db7b178 100644 --- a/src/diagnostics.rs +++ b/src/diagnostics.rs @@ -114,11 +114,14 @@ pub fn report_error<'tcx, 'mir>( }; e.print_backtrace(); + let msg = e.to_string(); + let result = report_msg(ecx, &format!("{}: {}", title, msg), msg, helps, true); + if let UndefinedBehavior(UndefinedBehaviorInfo::InvalidUndefBytes(Some(ptr))) = e.kind { ecx.memory.dump_alloc(ptr.alloc_id); } - let msg = e.to_string(); - report_msg(ecx, &format!("{}: {}", title, msg), msg, helps, true) + + result } /// Report an error or note (depending on the `error` argument) at the current frame's current statement. From d9ac84d05fc58aa82b4c4306f42231bb6aeb9226 Mon Sep 17 00:00:00 2001 From: David Cook Date: Thu, 23 Apr 2020 20:00:09 -0500 Subject: [PATCH 3/5] Add message before dumping alloc --- src/diagnostics.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/diagnostics.rs b/src/diagnostics.rs index 565db7b178..e72232323b 100644 --- a/src/diagnostics.rs +++ b/src/diagnostics.rs @@ -118,7 +118,12 @@ pub fn report_error<'tcx, 'mir>( let result = report_msg(ecx, &format!("{}: {}", title, msg), msg, helps, true); 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!(); } result From 7f92eab3c477f321a55f53b6e04ac8ca2cd04ebf Mon Sep 17 00:00:00 2001 From: David Cook Date: Thu, 23 Apr 2020 20:00:25 -0500 Subject: [PATCH 4/5] Add test to exercise InvalidUndefBytes --- tests/compile-fail/undefined_buffer.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 tests/compile-fail/undefined_buffer.rs 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); + } +} From e267fb4edec86c84b6c0e415a647ba9f8b9f0c9e Mon Sep 17 00:00:00 2001 From: David Cook Date: Sun, 26 Apr 2020 22:13:36 -0500 Subject: [PATCH 5/5] Review comments --- src/diagnostics.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/diagnostics.rs b/src/diagnostics.rs index e72232323b..114f1d9be3 100644 --- a/src/diagnostics.rs +++ b/src/diagnostics.rs @@ -115,8 +115,9 @@ pub fn report_error<'tcx, 'mir>( e.print_backtrace(); let msg = e.to_string(); - let result = 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:", @@ -126,7 +127,7 @@ pub fn report_error<'tcx, 'mir>( eprintln!(); } - result + None } /// Report an error or note (depending on the `error` argument) at the current frame's current statement. @@ -137,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 { @@ -178,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! {