Skip to content

Commit

Permalink
Merge pull request #4899 from YosysHQ/shr_int_max
Browse files Browse the repository at this point in the history
Fix runtime error on shift by INT_MAX
  • Loading branch information
KrystalDelusion authored Feb 14, 2025
2 parents df3c62a + 4c72896 commit 508e732
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions passes/opt/opt_expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1315,6 +1315,10 @@ void replace_const_cells(RTLIL::Design *design, RTLIL::Module *module, bool cons
RTLIL::SigSpec sig_a = assign_map(cell->getPort(ID::A));
RTLIL::SigSpec sig_y(cell->type == ID($shiftx) ? RTLIL::State::Sx : RTLIL::State::S0, cell->getParam(ID::Y_WIDTH).as_int());

// Limit indexing to the size of a, which is behaviourally identical (result is all 0)
// and avoids integer overflow of i + shift_bits when e.g. ID::B == INT_MAX
shift_bits = min(shift_bits, GetSize(sig_a));

if (cell->type != ID($shiftx) && GetSize(sig_a) < GetSize(sig_y))
sig_a.extend_u0(GetSize(sig_y), cell->getParam(ID::A_SIGNED).as_bool());

Expand Down
9 changes: 9 additions & 0 deletions tests/opt/opt_expr_shr_int_max.ys
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
read_verilog << EOF
module uut_00034(b, y);
input signed [30:0] b;
output [11:0] y = b >> ~31'b0; // shift by INT_MAX
endmodule
EOF

# This should succeed, even with UBSAN halt_on_error
opt_expr

0 comments on commit 508e732

Please sign in to comment.