From dadadcbcce1cf2d25ffb5122d196e2a5b4369c4d Mon Sep 17 00:00:00 2001 From: Chris Fallin Date: Tue, 1 Dec 2020 17:01:38 -0800 Subject: [PATCH] Debug info: two fixes in x64 backend. - Sort by generated-code offset to maintain invariant and avoid gimli panic. - Fix srcloc interaction with branch peephole optimization in MachBuffer: if a srcloc range overlaps with a branch that is truncated, remove that srcloc range. These issues were found while fuzzing the new backend (#2453); I suspect that they arise with the new backend because we can sink instructions (e.g. loads or extends) in more interesting ways than before, but I'm not entirely sure. Test coverage will be via the fuzz corpus once #2453 lands. --- cranelift/codegen/src/machinst/buffer.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/cranelift/codegen/src/machinst/buffer.rs b/cranelift/codegen/src/machinst/buffer.rs index b2187a9b68a0..7d75dd46d7b6 100644 --- a/cranelift/codegen/src/machinst/buffer.rs +++ b/cranelift/codegen/src/machinst/buffer.rs @@ -674,6 +674,12 @@ 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() { + if last_srcloc.end <= b.start { + break; + } + self.srclocs.pop(); + } // State: // [PRE CODE] // cur_off, Offset b.start, b.labels_at_this_branch: @@ -1184,12 +1190,15 @@ impl MachBuffer { // incorrect. assert!(self.fixup_records.is_empty()); + let mut srclocs = self.srclocs; + srclocs.sort_by_key(|entry| entry.start); + MachBufferFinalized { data: self.data, relocs: self.relocs, traps: self.traps, call_sites: self.call_sites, - srclocs: self.srclocs, + srclocs, stack_maps: self.stack_maps, } }