Skip to content

Commit a5b2549

Browse files
authored
Merge pull request #2514 from hvitved/csharp/code-contracts
C#: Recognize Code Contract assertions
2 parents 5cea452 + 374b0c0 commit a5b2549

File tree

8 files changed

+151
-7
lines changed

8 files changed

+151
-7
lines changed

change-notes/1.24/analysis-csharp.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ The following changes in version 1.24 affect C# analysis in all applications.
2121
## Changes to libraries
2222

2323
* The taint tracking library now tracks flow through (implicit or explicit) conversion operator calls.
24+
* [Code contracts](https://docs.microsoft.com/en-us/dotnet/framework/debug-trace-profile/code-contracts) are now recognized, and are treated like any other assertion methods.
2425

2526
## Changes to autobuilder
2627

csharp/ql/src/semmle/code/csharp/commons/Assertions.qll

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/** Provides classes for assertions. */
22

33
private import semmle.code.csharp.frameworks.system.Diagnostics
4+
private import semmle.code.csharp.frameworks.system.diagnostics.Contracts
45
private import semmle.code.csharp.frameworks.test.VisualStudio
56
private import semmle.code.csharp.frameworks.System
67
private import ControlFlow
@@ -169,6 +170,29 @@ class SystemDiagnosticsDebugAssertTrueMethod extends AssertTrueMethod {
169170
}
170171
}
171172

173+
/**
174+
* A `System.Diagnostics.Contracts.Contract` assertion method.
175+
*/
176+
class SystemDiagnosticsContractAssertTrueMethod extends AssertTrueMethod {
177+
SystemDiagnosticsContractAssertTrueMethod() {
178+
exists(SystemDiagnosticsContractsContractClass c |
179+
this = c.getAnAssertMethod()
180+
or
181+
this = c.getAnAssumeMethod()
182+
or
183+
this = c.getARequiresMethod()
184+
)
185+
}
186+
187+
override int getAssertionIndex() { result = 0 }
188+
189+
override Class getExceptionClass() {
190+
// A failing assertion generates a message box, see
191+
// https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.contracts.contract.assert
192+
none()
193+
}
194+
}
195+
172196
/** A Visual Studio assertion method. */
173197
class VSTestAssertTrueMethod extends AssertTrueMethod {
174198
VSTestAssertTrueMethod() { this = any(VSTestAssertClass c).getIsTrueMethod() }

csharp/ql/src/semmle/code/csharp/frameworks/system/Diagnostics.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class SystemDiagnosticsDebugClass extends SystemDiagnosticsClass {
2323
this.isStatic()
2424
}
2525

26-
/** Gets and `Assert(bool, ...)` method. */
26+
/** Gets an `Assert(bool, ...)` method. */
2727
Method getAssertMethod() {
2828
result.getDeclaringType() = this and
2929
result.hasName("Assert") and
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/** Provides definitions related to the namespace `System.Diagnostics.Contracts`. */
2+
3+
import semmle.code.csharp.Type
4+
private import semmle.code.csharp.frameworks.system.Diagnostics
5+
6+
/** The `System.Diagnostics.Contracts` namespace. */
7+
class SystemDiagnosticsContractsNamespace extends Namespace {
8+
SystemDiagnosticsContractsNamespace() {
9+
this.getParentNamespace() instanceof SystemDiagnosticsNamespace and
10+
this.hasName("Contracts")
11+
}
12+
}
13+
14+
/** A class in the `System.Diagnostics.Contracts` namespace. */
15+
class SystemDiagnosticsContractsClass extends Class {
16+
SystemDiagnosticsContractsClass() {
17+
this.getNamespace() instanceof SystemDiagnosticsContractsNamespace
18+
}
19+
}
20+
21+
/** The `System.Diagnostics.Contracts.Contract` class. */
22+
class SystemDiagnosticsContractsContractClass extends SystemDiagnosticsContractsClass {
23+
SystemDiagnosticsContractsContractClass() {
24+
this.hasName("Contract") and
25+
this.isStatic()
26+
}
27+
28+
/** Gets an `Assert(bool, ...)` method. */
29+
Method getAnAssertMethod() {
30+
result.getDeclaringType() = this and
31+
result.hasName("Assert") and
32+
result.getParameter(0).getType() instanceof BoolType and
33+
result.getReturnType() instanceof VoidType
34+
}
35+
36+
/** Gets an `Assume(bool, ...)` method. */
37+
Method getAnAssumeMethod() {
38+
result.getDeclaringType() = this and
39+
result.hasName("Assume") and
40+
result.getParameter(0).getType() instanceof BoolType and
41+
result.getReturnType() instanceof VoidType
42+
}
43+
44+
/** Gets a `Requires(bool, ...)` method. */
45+
Method getARequiresMethod() {
46+
result.getDeclaringType() = this and
47+
result.hasName("Requires") and
48+
result.getParameter(0).getType() instanceof BoolType and
49+
result.getReturnType() instanceof VoidType
50+
}
51+
}

csharp/ql/test/library-tests/commons/Assertions/Assertions.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Diagnostics;
3+
using System.Diagnostics.Contracts;
34
using Microsoft.VisualStudio.TestTools.UnitTesting;
45

56
public static class Forwarders
@@ -37,6 +38,18 @@ void Trivial()
3738
Forwarders.MyAssert2(false);
3839
Forwarders.MyAssert2(true);
3940
}
41+
42+
void CodeContracts(string s)
43+
{
44+
Contract.Requires(s != null);
45+
Contract.Requires(s != null, "s must be non-null");
46+
Contract.Requires<Exception>(s != null);
47+
Contract.Requires<Exception>(s != null, "s must be non-null");
48+
Contract.Assert(s != null);
49+
Contract.Assert(s != null, "s is non-null");
50+
Contract.Assume(s != null);
51+
Contract.Assume(s != null, "s is non-null");
52+
}
4053
}
4154

42-
// semmle-extractor-options: ${testdir}/../../../resources/stubs/Microsoft.VisualStudio.TestTools.UnitTesting.cs
55+
// semmle-extractor-options: ${testdir}/../../../resources/stubs/Microsoft.VisualStudio.TestTools.UnitTesting.cs /r:System.Diagnostics.Contracts.dll
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
assertTrue
2+
| Assertions.cs:8:44:8:59 | call to method IsTrue | Assertions.cs:8:58:8:58 | access to parameter b |
3+
| Assertions.cs:9:45:9:55 | call to method MyAssert | Assertions.cs:9:54:9:54 | access to parameter b |
4+
| Assertions.cs:17:9:17:31 | call to method Assert | Assertions.cs:17:22:17:30 | ... != ... |
5+
| Assertions.cs:20:9:20:32 | call to method IsTrue | Assertions.cs:20:23:20:31 | ... == ... |
6+
| Assertions.cs:21:9:21:32 | call to method IsTrue | Assertions.cs:21:23:21:31 | ... != ... |
7+
| Assertions.cs:24:9:24:38 | call to method MyAssert | Assertions.cs:24:29:24:37 | ... == ... |
8+
| Assertions.cs:25:9:25:39 | call to method MyAssert2 | Assertions.cs:25:30:25:38 | ... == ... |
9+
| Assertions.cs:30:9:30:27 | call to method Assert | Assertions.cs:30:22:30:26 | false |
10+
| Assertions.cs:31:9:31:26 | call to method Assert | Assertions.cs:31:22:31:25 | true |
11+
| Assertions.cs:32:9:32:28 | call to method IsTrue | Assertions.cs:32:23:32:27 | false |
12+
| Assertions.cs:33:9:33:27 | call to method IsTrue | Assertions.cs:33:23:33:26 | true |
13+
| Assertions.cs:36:9:36:34 | call to method MyAssert | Assertions.cs:36:29:36:33 | false |
14+
| Assertions.cs:37:9:37:33 | call to method MyAssert | Assertions.cs:37:29:37:32 | true |
15+
| Assertions.cs:38:9:38:35 | call to method MyAssert2 | Assertions.cs:38:30:38:34 | false |
16+
| Assertions.cs:39:9:39:34 | call to method MyAssert2 | Assertions.cs:39:30:39:33 | true |
17+
| Assertions.cs:44:9:44:36 | call to method Requires | Assertions.cs:44:27:44:35 | ... != ... |
18+
| Assertions.cs:45:9:45:58 | call to method Requires | Assertions.cs:45:27:45:35 | ... != ... |
19+
| Assertions.cs:46:9:46:47 | call to method Requires | Assertions.cs:46:38:46:46 | ... != ... |
20+
| Assertions.cs:47:9:47:69 | call to method Requires | Assertions.cs:47:38:47:46 | ... != ... |
21+
| Assertions.cs:48:9:48:34 | call to method Assert | Assertions.cs:48:25:48:33 | ... != ... |
22+
| Assertions.cs:49:9:49:51 | call to method Assert | Assertions.cs:49:25:49:33 | ... != ... |
23+
| Assertions.cs:50:9:50:34 | call to method Assume | Assertions.cs:50:25:50:33 | ... != ... |
24+
| Assertions.cs:51:9:51:51 | call to method Assume | Assertions.cs:51:25:51:33 | ... != ... |
25+
assertFalse
26+
| Assertions.cs:22:9:22:33 | call to method IsFalse | Assertions.cs:22:24:22:32 | ... != ... |
27+
| Assertions.cs:23:9:23:33 | call to method IsFalse | Assertions.cs:23:24:23:32 | ... == ... |
28+
| Assertions.cs:34:9:34:28 | call to method IsFalse | Assertions.cs:34:24:34:27 | true |
29+
| Assertions.cs:35:9:35:29 | call to method IsFalse | Assertions.cs:35:24:35:28 | false |
30+
assertNull
31+
| Assertions.cs:18:9:18:24 | call to method IsNull | Assertions.cs:18:23:18:23 | access to local variable s |
32+
assertNonNull
33+
| Assertions.cs:19:9:19:27 | call to method IsNotNull | Assertions.cs:19:26:19:26 | access to local variable s |
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import csharp
2+
import semmle.code.csharp.commons.Assertions
3+
4+
query predicate assertTrue(Assertion a, Expr e) {
5+
a.getExpr() = e and
6+
a.getTarget() instanceof AssertTrueMethod
7+
}
8+
9+
query predicate assertFalse(Assertion a, Expr e) {
10+
a.getExpr() = e and
11+
a.getTarget() instanceof AssertFalseMethod
12+
}
13+
14+
query predicate assertNull(Assertion a, Expr e) {
15+
a.getExpr() = e and
16+
a.getTarget() instanceof AssertNullMethod
17+
}
18+
19+
query predicate assertNonNull(Assertion a, Expr e) {
20+
a.getExpr() = e and
21+
a.getTarget() instanceof AssertNonNullMethod
22+
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
| Assertions.cs:29:9:29:27 | call to method Assert |
2-
| Assertions.cs:31:9:31:28 | call to method IsTrue |
3-
| Assertions.cs:33:9:33:28 | call to method IsFalse |
4-
| Assertions.cs:35:9:35:34 | call to method MyAssert |
5-
| Assertions.cs:37:9:37:35 | call to method MyAssert2 |
1+
| Assertions.cs:30:9:30:27 | call to method Assert |
2+
| Assertions.cs:32:9:32:28 | call to method IsTrue |
3+
| Assertions.cs:34:9:34:28 | call to method IsFalse |
4+
| Assertions.cs:36:9:36:34 | call to method MyAssert |
5+
| Assertions.cs:38:9:38:35 | call to method MyAssert2 |

0 commit comments

Comments
 (0)