From a90ab8a0cff1762a52bb665b909577d38d1d9ec5 Mon Sep 17 00:00:00 2001 From: Ulrich Weigand Date: Tue, 22 Jun 2021 13:53:47 +0200 Subject: [PATCH] Fix updating srclocs in truncate_last_branch The truncate_last_branch removes an instruction that had already been added to the buffer, and must update various bookkeeping. However, updating the "srclocs" field is incorrect: if there is a srclocs entry that spans both the removed branch *and some previous instruction*, that whole srclocs entry is removed, which makes those previous instructions now uncovered by any srclocs record. This can cause subsequent problems e.g. if one of those instructions traps. Fixed by just truncating instead of fully removing the srclocs record in this case. --- cranelift/codegen/src/machinst/buffer.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cranelift/codegen/src/machinst/buffer.rs b/cranelift/codegen/src/machinst/buffer.rs index 6a5c06c04e4d..03debd945468 100644 --- a/cranelift/codegen/src/machinst/buffer.rs +++ b/cranelift/codegen/src/machinst/buffer.rs @@ -680,10 +680,14 @@ impl MachBuffer { // (end of buffer) self.data.truncate(b.start as usize); self.fixup_records.truncate(b.fixup); - while let Some(last_srcloc) = self.srclocs.last() { + while let Some(mut last_srcloc) = self.srclocs.last_mut() { if last_srcloc.end <= b.start { break; } + if last_srcloc.start < b.start { + last_srcloc.end = b.start; + break; + } self.srclocs.pop(); } // State: