Skip to content

Commit

Permalink
Disallow access to protected members from class scoped randomize cons…
Browse files Browse the repository at this point in the history
…traint blocks
  • Loading branch information
MikePopoloski committed Oct 5, 2024
1 parent d80d984 commit 2ba4087
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
10 changes: 8 additions & 2 deletions source/ast/Lookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1621,10 +1621,16 @@ bool Lookup::withinClassRandomize(const ASTContext& context, const NameSyntax& s
if (!isClass.has_value() || !isClass.value())
return false;

return resolveColonNames(nameParts, colonParts, name, flags, result, classContext);
if (!resolveColonNames(nameParts, colonParts, name, flags, result, classContext))
return false;
}
else {
if (!lookupDownward(nameParts, name, classContext, flags, result))
return false;
}

return lookupDownward(nameParts, name, classContext, flags, result);
unwrapResult(*context.scope, syntax.sourceRange(), result);
return true;
}

bool Lookup::findAssertionLocalVar(const ASTContext& context, const NameSyntax& syntax,
Expand Down
27 changes: 27 additions & 0 deletions tests/unittests/ast/ClassTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3483,3 +3483,30 @@ endmodule
compilation.addSyntaxTree(tree);
NO_COMPILATION_ERRORS;
}

TEST_CASE("Class randomize can't access protected members") {
auto tree = SyntaxTree::fromText(R"(
class C;
protected int a;
rand bit b;
endclass
class T;
function f();
C c = new();
int i = c.randomize() with {
if (a == 3) {
b == 1'b1;
}
};
endfunction
endclass
)");

Compilation compilation;
compilation.addSyntaxTree(tree);

auto& diags = compilation.getAllDiagnostics();
REQUIRE(diags.size() == 1);
CHECK(diags[0].code == diag::ProtectedMemberAccess);
}

0 comments on commit 2ba4087

Please sign in to comment.