From 9ffaae91ffd4c0c062ec794899c05be5f64768de Mon Sep 17 00:00:00 2001 From: Aki Van Ness Date: Wed, 24 Aug 2022 11:28:51 -0400 Subject: [PATCH 1/3] frontend: rtlil: imposed a hard limit for wire width of 2^24 (closes #1206) --- frontends/rtlil/rtlil_parser.y | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/frontends/rtlil/rtlil_parser.y b/frontends/rtlil/rtlil_parser.y index 7d99b2c42..98f18db91 100644 --- a/frontends/rtlil/rtlil_parser.y +++ b/frontends/rtlil/rtlil_parser.y @@ -187,7 +187,11 @@ wire_stmt: wire_options: wire_options TOK_WIDTH TOK_INT { - current_wire->width = $3; + if ($3 > 0x1000000) { + rtlil_frontend_yyerror("RTLIL error: invalid wire width, must be less than 2^24"); + } else { + current_wire->width = $3; + } } | wire_options TOK_WIDTH TOK_INVALID { rtlil_frontend_yyerror("RTLIL error: invalid wire width"); From 2b9982101aa01213f880afcebd5d7ea2ce520d95 Mon Sep 17 00:00:00 2001 From: Aki Van Ness Date: Wed, 24 Aug 2022 11:31:28 -0400 Subject: [PATCH 2/3] kernel: rtlil: imposed a hard limit on RTLIL::Const of 2^24 (closes #3317) --- kernel/rtlil.cc | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/kernel/rtlil.cc b/kernel/rtlil.cc index 5211c3b3f..8df719aa9 100644 --- a/kernel/rtlil.cc +++ b/kernel/rtlil.cc @@ -201,6 +201,9 @@ const pool &RTLIL::builtin_ff_cell_types() { RTLIL::Const::Const(const std::string &str) { + if (str.size() * 8 > 0x1000000) + throw std::length_error("RTLIL Const width must be less than 2^24"); + flags = RTLIL::CONST_FLAG_STRING; bits.reserve(str.size() * 8); for (int i = str.size()-1; i >= 0; i--) { @@ -214,6 +217,12 @@ RTLIL::Const::Const(const std::string &str) RTLIL::Const::Const(int val, int width) { + if (width < 0) + throw std::length_error("RTLIL Const width must not be negative"); + + if (width > 0x1000000) + throw std::length_error("RTLIL Const width must be less than 2^24"); + flags = RTLIL::CONST_FLAG_NONE; bits.reserve(width); for (int i = 0; i < width; i++) { @@ -224,6 +233,12 @@ RTLIL::Const::Const(int val, int width) RTLIL::Const::Const(RTLIL::State bit, int width) { + if (width < 0) + throw std::length_error("RTLIL Const width must not be negative"); + + if (width > 0x1000000) + throw std::length_error("RTLIL Const width must be less than 2^24"); + flags = RTLIL::CONST_FLAG_NONE; bits.reserve(width); for (int i = 0; i < width; i++) @@ -232,6 +247,10 @@ RTLIL::Const::Const(RTLIL::State bit, int width) RTLIL::Const::Const(const std::vector &bits) { + + if (bits.size() > 0x1000000) + throw std::length_error("RTLIL Const width must be less than 2^24"); + flags = RTLIL::CONST_FLAG_NONE; this->bits.reserve(bits.size()); for (const auto &b : bits) @@ -296,6 +315,10 @@ std::string RTLIL::Const::as_string() const RTLIL::Const RTLIL::Const::from_string(const std::string &str) { + + if (str.size() > 0x1000000) + throw std::length_error("RTLIL width must be less than 2^24"); + Const c; c.bits.reserve(str.size()); for (auto it = str.rbegin(); it != str.rend(); it++) From 42e4610e3a7bdad57e41e4592cb5d161a3a2ff5b Mon Sep 17 00:00:00 2001 From: Aki Van Ness Date: Tue, 6 Sep 2022 19:43:46 -0400 Subject: [PATCH 3/3] kernel: rtlil: replaced the width limit exceptions with `log_error` --- kernel/rtlil.cc | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/kernel/rtlil.cc b/kernel/rtlil.cc index 8df719aa9..59ce28119 100644 --- a/kernel/rtlil.cc +++ b/kernel/rtlil.cc @@ -202,7 +202,7 @@ const pool &RTLIL::builtin_ff_cell_types() { RTLIL::Const::Const(const std::string &str) { if (str.size() * 8 > 0x1000000) - throw std::length_error("RTLIL Const width must be less than 2^24"); + log_error("RTLIL Const width must be less than 2^24"); flags = RTLIL::CONST_FLAG_STRING; bits.reserve(str.size() * 8); @@ -218,10 +218,10 @@ RTLIL::Const::Const(const std::string &str) RTLIL::Const::Const(int val, int width) { if (width < 0) - throw std::length_error("RTLIL Const width must not be negative"); + log_error("RTLIL Const width must not be negative"); if (width > 0x1000000) - throw std::length_error("RTLIL Const width must be less than 2^24"); + log_error("RTLIL Const width must be less than 2^24"); flags = RTLIL::CONST_FLAG_NONE; bits.reserve(width); @@ -234,10 +234,10 @@ RTLIL::Const::Const(int val, int width) RTLIL::Const::Const(RTLIL::State bit, int width) { if (width < 0) - throw std::length_error("RTLIL Const width must not be negative"); + log_error("RTLIL Const width must not be negative"); if (width > 0x1000000) - throw std::length_error("RTLIL Const width must be less than 2^24"); + log_error("RTLIL Const width must be less than 2^24"); flags = RTLIL::CONST_FLAG_NONE; bits.reserve(width); @@ -249,7 +249,7 @@ RTLIL::Const::Const(const std::vector &bits) { if (bits.size() > 0x1000000) - throw std::length_error("RTLIL Const width must be less than 2^24"); + log_error("RTLIL Const width must be less than 2^24"); flags = RTLIL::CONST_FLAG_NONE; this->bits.reserve(bits.size()); @@ -317,7 +317,7 @@ RTLIL::Const RTLIL::Const::from_string(const std::string &str) { if (str.size() > 0x1000000) - throw std::length_error("RTLIL width must be less than 2^24"); + log_error("RTLIL width must be less than 2^24"); Const c; c.bits.reserve(str.size());