|
| 1 | +private predicate hasDefinition(@globalvariable g) { |
| 2 | + exists(@var_decl vd | var_decls(vd, g, _, _, _) | var_def(vd)) |
| 3 | +} |
| 4 | + |
| 5 | +pragma[noinline] |
| 6 | +private predicate onlyOneCompleteGlobalVariableExistsWithMangledName(@mangledname name) { |
| 7 | + strictcount(@globalvariable g | hasDefinition(g) and mangled_name(g, name)) = 1 |
| 8 | +} |
| 9 | + |
| 10 | +/** Holds if `g` is a unique global variable with a definition named `name`. */ |
| 11 | +pragma[noinline] |
| 12 | +private predicate isGlobalWithMangledNameAndWithDefinition(@mangledname name, @globalvariable g) { |
| 13 | + hasDefinition(g) and |
| 14 | + mangled_name(g, name) and |
| 15 | + onlyOneCompleteGlobalVariableExistsWithMangledName(name) |
| 16 | +} |
| 17 | + |
| 18 | +/** Holds if `g` is a global variable without a definition named `name`. */ |
| 19 | +pragma[noinline] |
| 20 | +private predicate isGlobalWithMangledNameAndWithoutDefinition(@mangledname name, @globalvariable g) { |
| 21 | + not hasDefinition(g) and |
| 22 | + mangled_name(g, name) |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Holds if `incomplete` is a global variable without a definition, and there exists |
| 27 | + * a unique global variable `complete` with the same name that does have a definition. |
| 28 | + */ |
| 29 | +private predicate hasTwinWithDefinition(@globalvariable incomplete, @globalvariable complete) { |
| 30 | + exists(@mangledname name | |
| 31 | + not variable_instantiation(incomplete, complete) and |
| 32 | + isGlobalWithMangledNameAndWithoutDefinition(name, incomplete) and |
| 33 | + isGlobalWithMangledNameAndWithDefinition(name, complete) |
| 34 | + ) |
| 35 | +} |
| 36 | + |
| 37 | +import Cached |
| 38 | + |
| 39 | +cached |
| 40 | +private module Cached { |
| 41 | + /** |
| 42 | + * If `v` is a global variable without a definition, and there exists a unique |
| 43 | + * global variable with the same name that does have a definition, then the |
| 44 | + * result is that unique global variable. Otherwise, the result is `v`. |
| 45 | + */ |
| 46 | + cached |
| 47 | + @variable resolveGlobalVariable(@variable v) { |
| 48 | + hasTwinWithDefinition(v, result) |
| 49 | + or |
| 50 | + not hasTwinWithDefinition(v, _) and |
| 51 | + result = v |
| 52 | + } |
| 53 | + |
| 54 | + cached |
| 55 | + predicate isVariable(@variable v) { |
| 56 | + not v instanceof @globalvariable |
| 57 | + or |
| 58 | + v = resolveGlobalVariable(_) |
| 59 | + } |
| 60 | +} |
0 commit comments