-
Notifications
You must be signed in to change notification settings - Fork 88
Improve memOutOfBounds precision for points-to sets which mix allocs with others #2030
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
Merged
+107
−108
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -79,64 +79,54 @@ struct | |
| in | ||
| host_contains_a_ptr host || offset_contains_a_ptr offset | ||
|
|
||
| let points_to_alloc_only man ptr = | ||
| let get_size_of_ptr_target man ptr = (* TODO: deduplicate with base (this uses IsAllocVar) *) | ||
| match man.ask (Queries.MayPointTo ptr) with | ||
| | a when not (Queries.AD.is_top a)-> | ||
| Queries.AD.for_all (function | ||
| | Addr (v, o) -> man.ask (Queries.IsAllocVar v) | ||
| | _ -> false | ||
| ) a | ||
| | _ -> false | ||
|
|
||
| let get_size_of_ptr_target man ptr = (* TODO: deduplicate with base *) | ||
| if points_to_alloc_only man ptr then | ||
| (* Ask for BlobSize from the base address (the second component being set to true) in order to avoid BlobSize giving us bot *) | ||
| man.ask (Queries.BlobSize {exp = ptr; base_address = true}) | ||
| else | ||
| match man.ask (Queries.MayPointTo ptr) with | ||
| | a when not (Queries.AD.is_top a) -> | ||
| let pts_list = Queries.AD.elements a in | ||
| let pts_elems_to_sizes (addr: Queries.AD.elt) = | ||
| begin match addr with | ||
| | Addr (v, _) -> | ||
| if hasAttribute "goblint_cil_nested" v.vattr then ( | ||
| set_mem_safety_flag InvalidDeref; | ||
| M.warn "Var %a is potentially accessed out-of-scope. Invalid memory access may occur" CilType.Varinfo.pretty v; | ||
| Checks.warn Checks.Category.InvalidMemoryAccess "Var %a is potentially accessed out-of-scope. Invalid memory access may occur" CilType.Varinfo.pretty v | ||
| ); | ||
| begin match Cil.unrollType v.vtype with | ||
| | TArray (item_typ, _, _) -> | ||
| let item_typ_size_in_bytes = size_of_type_in_bytes item_typ in | ||
| begin match man.ask (Queries.EvalLength ptr) with | ||
| | `Lifted arr_len -> | ||
| let arr_len_casted = ID.cast_to ~kind:Internal (Cilfacade.ptrdiff_ikind ()) arr_len in (* TODO: proper castkind *) | ||
| begin | ||
| try `Lifted (ID.mul item_typ_size_in_bytes arr_len_casted) | ||
| with IntDomain.ArithmeticOnIntegerBot _ -> `Bot | ||
| end | ||
| | `Bot -> `Bot | ||
| | `Top -> `Top | ||
| end | ||
| | _ -> | ||
| let type_size_in_bytes = size_of_type_in_bytes v.vtype in | ||
| `Lifted type_size_in_bytes | ||
| end | ||
| | _ -> `Top | ||
| end | ||
| in | ||
| (* Map each points-to-set element to its size *) | ||
| let pts_sizes = List.map pts_elems_to_sizes pts_list in | ||
| (* Take the smallest of all sizes that ptr's contents may have *) | ||
| begin match pts_sizes with | ||
| | [] -> `Bot | ||
| | [x] -> x | ||
| | x::xs -> List.fold_left VDQ.ID.join x xs | ||
| | a when not (Queries.AD.is_top a) -> | ||
| let pts_list = Queries.AD.elements a in | ||
| let pts_elems_to_sizes (addr: Queries.AD.elt) = | ||
| begin match addr with | ||
| | Addr (v, _) when man.ask (Queries.IsAllocVar v) -> | ||
| (* Ask for BlobSize from the base address (the second component being set to true) in order to avoid BlobSize giving us bot *) | ||
| man.ask (Queries.BlobSize {exp = ptr; base_address = true}) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This currently doesn't make a difference because the sizes are joined here anyway. The same issue is with |
||
| | Addr (v, _) -> | ||
| if hasAttribute "goblint_cil_nested" v.vattr then ( | ||
| set_mem_safety_flag InvalidDeref; | ||
| M.warn "Var %a is potentially accessed out-of-scope. Invalid memory access may occur" CilType.Varinfo.pretty v; | ||
| Checks.warn Checks.Category.InvalidMemoryAccess "Var %a is potentially accessed out-of-scope. Invalid memory access may occur" CilType.Varinfo.pretty v | ||
| ); | ||
| begin match Cil.unrollType v.vtype with | ||
| | TArray (item_typ, _, _) -> | ||
| let item_typ_size_in_bytes = size_of_type_in_bytes item_typ in | ||
| begin match man.ask (Queries.EvalLength ptr) with | ||
| | `Lifted arr_len -> | ||
| let arr_len_casted = ID.cast_to ~kind:Internal (Cilfacade.ptrdiff_ikind ()) arr_len in (* TODO: proper castkind *) | ||
| begin | ||
| try `Lifted (ID.mul item_typ_size_in_bytes arr_len_casted) | ||
| with IntDomain.ArithmeticOnIntegerBot _ -> `Bot | ||
| end | ||
| | `Bot -> `Bot | ||
| | `Top -> `Top | ||
| end | ||
| | _ -> | ||
| let type_size_in_bytes = size_of_type_in_bytes v.vtype in | ||
| `Lifted type_size_in_bytes | ||
| end | ||
| | _ -> `Top | ||
| end | ||
| | _ -> | ||
| (set_mem_safety_flag InvalidDeref; | ||
| M.warn "Pointer %a has a points-to-set of top. An invalid memory access might occur" d_exp ptr; | ||
| Checks.warn Checks.Category.InvalidMemoryAccess "Pointer %a has a points-to-set of top. An invalid memory access might occur" d_exp ptr; | ||
| `Top) | ||
| in | ||
| (* Map each points-to-set element to its size *) | ||
| let pts_sizes = List.map pts_elems_to_sizes pts_list in | ||
| (* Take the smallest of all sizes that ptr's contents may have *) | ||
| begin match pts_sizes with | ||
| | [] -> `Bot | ||
| | [x] -> x | ||
| | x::xs -> List.fold_left VDQ.ID.join x xs | ||
| end | ||
| | _ -> | ||
| (set_mem_safety_flag InvalidDeref; | ||
| M.warn "Pointer %a has a points-to-set of top. An invalid memory access might occur" d_exp ptr; | ||
| Checks.warn Checks.Category.InvalidMemoryAccess "Pointer %a has a points-to-set of top. An invalid memory access might occur" d_exp ptr; | ||
| `Top) | ||
|
|
||
| let get_ptr_deref_type ptr_typ = | ||
| match Cil.unrollType ptr_typ with | ||
|
|
||
19 changes: 19 additions & 0 deletions
19
tests/regression/74-invalid_deref/38-oob-alloc-array-mix.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| // PARAM: --enable ana.sv-comp.functions --set ana.activated[+] memOutOfBounds --enable ana.int.interval | ||
| #include <stdlib.h> | ||
| #include <stdio.h> | ||
|
|
||
| extern _Bool __VERIFIER_nondet_bool(); | ||
|
|
||
| int main(void) { | ||
| int foo[4]; | ||
| int *bar = malloc(4 * sizeof(int)); | ||
|
|
||
| int *p; | ||
| if (__VERIFIER_nondet_bool()) | ||
| p = &foo[3]; | ||
| else | ||
| p = &bar[3]; | ||
|
|
||
| *p = 42; // NOWARN | ||
| return 0; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.