From f9aadc13b56dee13a0bbfd1ec43ddcf46f5f2c72 Mon Sep 17 00:00:00 2001 From: Lars-Peter Clausen Date: Tue, 20 Sep 2022 19:57:27 +0200 Subject: [PATCH 1/3] Fix memory leaks when parsing function calls For function calls when calling the PECallFunction() constructor a copy is made of the argument expression list. This means the original list should be deleted within the rule. Signed-off-by: Lars-Peter Clausen --- parse.y | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/parse.y b/parse.y index a8719965d..a20a98615 100644 --- a/parse.y +++ b/parse.y @@ -3704,6 +3704,7 @@ expr_primary PECallFunction*tmp = pform_make_call_function(@1, *$1, *expr_list); delete $1; delete $2; + delete expr_list; $$ = tmp; } | class_hierarchy_identifier '(' expression_list_with_nuls ')' @@ -3711,6 +3712,7 @@ expr_primary strip_tail_items(expr_list); PECallFunction*tmp = pform_make_call_function(@1, *$1, *expr_list); delete $1; + delete expr_list; $$ = tmp; } | SYSTEM_IDENTIFIER '(' expression_list_proper ')' @@ -3718,6 +3720,7 @@ expr_primary PECallFunction*tmp = new PECallFunction(tn, *$3); FILE_NAME(tmp, @1); delete[]$1; + delete $3; $$ = tmp; } | PACKAGE_IDENTIFIER K_SCOPE_RES IDENTIFIER '(' expression_list_proper ')' @@ -3725,6 +3728,7 @@ expr_primary PECallFunction*tmp = new PECallFunction($1, use_name, *$5); FILE_NAME(tmp, @3); delete[]$3; + delete $5; $$ = tmp; } | SYSTEM_IDENTIFIER '(' ')' From fb8681a37688c23e14952d80cc542979079dff20 Mon Sep 17 00:00:00 2001 From: Lars-Peter Clausen Date: Tue, 20 Sep 2022 12:54:48 +0200 Subject: [PATCH 2/3] Allow package scoped functions to be called without arguments The parser currently only allows package scoped functions to be called if there is at least one argument. But package scoped functions are the same as normal functions and it is allowed to call them with no arguments. It is even possible to pass no value for a positional argument, if the positional argument has a default value. Update the parser to handle this. Signed-off-by: Lars-Peter Clausen --- parse.y | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parse.y b/parse.y index a20a98615..824ccf9cb 100644 --- a/parse.y +++ b/parse.y @@ -3723,7 +3723,7 @@ expr_primary delete $3; $$ = tmp; } - | PACKAGE_IDENTIFIER K_SCOPE_RES IDENTIFIER '(' expression_list_proper ')' + | PACKAGE_IDENTIFIER K_SCOPE_RES IDENTIFIER '(' expression_list_with_nuls ')' { perm_string use_name = lex_strings.make($3); PECallFunction*tmp = new PECallFunction($1, use_name, *$5); FILE_NAME(tmp, @3); From 6fe3e52085d307d924f54f308257661234872299 Mon Sep 17 00:00:00 2001 From: Lars-Peter Clausen Date: Tue, 20 Sep 2022 19:52:28 +0200 Subject: [PATCH 3/3] Add regression tests for package scoped function calls Check that package scope function calls work with and without arguments as well as empty positional arguments. Signed-off-by: Lars-Peter Clausen --- ivtest/ivltests/sv_ps_function1.v | 20 ++++++++++++++++++++ ivtest/ivltests/sv_ps_function2.v | 21 +++++++++++++++++++++ ivtest/ivltests/sv_ps_function3.v | 21 +++++++++++++++++++++ ivtest/regress-sv.list | 3 +++ 4 files changed, 65 insertions(+) create mode 100644 ivtest/ivltests/sv_ps_function1.v create mode 100644 ivtest/ivltests/sv_ps_function2.v create mode 100644 ivtest/ivltests/sv_ps_function3.v diff --git a/ivtest/ivltests/sv_ps_function1.v b/ivtest/ivltests/sv_ps_function1.v new file mode 100644 index 000000000..17edbb7a4 --- /dev/null +++ b/ivtest/ivltests/sv_ps_function1.v @@ -0,0 +1,20 @@ +// Check that it is possible to call a package scoped function. + +package P; + function integer T(integer x, integer y); + return x + y; + endfunction +endpackage + +module test; + initial begin + integer x; + x = P::T(1, 2); + if (x === 3) begin + $display("PASSED"); + end else begin + $display("FAILED. x = %d, expected 3", x); + end + end + +endmodule diff --git a/ivtest/ivltests/sv_ps_function2.v b/ivtest/ivltests/sv_ps_function2.v new file mode 100644 index 000000000..cd6c7b7f6 --- /dev/null +++ b/ivtest/ivltests/sv_ps_function2.v @@ -0,0 +1,21 @@ +// Check that it is possible to call a package scoped function with no +// arguments. + +package P; + function integer T(); + return 1; + endfunction +endpackage + +module test; + initial begin + integer x; + x = P::T(); + if (x === 1) begin + $display("PASSED"); + end else begin + $display("FAILED: x = %d, expected 1", x); + end + end + +endmodule diff --git a/ivtest/ivltests/sv_ps_function3.v b/ivtest/ivltests/sv_ps_function3.v new file mode 100644 index 000000000..92d45a1c3 --- /dev/null +++ b/ivtest/ivltests/sv_ps_function3.v @@ -0,0 +1,21 @@ +// Check that it is possible to call a package scoped function with empty +// positional arguments. + +package P; + function integer T(integer x = 1, integer y = 2); + return x + y; + endfunction +endpackage + +module test; + initial begin + integer x; + x = P::T(, 4); + if (x === 5) begin + $display("PASSED"); + end else begin + $display("FAILED. x = %d, expected 5", x); + end + end + +endmodule diff --git a/ivtest/regress-sv.list b/ivtest/regress-sv.list index acc997669..91afa0244 100644 --- a/ivtest/regress-sv.list +++ b/ivtest/regress-sv.list @@ -609,6 +609,9 @@ sv_port_default11 normal,-g2009 ivltests sv_port_default12 normal,-g2009 ivltests sv_port_default13 CE,-g2009 ivltests sv_port_default14 CE,-g2009 ivltests +sv_ps_function1 normal,-g2009 ivltests +sv_ps_function2 normal,-g2009 ivltests +sv_ps_function3 normal,-g2009 ivltests sv_queue1 normal,-g2009 ivltests sv_queue2 normal,-g2009 ivltests sv_queue3 normal,-g2009 ivltests