VAlloy - Virtual Functions Meet a Relational Language

Darko Marinov, Sarfraz KhurshidFme 2002

TL;DR

This paper introduces VAlloy, an extension to the declarative language Alloy, that enables the modeling of dynamic dispatch and inheritance in object-oriented software. By translating VAlloy specifications into Alloy, it allows for automatic verification of object-oriented designs, particularly addressing the complexities of method overriding like Java's equals method.

What Problem Does This Paper Address?

Object-oriented programming (OOP) relies heavily on inheritance and dynamic dispatch, where the actual function executed depends on the runtime type of an object. While declarative languages like Alloy are excellent for specifying structural properties of software, they traditionally lack built-in support for these dynamic OOP features. This makes it challenging to formally model and verify aspects of OOP languages, such as the correct implementation of methods like Java's equals, which is notoriously difficult to get right due to its requirement to form an equivalence relation and its interaction with inheritance. Existing approaches often require manual modeling of dynamic dispatch, which is error-prone and doesn't scale well.

Methods

VAlloy extends Alloy by introducing:

  • virtual function modifier: This keyword signifies that a function's invocation is dynamically dispatched based on the receiver object's class.
  • class declarations: These introduce VAlloy classes, supporting inheritance.
  • super keyword: This allows explicit calls to the superclass's version of a method, mirroring Java's behavior.
  • getClass function: This models Java's Object.getClass() method, crucial for type-based dispatch.

The core of VAlloy's approach is a formal semantics defined by a translation to standard Alloy. This translation involves:

  1. Building a class hierarchy.
  2. Creating Alloy signatures for classes and objects, associating each object with its class.
  3. Transforming VAlloy classes into Alloy disj sig declarations.
  4. Renaming virtual functions to ensure uniqueness.
  5. Introducing "dispatching functions" that model dynamic dispatch by checking the object's class and calling the appropriate translated function.
  6. Replacing super calls with invocations to the corresponding translated static functions.

This translation allows VAlloy specifications to be analyzed by the existing Alloy Analyzer (AA).

Datasets

Not discussed in the paper. The paper focuses on formal modeling and specification rather than empirical evaluation on datasets.

Key Insights and Ideas

  • Bridging Declarative and OOP: VAlloy successfully bridges the gap between declarative specification languages (like Alloy) and the dynamic nature of object-oriented programming, enabling formal analysis of OOP concepts.
  • Formalizing Dynamic Dispatch: The translation mechanism effectively models dynamic dispatch by creating explicit dispatching functions in Alloy, allowing static analysis tools to reason about dynamic behavior.
  • The Challenge of equals: The paper highlights the inherent difficulty in correctly implementing the equals method in OOP, especially with inheritance, and demonstrates how VAlloy can help verify these critical properties.
  • Leveraging Existing Tools: By translating to Alloy, VAlloy leverages the power of existing SAT solvers and the Alloy Analyzer, providing a robust verification infrastructure without needing to build a new one from scratch.

Key Results

The paper demonstrates VAlloy's effectiveness through an example of modeling Java's equals method and its overriding in subclasses.

  • An initial Alloy specification of java.awt.Dimension.equals was found to satisfy equivalence properties.
  • However, when modeling a subclass Dimension3D with an overridden equals method, the Alloy Analyzer, using the VAlloy translation, revealed a violation of the symmetry property. This was because the Dimension class's equals was unaware of the depth field in Dimension3D.
  • By adapting the equals method to use getClass() (as suggested for Java) and modeling this in VAlloy, the translated Alloy specification passed the equivalence assertion, demonstrating VAlloy's ability to correctly model and verify such scenarios.
  • The paper also sketches VAlloy specifications for Set and Map interfaces, showcasing its applicability to modeling collection data structures.

Limitations and Future Work

  • State Modeling: The current VAlloy specification is functional and does not model state modifications or object sharing. The authors suggest adapting approaches from prior work for state modeling.
  • Bulk Operations: Bulk operations on collections (like addAll) are not modeled, as they often rely on iterators, which are not explicitly handled.
  • Null Handling: Modeling null values is mentioned as a minor extension.
  • Future Work: The authors plan to extend VAlloy to support:
  • Multiple inheritance and multi-method dispatch.
  • Method overloading.
  • Interfaces.
  • Encapsulation.
  • State modeling.
  • Exceptions for modeling exceptional behavior and bound checking.
  • Integration with tools like Daikon for invariant detection and TestEra for test generation.

Summary updated

BibTeX
@inproceedings{marinov2002valloy,
  author = {Darko Marinov and Sarfraz Khurshid},
  title = {{VAlloy - Virtual Functions Meet a Relational Language}},
  booktitle = {Fme},
  year = {2002},
  doi = {10.1007/3-540-45614-7\_14},
}

Loading related content...

Figure