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

Writes to a header field implicitly read the header valid bit #2891

Merged
merged 1 commit into from
Sep 1, 2021

Conversation

mihaibudiu
Copy link
Contributor

Fixes #2890

@mihaibudiu
Copy link
Contributor Author

@fruffy this is a subtle issue of semantics that you may want to revise in Gauntlet too.
So essentially if you have a header H and a variable H x, the assignment x.a = 1 in fact in a surreptitious way looks at the valid bit of x: the x is not valid, then the assignment may actually do nothing. So any prior statement that sets the valid bit of x is actually live. This shows up in examples like:
x.a = 1; r = x.a;. Here you would think that you can copy-propagate the 1 into r, but this is true only of x is known to be valid.

@mihaibudiu mihaibudiu requested a review from fruffy September 1, 2021 05:52
@mihaibudiu
Copy link
Contributor Author

The fix is in the def-use analysis: when someone assigns to x.a we mark the valid bit of the enclosing header (if any) as being read by the instruction that makes the assignment.

@fruffy
Copy link
Collaborator

fruffy commented Sep 1, 2021

How does this relate to #2323? The discussion in this issue prompted me to implicitly propagate in Gauntlet, too

@@ -0,0 +1,59 @@
#include <core.p4>
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should probably find a way to fail tests if they are missing their reference file, so that they do not have to be added in later pull requests.

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 file a new issue for that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@fruffy
Copy link
Collaborator

fruffy commented Sep 1, 2021

For example, looking at the copy propagation of programs such as header-bmv2.p4

control ingress(inout Headers h, inout Meta m, inout standard_metadata_t sm) {
    @name("ingress.c.tmp") hdr c_tmp;
    apply {
        c_tmp.setInvalid();
        c_tmp.f = h.h.f + 32w1;
        h.h.f = c_tmp.f;
        sm.egress_spec = 9w0;
    }
}
control ingress(inout Headers h, inout Meta m, inout standard_metadata_t sm) {
    @name("ingress.c.tmp") hdr c_tmp;
    apply {
        c_tmp.setInvalid();
        c_tmp.f = h.h.f + 32w1;
        h.h.f = h.h.f + 32w1;
        sm.egress_spec = 9w0;
    }
}

This is still acceptable behavior, right? Since h.h.f is reading from an uninitialized variable.

@mihaibudiu
Copy link
Contributor Author

How does this relate to #2323? The discussion in this issue prompted me to implicitly propagate in Gauntlet, too

I think propagating through invalid header field is a legal behavior. The result is undefined anyway, and this is as undefined as anything else.

But I also think that here we are fixing a true bug; the programs before and after were not equivalent.

@mihaibudiu
Copy link
Contributor Author

Yes, the copy propagation does something that is acceptable. Both the input and output programs have the same undefined behaviors.
What is not acceptable is to make the program less defined. Removing a setValid converts a program with defined behavior into an a program with undefined behavior.

@mihaibudiu
Copy link
Contributor Author

mihaibudiu commented Sep 1, 2021

This upcoming PR will help catch many undefined programs: #2881
The number of warning it is producing shows that this is a very useful analysis.

Copy link
Collaborator

@fruffy fruffy left a comment

Choose a reason for hiding this comment

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

Sounds good. I tightened the semantics of Gauntlet accordingly. It reports a lot more issues with undefined behavior now, but those are all benign. The only real issue that is detected is fixed with this pull request. Once it is merged in I will update the tool.

@mihaibudiu mihaibudiu merged commit eb5ad15 into p4lang:main Sep 1, 2021
@mihaibudiu mihaibudiu deleted the issue2890 branch September 1, 2021 19:40
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.

Removing the assignment in SimplifyDefUse causes the header to be invalid
2 participants