Skip to content

Commit aebb40c

Browse files
cdlearycopybara-github
authored andcommitted
Fix fuzz bug that found 0-bit width issue. (Resulted in overshift.)
PiperOrigin-RevId: 586138891
1 parent 40bcfc6 commit aebb40c

File tree

6 files changed

+20
-0
lines changed

6 files changed

+20
-0
lines changed

xls/dslx/type_system/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ cc_library(
9393
":parametric_env",
9494
":parametric_instantiator",
9595
":type_and_parametric_env",
96+
":type_info",
9697
":unwrap_meta_type",
9798
"@com_google_absl//absl/algorithm:container",
9899
"@com_google_absl//absl/cleanup",

xls/dslx/type_system/deduce.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
#include "xls/dslx/type_system/parametric_env.h"
6161
#include "xls/dslx/type_system/parametric_instantiator.h"
6262
#include "xls/dslx/type_system/type_and_parametric_env.h"
63+
#include "xls/dslx/type_system/type_info.h"
6364
#include "xls/dslx/type_system/unwrap_meta_type.h"
6465
#include "xls/dslx/warning_kind.h"
6566
#include "xls/ir/bits.h"

xls/dslx/type_system/deduce.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "absl/status/statusor.h"
2222
#include "absl/types/span.h"
23+
#include "xls/dslx/frontend/ast.h"
2324
#include "xls/dslx/frontend/ast_node.h"
2425
#include "xls/dslx/type_system/concrete_type.h"
2526
#include "xls/dslx/type_system/deduce_ctx.h"

xls/dslx/type_system/typecheck_test.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,16 @@ fn main(p: u32) -> () {
405405
HasSubstr("const_assert! expression is not constexpr")));
406406
}
407407

408+
TEST(TypecheckErrorTest, FitsInTypeSN0) {
409+
EXPECT_THAT(Typecheck(R"(
410+
fn main() -> sN[0] {
411+
sN[0]:0xffff_ffff_ffff_ffff_ffff
412+
})"),
413+
StatusIs(absl::StatusCode::kInvalidArgument,
414+
HasSubstr("Value '0xffff_ffff_ffff_ffff_ffff' does not "
415+
"fit in the bitwidth of a sN[0]")));
416+
}
417+
408418
TEST(TypecheckTest, ForBuiltinInBody) {
409419
XLS_EXPECT_OK(Typecheck(R"(
410420
fn f() -> u32 {

xls/ir/bits.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,17 @@ Bits Bits::AllOnes(int64_t bit_count) {
7575

7676
/* static */
7777
Bits Bits::MaxSigned(int64_t bit_count) {
78+
if (bit_count == 0) {
79+
return SBits(0, 0);
80+
}
7881
return Bits::AllOnes(bit_count).UpdateWithSet(bit_count - 1, false);
7982
}
8083

8184
/* static */
8285
Bits Bits::MinSigned(int64_t bit_count) {
86+
if (bit_count == 0) {
87+
return SBits(0, 0);
88+
}
8389
return Bits::PowerOfTwo(bit_count - 1, bit_count);
8490
}
8591

xls/ir/bits.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <vector>
2222

2323
#include "absl/status/statusor.h"
24+
#include "absl/types/span.h"
2425
#include "xls/common/logging/logging.h"
2526
#include "xls/common/math_util.h"
2627
#include "xls/data_structures/inline_bitmap.h"

0 commit comments

Comments
 (0)