Skip to content

When doing module and division, first cast to unsigned 32 bit then promote to 64 bit #731

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

Merged
merged 3 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 23 additions & 2 deletions src/crab/split_dbm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1008,9 +1008,30 @@ void SplitDBM::apply(arith_binop_t op, variable_t x, variable_t y, const number_
case arith_binop_t::MUL: assign(x, linear_expression_t(k, y)); break;
// For the rest of operations, we fall back on intervals.
case arith_binop_t::SDIV: set(x, get_interval(y, finite_width).SDiv(interval_t(k))); break;
case arith_binop_t::UDIV: set(x, get_interval(y, finite_width).UDiv(interval_t(k))); break;
case arith_binop_t::UDIV: {
if (finite_width == 32) {
// As defined in the BPF ISA specification, for 32 bit unsigned division the immediate value is
// treated as a 32-bit unsigned integer, so we need to cast it to uint32_t.
set(x, get_interval(y, finite_width).UDiv(interval_t(k.cast_to<uint32_t>())));
} else {
// As defined in the BPF ISA specification, for 64 bit unsigned division the immediate value is
// treated as a 32-bit signed integer, so we need to cast it to int32_t.
set(x, get_interval(y, finite_width).UDiv(interval_t(k.cast_to<int32_t>())));
}
break;
}
case arith_binop_t::SREM: set(x, get_interval(y, finite_width).SRem(interval_t(k))); break;
case arith_binop_t::UREM: set(x, get_interval(y, finite_width).URem(interval_t(k))); break;
case arith_binop_t::UREM:
if (finite_width == 32) {
// As defined in the BPF ISA specification, for 32 bit unsigned modulo the immediate value is
// treated as a 32-bit unsigned integer, so we need to cast it to uint32_t.
set(x, get_interval(y, finite_width).URem(interval_t(k.cast_to<uint32_t>())));
} else {
// As defined in the BPF ISA specification, for 64 bit unsigned modulo the immediate value is
// treated as a 32-bit signed integer, so we need to cast it to int32_t.
set(x, get_interval(y, finite_width).URem(interval_t(k.cast_to<int32_t>())));
}
break;
default: CRAB_ERROR("DBM: unreachable");
}
normalize();
Expand Down
1 change: 1 addition & 0 deletions src/test/test_yaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ YAML_CASE("test-data/full64.yaml")
YAML_CASE("test-data/jump.yaml")
YAML_CASE("test-data/loop.yaml")
YAML_CASE("test-data/movsx.yaml")
YAML_CASE("test-data/muldiv.yaml")
YAML_CASE("test-data/packet.yaml")
YAML_CASE("test-data/parse.yaml")
YAML_CASE("test-data/sext.yaml")
Expand Down
60 changes: 60 additions & 0 deletions test-data/muldiv.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Copyright (c) Prevail Verifier contributors.
# SPDX-License-Identifier: MIT
---
test-case: 32bit division - negative divisor

pre: ["r0.type=number", "r0.svalue=0", "r0.uvalue=0"]

code:
<start>: |
w0 = 4194435072
w0 /= 4096655360

post:
- r0.type=number
- r0.svalue=1
- r0.uvalue=1

---
test-case: 32bit modulo - negative divisor
pre: ["r0.type=number", "r0.svalue=0", "r0.uvalue=0"]

code:
<start>: |
w0 = 4194435072
w0 %= 4096655360

post:
- r0.type=number
- r0.svalue=97779712
- r0.uvalue=97779712

---
test-case: 64bit division - negative divisor

pre: ["r0.type=number", "r0.svalue=0", "r0.uvalue=0"]

code:
<start>: |
r0 = 4194435072
r0 /= 4096655360

post:
- r0.type=number
- r0.svalue=1
- r0.uvalue=1

---
test-case: 64bit modulo - negative divisor

pre: ["r0.type=number", "r0.svalue=0", "r0.uvalue=0"]

code:
<start>: |
r0 = 4194435072
r0 %= 4096655360

post:
- r0.type=number
- r0.svalue=-100532224
- r0.uvalue=18446744073609019392
Loading