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

SE: Add Plus- and MinusOperation for literals #7174

Merged
merged 6 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System;
using System.Numerics;

namespace SonarAnalyzer.SymbolicExecution.Constraints;

public sealed class NumberConstraint : SymbolicConstraint
{
public static readonly NumberConstraint Empty = new(null, null);
public static readonly NumberConstraint Zero = new(0, 0);
public static readonly NumberConstraint One = new(1, 1);

Expand Down Expand Up @@ -93,7 +91,7 @@ public static NumberConstraint From(BigInteger? min, BigInteger? max)
}
else
{
return Empty;
return null;
pavel-mikula-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@ protected override ProgramState LearnBranchingConstraint(ProgramState state, IBi
return state;
}

protected override ProgramState PreProcess(ProgramState state, IBinaryOperationWrapper operation)
pavel-mikula-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
{
if (state[operation.LeftOperand]?.Constraint<NumberConstraint>() is { } leftNumber
&& state[operation.RightOperand]?.Constraint<NumberConstraint>() is { } rightNumber
&& Calculate(operation.OperatorKind, leftNumber, rightNumber) is { } constraint)
{
state = state.SetOperationConstraint(operation, constraint);
}
return state;
}

private static NumberConstraint Calculate(BinaryOperatorKind kind, NumberConstraint left, NumberConstraint right) => kind switch
{
BinaryOperatorKind.Add => NumberConstraint.From(left.Min + right.Min, left.Max + right.Max),
BinaryOperatorKind.Subtract => NumberConstraint.From(left.Min - right.Max, left.Max - right.Min),
_ => null
};

private static ProgramState LearnBranchingEqualityConstraint<T>(ProgramState state, IBinaryOperationWrapper binary, bool falseBranch)
where T : SymbolicConstraint
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ public ProgramState SetOperationValue(IOperation operation, SymbolicValue value)
? this with { OperationValue = OperationValue.Remove(ResolveCapture(operation)) }
: this with { OperationValue = OperationValue.SetItem(ResolveCapture(operation), value) };

public ProgramState SetOperationConstraint(IOperationWrapper operation, SymbolicConstraint constraint) =>
SetOperationConstraint(operation.WrappedOperation, constraint);

public ProgramState SetOperationConstraint(IOperationWrapperSonar operation, SymbolicConstraint constraint) =>
SetOperationConstraint(operation.Instance, constraint);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ public void From_Object_OtherTypes()
NumberConstraint.From(new Exception()).Should().BeNull();
NumberConstraint.From("Lorem ipsum").Should().BeNull();
NumberConstraint.From((object)'*').Should().BeNull();
NumberConstraint.From(null, null).Should().BeNull();
}

[TestMethod]
public void IsSingleValue()
{
NumberConstraint.From(42).IsSingleValue.Should().BeTrue();
NumberConstraint.From(1, 1).IsSingleValue.Should().BeTrue();
NumberConstraint.From(null, null).IsSingleValue.Should().BeFalse();
NumberConstraint.From(null, 42).IsSingleValue.Should().BeFalse();
NumberConstraint.From(42, null).IsSingleValue.Should().BeFalse();
}
Expand All @@ -98,15 +98,13 @@ public void ToString_Serialization()
NumberConstraint.From(0).ToString().Should().Be("Number 0");
NumberConstraint.From(42).ToString().Should().Be("Number 42");
NumberConstraint.From(1, 1).ToString().Should().Be("Number 1");
NumberConstraint.From(null, null).ToString().Should().Be("Number from * to *");
NumberConstraint.From(null, 42).ToString().Should().Be("Number from * to 42");
NumberConstraint.From(42, null).ToString().Should().Be("Number from 42 to *");
NumberConstraint.From(-1, null).ToString().Should().Be("Number from -1 to *");
NumberConstraint.From(-4321, -42).ToString().Should().Be("Number from -4321 to -42");
}

[DataTestMethod]
[DataRow(null, null)]
[DataRow(42, null)]
[DataRow(null, 42)]
[DataRow(42, 42)]
Expand All @@ -121,7 +119,6 @@ public void Equals_ReturnsTrueForEquivalent(int? min, int? max)
}

[DataTestMethod]
[DataRow(null, null)]
[DataRow(42, null)]
[DataRow(null, 42)]
[DataRow(42, 42)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -717,4 +717,44 @@ public void Binary_NumberLiteral_Unknown(string expression)
var validator = SETestContext.CreateCS(code, "int arg").Validator;
validator.ValidateTag("Result", x => x.Should().HaveOnlyConstraint(ObjectConstraint.NotNull));
}

[DataTestMethod]
[DataRow("i + j", 47)]
[DataRow("i + 1", 43)]
[DataRow("i + j + 1", 48)]
[DataRow("(i + 1) + j", 48)]
[DataRow("(i + j) + 1", 48)]
[DataRow("i + (1 + j)", 48)]
[DataRow("1 + (i + j)", 48)]
[DataRow("i - j", 37)]
[DataRow("j - i", -37)]
[DataRow("i - 1", 41)]
[DataRow("i - j - 1", 36)]
[DataRow("(i - 1) - j", 36)]
[DataRow("(i - j) - 1", 36)]
[DataRow("i - (j - 1)", 38)]
[DataRow("i - (1 - j)", 46)]
[DataRow("1 - (i - j)", -36)]
[DataRow("i + j - 1", 46)]
[DataRow("(i + j) - 1", 46)]
[DataRow("(i + 1) - j", 38)]
[DataRow("i + (j - 1)", 46)]
[DataRow("i + (1 - j)", 38)]
[DataRow("1 + (i - j)", 38)]
[DataRow("i - j + 1", 38)]
[DataRow("(i - j) + 1", 38)]
[DataRow("(i - 1) + j", 46)]
[DataRow("i - (j + 1)", 36)]
[DataRow("1 - (i + j)", -46)]
public void Binary_Calculations_UpdatesNumberConstraint(string expression, int expected)
{
var code = $"""
var i = 42;
var j = 5;
var value = {expression};
Tag("Value", value);
""";
var validator = SETestContext.CreateCS(code).Validator;
validator.ValidateTag("Value", x => x.Should().HaveOnlyConstraints(ObjectConstraint.NotNull, NumberConstraint.From(expected)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ Namespace Monitor_Loops

Private Condition As Boolean
Private Obj As New Object()
Private Other As New Object()

Public Sub Method1()
Monitor.Enter(Obj) ' Noncompliant tricky FP, as the execution should always reach number 9, but we don't track that
Monitor.Enter(Obj) ' Compliant for the wrong reason. Should be FP, as the execution should always reach number 9, but we don't track that
For i As Integer = 0 To 9
If i = 9 Then Monitor.Exit(Obj)
Next
Monitor.Enter(Other) ' FN, exploration stopped after loop
If Condition Then Monitor.Exit(Other)
Comment on lines +16 to +17
Copy link
Contributor

Choose a reason for hiding this comment

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

Every lost Noncompliant should have these two lines too

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will add it. Can you elaborate on your reason, though? I am trying to understand why the comment is insufficient for the other cases.

Copy link
Contributor

Choose a reason for hiding this comment

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

To demonstrate, that we stop exploring whatever follows. There are two problems:

  • 1st related to the content of the For loop. And that is FN for one reason.
  • 2nd related to the fact that we don't explore code after For loop

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The two cases that did not have the additional two lines are not FNs or FPs, though. They are just regular Noncompliant cases.

End Sub

Public Sub Method2()
Expand All @@ -23,27 +26,33 @@ Namespace Monitor_Loops
End Sub

Public Sub Method3()
Monitor.Enter(Obj) ' Noncompliant
Monitor.Enter(Obj) ' FN, exploration stopped after loop
For i As Integer = 0 To 9
If i = 5 Then Exit For
If i = 9 Then Monitor.Exit(Obj)
Next
Monitor.Enter(Other) ' FN, exploration stopped after loop
If Condition Then Monitor.Exit(Other)
End Sub

Public Sub Method4()
Monitor.Enter(Obj) ' Noncompliant tricky FP, as the execution should always reach number 9, but we don't track that
Monitor.Enter(Obj) ' Compliant for the wrong reason. Should be FP, as the execution should always reach number 9, but we don't track that
For i As Integer = 0 To 9
If i = 10 Then Exit For
If i = 9 Then Monitor.Exit(Obj)
Next
Monitor.Enter(Other) ' FN, exploration stopped after loop
If Condition Then Monitor.Exit(Other)
End Sub

Public Sub Method5()
Monitor.Enter(Obj) ' Noncompliant
Monitor.Enter(Obj) ' FN, exploration stopped after loop
For i As Integer = 0 To 9
If i = 9 Then Continue For
If i = 9 Then Monitor.Exit(Obj)
Next
Monitor.Enter(Other) ' FN, exploration stopped after loop
If Condition Then Monitor.Exit(Other)
End Sub

Public Sub Method6(Array() As Byte)
Expand Down