From f4d55081ed7053b777617542fd5ac3f607268ff0 Mon Sep 17 00:00:00 2001 From: Lars-Peter Clausen Date: Sun, 25 Sep 2022 19:00:46 +0200 Subject: [PATCH 1/2] Don't allow access to imported identifiers through hierarchical names Imported identifiers should only be visible in the scope they have been imported too. They should not be accessible through hierarchical names into that scope. This is defined in section 26.3 ("Referencing data in packages") of the LRM (1800-2017). Modify the symbol search to not look at imports if the name is part of a hierarchical path. Signed-off-by: Lars-Peter Clausen --- symbol_search.cc | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/symbol_search.cc b/symbol_search.cc index 2b6cc707f..b7e3df15b 100644 --- a/symbol_search.cc +++ b/symbol_search.cc @@ -202,11 +202,6 @@ bool symbol_search(const LineInfo*li, Design*des, NetScope*scope, } } - if (NetScope*import_scope = scope->find_import(des, path_tail.name)) { - scope = import_scope; - continue; - } - // Could not find an object. Maybe this is a child scope name? If // so, evaluate the path components to find the exact scope this // refers to. This item might be: @@ -234,6 +229,12 @@ bool symbol_search(const LineInfo*li, Design*des, NetScope*scope, if (prefix_scope) break; + // Imports are not visible through hierachical names + if (NetScope*import_scope = scope->find_import(des, path_tail.name)) { + scope = import_scope; + continue; + } + // Special case: We can match the module name of a parent // module. That means if the current scope is a module of type // "mod", then "mod" matches the current scope. This is fairly From f9a0e2f401dca60a9e72f41620b81a318e74b329 Mon Sep 17 00:00:00 2001 From: Lars-Peter Clausen Date: Sat, 31 Dec 2022 14:28:13 -0800 Subject: [PATCH 2/2] Add regression tests for accessing imported identifiers through hierarchical names Check that an error is reported when trying to access an imported identifier through a hierarchical name. Signed-off-by: Lars-Peter Clausen --- ivtest/ivltests/sv_import_hier_fail1.v | 24 ++++++++++++++++++++++++ ivtest/ivltests/sv_import_hier_fail2.v | 24 ++++++++++++++++++++++++ ivtest/ivltests/sv_import_hier_fail3.v | 20 ++++++++++++++++++++ ivtest/regress-sv.list | 3 +++ 4 files changed, 71 insertions(+) create mode 100644 ivtest/ivltests/sv_import_hier_fail1.v create mode 100644 ivtest/ivltests/sv_import_hier_fail2.v create mode 100644 ivtest/ivltests/sv_import_hier_fail3.v diff --git a/ivtest/ivltests/sv_import_hier_fail1.v b/ivtest/ivltests/sv_import_hier_fail1.v new file mode 100644 index 000000000..7504d2e93 --- /dev/null +++ b/ivtest/ivltests/sv_import_hier_fail1.v @@ -0,0 +1,24 @@ +// Check that imported identifiers can't be accessed through hierarchical names. + +package P; + integer x; +endpackage + +module M; + import P::x; + integer y; + always_comb y = x; +endmodule + +module test; + + M m (); + + initial begin + integer y; + y = m.x; // This should fail. Imported identifiers are not visible through + // hierarchical names. + $display("FAILED"); + end + +endmodule diff --git a/ivtest/ivltests/sv_import_hier_fail2.v b/ivtest/ivltests/sv_import_hier_fail2.v new file mode 100644 index 000000000..be71fca59 --- /dev/null +++ b/ivtest/ivltests/sv_import_hier_fail2.v @@ -0,0 +1,24 @@ +// Check that imported identifiers can't be accessed through hierarchical names. + +package P; + integer x; +endpackage + +module M; + import P::*; + integer y; + always_comb y = x; +endmodule + +module test; + + M m (); + + initial begin + integer y; + y = m.x; // This should fail. Imported identifiers are not visible through + // hierarchical names. + $display("FAILED"); + end + +endmodule diff --git a/ivtest/ivltests/sv_import_hier_fail3.v b/ivtest/ivltests/sv_import_hier_fail3.v new file mode 100644 index 000000000..4db81ecad --- /dev/null +++ b/ivtest/ivltests/sv_import_hier_fail3.v @@ -0,0 +1,20 @@ +// Check that imported identifiers can't be accessed through hierarchical names. + +package P; + integer x; +endpackage + +module test; + + initial begin : outer + integer y; + begin: inner + import P::x; + y = x; + end + y = inner.x; // This should fail. Imported identifiers are not visible + // through hierarchical names. + $display("FAILED"); + end + +endmodule diff --git a/ivtest/regress-sv.list b/ivtest/regress-sv.list index df5cb1a6c..bc40acc85 100644 --- a/ivtest/regress-sv.list +++ b/ivtest/regress-sv.list @@ -656,6 +656,9 @@ sv_foreach8 normal,-g2009 ivltests gold=sv_foreach8.gold sv_foreach_fail1 CE,-g2009 ivltests sv_immediate_assert normal,-g2009 ivltests gold=sv_immediate_assert.gold sv_immediate_assume normal,-g2009 ivltests gold=sv_immediate_assume.gold +sv_import_hier_fail1 CE,-g2005-sv ivltests +sv_import_hier_fail2 CE,-g2005-sv ivltests +sv_import_hier_fail3 CE,-g2005-sv ivltests sv_macro normal,-g2009 ivltests sv_macro2 normal,-g2009 ivltests gold=sv_macro2.gold sv_macro3a normal,-g2009 ivltests gold=sv_macro3.gold