Skip to content

Commit

Permalink
Fix fuzz bug that found 0-bit width issue. (Resulted in overshift.)
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 586138891
  • Loading branch information
cdleary authored and copybara-github committed Nov 29, 2023
1 parent 40bcfc6 commit aebb40c
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions xls/dslx/type_system/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ cc_library(
":parametric_env",
":parametric_instantiator",
":type_and_parametric_env",
":type_info",
":unwrap_meta_type",
"@com_google_absl//absl/algorithm:container",
"@com_google_absl//absl/cleanup",
Expand Down
1 change: 1 addition & 0 deletions xls/dslx/type_system/deduce.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
#include "xls/dslx/type_system/parametric_env.h"
#include "xls/dslx/type_system/parametric_instantiator.h"
#include "xls/dslx/type_system/type_and_parametric_env.h"
#include "xls/dslx/type_system/type_info.h"
#include "xls/dslx/type_system/unwrap_meta_type.h"
#include "xls/dslx/warning_kind.h"
#include "xls/ir/bits.h"
Expand Down
1 change: 1 addition & 0 deletions xls/dslx/type_system/deduce.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "absl/status/statusor.h"
#include "absl/types/span.h"
#include "xls/dslx/frontend/ast.h"
#include "xls/dslx/frontend/ast_node.h"
#include "xls/dslx/type_system/concrete_type.h"
#include "xls/dslx/type_system/deduce_ctx.h"
Expand Down
10 changes: 10 additions & 0 deletions xls/dslx/type_system/typecheck_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,16 @@ fn main(p: u32) -> () {
HasSubstr("const_assert! expression is not constexpr")));
}

TEST(TypecheckErrorTest, FitsInTypeSN0) {
EXPECT_THAT(Typecheck(R"(
fn main() -> sN[0] {
sN[0]:0xffff_ffff_ffff_ffff_ffff
})"),
StatusIs(absl::StatusCode::kInvalidArgument,
HasSubstr("Value '0xffff_ffff_ffff_ffff_ffff' does not "
"fit in the bitwidth of a sN[0]")));
}

TEST(TypecheckTest, ForBuiltinInBody) {
XLS_EXPECT_OK(Typecheck(R"(
fn f() -> u32 {
Expand Down
6 changes: 6 additions & 0 deletions xls/ir/bits.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,17 @@ Bits Bits::AllOnes(int64_t bit_count) {

/* static */
Bits Bits::MaxSigned(int64_t bit_count) {
if (bit_count == 0) {
return SBits(0, 0);
}
return Bits::AllOnes(bit_count).UpdateWithSet(bit_count - 1, false);
}

/* static */
Bits Bits::MinSigned(int64_t bit_count) {
if (bit_count == 0) {
return SBits(0, 0);
}
return Bits::PowerOfTwo(bit_count - 1, bit_count);
}

Expand Down
1 change: 1 addition & 0 deletions xls/ir/bits.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <vector>

#include "absl/status/statusor.h"
#include "absl/types/span.h"
#include "xls/common/logging/logging.h"
#include "xls/common/math_util.h"
#include "xls/data_structures/inline_bitmap.h"
Expand Down

0 comments on commit aebb40c

Please sign in to comment.