Skip to content

Commit 257dadd

Browse files
authored
Merge pull request #1702 from mgrettondann/cpp-add-thread_local-support-external
C++: add thread_local support
2 parents 327d5ac + 90cfde5 commit 257dadd

File tree

12 files changed

+37
-2
lines changed

12 files changed

+37
-2
lines changed

change-notes/1.22/analysis-cpp.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@
2727
- The `semmle.code.cpp.security.TaintTracking` library now considers a pointer difference calculation as blocking taint flow.
2828
- Fixed the `LocalScopeVariableReachability.qll` library's handling of loops with an entry condition is both always true upon first entry, and where there is more than one control flow path through the loop condition. This change increases the accuracy of the `LocalScopeVariableReachability.qll` library and queries which depend on it.
2929
- The `semmle.code.cpp.models` library now models data flow through `std::swap`.
30+
- There is a new `Variable.isThreadLocal()` predicate. It can be used to tell whether a variable is `thread_local`.

cpp/ql/src/Likely Bugs/Memory Management/ReturnStackAllocatedMemory.ql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ predicate hasNontrivialConversion(Expr e) {
4545
from LocalScopeVariable var, VariableAccess va, ReturnStmt r
4646
where
4747
not var.isStatic() and
48+
not var.isThreadLocal() and
4849
not var.getUnspecifiedType() instanceof ReferenceType and
4950
not r.isFromUninstantiatedTemplate(_) and
5051
va = var.getAnAccess() and

cpp/ql/src/semmle/code/cpp/Variable.qll

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,13 @@ class Variable extends Declaration, @variable {
139139
this.hasSpecifier("is_constexpr")
140140
}
141141

142+
/**
143+
* Holds if this variable is `thread_local`.
144+
*/
145+
predicate isThreadLocal() {
146+
this.hasSpecifier("is_thread_local")
147+
}
148+
142149
/**
143150
* Holds if this variable is constructed from `v` as a result
144151
* of template instantiation. If so, it originates either from a template

cpp/ql/test/library-tests/clang_ms/element.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
| file://:0:0:0:0 | inline |
8989
| file://:0:0:0:0 | int |
9090
| file://:0:0:0:0 | is_constexpr |
91+
| file://:0:0:0:0 | is_thread_local |
9192
| file://:0:0:0:0 | long |
9293
| file://:0:0:0:0 | long double |
9394
| file://:0:0:0:0 | long long |

cpp/ql/test/library-tests/conditions/elements.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
| file://:0:0:0:0 | inline |
6666
| file://:0:0:0:0 | int |
6767
| file://:0:0:0:0 | is_constexpr |
68+
| file://:0:0:0:0 | is_thread_local |
6869
| file://:0:0:0:0 | long |
6970
| file://:0:0:0:0 | long double |
7071
| file://:0:0:0:0 | long long |

cpp/ql/test/library-tests/templates/instantiations_functions/elements.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
| file://:0:0:0:0 | int & |
114114
| file://:0:0:0:0 | int * |
115115
| file://:0:0:0:0 | is_constexpr |
116+
| file://:0:0:0:0 | is_thread_local |
116117
| file://:0:0:0:0 | long |
117118
| file://:0:0:0:0 | long double |
118119
| file://:0:0:0:0 | long long |

cpp/ql/test/library-tests/unnamed/elements.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
| file://:0:0:0:0 | int * | Other |
5959
| file://:0:0:0:0 | int[0] | Other |
6060
| file://:0:0:0:0 | is_constexpr | Other |
61+
| file://:0:0:0:0 | is_thread_local | Other |
6162
| file://:0:0:0:0 | long | Other |
6263
| file://:0:0:0:0 | long double | Other |
6364
| file://:0:0:0:0 | long long | Other |
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
void returnThreadLocal() {
3+
thread_local int threadLocal;
4+
int not_threadLocal;
5+
static thread_local int threadLocalStatic;
6+
extern thread_local int threadLocalExtern;
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
| file://:0:0:0:0 | fp_offset | false |
2+
| file://:0:0:0:0 | gp_offset | false |
3+
| file://:0:0:0:0 | overflow_arg_area | false |
4+
| file://:0:0:0:0 | p#0 | false |
5+
| file://:0:0:0:0 | p#0 | false |
6+
| file://:0:0:0:0 | reg_save_area | false |
7+
| thread_local.cpp:3:20:3:30 | threadLocal | true |
8+
| thread_local.cpp:4:7:4:21 | not_threadLocal | false |
9+
| thread_local.cpp:5:27:5:43 | threadLocalStatic | true |
10+
| thread_local.cpp:6:27:6:43 | threadLocalExtern | true |
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import cpp
2+
3+
from Variable v
4+
select v,
5+
any(boolean b | if v.isThreadLocal() then b = true else b = false)
6+

0 commit comments

Comments
 (0)