Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash on package definition in interface decl. #1083

Merged
merged 1 commit into from
Dec 5, 2024

Conversation

NikLeberg
Copy link
Contributor

My own instance of a fuzzer (libFuzzer based, nvc-fuzz) found a crash.

If instead of the usual package <x> is new ... in an interface a full package definition package <x> is ... end package; was given, then parsing did crash.

With the added simple checks, the crash no longer happens and the parser gives the reasonable error of unexpected end while parsing interface package declaration, expecting new.

Note: The standard is a bit moot in the definition, but I think to understand it so that generic package in interfaces may only be defined in generic interfaces.

An interface package declaration declares an interface package that appears in a generic clause. [VHDL2019, Chapter 6.5.5]

But NVC allows them everywhere. E.g. the following is analyzing, elaborating and running without error:

package gen_pkg is
    generic (N : integer);
    constant c_pkg : integer := N;
end package;

entity ent is
    generic (
        package p0 is new work.gen_pkg generic map(<>);
    );
    port (
        package p1 is new work.gen_pkg generic map(<>); -- no error?
    );
end entity;

architecture arch of ent is
    constant c0 : integer := p0.c_pkg;
    constant c1 : integer := p1.c_pkg;
begin
end architecture;

Cheers

@NikLeberg NikLeberg force-pushed the pkg_in_decl branch 2 times, most recently from 223658d to 14e19ec Compare December 3, 2024 07:54
@nickg
Copy link
Owner

nickg commented Dec 3, 2024

Note: The standard is a bit moot in the definition, but I think to understand it so that generic package in interfaces may only be defined in generic interfaces.

I think the relevant bit is in 6.5.6.1:

A generic interface list consists entirely of interface constant declarations, interface type declarations, interface subprogram declarations, and interface package declarations. A port interface list consists entirely of interface signal declarations. A parameter interface list may contain interface constant declarations, interface signal declarations, interface variable declarations, interface file declarations, or any combination thereof.

sem_check_param_decl is missing this check, I guess because any class was valid in 1993. E.g. sem_check_port_decl does

   ...
   else if (class != C_SIGNAL)
      sem_error(t, "invalid object class %s for port %s",
                class_str(class), istr(tree_ident(t)));

src/parse.c Outdated
Comment on lines 5539 to 5540
if (tree_has_type(p0))
type_add_param(type, tree_type(p0));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (tree_has_type(p0))
type_add_param(type, tree_type(p0));
if (class_has_type(tree_class(p0)))
type_add_param(type, tree_type(p0));
else
type_add_param(type, type_new(T_NONE)); // Will raise error later

src/parse.c Outdated
Comment on lines 5542 to 5543
if (tree_has_value(p0))
tree_set_flag(decl, TREE_F_CALL_NO_ARGS);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can merge this into the loop below to avoid duplicating the check above. E.g.

if (i == 0 && tree_has_value(p))
   tree_set_flag(decl, TREE_F_CALL_NO_ARGS);

@NikLeberg
Copy link
Contributor Author

Thanks for the review. Implemented the suggested change and merged the special case for the first port into the loop.

@nickg nickg merged commit b6b047d into nickg:master Dec 5, 2024
12 checks passed
@NikLeberg NikLeberg deleted the pkg_in_decl branch December 5, 2024 09:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants