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

[Counterexamples] Fix stack overflow during counterexample extraction for some programs #4392

Merged
merged 5 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix stack overflow
  • Loading branch information
Aleksandr Fedchin committed Aug 4, 2023
commit 59b2066a2ebed245c7bc8c458040182d65c4798f
24 changes: 24 additions & 0 deletions Source/DafnyLanguageServer.Test/Various/CounterExampleTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1533,6 +1533,30 @@ class C<T> {
Assert.True(counterExamples[0].Variables.ContainsKey("b:M.C.Equal$T"));
Assert.True(counterExamples[0].Variables.ContainsKey("this:M.C<M.C$T>"));
}

/// <summary>
/// This test case would previously lead to stack overflow because of infinite recursion in GetDafnyType
/// </summary>
[Theory]
[MemberData(nameof(OptionSettings))]
public async Task GetDafnyTypeInfiniteRecursion(List<Action<DafnyOptions>> optionSettings) {
await SetUpOptions(optionSettings);
var source = @"
class Seq {
var s:seq<int>
method test(i0:nat, val0:int, i1:nat, val1:int)
modifies this {
assume 0 <= i0 < i1 < |s|;
s := s[i0 := val0];
s := s[i1 := val1];
assert s[0] != 0;
}
}
".TrimStart();
var documentItem = CreateTestDocument(source);
await client.OpenDocumentAndWaitAsync(documentItem, CancellationToken);
(await RequestCounterExamples(documentItem.Uri)).ToList();;
}

public CounterExampleTest(ITestOutputHelper output) : base(output) {
}
Expand Down
20 changes: 10 additions & 10 deletions Source/DafnyLanguageServer/CounterExampleGeneration/DafnyModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,16 @@ public class DafnyModel {
/// This method tries to extract the base type (so seq<char> instead of string)
/// </summary>
private Type GetDafnyType(Model.Uninterpreted element) {
var finalResult = UnknownType;
foreach (var typeElement in GetIsResults(element)) {
var reconstructedType = ReconstructType(typeElement);
if (reconstructedType is not UserDefinedType userDefinedType) {
return reconstructedType;
}
if (finalResult.Name.EndsWith("?") || finalResult == UnknownType) {
finalResult = userDefinedType;
}
}
var seqOperation = fSeqAppend.AppWithResult(element);
seqOperation ??= fSeqDrop.AppWithResult(element);
seqOperation ??= fSeqTake.AppWithResult(element);
Expand Down Expand Up @@ -351,16 +361,6 @@ public class DafnyModel {
if (fCharToInt.OptEval(element) != null) {
return Type.Char;
}
var finalResult = UnknownType;
foreach (var typeElement in GetIsResults(element)) {
var reconstructedType = ReconstructType(typeElement);
if (reconstructedType is not UserDefinedType userDefinedType) {
return reconstructedType;
}
if (finalResult.Name.EndsWith("?") || finalResult == UnknownType) {
finalResult = userDefinedType;
}
}
if (finalResult != UnknownType) {
return finalResult;
}
Expand Down