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 extern contract checking on abstract modules #4910

Merged
merged 11 commits into from
Jan 4, 2024

Conversation

atomb
Copy link
Member

@atomb atomb commented Dec 20, 2023

Description

Previously, compiling an abstract module with an {:extern} method using --test-assumptions Externs would cause an assertion failure. This was due to interleaving between the contract checking wrapper generator and the module refinement rewriter. This PR fixes the problem by generating contract checking wrappers for the whole program after resolution is finished, rather than as each module finishes resolution.

Fixes #4845

How has this been tested?

Tested by contract-wrappers/RefineContract.dfy.

By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.

@atomb atomb self-assigned this Dec 20, 2023
@atomb atomb enabled auto-merge (squash) December 20, 2023 22:17
Copy link
Member

@MikaelMayer MikaelMayer left a comment

Choose a reason for hiding this comment

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

Sounds reasonable. Thanks for writing the tests.

Source/DafnyCore/Rewriters/ExpectContracts.cs Outdated Show resolved Hide resolved
Copy link
Member

@keyboardDrummer keyboardDrummer left a comment

Choose a reason for hiding this comment

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

Previously, compiling an abstract module with an {:extern} method using --test-assumptions Externs would cause an assertion failure. This was due to interleaving between the contract checking wrapper generator and the module refinement rewriter.

What is the problem caused by interleaving?

This PR fixes the problem by generating contract checking wrappers for the whole program after resolution is finished, rather than as each module finishes resolution.

Resolution caching is done on a per-module level. Any operation done in internal override void PostResolve(Program program) { is not cached, so we shouldn't have such methods unless they don't relate to a particular module, like RunAllTestsMainMethod which adds a single main method for the whole program.

@atomb
Copy link
Member Author

atomb commented Dec 21, 2023

Resolution caching is done on a per-module level. Any operation done in internal override void PostResolve(Program program) { is not cached, so we shouldn't have such methods unless they don't relate to a particular module, like RunAllTestsMainMethod which adds a single main method for the whole program.

It seems right to me that this shouldn't be cached as part of resolution. I'd consider it to essentially be part of the compiler, and not something that it would make sense to enable in the IDE, for example. It really is a whole-program transformation, in essence, because it introduces wrappers in one module that are then called (by redirection) from other modules.

Apply suggestion from @MikaelMayer

Co-authored-by: Mikaël Mayer <[email protected]>
@atomb
Copy link
Member Author

atomb commented Dec 21, 2023

What is the problem caused by interleaving?

The contract checking code creates a resolved wrapper in the abstract module, which is then copied, with resolution information removed, to the refining module, which hasn't yet been resolved. The removal of the resolution information causes the resolver to crash when checking the refining module. That, on its own, could perhaps be fixed in other ways. But even if it was, the contract checking code would then try to create that wrapper again after resolving the refining module. That could be fixed by adding a special case, but that would additionally complicate the code. By running after all modules are revolved, the approach can be straightforward, without complicating special cases that we could easily get wrong.

@keyboardDrummer
Copy link
Member

keyboardDrummer commented Dec 22, 2023

It seems right to me that this shouldn't be cached as part of resolution. I'd consider it to essentially be part of the compiler, and not something that it would make sense to enable in the IDE, for example.

Fair, if you do it in internal override void PostResolve(Program program) though, it will be part of resolution and be run by the IDE.

It really is a whole-program transformation, in essence, because it introduces wrappers in one module that are then called (by redirection) from other modules.

In my experience the term whole-program analysis is used when the analysis of a definition (often a callee, but in this case an imported module) is affected by the usages (often the caller, but in this case an importing module), which I think is not the case here. Case in point: leaving out the refinement related bug, contract-wrapping is already working and it is done one module at a time.

It seems right to me that this shouldn't be cached as part of resolution. I'd consider it to essentially be part of the compiler

But it should be cached! Everything should be cached. We should design the whole compilation pipeline (by compilation pipeline I mean everything from parsing to code generation) with caching in mind. I'll admit that caching is less important for code generation, but I would still argue that it is better if it is designed so that it could be cached. If code generation can be done for modules independently, then it could also be run in parallel.

No need to take action for this now though.

The contract checking code creates a resolved wrapper in the abstract module, which is then copied, with resolution information removed, to the refining module, which hasn't yet been resolved. The removal of the resolution information causes the resolver to crash when checking the refining module. That, on its own, could perhaps be fixed in other ways.

Seems like a general bug not tied to contract-checking

But even if it was, the contract checking code would then try to create that wrapper again after resolving the refining module. That could be fixed by adding a special case, but that would additionally complicate the code. By running after all modules are revolved, the approach can be straightforward, without complicating special cases that we could easily get wrong.

Refinement modules in general collide with meta-programming transformations. Attributes such as {:autocontracts} and {:autoReq} have a similar problem.

They contains code such as

if (member.RefinementBase != null) {
  // member is inherited from a module where it was already processed
  continue;
}

to deal with this.

That said, those contracts need to be part of refinement, and contract wrapping does not. I think it is indeed easier to do the wrapping transformation after all refinement is done. Could you move the transformation to the compilers/code-generators? It seems there's already a handle void PostVerification(Program program) that allows this.

@atomb
Copy link
Member Author

atomb commented Dec 22, 2023

It seems right to me that this shouldn't be cached as part of resolution. I'd consider it to essentially be part of the compiler, and not something that it would make sense to enable in the IDE, for example.

Fair, if you do it in internal override void PostResolve(Program program) though, it will be part of resolution and be run by the IDE.

But it won't do anything unless the option is enabled, which only makes sense when you're compiling.

It really is a whole-program transformation, in essence, because it introduces wrappers in one module that are then called (by redirection) from other modules.

In my experience the term whole-program analysis is used when the analysis of a definition (often a callee, but in this case an imported module) is affected by the usages (often the caller, but in this case an importing module), which I think is not the case here. Case in point: leaving out the refinement related bug, contract-wrapping is already working and it is done one module at a time.

It processes one module at a time, but it generates calls that go between modules and need to be consistent between them.

That said, those contracts need to be part of refinement, and contract wrapping does not. I think it is indeed easier to do the wrapping transformation after all refinement is done. Could you move the transformation to the compilers/code-generators? It seems there's already a handle void PostVerification(Program program) that allows this.

That seems reasonable. Part of the process already happens in PostVerification(Program), and the rest could be moved. I think the fact that it's separate is, IIRC, an artifact of the fact that it used to run pre-resolution in an attempt to have resolution help detect cases where we generate badly-structured code. But we already had to move it after resolution to get ghostness information, so that's no longer a consideration.

When it comes to finishing this PR, is moving the code to all run in PostVerification the one change you'd concretely like to see in place?

@keyboardDrummer
Copy link
Member

But it won't do anything unless the option is enabled, which only makes sense when you're compiling.

I see. That helps but I'd rather not depend on that.

When it comes to finishing this PR, is moving the code to all run in PostVerification the one change you'd concretely like to see in place?

Yes

@atomb
Copy link
Member Author

atomb commented Jan 2, 2024

When it comes to finishing this PR, is moving the code to all run in PostVerification the one change you'd concretely like to see in place?

Yes

Got it. The latest commit makes that change. Let me know what you think.

/// the original members.
///
/// This runs after resolution so that it has access to ghostness
/// information, attributes and call targets. It runs on the entire
Copy link
Member

@keyboardDrummer keyboardDrummer Jan 3, 2024

Choose a reason for hiding this comment

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

The sentence 'it runs on the entire program' doesn't make sense to me.

I think this is a transformation where modules are only affected by modules they import, but not the other way around, so you can do it module by module, which the existing loops indicate. I think you can also cache the results of processing a module, even though we don't need to do that now.

Copy link
Member Author

Choose a reason for hiding this comment

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

I guess that comment isn't as relevant as it was before. When running after resolution, there are two possible methods to override, and this was an explanation for why the code overrides the Program version instead of the Module version. After verification, there's only one option, though.

if (ShouldGenerateWrapper(decl)) {
membersToWrap.Add((topLevelDecl, decl));
/// <param name="program">The program to generate wrappers for and in.</param>
public override void PostVerification(Program program) {
Copy link
Member

Choose a reason for hiding this comment

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

Should

if (Reporter.Options.TestContracts != DafnyOptions.ContractTestingMode.TestedExterns) {
      return;
    }

on line 270, be moved to the top of the method?

Copy link
Member Author

Choose a reason for hiding this comment

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

No. The first two loops should run regardless of that option.

Copy link
Member

Choose a reason for hiding this comment

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

I see.

/// <param name="program">The program to generate wrappers for and in.</param>
public override void PostVerification(Program program) {
// Create wrappers
foreach (var moduleDefinition in program.Modules()) {
Copy link
Member

Choose a reason for hiding this comment

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

Sometimes I see program.CompileModules being used and sometimes program.Modules(). Is this difference intentional? They're very similar but one also includes the system module.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, good catch. I think they should all be program.Modules().

if (ShouldGenerateWrapper(decl)) {
membersToWrap.Add((topLevelDecl, decl));
/// <param name="program">The program to generate wrappers for and in.</param>
public override void PostVerification(Program program) {
Copy link
Member

Choose a reason for hiding this comment

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

There are three loops in this method that all iterate over modules. Can they be merged into one loop?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not without more complex logic that would be easy to get wrong. It needs to ensure that wrappers exist before they're called. The most straightforward way to do that is to construct them all before redirecting calls.

Copy link
Member

@keyboardDrummer keyboardDrummer Jan 3, 2024

Choose a reason for hiding this comment

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

But the modules returned by .Modules() are already sorted in topological order of the import graph. How would having a single loop be able to get the order wrong?

You can't call a wrapper from a module unless you imported that module, and the imported module has already been processed so its wrappers exist.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, right! I hadn't realized it was already sorted. So, yes, the first two loops can be merged. I'll do that.

The final loop is checking for whether any extern exists that hasn't been called by a test, and a test could always show up in a later module in the dependency chain, so I think it still needs to run last.

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good

@atomb atomb enabled auto-merge (squash) January 3, 2024 17:16
@atomb atomb merged commit ab22a7d into dafny-lang:master Jan 4, 2024
20 checks passed
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.

Crash when running -testContracts on refinement of abstract module
3 participants