Skip to content

Commit

Permalink
Merge pull request #1561 from ltratt/bork_signed_ptrs
Browse files Browse the repository at this point in the history
Don't allow signed pointer comparison.
  • Loading branch information
vext01 authored Jan 24, 2025
2 parents d9d5b0e + 2952676 commit 763be58
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
25 changes: 25 additions & 0 deletions ykrt/src/compile/jitc_yk/jit_ir/well_formed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,15 @@ impl Module {
self.inst(iidx).display(self, iidx)
);
}

if let Ty::Ptr = self.type_(x.lhs(self).tyidx(self)) {
if x.predicate().signed() {
panic!(
"Instruction at position {iidx} compares pointers using a signed predicate\n {}",
self.inst(iidx).display(self, iidx)
);
}
}
}
Inst::Select(x) => {
let Ty::Integer(bitsize) = self.type_(x.cond(self).tyidx(self)) else {
Expand Down Expand Up @@ -422,6 +431,22 @@ mod tests {
);
}

#[test]
#[should_panic(
expected = "Instruction at position 2 compares pointers using a signed predicate"
)]
fn cg_icmp_ptr_signed() {
Module::from_str(
"
entry:
%0: ptr = param 0
%1: ptr = param 1
%2: i1 = slt %0, %1
black_box %2
",
);
}

#[test]
#[should_panic(expected = "Instruction at position 1 trying to select on a non-i1")]
fn select_bad_width() {
Expand Down
8 changes: 4 additions & 4 deletions ykrt/src/compile/jitc_yk/opt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,10 +677,10 @@ impl Opt {
Predicate::UnsignedGreaterEqual => x >= y,
Predicate::UnsignedLess => x < y,
Predicate::UnsignedLessEqual => x <= y,
Predicate::SignedGreater => (x as i64) > (y as i64),
Predicate::SignedGreaterEqual => (x as i64) >= (y as i64),
Predicate::SignedLess => (x as i64) < (y as i64),
Predicate::SignedLessEqual => (x as i64) <= (y as i64),
Predicate::SignedGreater
| Predicate::SignedGreaterEqual
| Predicate::SignedLess
| Predicate::SignedLessEqual => unreachable!(),
};

self.m.replace(
Expand Down

0 comments on commit 763be58

Please sign in to comment.