From e5836ab740a241e393547d72355b8a64c475538d Mon Sep 17 00:00:00 2001 From: tignear Date: Mon, 26 Oct 2026 05:13:34 +0900 Subject: [PATCH] fix(sv)!: enforce class member access visibility Reject local and protected property and method access from unauthorized lexical class scopes, including static members and constructors. Preserve same-class access through another instance, inherited base methods, and protected access from derived classes (IEEE 1800-2023 8.18). Use a dedicated InaccessibleClassMember diagnostic and share the access check with randomization. Update the five affected negative sv-tests cases in both state modes; CI verifies the full release-driver corpus. Validation: analyzer unit tests (812 passed, 3 existing ignored), combined analyzer tests (814 passed, 3 ignored), focused positive/negative access regressions, Clippy and formatting. Full release sv-tests runs in CI as requested. Class backend integration is deferred to CI; the local optimized executor build was stopped before running cases. BREAKING CHANGE: AnalyzerError gains InaccessibleClassMember; exhaustive Rust matches must handle the new diagnostic. Change-Id: I8d06c5438d529abac8c32c9ce7eec9cdd36764a9 --- conformance/sv-tests/expected.tsv | 20 +-- crates/celox-sv-analyzer/src/ast/classes.rs | 4 + .../src/ast/expression_binding.rs | 6 +- .../src/ast/expression_binding/objects.rs | 138 +++++++++++++---- .../celox-sv-analyzer/src/ast/procedural.rs | 15 ++ crates/celox-sv-analyzer/src/lib.rs | 7 + crates/celox-sv-analyzer/src/tests.rs | 141 +++++++++++++++++- 7 files changed, 288 insertions(+), 43 deletions(-) diff --git a/conformance/sv-tests/expected.tsv b/conformance/sv-tests/expected.tsv index dd7fd87a78c..b4e64f909ce 100644 --- a/conformance/sv-tests/expected.tsv +++ b/conformance/sv-tests/expected.tsv @@ -1833,22 +1833,22 @@ generated/binary_op/11.4.8--binary_op_bit_xor.sv 2state pass generated/binary_op/11.4.8--binary_op_bit_xor.sv 4state pass generated/easyUVM/top.sv 2state fail generated/easyUVM/top.sv 4state fail -generated/encapsulation/8.18--inherited_local_from_inside.sv 2state fail -generated/encapsulation/8.18--inherited_local_from_inside.sv 4state fail -generated/encapsulation/8.18--inherited_local_from_outside.sv 2state fail -generated/encapsulation/8.18--inherited_local_from_outside.sv 4state fail +generated/encapsulation/8.18--inherited_local_from_inside.sv 2state pass +generated/encapsulation/8.18--inherited_local_from_inside.sv 4state pass +generated/encapsulation/8.18--inherited_local_from_outside.sv 2state pass +generated/encapsulation/8.18--inherited_local_from_outside.sv 4state pass generated/encapsulation/8.18--inherited_prot_from_inside.sv 2state pass generated/encapsulation/8.18--inherited_prot_from_inside.sv 4state pass -generated/encapsulation/8.18--inherited_prot_from_outside.sv 2state fail -generated/encapsulation/8.18--inherited_prot_from_outside.sv 4state fail +generated/encapsulation/8.18--inherited_prot_from_outside.sv 2state pass +generated/encapsulation/8.18--inherited_prot_from_outside.sv 4state pass generated/encapsulation/8.18--local_from_inside.sv 2state pass generated/encapsulation/8.18--local_from_inside.sv 4state pass -generated/encapsulation/8.18--local_from_outside.sv 2state fail -generated/encapsulation/8.18--local_from_outside.sv 4state fail +generated/encapsulation/8.18--local_from_outside.sv 2state pass +generated/encapsulation/8.18--local_from_outside.sv 4state pass generated/encapsulation/8.18--prot_from_inside.sv 2state pass generated/encapsulation/8.18--prot_from_inside.sv 4state pass -generated/encapsulation/8.18--prot_from_outside.sv 2state fail -generated/encapsulation/8.18--prot_from_outside.sv 4state fail +generated/encapsulation/8.18--prot_from_outside.sv 2state pass +generated/encapsulation/8.18--prot_from_outside.sv 4state pass generated/equality_operators_sim/11.4.5--equality_operator_0.sv 2state fail generated/equality_operators_sim/11.4.5--equality_operator_0.sv 4state fail generated/equality_operators_sim/11.4.5--equality_operator_1.sv 2state fail diff --git a/crates/celox-sv-analyzer/src/ast/classes.rs b/crates/celox-sv-analyzer/src/ast/classes.rs index 467292a8c8e..4d60ddf1d3c 100644 --- a/crates/celox-sv-analyzer/src/ast/classes.rs +++ b/crates/celox-sv-analyzer/src/ast/classes.rs @@ -58,6 +58,8 @@ impl Property { #[derive(Debug, Clone, PartialEq, Eq)] pub(super) struct StaticProperty { pub name: String, + pub local: bool, + pub protected: bool, /// The class that declares the property. pub owner: String, pub r#type: Type, @@ -460,6 +462,8 @@ pub(super) fn class( if property.qualifiers.is_static { statics.retain(|known| known.name != property_name); statics.push(StaticProperty { + local: property.qualifiers.local, + protected: property.qualifiers.protected, variable: Class::subroutine_name(name, &property_name), name: property_name, owner: name.to_owned(), diff --git a/crates/celox-sv-analyzer/src/ast/expression_binding.rs b/crates/celox-sv-analyzer/src/ast/expression_binding.rs index aa217e1c1b2..dc6bc26f503 100644 --- a/crates/celox-sv-analyzer/src/ast/expression_binding.rs +++ b/crates/celox-sv-analyzer/src/ast/expression_binding.rs @@ -16,9 +16,9 @@ pub(super) use direct::{ pub(super) use lvalues::{net_lvalue, variable_lvalue}; pub(super) use objects::{ Cast as ObjectCast, Receiver as ObjectReceiver, cast as object_cast, - check_assignment as check_handle_assignment, constructor as object_constructor, - method as object_method, property as object_property, rand_mode_call as object_rand_mode_call, - receiver as object_receiver, this as object_this, + check_access as check_member_access, check_assignment as check_handle_assignment, + constructor as object_constructor, method as object_method, property as object_property, + rand_mode_call as object_rand_mode_call, receiver as object_receiver, this as object_this, }; pub(super) use strings::{ call as string_call, method as string_method, native_string, to_string as string_operand, diff --git a/crates/celox-sv-analyzer/src/ast/expression_binding/objects.rs b/crates/celox-sv-analyzer/src/ast/expression_binding/objects.rs index 1c91118b142..c5417adf83e 100644 --- a/crates/celox-sv-analyzer/src/ast/expression_binding/objects.rs +++ b/crates/celox-sv-analyzer/src/ast/expression_binding/objects.rs @@ -53,6 +53,38 @@ pub(in crate::ast) fn native_class( property.r#type.class().map(str::to_owned) } +/// Access depends on the lexical class, not the class of the receiver (8.18). +pub(in crate::ast) fn check_access( + tree: &AnalysisContext<'_>, + dimensions: &PackedDimensions, + owner: &str, + name: &str, + local: bool, + protected: bool, +) -> Converted<()> { + if !local && !protected { + return Ok(()); + } + if let Some(scope) = dimensions.class_scope.as_deref() { + if scope == owner + || (!local + && class(tree, dimensions, scope)?.derives_from( + tree, + owner, + &dimensions.const_env, + &dimensions.type_aliases, + )) + { + return Ok(()); + } + } + Err(AnalyzerError::InaccessibleClassMember { + owner: owner.to_owned(), + name: name.to_owned(), + visibility: if local { "local" } else { "protected" }, + }) +} + /// The class whose method is being bound, whose properties and methods /// unqualified names may refer to. pub(in crate::ast) fn scope_class(dimensions: &PackedDimensions) -> Option { @@ -115,9 +147,20 @@ pub(in crate::ast) fn static_property( let Some(class_name) = class_name else { return Ok(None); }; - Ok(class(tree, dimensions, &class_name)? - .static_property(&instances::normalize_identifier(&name.spelling)) - .map(|property| property.variable.clone())) + let class = class(tree, dimensions, &class_name)?; + let Some(property) = class.static_property(&instances::normalize_identifier(&name.spelling)) + else { + return Ok(None); + }; + check_access( + tree, + dimensions, + &property.owner, + &property.name, + property.local, + property.protected, + )?; + Ok(Some(property.variable.clone())) } /// The property that native expression `id` names, `h.p` or an unqualified @@ -157,9 +200,28 @@ pub(in crate::ast) fn property( _ => return Ok(None), }; let class = class(tree, dimensions, &class_name)?; - let Some(property) = class.property(&instances::normalize_identifier(&name.spelling)) else { + let name = instances::normalize_identifier(&name.spelling); + let Some(property) = class.property(&name) else { + if let Some(property) = class.static_property(&name) { + check_access( + tree, + dimensions, + &property.owner, + &property.name, + property.local, + property.protected, + )?; + } return Ok(None); }; + check_access( + tree, + dimensions, + &property.owner, + &property.name, + property.local, + property.protected, + )?; if receiver.is_none() && dimensions.get(THIS).is_none() { return Err(unsupported(format!( "non-static property `{}` in a static method", @@ -202,8 +264,26 @@ pub(in crate::ast) fn unqualified_property( }; let class = class(tree, dimensions, &class_name)?; let Some(property) = class.property(name) else { + if let Some(property) = class.static_property(name) { + check_access( + tree, + dimensions, + &property.owner, + &property.name, + property.local, + property.protected, + )?; + } return Ok(None); }; + check_access( + tree, + dimensions, + &property.owner, + &property.name, + property.local, + property.protected, + )?; if dimensions.get(THIS).is_none() { return Err(unsupported(format!( "non-static property `{name}` in a static method" @@ -300,6 +380,14 @@ pub(in crate::ast) fn method( let Some(method) = class.method(&method_name) else { return Ok(None); }; + check_access( + tree, + dimensions, + &method.owner, + &method.name, + method.qualifiers.local, + method.qualifiers.protected, + )?; if through_super && method.is_pure() { return Err(unsupported(format!( "pure virtual method `{}` called through `super`", @@ -485,6 +573,16 @@ pub(in crate::ast) fn constructor( _ => return Ok(None), }; let class = class(tree, dimensions, &class_name)?; + if let Some(method) = class.method("new") { + check_access( + tree, + dimensions, + &method.owner, + &method.name, + method.qualifiers.local, + method.qualifiers.protected, + )?; + } if class.is_abstract { return Err(unsupported(format!( "object of abstract class `{class_name}`" @@ -674,28 +772,12 @@ fn check_random_access( dimensions: &PackedDimensions, property: &classes::Property, ) -> Converted<()> { - let scope = scope_class(dimensions); - let accessible = if property.local { - scope.as_deref() == Some(&property.owner) - } else if property.protected { - scope - .and_then(|name| class(tree, dimensions, &name).ok()) - .is_some_and(|class| { - class.derives_from( - tree, - &property.owner, - &dimensions.const_env, - &dimensions.type_aliases, - ) - }) - } else { - true - }; - if !accessible { - return Err(unsupported(format!( - "randomization access to non-public property `{}`", - property.name - ))); - } - Ok(()) + check_access( + tree, + dimensions, + &property.owner, + &property.name, + property.local, + property.protected, + ) } diff --git a/crates/celox-sv-analyzer/src/ast/procedural.rs b/crates/celox-sv-analyzer/src/ast/procedural.rs index 17604d9017c..ec9374ac1a0 100644 --- a/crates/celox-sv-analyzer/src/ast/procedural.rs +++ b/crates/celox-sv-analyzer/src/ast/procedural.rs @@ -283,6 +283,11 @@ impl<'s, 't, 'a> BodyBuilder<'s, 't, 'a> { .push(entry); } for property in &class.statics { + // A base's local static is not visible in the derived class (8.18). + // Keep it out of the alias table so binding can diagnose its use. + if property.local && property.owner != class.name { + continue; + } let entry = self.take_binding( &property.name, Some(property.variable.clone()), @@ -2468,6 +2473,16 @@ impl BodyBuilder<'_, '_, '_> { &self.dims.const_env, &self.dims.type_aliases, )?; + if let Some(method) = base.method("new") { + expression_binding::check_member_access( + self.tree, + &self.dims, + &method.owner, + &method.name, + method.qualifiers.local, + method.qualifiers.protected, + )?; + } let formals = base.constructor_formals(native); if arguments.len() > formals { return Err(unsupported(format!( diff --git a/crates/celox-sv-analyzer/src/lib.rs b/crates/celox-sv-analyzer/src/lib.rs index 35997a2c289..09e54787e5b 100644 --- a/crates/celox-sv-analyzer/src/lib.rs +++ b/crates/celox-sv-analyzer/src/lib.rs @@ -66,6 +66,13 @@ pub enum AnalyzerError { Parse(String), #[error("Unsupported SystemVerilog construct: {0}")] Unsupported(String), + /// A class member is inaccessible from the lexical scope (IEEE 1800-2023 8.18). + #[error("{visibility} class member `{owner}::{name}` is not accessible from this scope")] + InaccessibleClassMember { + owner: String, + name: String, + visibility: &'static str, + }, /// A dotted name whose first component names no variable or interface /// of the scope: a hierarchical name (IEEE 1800-2023 23.6) that the /// caller did not resolve to a signal of another instance. diff --git a/crates/celox-sv-analyzer/src/tests.rs b/crates/celox-sv-analyzer/src/tests.rs index c3c271322e4..5c6a2c29d8c 100644 --- a/crates/celox-sv-analyzer/src/tests.rs +++ b/crates/celox-sv-analyzer/src/tests.rs @@ -6628,8 +6628,8 @@ fn class_randomize_rejects_unsupported_semantics() { "not an instance property", ), ("const int x = 1;", "c.randomize(x)", "const"), - ("local int x;", "c.randomize(x)", "non-public"), - ("protected int x;", "c.randomize(x)", "non-public"), + ("local int x;", "c.randomize(x)", "not accessible"), + ("protected int x;", "c.randomize(x)", "not accessible"), ("rand int x;", "c.rand_mode()", "getter requires"), ("int x;", "c.x.rand_mode()", "rand or randc"), ( @@ -6822,3 +6822,140 @@ fn class_randc_checks_domain_limits() { } } } + +#[test] +fn class_visibility_rejects_inaccessible_members() { + for visibility in ["local", "protected"] { + for member in [ + "int x;", + "static int x;", + "function int x(); return 1; endfunction", + "static function int x(); return 1; endfunction", + ] { + let access = if member.contains("function") { + "c.x()" + } else { + "c.x" + }; + for derived in [false, true] { + let declaration = if derived { + "class D extends C; endclass D c;" + } else { + "C c;" + }; + let source = format!( + "module Top; class C; {visibility} {member} endclass {declaration} int result; initial begin c=new; result={access}; end endmodule" + ); + let error = analyze_source(&source, Path::new("visibility.sv")).unwrap_err(); + assert!( + matches!(&error, AnalyzerError::InaccessibleClassMember { owner, .. } if owner == "C"), + "{source}: {error}" + ); + } + } + } + for member in [ + "int x;", + "static int x;", + "function int x(); return 1; endfunction", + "static function int x(); return 1; endfunction", + ] { + let access = if member.contains("function") { + "x()" + } else { + "x" + }; + for prefix in ["", "this.", "super."] { + let source = format!( + "module Top; class C; local {member} endclass class D extends C; function int read(); return {prefix}{access}; endfunction endclass D c; int result; initial begin c=new; result=c.read(); end endmodule" + ); + let error = analyze_source(&source, Path::new("visibility.sv")).unwrap_err(); + assert!( + matches!(&error, AnalyzerError::InaccessibleClassMember { owner, .. } if owner == "C"), + "{source}: {error}" + ); + } + } + for access in ["x=2", "x++"] { + let source = format!( + "module Top; class C; local static int x; endclass class D extends C; function void write(); {access}; endfunction endclass D c; initial begin c=new; c.write(); end endmodule" + ); + let error = analyze_source(&source, Path::new("visibility.sv")).unwrap_err(); + assert!( + matches!(&error, AnalyzerError::InaccessibleClassMember { owner, .. } if owner == "C"), + "{source}: {error}" + ); + } + for access in ["c.x=2", "c.x++", "C::x=2"] { + let source = format!( + "module Top; class C; local static int x; endclass C c; initial begin c=new; {access}; end endmodule" + ); + let error = analyze_source(&source, Path::new("visibility.sv")).unwrap_err(); + assert!( + matches!(&error, AnalyzerError::InaccessibleClassMember { owner, .. } if owner == "C"), + "{source}: {error}" + ); + } +} + +#[test] +fn class_visibility_accepts_lexical_access_and_shadowing() { + for member in [ + "int x;", + "static int x;", + "function int x(); return 1; endfunction", + "static function int x(); return 1; endfunction", + ] { + let access = if member.contains("function") { + "x()" + } else { + "x" + }; + for visibility in ["local", "protected"] { + let source = format!( + "module Top; class C; {visibility} {member} function int read(C other); return other.{access}; endfunction endclass class D extends C; endclass D c; int result; initial begin c=new; result=c.read(c); end endmodule" + ); + analyze_source(&source, Path::new("visibility.sv")) + .unwrap_or_else(|e| panic!("{source}: {e}")); + } + for prefix in ["", "this.", "super."] { + let source = format!( + "module Top; class C; protected {member} endclass class D extends C; function int read(); return {prefix}{access}; endfunction endclass D c; int result; initial begin c=new; result=c.read(); end endmodule" + ); + analyze_source(&source, Path::new("visibility.sv")) + .unwrap_or_else(|e| panic!("{source}: {e}")); + } + } + for storage in ["", "static"] { + let source = format!( + "module Top; class C; local {storage} int x; endclass class D extends C; int x; function int read(int x); return x; endfunction function int own(); return x; endfunction endclass D c; int result; initial begin c=new; c.x=2; result=c.own(); end endmodule" + ); + analyze_source(&source, Path::new("visibility.sv")).unwrap(); + } +} + +#[test] +fn class_visibility_checks_constructors() { + for visibility in ["local", "protected"] { + let source = format!( + "module Top; class C; {visibility} function new(); endfunction endclass C c; initial c=new; endmodule" + ); + let error = analyze_source(&source, Path::new("visibility.sv")).unwrap_err(); + assert!( + matches!(&error, AnalyzerError::InaccessibleClassMember { owner, .. } if owner == "C"), + "{source}: {error}" + ); + let source = format!( + "module Top; class C; {visibility} function new(); endfunction endclass class D extends C; endclass D c; initial c=new; endmodule" + ); + let result = analyze_source(&source, Path::new("visibility.sv")); + if visibility == "local" { + assert!(matches!( + result.unwrap_err(), + AnalyzerError::InaccessibleClassMember { .. } + )); + } else { + result.unwrap(); + } + } +}