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

Missing type-characteristic checks #5274

Closed
RustanLeino opened this issue Mar 28, 2024 · 0 comments · Fixed by #5278
Closed

Missing type-characteristic checks #5274

RustanLeino opened this issue Mar 28, 2024 · 0 comments · Fixed by #5278
Assignees
Labels
during 3: execution of incorrect program An bug in the verifier that allows Dafny to run a program that does not correctly implement its spec kind: bug Crashes, unsoundness, incorrect output, etc. If possible, add a `part:` label part: resolver Resolution and typechecking

Comments

@RustanLeino
Copy link
Collaborator

Dafny version

4.6.0

Code to produce this issue

datatype Pair<T(==)> = MakePair(0: T, 1: T) {
  function Same(): bool {
    this.0 == this.1
  }
}

method ReturnFalse() returns (b: bool)
  ensures !b
{
  var c := (2, ghost 5);
  var d := (2, ghost 6);
  assert c != d;
  b := MakePair(c, d).Same(); // BOGUS: this should not be allowed, because it uses the type Pair incorrectly

  print MakePair(c, d); // BOGUS: this should not be allowed, because it uses the type Pair incorrectly
  print "\n";
}

method Main() {
  var b := ReturnFalse();
  if b {
    // we should never get here
    print 10 / 0;
  }
}

Command to run and resulting output

% dafny run test.dfy

Dafny program verifier finished with 2 verified, 0 errors
Unhandled exception. System.DivideByZeroException: Attempted to divide by zero.
   at System.Numerics.BigInteger.op_Division(BigInteger dividend, BigInteger divisor)
   at System.Numerics.BigInteger.Divide(BigInteger dividend, BigInteger divisor)
   at Dafny.Helpers.EuclideanDivision(BigInteger a, BigInteger b)
   at _module.__default._Main(ISequence`1 __noArgsParameter)
   at __CallToMain.<>c__DisplayClass0_0.<Main>b__0()
   at Dafny.Helpers.WithHaltHandling(Action action)
   at __CallToMain.Main(String[] args)
Pair.MakePair(2, 2)

What happened?

Type Pair says that its type parameter must support equality in compiled contexts. The type (int, ghost int) does not support equality in compiled context, so it should not be possible to use that type as the type argument to Pair. Alas, those checks are missing for the DatatypeValue AST nodes. The program should have been rejected by the resolver, but instead sails through both the verifier and compiler and ends up in a run-time crash.

What type of operating system are you experiencing the problem on?

Mac

@RustanLeino RustanLeino added kind: bug Crashes, unsoundness, incorrect output, etc. If possible, add a `part:` label part: resolver Resolution and typechecking during 3: execution of incorrect program An bug in the verifier that allows Dafny to run a program that does not correctly implement its spec labels Mar 28, 2024
@RustanLeino RustanLeino self-assigned this Mar 28, 2024
RustanLeino added a commit that referenced this issue Apr 2, 2024
)

This PR add a visit to `DatatypeValue`s to look for illegally
instantiated type parameters of types.

It also fixes the error location that gets passed forward in some cases
for the new resolver.

Fixes #5274 

<small>By submitting this pull request, I confirm that my contribution
is made under the terms of the [MIT
license](https://github.com/dafny-lang/dafny/blob/master/LICENSE.txt).</small>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
during 3: execution of incorrect program An bug in the verifier that allows Dafny to run a program that does not correctly implement its spec kind: bug Crashes, unsoundness, incorrect output, etc. If possible, add a `part:` label part: resolver Resolution and typechecking
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant