Skip to content

Commit f777ad2

Browse files
bnjbvrmchesser
authored andcommitted
debug: Avoid underflow when scanning for landing pad bytes (bytecodealliance#2866)
1 parent e05ff1b commit f777ad2

1 file changed

Lines changed: 16 additions & 12 deletions

File tree

crates/debug/src/transform/expression.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -512,24 +512,28 @@ where
512512
}
513513
};
514514
}
515+
515516
// Find all landing pads by scanning bytes, do not care about
516517
// false location at this moment.
517518
// Looks hacky but it is fast; does not need to be really exact.
518-
for i in 0..buf.len() - 2 {
519-
let op = buf[i];
520-
if op == gimli::constants::DW_OP_bra.0 || op == gimli::constants::DW_OP_skip.0 {
521-
// TODO fix for big-endian
522-
let offset = i16::from_le_bytes([buf[i + 1], buf[i + 2]]);
523-
let origin = i + 3;
524-
// Discarding out-of-bounds jumps (also some of falsely detected ops)
525-
if (offset >= 0 && offset as usize + origin <= buf.len())
526-
|| (offset < 0 && -offset as usize <= origin)
527-
{
528-
let target = buf.len() as isize - origin as isize - offset as isize;
529-
jump_targets.insert(target as u64, JumpTargetMarker::new());
519+
if buf.len() > 2 {
520+
for i in 0..buf.len() - 2 {
521+
let op = buf[i];
522+
if op == gimli::constants::DW_OP_bra.0 || op == gimli::constants::DW_OP_skip.0 {
523+
// TODO fix for big-endian
524+
let offset = i16::from_le_bytes([buf[i + 1], buf[i + 2]]);
525+
let origin = i + 3;
526+
// Discarding out-of-bounds jumps (also some of falsely detected ops)
527+
if (offset >= 0 && offset as usize + origin <= buf.len())
528+
|| (offset < 0 && -offset as usize <= origin)
529+
{
530+
let target = buf.len() as isize - origin as isize - offset as isize;
531+
jump_targets.insert(target as u64, JumpTargetMarker::new());
532+
}
530533
}
531534
}
532535
}
536+
533537
while !pc.is_empty() {
534538
let unread_bytes = pc.len().into_u64();
535539
if let Some(marker) = jump_targets.get(&unread_bytes) {

0 commit comments

Comments
 (0)