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

Add attribute goblint_cil_nested to local varinfos that are not declared at top scope #155

Merged
merged 4 commits into from
Oct 2, 2023

Conversation

michael-schwarz
Copy link
Member

CIL pulls up all declarations inside a function to the top-level scope of that function. This transformation turns code that has Undefined Behavior into code without any UB, which is of course perfectly ok for a compiler to do, but bad for tools who want to flag this type of UB despite working on the output of CIL.

An example of this is the following snippet:

int* ptr;

for(int i =0; i <20; i++) {
    int j =8;
    
    if(i ==0) {
        ptr = &j;
    }
    
    // Will work with most (all) compilers without a hitch, but technically is UB,
    // as ptr points to an object whose lifetime has ended in all but the first iteration 
    *ptr = 5;
}

After CIL, j is pulled to the top scope and there is no more UB.

A rewrite to properly handle scopes seems tedious, and also of questionable value, since it would be a breaking change and would make using CIL more complicated.

This adds an option addNestedScopeAttr to cabs2cil. When it is enabled, this adds an attribute goblint_cil_nested to all varinfos for locals that occur inside a nested scope. This way, sound tools can overapproximate for which variables there may be issues.

As the flag is off by default, this should not cause any changes in behavior for other users.

c.f. goblint/analyzer#1199

@michael-schwarz michael-schwarz merged commit 44f156c into develop Oct 2, 2023
46 checks passed
@michael-schwarz michael-schwarz deleted the attribute_scope branch October 2, 2023 07:53
sim642 added a commit to sim642/opam-repository that referenced this pull request Nov 20, 2023
CHANGES:

* Add `asm inline` parsing (goblint/cil#151).
* Ignore top level qualifiers in `__builtin_types_compatible_p` (goblint/cil#157).
* Add attribute `goblint_cil_nested` to local variables in inner scopes (goblint/cil#155).
* Expose `Cil.typeSigAddAttrs`.
* Add option to suppress `long double` warnings (goblint/cil#136, goblint/cil#156).
* Fix syntactic search (goblint/cil#147).
nberth pushed a commit to nberth/opam-repository that referenced this pull request Jun 18, 2024
CHANGES:

* Add `asm inline` parsing (goblint/cil#151).
* Ignore top level qualifiers in `__builtin_types_compatible_p` (goblint/cil#157).
* Add attribute `goblint_cil_nested` to local variables in inner scopes (goblint/cil#155).
* Expose `Cil.typeSigAddAttrs`.
* Add option to suppress `long double` warnings (goblint/cil#136, goblint/cil#156).
* Fix syntactic search (goblint/cil#147).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants