diff --git a/Foxtrot/Foxtrot/Checker/PostExtractorChecker.cs b/Foxtrot/Foxtrot/Checker/PostExtractorChecker.cs index 58778293..31baeaa7 100644 --- a/Foxtrot/Foxtrot/Checker/PostExtractorChecker.cs +++ b/Foxtrot/Foxtrot/Checker/PostExtractorChecker.cs @@ -2251,9 +2251,9 @@ namespace Microsoft.Contracts.Foxtrot // Roslyn is not introducing local variable for holding an array. // Instead of that it uses "dup" instruction and assignes parameter expression // directly to the stack slot. - // This change should be addressed here, because otherwise ccrewrite will fail + // This change is addressed here, because otherwise ccrewrite will fail // with an error. - if (idxr != null && (idxr.Object is Local || ExpressionTreeInitialization(assignment))) + if (idxr != null && (idxr.Object is Local || IsConstructedArrayIndexer(idxr) || ExpressionTreeInitialization(assignment))) targetIsLocal = true; // Assignments to locals that are structs show up as address deference @@ -2284,7 +2284,7 @@ namespace Microsoft.Contracts.Foxtrot base.VisitAssignmentStatement(assignment); } - + /// /// Returns true the assignment was done from local variable that holds parameter expression /// to the indexer. @@ -2297,7 +2297,35 @@ namespace Microsoft.Contracts.Foxtrot var sourceAsLocal = assignment.Source as Local; return NameUtils.IsExpressionTreeLocal(sourceAsLocal); } + + /// + /// Determines whether the specified indexer refers to an array + /// created in the currently validated contract. + /// + /// + /// An indexer which references object origin shall be determined. + /// + /// + /// if the specified indexer refers to an array + /// created in the currently validated contract; + /// , otherwise. + /// + private bool IsConstructedArrayIndexer(Indexer indexer) + { + if (!seenConstructArray || !seenDup) + { + return false; + } + Node obj = indexer.Object; + if (obj == null) + { + return false; + } + + return obj.NodeType == NodeType.Pop; + } + /// /// Verifies that method calls are to pure methods. /// @@ -2374,37 +2402,50 @@ namespace Microsoft.Contracts.Foxtrot } private bool seenDup; + private bool seenConstructArray; public override void VisitEnsuresExceptional(EnsuresExceptional exceptional) { seenDup = false; + seenConstructArray = false; base.VisitEnsuresExceptional(exceptional); } public override void VisitEnsuresNormal(EnsuresNormal normal) { seenDup = false; + seenConstructArray = false; base.VisitEnsuresNormal(normal); } public override void VisitInvariant(Invariant invariant) { seenDup = false; + seenConstructArray = false; base.VisitInvariant(invariant); } public override void VisitRequiresOtherwise(RequiresOtherwise otherwise) { seenDup = false; + seenConstructArray = false; base.VisitRequiresOtherwise(otherwise); } public override void VisitRequiresPlain(RequiresPlain plain) { seenDup = false; + seenConstructArray = false; base.VisitRequiresPlain(plain); } + public override void VisitConstructArray(ConstructArray consArr) + { + this.seenConstructArray = true; + + base.VisitConstructArray(consArr); + } + public void VisitPop() { Contract.Assume(this.CurrentMethod != null); diff --git a/Foxtrot/Tests/FoxtrotTests10.csproj b/Foxtrot/Tests/FoxtrotTests10.csproj index f1e34765..09bc2832 100644 --- a/Foxtrot/Tests/FoxtrotTests10.csproj +++ b/Foxtrot/Tests/FoxtrotTests10.csproj @@ -116,6 +116,7 @@ + diff --git a/Foxtrot/Tests/RewriterTests/RewriterTest.cs b/Foxtrot/Tests/RewriterTests/RewriterTest.cs index e69e9578..39b8e626 100644 --- a/Foxtrot/Tests/RewriterTests/RewriterTest.cs +++ b/Foxtrot/Tests/RewriterTests/RewriterTest.cs @@ -990,6 +990,17 @@ namespace Tests useBinDir: false, useExe: true, mustSucceed: true); + yield return new Options( + sourceFile: @"Foxtrot\Tests\Sources\ArrayCreationInPrecondition.cs", + foxtrotOptions: @"", + useContractReferenceAssemblies: true, + compilerOptions: null, + references: new string[0], + libPaths: new[] { @"Microsoft.Research\RegressionTest\ClousotTestHarness\bin\debug" }, + compilerCode: "CS", + useBinDir: false, + useExe: true, + mustSucceed: true); } } diff --git a/Foxtrot/Tests/Sources/ArrayCreationInPrecondition.cs b/Foxtrot/Tests/Sources/ArrayCreationInPrecondition.cs new file mode 100644 index 00000000..804834a2 --- /dev/null +++ b/Foxtrot/Tests/Sources/ArrayCreationInPrecondition.cs @@ -0,0 +1,49 @@ +// CodeContracts +// +// Copyright (c) Microsoft Corporation +// +// All rights reserved. +// +// MIT License +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +using System; +using System.Collections.Generic; +using System.Text; +using System.Diagnostics.Contracts; + +namespace Tests.Sources +{ + partial class TestMain + { + void Test(object trigger) + { + // both trigger malformed contract error + Contract.Requires(new object[] { string.Empty } != null); + Contract.Requires(new object[] { 0 } != null); + // this is OK + Contract.Requires(new object[] { null } != null); + Contract.Requires(trigger != null); + } + + partial void Run() + { + if (behave) + { + this.Test(new object()); + } + else + { + this.Test(null); + } + } + + public ContractFailureKind NegativeExpectedKind = ContractFailureKind.Precondition; + public string NegativeExpectedCondition = "trigger != null"; + } +} \ No newline at end of file