Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix overflow in conversion from uvalue to svalue #769

Merged
merged 9 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions src/crab/ebpf_domain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2374,12 +2374,9 @@ void ebpf_domain_t::lshr(const Reg& dst_reg, int imm, int finite_width) {
}
}
m_inv.set(dst.uvalue, interval_t{lb_n, ub_n});
if (ub_n.narrow<int64_t>() >= lb_n.narrow<int64_t>()) {
// ? m_inv.set(dst.svalue, crab::interval_t{number_t{(int64_t)lb_n}, number_t{(int64_t)ub_n}});
m_inv.assign(dst.svalue, dst.uvalue);
} else {
havoc(dst.svalue);
}
m_inv.assign(dst.svalue, dst.uvalue);
overflow_signed(m_inv, dst.svalue, finite_width);
overflow_unsigned(m_inv, dst.uvalue, finite_width);
return;
}
havoc(dst.svalue);
Expand Down
164 changes: 164 additions & 0 deletions test-data/shift.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1104,3 +1104,167 @@ post:
- r2.type=number
- r2.svalue=32
- r2.uvalue=32

---
test-case: check that unsigned right shift by 0 is idempotent - svalue out of range

pre:
- r0.type=number
# This is 0x8000000000000000, which is out of range for a signed 64-bit number.
Alan-Jowett marked this conversation as resolved.
Show resolved Hide resolved
- r0.svalue=-9223372036854775808
- r0.uvalue=9223372036854775808
- r1.type=number
- r1.svalue=-9223372036854775808
- r1.uvalue=9223372036854775808

code:
<start>: |
r0 >>= 0
if r1 == r0 goto <one>
r2 = 0
exit
<one>: |
r2 = 1
exit

post:
- r0.type=number
- r0.svalue=-9223372036854775808
- r0.uvalue=9223372036854775808
- r1.type=number
- r1.svalue=-9223372036854775808
- r1.uvalue=9223372036854775808
- r0.svalue=r1.svalue
- r0.uvalue=r1.uvalue
- r2.svalue=1
- r2.type=number
- r2.uvalue=1

messages:
- "1:2: Code is unreachable after 1:2"

---
test-case: check that unsigned right shift by 0 is idempotent - valid svalue

pre:
- r0.type=number
# This is 0x7fffffffffffffff, which is in range for a signed 64-bit number.
Alan-Jowett marked this conversation as resolved.
Show resolved Hide resolved
- r0.svalue=9223372036854775807
- r0.uvalue=9223372036854775807
- r1.type=number
- r1.svalue=9223372036854775807
- r1.uvalue=9223372036854775807

code:
<start>: |
r0 >>= 0
if r1 == r0 goto <one>
r2 = 0
exit
<one>: |
r2 = 1
exit

post:
- r0.svalue=9223372036854775807
- r0.svalue=r1.svalue
- r0.type=number
- r0.uvalue=9223372036854775807
- r0.uvalue=r1.uvalue
- r1.svalue=9223372036854775807
- r1.type=number
- r1.uvalue=9223372036854775807
- r2.svalue=1
- r2.type=number
- r2.uvalue=1

messages:
- "1:2: Code is unreachable after 1:2"

---
test-case: check that unsigned right shift by 0 is idempotent - svalue out of range - via register

pre:
- r0.type=number
# This is 0x8000000000000000, which is out of range for a signed 64-bit number.
Alan-Jowett marked this conversation as resolved.
Show resolved Hide resolved
- r0.svalue=-9223372036854775808
- r0.uvalue=9223372036854775808
- r1.type=number
- r1.svalue=-9223372036854775808
- r1.uvalue=9223372036854775808
- r3.type=number
- r3.uvalue=0
- r3.svalue=0

code:
<start>: |
r0 >>= r3
if r1 == r0 goto <one>
r2 = 0
exit
<one>: |
r2 = 1
exit

post:
- r0.type=number
- r0.svalue=-9223372036854775808
- r0.uvalue=9223372036854775808
- r1.type=number
- r1.svalue=-9223372036854775808
- r1.uvalue=9223372036854775808
- r0.svalue=r1.svalue
- r0.uvalue=r1.uvalue
- r2.svalue=1
- r2.type=number
- r2.uvalue=1
- r3.type=number
- r3.uvalue=0
- r3.svalue=0

messages:
- "1:2: Code is unreachable after 1:2"

---
test-case: check that unsigned right shift by 0 is idempotent - valid svalue

pre:
- r0.type=number
# This is 0x7fffffffffffffff, which is in range for a signed 64-bit number.
Alan-Jowett marked this conversation as resolved.
Show resolved Hide resolved
- r0.svalue=9223372036854775807
- r0.uvalue=9223372036854775807
- r1.type=number
- r1.svalue=9223372036854775807
- r1.uvalue=9223372036854775807
- r3.type=number
- r3.uvalue=0
- r3.svalue=0

code:
<start>: |
r0 >>= r3
if r1 == r0 goto <one>
r2 = 0
exit
<one>: |
r2 = 1
exit

post:
- r0.svalue=9223372036854775807
- r0.svalue=r1.svalue
- r0.type=number
- r0.uvalue=9223372036854775807
- r0.uvalue=r1.uvalue
- r1.svalue=9223372036854775807
- r1.type=number
- r1.uvalue=9223372036854775807
- r2.svalue=1
- r2.type=number
- r2.uvalue=1
- r3.type=number
- r3.uvalue=0
- r3.svalue=0

messages:
- "1:2: Code is unreachable after 1:2"
Alan-Jowett marked this conversation as resolved.
Show resolved Hide resolved
Loading