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 - Nullable: Copy constraints to Value property #6841

Merged
merged 2 commits into from
Mar 2, 2023

Conversation

pavel-mikula-sonarsource
Copy link
Contributor

@pavel-mikula-sonarsource pavel-mikula-sonarsource commented Mar 2, 2023

Part of #6812

Copy known constraints from nullable symbol to .Value property.

bool? nullable = true;          // nullable symbol is known to have True constraint after this
var result = nullable.Value;  // True constraint from symbol nullable should get propagated to its Value property so it can be assigned to the result symbol later

@pavel-mikula-sonarsource pavel-mikula-sonarsource changed the title Pavel/se/value SE - Nullable: Copy constraints to Value property Mar 2, 2023
@sonarcloud
Copy link

sonarcloud bot commented Mar 2, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@sonarcloud
Copy link

sonarcloud bot commented Mar 2, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

100.0% 100.0% Coverage
0.0% 0.0% Duplication

Copy link
Contributor

Choose a reason for hiding this comment

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

LGTM. I left some questions to make sure I got the overall plan right.

var setter = new PreProcessTestCheck(OperationKind.Literal, x => x.Operation.Instance.ConstantValue.Value is false ? x.SetOperationConstraint(TestConstraint.First) : x.State);
var validator = SETestContext.CreateCS(code, ", bool? arg", setter).Validator;
validator.ValidateTag("Unknown", x => x.Should().BeNull());
validator.ValidateTag("True", x => x.HasConstraint(BoolConstraint.True).Should().BeTrue());

Choose a reason for hiding this comment

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

Suggested change
validator.ValidateTag("True", x => x.HasConstraint(BoolConstraint.True).Should().BeTrue());
validator.ValidateTag("True", x => x.HasConstraint(BoolConstraint.True).Should().BeTrue());
validator.ValidateTag("True", x => x.HasConstraint(TestConstraint.First).Should().BeFalse());

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This would assert that scaffolding works. We don't need that. The point of this is that all constraints are propagated.

Comment on lines +79 to +81
return propertyReference.Property.Name == "Value" && propertyReference.Instance.Type.IsNullableValueType() && context.State[symbol] is { } value
? state.SetOperationValue(context.Operation, value)
: state;

Choose a reason for hiding this comment

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

Nice. This should work.
Just to make sure, I understand the plan correctly:
I suppose the idea is to later also learn

  • ObjectConstraint.NotNull for symbol whenever HasValue gets the BoolConstraint.True
  • ObjectConstraint.IsNull for symbol whenever HasValue gets the BoolConstraint.False
if (nullable.HasValue)
{
// nullable has ObjectConstraint.NotNull
}
else
{
// nullable has ObjectConstraint.Null
}

If so we need to learn

  • ObjectConstraint.NotNull for symbol here as well as it is known to be NotNull from here on.

I suppose these are the next steps, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that's exactly the plan. See 2nd step on #6812

{
const string code = """
var value = arg.Value;
Tag("Unknown", value);

Choose a reason for hiding this comment

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

I suppose this will change to Tag("NoNull", value); in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that will most likely happen on your PR.

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

💥

Copy link
Contributor

@antonioaversa antonioaversa left a comment

Choose a reason for hiding this comment

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

Educational questions, for my and other people's benefit.

if (propertyReference.Instance.TrackedSymbol() is { } symbol)
{
var state = context.State.SetSymbolConstraint(symbol, ObjectConstraint.NotNull);
return propertyReference.Property.Name == "Value" && propertyReference.Instance.Type.IsNullableValueType() && context.State[symbol] is { } value
Copy link
Contributor

Choose a reason for hiding this comment

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

Educational, to clarify levels of abstractions: Is the following statement correct?

"Value" here is the name of the property at SE level, directly coming from the symbol at Semantic level and not the syntactical token. So, if the code being symbolic-analyzed is VB.NET and the user types .value, the Name will still be "Value". That's why you don't use nameof(Nullable.Value), which would return the name at syntactical level.

Extension properties (as defined here) would not break this, for the same reason.

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 "Value" statement is correct. It could have used nameof if that would compile (but it doesn't).

propertyReference.Instance.TrackedSymbol() is { } symbol
? context.SetSymbolConstraint(symbol, ObjectConstraint.NotNull)
: context.State;
protected override ProgramState Process(SymbolicContext context, IPropertyReferenceOperationWrapper propertyReference)
Copy link
Contributor

Choose a reason for hiding this comment

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

Educational: explicit cast, while calling .Value, is not supported by this because the cast is a method call at CFG level (is it?) and SE is not cross-level. So you will need to learn the same you learn here (i.e. Value as a non-null value) in a "IInvocationOperationWrapper". Correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All casts are represented as ConversionOperation (implicit or explicit). That will be handled later.

InvocationOperation is not relevant here.

{
const string code = """
bool? value = null;
Tag("Null", value);
Copy link
Contributor

Choose a reason for hiding this comment

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

Educational. The method built by CreateCS is empty. Why "Null" learns to be null and "True" learns to be true? The two parameters of Tag seem not correlated to each other.

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 method built by CreateCS is not empty. It has this body.

The Tag method is a special empty method in the SETestContext that we use to tag a capture a specific state. It works like a probe.

The first argument is a string and servers as a key. Because we can tag more things in a single run.
As the constraints evolves over time, we're probing it twice, asserting different state on each place of code.

""";
var setter = new PreProcessTestCheck(OperationKind.Literal, x => x.Operation.Instance.ConstantValue.Value is false ? x.SetOperationConstraint(TestConstraint.First) : x.State);
var validator = SETestContext.CreateCS(code, ", bool? arg", setter).Validator;
validator.ValidateTag("Unknown", x => x.Should().BeNull());
Copy link
Contributor

Choose a reason for hiding this comment

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

Educational. Shouldn't this be unknown, because arg is unknown in code? Am I missing something?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

arg is a parameter of the method. So the method goes like

    public void Main(bool boolParameter, bool? arg)
    {
        var value = arg.Value;
        Tag("Unknown", value);
        // ...
    }

and as arg is a parameter, we cannot infer anything about its initial value. So the value is unknown to us, it has no constraints.
Then we change it, and after that, we can assume some constraints.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For C#, you can see everything that is available (and that we can use in the snippets) here:

private static string ClassCodeCS(string methodBody, string additionalParameters) =>
$@"
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using static Tagger;
public unsafe class Sample
{{
public object ObjectField;
public static int StaticField;
public static object StaticObjectField;
public static int StaticProperty {{ get; set; }}
public static event EventHandler StaticEvent;
public event EventHandler Event;
public int Property {{ get; set; }}
public Sample SampleProperty {{ get; set; }}
public NotImplementedException PropertyException {{ get; set; }}
public int this[int index] {{get => 42; set {{ }} }}
private int field;
private NotImplementedException fieldException;
private bool Condition => Environment.ProcessorCount == 42; // Something that cannot have constraint
public Sample(){{ }}
public Sample(int i){{ }}
public void Main(bool boolParameter{additionalParameters})
{{
{methodBody}
}}
public NotImplementedException CreateException() => new NotImplementedException();
public void InstanceMethod(object parameter = null) {{ }}
public static void StaticMethod() {{ }}
}}
public class Person : PersonBase
{{
public static string StaticProperty {{ get; set; }}
public string Field;
public event EventHandler Event;
public string Method() => null;
public static void StaticMethod() {{ }}
}}
public class PersonBase
{{
}}
public static class Tagger
{{
public static void Tag(string name, object arg = null) {{ }}
public static void Tag(this object o, string name) {{ }}
public static T Unknown<T>() => default;
}}
";
}

@martin-strecker-sonarsource martin-strecker-sonarsource changed the title SE - Nullable: Copy constraints to Value property SE - Non-nullable value type: Copy constraints to Value property Mar 2, 2023
@martin-strecker-sonarsource martin-strecker-sonarsource changed the title SE - Non-nullable value type: Copy constraints to Value property SE - Nullable: Copy constraints to Value property Mar 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants