runtime: set library name based on compiler name
[official-gcc.git] / gcc / tsan.c
blob53f30a1b833328108b3b83d2a465089350152a19
1 /* GCC instrumentation plugin for ThreadSanitizer.
2 Copyright (C) 2011-2014 Free Software Foundation, Inc.
3 Contributed by Dmitry Vyukov <dvyukov@google.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tree.h"
26 #include "expr.h"
27 #include "intl.h"
28 #include "tm.h"
29 #include "predict.h"
30 #include "vec.h"
31 #include "hashtab.h"
32 #include "hash-set.h"
33 #include "machmode.h"
34 #include "hard-reg-set.h"
35 #include "input.h"
36 #include "function.h"
37 #include "dominance.h"
38 #include "cfg.h"
39 #include "basic-block.h"
40 #include "tree-ssa-alias.h"
41 #include "internal-fn.h"
42 #include "gimple-expr.h"
43 #include "is-a.h"
44 #include "gimple.h"
45 #include "gimplify.h"
46 #include "gimple-iterator.h"
47 #include "gimple-ssa.h"
48 #include "hash-map.h"
49 #include "plugin-api.h"
50 #include "ipa-ref.h"
51 #include "cgraph.h"
52 #include "tree-cfg.h"
53 #include "stringpool.h"
54 #include "tree-ssanames.h"
55 #include "tree-pass.h"
56 #include "tree-iterator.h"
57 #include "langhooks.h"
58 #include "output.h"
59 #include "options.h"
60 #include "target.h"
61 #include "diagnostic.h"
62 #include "tree-ssa-propagate.h"
63 #include "tsan.h"
64 #include "asan.h"
66 /* Number of instrumented memory accesses in the current function. */
68 /* Builds the following decl
69 void __tsan_read/writeX (void *addr); */
71 static tree
72 get_memory_access_decl (bool is_write, unsigned size)
74 enum built_in_function fcode;
76 if (size <= 1)
77 fcode = is_write ? BUILT_IN_TSAN_WRITE1
78 : BUILT_IN_TSAN_READ1;
79 else if (size <= 3)
80 fcode = is_write ? BUILT_IN_TSAN_WRITE2
81 : BUILT_IN_TSAN_READ2;
82 else if (size <= 7)
83 fcode = is_write ? BUILT_IN_TSAN_WRITE4
84 : BUILT_IN_TSAN_READ4;
85 else if (size <= 15)
86 fcode = is_write ? BUILT_IN_TSAN_WRITE8
87 : BUILT_IN_TSAN_READ8;
88 else
89 fcode = is_write ? BUILT_IN_TSAN_WRITE16
90 : BUILT_IN_TSAN_READ16;
92 return builtin_decl_implicit (fcode);
95 /* Check as to whether EXPR refers to a store to vptr. */
97 static tree
98 is_vptr_store (gimple stmt, tree expr, bool is_write)
100 if (is_write == true
101 && gimple_assign_single_p (stmt)
102 && TREE_CODE (expr) == COMPONENT_REF)
104 tree field = TREE_OPERAND (expr, 1);
105 if (TREE_CODE (field) == FIELD_DECL
106 && DECL_VIRTUAL_P (field))
107 return gimple_assign_rhs1 (stmt);
109 return NULL;
112 /* Instruments EXPR if needed. If any instrumentation is inserted,
113 return true. */
115 static bool
116 instrument_expr (gimple_stmt_iterator gsi, tree expr, bool is_write)
118 tree base, rhs, expr_ptr, builtin_decl;
119 basic_block bb;
120 HOST_WIDE_INT size;
121 gimple stmt, g;
122 gimple_seq seq;
123 location_t loc;
125 size = int_size_in_bytes (TREE_TYPE (expr));
126 if (size == -1)
127 return false;
129 /* For now just avoid instrumenting bit field acceses.
130 TODO: handle bit-fields as if touching the whole field. */
131 HOST_WIDE_INT bitsize, bitpos;
132 tree offset;
133 machine_mode mode;
134 int volatilep = 0, unsignedp = 0;
135 base = get_inner_reference (expr, &bitsize, &bitpos, &offset,
136 &mode, &unsignedp, &volatilep, false);
138 /* No need to instrument accesses to decls that don't escape,
139 they can't escape to other threads then. */
140 if (DECL_P (base))
142 struct pt_solution pt;
143 memset (&pt, 0, sizeof (pt));
144 pt.escaped = 1;
145 pt.ipa_escaped = flag_ipa_pta != 0;
146 pt.nonlocal = 1;
147 if (!pt_solution_includes (&pt, base))
148 return false;
149 if (!is_global_var (base) && !may_be_aliased (base))
150 return false;
153 if (TREE_READONLY (base)
154 || (TREE_CODE (base) == VAR_DECL
155 && DECL_HARD_REGISTER (base)))
156 return false;
158 if (size == 0
159 || bitpos % (size * BITS_PER_UNIT)
160 || bitsize != size * BITS_PER_UNIT)
161 return false;
163 stmt = gsi_stmt (gsi);
164 loc = gimple_location (stmt);
165 rhs = is_vptr_store (stmt, expr, is_write);
166 gcc_checking_assert (rhs != NULL || is_gimple_addressable (expr));
167 expr_ptr = build_fold_addr_expr (unshare_expr (expr));
168 seq = NULL;
169 if (!is_gimple_val (expr_ptr))
171 g = gimple_build_assign (make_ssa_name (TREE_TYPE (expr_ptr), NULL),
172 expr_ptr);
173 expr_ptr = gimple_assign_lhs (g);
174 gimple_set_location (g, loc);
175 gimple_seq_add_stmt_without_update (&seq, g);
177 if (rhs == NULL)
178 g = gimple_build_call (get_memory_access_decl (is_write, size),
179 1, expr_ptr);
180 else
182 builtin_decl = builtin_decl_implicit (BUILT_IN_TSAN_VPTR_UPDATE);
183 g = gimple_build_call (builtin_decl, 1, expr_ptr);
185 gimple_set_location (g, loc);
186 gimple_seq_add_stmt_without_update (&seq, g);
187 /* Instrumentation for assignment of a function result
188 must be inserted after the call. Instrumentation for
189 reads of function arguments must be inserted before the call.
190 That's because the call can contain synchronization. */
191 if (is_gimple_call (stmt) && is_write)
193 /* If the call can throw, it must be the last stmt in
194 a basic block, so the instrumented stmts need to be
195 inserted in successor bbs. */
196 if (is_ctrl_altering_stmt (stmt))
198 edge e;
200 bb = gsi_bb (gsi);
201 e = find_fallthru_edge (bb->succs);
202 if (e)
203 gsi_insert_seq_on_edge_immediate (e, seq);
205 else
206 gsi_insert_seq_after (&gsi, seq, GSI_NEW_STMT);
208 else
209 gsi_insert_seq_before (&gsi, seq, GSI_SAME_STMT);
211 return true;
214 /* Actions for sync/atomic builtin transformations. */
215 enum tsan_atomic_action
217 check_last, add_seq_cst, add_acquire, weak_cas, strong_cas,
218 bool_cas, val_cas, lock_release, fetch_op, fetch_op_seq_cst
221 /* Table how to map sync/atomic builtins to their corresponding
222 tsan equivalents. */
223 static const struct tsan_map_atomic
225 enum built_in_function fcode, tsan_fcode;
226 enum tsan_atomic_action action;
227 enum tree_code code;
228 } tsan_atomic_table[] =
230 #define TRANSFORM(fcode, tsan_fcode, action, code) \
231 { BUILT_IN_##fcode, BUILT_IN_##tsan_fcode, action, code }
232 #define CHECK_LAST(fcode, tsan_fcode) \
233 TRANSFORM (fcode, tsan_fcode, check_last, ERROR_MARK)
234 #define ADD_SEQ_CST(fcode, tsan_fcode) \
235 TRANSFORM (fcode, tsan_fcode, add_seq_cst, ERROR_MARK)
236 #define ADD_ACQUIRE(fcode, tsan_fcode) \
237 TRANSFORM (fcode, tsan_fcode, add_acquire, ERROR_MARK)
238 #define WEAK_CAS(fcode, tsan_fcode) \
239 TRANSFORM (fcode, tsan_fcode, weak_cas, ERROR_MARK)
240 #define STRONG_CAS(fcode, tsan_fcode) \
241 TRANSFORM (fcode, tsan_fcode, strong_cas, ERROR_MARK)
242 #define BOOL_CAS(fcode, tsan_fcode) \
243 TRANSFORM (fcode, tsan_fcode, bool_cas, ERROR_MARK)
244 #define VAL_CAS(fcode, tsan_fcode) \
245 TRANSFORM (fcode, tsan_fcode, val_cas, ERROR_MARK)
246 #define LOCK_RELEASE(fcode, tsan_fcode) \
247 TRANSFORM (fcode, tsan_fcode, lock_release, ERROR_MARK)
248 #define FETCH_OP(fcode, tsan_fcode, code) \
249 TRANSFORM (fcode, tsan_fcode, fetch_op, code)
250 #define FETCH_OPS(fcode, tsan_fcode, code) \
251 TRANSFORM (fcode, tsan_fcode, fetch_op_seq_cst, code)
253 CHECK_LAST (ATOMIC_LOAD_1, TSAN_ATOMIC8_LOAD),
254 CHECK_LAST (ATOMIC_LOAD_2, TSAN_ATOMIC16_LOAD),
255 CHECK_LAST (ATOMIC_LOAD_4, TSAN_ATOMIC32_LOAD),
256 CHECK_LAST (ATOMIC_LOAD_8, TSAN_ATOMIC64_LOAD),
257 CHECK_LAST (ATOMIC_LOAD_16, TSAN_ATOMIC128_LOAD),
258 CHECK_LAST (ATOMIC_STORE_1, TSAN_ATOMIC8_STORE),
259 CHECK_LAST (ATOMIC_STORE_2, TSAN_ATOMIC16_STORE),
260 CHECK_LAST (ATOMIC_STORE_4, TSAN_ATOMIC32_STORE),
261 CHECK_LAST (ATOMIC_STORE_8, TSAN_ATOMIC64_STORE),
262 CHECK_LAST (ATOMIC_STORE_16, TSAN_ATOMIC128_STORE),
263 CHECK_LAST (ATOMIC_EXCHANGE_1, TSAN_ATOMIC8_EXCHANGE),
264 CHECK_LAST (ATOMIC_EXCHANGE_2, TSAN_ATOMIC16_EXCHANGE),
265 CHECK_LAST (ATOMIC_EXCHANGE_4, TSAN_ATOMIC32_EXCHANGE),
266 CHECK_LAST (ATOMIC_EXCHANGE_8, TSAN_ATOMIC64_EXCHANGE),
267 CHECK_LAST (ATOMIC_EXCHANGE_16, TSAN_ATOMIC128_EXCHANGE),
268 CHECK_LAST (ATOMIC_FETCH_ADD_1, TSAN_ATOMIC8_FETCH_ADD),
269 CHECK_LAST (ATOMIC_FETCH_ADD_2, TSAN_ATOMIC16_FETCH_ADD),
270 CHECK_LAST (ATOMIC_FETCH_ADD_4, TSAN_ATOMIC32_FETCH_ADD),
271 CHECK_LAST (ATOMIC_FETCH_ADD_8, TSAN_ATOMIC64_FETCH_ADD),
272 CHECK_LAST (ATOMIC_FETCH_ADD_16, TSAN_ATOMIC128_FETCH_ADD),
273 CHECK_LAST (ATOMIC_FETCH_SUB_1, TSAN_ATOMIC8_FETCH_SUB),
274 CHECK_LAST (ATOMIC_FETCH_SUB_2, TSAN_ATOMIC16_FETCH_SUB),
275 CHECK_LAST (ATOMIC_FETCH_SUB_4, TSAN_ATOMIC32_FETCH_SUB),
276 CHECK_LAST (ATOMIC_FETCH_SUB_8, TSAN_ATOMIC64_FETCH_SUB),
277 CHECK_LAST (ATOMIC_FETCH_SUB_16, TSAN_ATOMIC128_FETCH_SUB),
278 CHECK_LAST (ATOMIC_FETCH_AND_1, TSAN_ATOMIC8_FETCH_AND),
279 CHECK_LAST (ATOMIC_FETCH_AND_2, TSAN_ATOMIC16_FETCH_AND),
280 CHECK_LAST (ATOMIC_FETCH_AND_4, TSAN_ATOMIC32_FETCH_AND),
281 CHECK_LAST (ATOMIC_FETCH_AND_8, TSAN_ATOMIC64_FETCH_AND),
282 CHECK_LAST (ATOMIC_FETCH_AND_16, TSAN_ATOMIC128_FETCH_AND),
283 CHECK_LAST (ATOMIC_FETCH_OR_1, TSAN_ATOMIC8_FETCH_OR),
284 CHECK_LAST (ATOMIC_FETCH_OR_2, TSAN_ATOMIC16_FETCH_OR),
285 CHECK_LAST (ATOMIC_FETCH_OR_4, TSAN_ATOMIC32_FETCH_OR),
286 CHECK_LAST (ATOMIC_FETCH_OR_8, TSAN_ATOMIC64_FETCH_OR),
287 CHECK_LAST (ATOMIC_FETCH_OR_16, TSAN_ATOMIC128_FETCH_OR),
288 CHECK_LAST (ATOMIC_FETCH_XOR_1, TSAN_ATOMIC8_FETCH_XOR),
289 CHECK_LAST (ATOMIC_FETCH_XOR_2, TSAN_ATOMIC16_FETCH_XOR),
290 CHECK_LAST (ATOMIC_FETCH_XOR_4, TSAN_ATOMIC32_FETCH_XOR),
291 CHECK_LAST (ATOMIC_FETCH_XOR_8, TSAN_ATOMIC64_FETCH_XOR),
292 CHECK_LAST (ATOMIC_FETCH_XOR_16, TSAN_ATOMIC128_FETCH_XOR),
293 CHECK_LAST (ATOMIC_FETCH_NAND_1, TSAN_ATOMIC8_FETCH_NAND),
294 CHECK_LAST (ATOMIC_FETCH_NAND_2, TSAN_ATOMIC16_FETCH_NAND),
295 CHECK_LAST (ATOMIC_FETCH_NAND_4, TSAN_ATOMIC32_FETCH_NAND),
296 CHECK_LAST (ATOMIC_FETCH_NAND_8, TSAN_ATOMIC64_FETCH_NAND),
297 CHECK_LAST (ATOMIC_FETCH_NAND_16, TSAN_ATOMIC128_FETCH_NAND),
299 CHECK_LAST (ATOMIC_THREAD_FENCE, TSAN_ATOMIC_THREAD_FENCE),
300 CHECK_LAST (ATOMIC_SIGNAL_FENCE, TSAN_ATOMIC_SIGNAL_FENCE),
302 FETCH_OP (ATOMIC_ADD_FETCH_1, TSAN_ATOMIC8_FETCH_ADD, PLUS_EXPR),
303 FETCH_OP (ATOMIC_ADD_FETCH_2, TSAN_ATOMIC16_FETCH_ADD, PLUS_EXPR),
304 FETCH_OP (ATOMIC_ADD_FETCH_4, TSAN_ATOMIC32_FETCH_ADD, PLUS_EXPR),
305 FETCH_OP (ATOMIC_ADD_FETCH_8, TSAN_ATOMIC64_FETCH_ADD, PLUS_EXPR),
306 FETCH_OP (ATOMIC_ADD_FETCH_16, TSAN_ATOMIC128_FETCH_ADD, PLUS_EXPR),
307 FETCH_OP (ATOMIC_SUB_FETCH_1, TSAN_ATOMIC8_FETCH_SUB, MINUS_EXPR),
308 FETCH_OP (ATOMIC_SUB_FETCH_2, TSAN_ATOMIC16_FETCH_SUB, MINUS_EXPR),
309 FETCH_OP (ATOMIC_SUB_FETCH_4, TSAN_ATOMIC32_FETCH_SUB, MINUS_EXPR),
310 FETCH_OP (ATOMIC_SUB_FETCH_8, TSAN_ATOMIC64_FETCH_SUB, MINUS_EXPR),
311 FETCH_OP (ATOMIC_SUB_FETCH_16, TSAN_ATOMIC128_FETCH_SUB, MINUS_EXPR),
312 FETCH_OP (ATOMIC_AND_FETCH_1, TSAN_ATOMIC8_FETCH_AND, BIT_AND_EXPR),
313 FETCH_OP (ATOMIC_AND_FETCH_2, TSAN_ATOMIC16_FETCH_AND, BIT_AND_EXPR),
314 FETCH_OP (ATOMIC_AND_FETCH_4, TSAN_ATOMIC32_FETCH_AND, BIT_AND_EXPR),
315 FETCH_OP (ATOMIC_AND_FETCH_8, TSAN_ATOMIC64_FETCH_AND, BIT_AND_EXPR),
316 FETCH_OP (ATOMIC_AND_FETCH_16, TSAN_ATOMIC128_FETCH_AND, BIT_AND_EXPR),
317 FETCH_OP (ATOMIC_OR_FETCH_1, TSAN_ATOMIC8_FETCH_OR, BIT_IOR_EXPR),
318 FETCH_OP (ATOMIC_OR_FETCH_2, TSAN_ATOMIC16_FETCH_OR, BIT_IOR_EXPR),
319 FETCH_OP (ATOMIC_OR_FETCH_4, TSAN_ATOMIC32_FETCH_OR, BIT_IOR_EXPR),
320 FETCH_OP (ATOMIC_OR_FETCH_8, TSAN_ATOMIC64_FETCH_OR, BIT_IOR_EXPR),
321 FETCH_OP (ATOMIC_OR_FETCH_16, TSAN_ATOMIC128_FETCH_OR, BIT_IOR_EXPR),
322 FETCH_OP (ATOMIC_XOR_FETCH_1, TSAN_ATOMIC8_FETCH_XOR, BIT_XOR_EXPR),
323 FETCH_OP (ATOMIC_XOR_FETCH_2, TSAN_ATOMIC16_FETCH_XOR, BIT_XOR_EXPR),
324 FETCH_OP (ATOMIC_XOR_FETCH_4, TSAN_ATOMIC32_FETCH_XOR, BIT_XOR_EXPR),
325 FETCH_OP (ATOMIC_XOR_FETCH_8, TSAN_ATOMIC64_FETCH_XOR, BIT_XOR_EXPR),
326 FETCH_OP (ATOMIC_XOR_FETCH_16, TSAN_ATOMIC128_FETCH_XOR, BIT_XOR_EXPR),
327 FETCH_OP (ATOMIC_NAND_FETCH_1, TSAN_ATOMIC8_FETCH_NAND, BIT_NOT_EXPR),
328 FETCH_OP (ATOMIC_NAND_FETCH_2, TSAN_ATOMIC16_FETCH_NAND, BIT_NOT_EXPR),
329 FETCH_OP (ATOMIC_NAND_FETCH_4, TSAN_ATOMIC32_FETCH_NAND, BIT_NOT_EXPR),
330 FETCH_OP (ATOMIC_NAND_FETCH_8, TSAN_ATOMIC64_FETCH_NAND, BIT_NOT_EXPR),
331 FETCH_OP (ATOMIC_NAND_FETCH_16, TSAN_ATOMIC128_FETCH_NAND, BIT_NOT_EXPR),
333 ADD_ACQUIRE (SYNC_LOCK_TEST_AND_SET_1, TSAN_ATOMIC8_EXCHANGE),
334 ADD_ACQUIRE (SYNC_LOCK_TEST_AND_SET_2, TSAN_ATOMIC16_EXCHANGE),
335 ADD_ACQUIRE (SYNC_LOCK_TEST_AND_SET_4, TSAN_ATOMIC32_EXCHANGE),
336 ADD_ACQUIRE (SYNC_LOCK_TEST_AND_SET_8, TSAN_ATOMIC64_EXCHANGE),
337 ADD_ACQUIRE (SYNC_LOCK_TEST_AND_SET_16, TSAN_ATOMIC128_EXCHANGE),
339 ADD_SEQ_CST (SYNC_FETCH_AND_ADD_1, TSAN_ATOMIC8_FETCH_ADD),
340 ADD_SEQ_CST (SYNC_FETCH_AND_ADD_2, TSAN_ATOMIC16_FETCH_ADD),
341 ADD_SEQ_CST (SYNC_FETCH_AND_ADD_4, TSAN_ATOMIC32_FETCH_ADD),
342 ADD_SEQ_CST (SYNC_FETCH_AND_ADD_8, TSAN_ATOMIC64_FETCH_ADD),
343 ADD_SEQ_CST (SYNC_FETCH_AND_ADD_16, TSAN_ATOMIC128_FETCH_ADD),
344 ADD_SEQ_CST (SYNC_FETCH_AND_SUB_1, TSAN_ATOMIC8_FETCH_SUB),
345 ADD_SEQ_CST (SYNC_FETCH_AND_SUB_2, TSAN_ATOMIC16_FETCH_SUB),
346 ADD_SEQ_CST (SYNC_FETCH_AND_SUB_4, TSAN_ATOMIC32_FETCH_SUB),
347 ADD_SEQ_CST (SYNC_FETCH_AND_SUB_8, TSAN_ATOMIC64_FETCH_SUB),
348 ADD_SEQ_CST (SYNC_FETCH_AND_SUB_16, TSAN_ATOMIC128_FETCH_SUB),
349 ADD_SEQ_CST (SYNC_FETCH_AND_AND_1, TSAN_ATOMIC8_FETCH_AND),
350 ADD_SEQ_CST (SYNC_FETCH_AND_AND_2, TSAN_ATOMIC16_FETCH_AND),
351 ADD_SEQ_CST (SYNC_FETCH_AND_AND_4, TSAN_ATOMIC32_FETCH_AND),
352 ADD_SEQ_CST (SYNC_FETCH_AND_AND_8, TSAN_ATOMIC64_FETCH_AND),
353 ADD_SEQ_CST (SYNC_FETCH_AND_AND_16, TSAN_ATOMIC128_FETCH_AND),
354 ADD_SEQ_CST (SYNC_FETCH_AND_OR_1, TSAN_ATOMIC8_FETCH_OR),
355 ADD_SEQ_CST (SYNC_FETCH_AND_OR_2, TSAN_ATOMIC16_FETCH_OR),
356 ADD_SEQ_CST (SYNC_FETCH_AND_OR_4, TSAN_ATOMIC32_FETCH_OR),
357 ADD_SEQ_CST (SYNC_FETCH_AND_OR_8, TSAN_ATOMIC64_FETCH_OR),
358 ADD_SEQ_CST (SYNC_FETCH_AND_OR_16, TSAN_ATOMIC128_FETCH_OR),
359 ADD_SEQ_CST (SYNC_FETCH_AND_XOR_1, TSAN_ATOMIC8_FETCH_XOR),
360 ADD_SEQ_CST (SYNC_FETCH_AND_XOR_2, TSAN_ATOMIC16_FETCH_XOR),
361 ADD_SEQ_CST (SYNC_FETCH_AND_XOR_4, TSAN_ATOMIC32_FETCH_XOR),
362 ADD_SEQ_CST (SYNC_FETCH_AND_XOR_8, TSAN_ATOMIC64_FETCH_XOR),
363 ADD_SEQ_CST (SYNC_FETCH_AND_XOR_16, TSAN_ATOMIC128_FETCH_XOR),
364 ADD_SEQ_CST (SYNC_FETCH_AND_NAND_1, TSAN_ATOMIC8_FETCH_NAND),
365 ADD_SEQ_CST (SYNC_FETCH_AND_NAND_2, TSAN_ATOMIC16_FETCH_NAND),
366 ADD_SEQ_CST (SYNC_FETCH_AND_NAND_4, TSAN_ATOMIC32_FETCH_NAND),
367 ADD_SEQ_CST (SYNC_FETCH_AND_NAND_8, TSAN_ATOMIC64_FETCH_NAND),
368 ADD_SEQ_CST (SYNC_FETCH_AND_NAND_16, TSAN_ATOMIC128_FETCH_NAND),
370 ADD_SEQ_CST (SYNC_SYNCHRONIZE, TSAN_ATOMIC_THREAD_FENCE),
372 FETCH_OPS (SYNC_ADD_AND_FETCH_1, TSAN_ATOMIC8_FETCH_ADD, PLUS_EXPR),
373 FETCH_OPS (SYNC_ADD_AND_FETCH_2, TSAN_ATOMIC16_FETCH_ADD, PLUS_EXPR),
374 FETCH_OPS (SYNC_ADD_AND_FETCH_4, TSAN_ATOMIC32_FETCH_ADD, PLUS_EXPR),
375 FETCH_OPS (SYNC_ADD_AND_FETCH_8, TSAN_ATOMIC64_FETCH_ADD, PLUS_EXPR),
376 FETCH_OPS (SYNC_ADD_AND_FETCH_16, TSAN_ATOMIC128_FETCH_ADD, PLUS_EXPR),
377 FETCH_OPS (SYNC_SUB_AND_FETCH_1, TSAN_ATOMIC8_FETCH_SUB, MINUS_EXPR),
378 FETCH_OPS (SYNC_SUB_AND_FETCH_2, TSAN_ATOMIC16_FETCH_SUB, MINUS_EXPR),
379 FETCH_OPS (SYNC_SUB_AND_FETCH_4, TSAN_ATOMIC32_FETCH_SUB, MINUS_EXPR),
380 FETCH_OPS (SYNC_SUB_AND_FETCH_8, TSAN_ATOMIC64_FETCH_SUB, MINUS_EXPR),
381 FETCH_OPS (SYNC_SUB_AND_FETCH_16, TSAN_ATOMIC128_FETCH_SUB, MINUS_EXPR),
382 FETCH_OPS (SYNC_AND_AND_FETCH_1, TSAN_ATOMIC8_FETCH_AND, BIT_AND_EXPR),
383 FETCH_OPS (SYNC_AND_AND_FETCH_2, TSAN_ATOMIC16_FETCH_AND, BIT_AND_EXPR),
384 FETCH_OPS (SYNC_AND_AND_FETCH_4, TSAN_ATOMIC32_FETCH_AND, BIT_AND_EXPR),
385 FETCH_OPS (SYNC_AND_AND_FETCH_8, TSAN_ATOMIC64_FETCH_AND, BIT_AND_EXPR),
386 FETCH_OPS (SYNC_AND_AND_FETCH_16, TSAN_ATOMIC128_FETCH_AND, BIT_AND_EXPR),
387 FETCH_OPS (SYNC_OR_AND_FETCH_1, TSAN_ATOMIC8_FETCH_OR, BIT_IOR_EXPR),
388 FETCH_OPS (SYNC_OR_AND_FETCH_2, TSAN_ATOMIC16_FETCH_OR, BIT_IOR_EXPR),
389 FETCH_OPS (SYNC_OR_AND_FETCH_4, TSAN_ATOMIC32_FETCH_OR, BIT_IOR_EXPR),
390 FETCH_OPS (SYNC_OR_AND_FETCH_8, TSAN_ATOMIC64_FETCH_OR, BIT_IOR_EXPR),
391 FETCH_OPS (SYNC_OR_AND_FETCH_16, TSAN_ATOMIC128_FETCH_OR, BIT_IOR_EXPR),
392 FETCH_OPS (SYNC_XOR_AND_FETCH_1, TSAN_ATOMIC8_FETCH_XOR, BIT_XOR_EXPR),
393 FETCH_OPS (SYNC_XOR_AND_FETCH_2, TSAN_ATOMIC16_FETCH_XOR, BIT_XOR_EXPR),
394 FETCH_OPS (SYNC_XOR_AND_FETCH_4, TSAN_ATOMIC32_FETCH_XOR, BIT_XOR_EXPR),
395 FETCH_OPS (SYNC_XOR_AND_FETCH_8, TSAN_ATOMIC64_FETCH_XOR, BIT_XOR_EXPR),
396 FETCH_OPS (SYNC_XOR_AND_FETCH_16, TSAN_ATOMIC128_FETCH_XOR, BIT_XOR_EXPR),
397 FETCH_OPS (SYNC_NAND_AND_FETCH_1, TSAN_ATOMIC8_FETCH_NAND, BIT_NOT_EXPR),
398 FETCH_OPS (SYNC_NAND_AND_FETCH_2, TSAN_ATOMIC16_FETCH_NAND, BIT_NOT_EXPR),
399 FETCH_OPS (SYNC_NAND_AND_FETCH_4, TSAN_ATOMIC32_FETCH_NAND, BIT_NOT_EXPR),
400 FETCH_OPS (SYNC_NAND_AND_FETCH_8, TSAN_ATOMIC64_FETCH_NAND, BIT_NOT_EXPR),
401 FETCH_OPS (SYNC_NAND_AND_FETCH_16, TSAN_ATOMIC128_FETCH_NAND, BIT_NOT_EXPR),
403 WEAK_CAS (ATOMIC_COMPARE_EXCHANGE_1, TSAN_ATOMIC8_COMPARE_EXCHANGE_WEAK),
404 WEAK_CAS (ATOMIC_COMPARE_EXCHANGE_2, TSAN_ATOMIC16_COMPARE_EXCHANGE_WEAK),
405 WEAK_CAS (ATOMIC_COMPARE_EXCHANGE_4, TSAN_ATOMIC32_COMPARE_EXCHANGE_WEAK),
406 WEAK_CAS (ATOMIC_COMPARE_EXCHANGE_8, TSAN_ATOMIC64_COMPARE_EXCHANGE_WEAK),
407 WEAK_CAS (ATOMIC_COMPARE_EXCHANGE_16, TSAN_ATOMIC128_COMPARE_EXCHANGE_WEAK),
409 STRONG_CAS (ATOMIC_COMPARE_EXCHANGE_1, TSAN_ATOMIC8_COMPARE_EXCHANGE_STRONG),
410 STRONG_CAS (ATOMIC_COMPARE_EXCHANGE_2,
411 TSAN_ATOMIC16_COMPARE_EXCHANGE_STRONG),
412 STRONG_CAS (ATOMIC_COMPARE_EXCHANGE_4,
413 TSAN_ATOMIC32_COMPARE_EXCHANGE_STRONG),
414 STRONG_CAS (ATOMIC_COMPARE_EXCHANGE_8,
415 TSAN_ATOMIC64_COMPARE_EXCHANGE_STRONG),
416 STRONG_CAS (ATOMIC_COMPARE_EXCHANGE_16,
417 TSAN_ATOMIC128_COMPARE_EXCHANGE_STRONG),
419 BOOL_CAS (SYNC_BOOL_COMPARE_AND_SWAP_1,
420 TSAN_ATOMIC8_COMPARE_EXCHANGE_STRONG),
421 BOOL_CAS (SYNC_BOOL_COMPARE_AND_SWAP_2,
422 TSAN_ATOMIC16_COMPARE_EXCHANGE_STRONG),
423 BOOL_CAS (SYNC_BOOL_COMPARE_AND_SWAP_4,
424 TSAN_ATOMIC32_COMPARE_EXCHANGE_STRONG),
425 BOOL_CAS (SYNC_BOOL_COMPARE_AND_SWAP_8,
426 TSAN_ATOMIC64_COMPARE_EXCHANGE_STRONG),
427 BOOL_CAS (SYNC_BOOL_COMPARE_AND_SWAP_16,
428 TSAN_ATOMIC128_COMPARE_EXCHANGE_STRONG),
430 VAL_CAS (SYNC_VAL_COMPARE_AND_SWAP_1, TSAN_ATOMIC8_COMPARE_EXCHANGE_STRONG),
431 VAL_CAS (SYNC_VAL_COMPARE_AND_SWAP_2, TSAN_ATOMIC16_COMPARE_EXCHANGE_STRONG),
432 VAL_CAS (SYNC_VAL_COMPARE_AND_SWAP_4, TSAN_ATOMIC32_COMPARE_EXCHANGE_STRONG),
433 VAL_CAS (SYNC_VAL_COMPARE_AND_SWAP_8, TSAN_ATOMIC64_COMPARE_EXCHANGE_STRONG),
434 VAL_CAS (SYNC_VAL_COMPARE_AND_SWAP_16,
435 TSAN_ATOMIC128_COMPARE_EXCHANGE_STRONG),
437 LOCK_RELEASE (SYNC_LOCK_RELEASE_1, TSAN_ATOMIC8_STORE),
438 LOCK_RELEASE (SYNC_LOCK_RELEASE_2, TSAN_ATOMIC16_STORE),
439 LOCK_RELEASE (SYNC_LOCK_RELEASE_4, TSAN_ATOMIC32_STORE),
440 LOCK_RELEASE (SYNC_LOCK_RELEASE_8, TSAN_ATOMIC64_STORE),
441 LOCK_RELEASE (SYNC_LOCK_RELEASE_16, TSAN_ATOMIC128_STORE)
444 /* Instrument an atomic builtin. */
446 static void
447 instrument_builtin_call (gimple_stmt_iterator *gsi)
449 gimple stmt = gsi_stmt (*gsi), g;
450 tree callee = gimple_call_fndecl (stmt), last_arg, args[6], t, lhs;
451 enum built_in_function fcode = DECL_FUNCTION_CODE (callee);
452 unsigned int i, num = gimple_call_num_args (stmt), j;
453 for (j = 0; j < 6 && j < num; j++)
454 args[j] = gimple_call_arg (stmt, j);
455 for (i = 0; i < ARRAY_SIZE (tsan_atomic_table); i++)
456 if (fcode != tsan_atomic_table[i].fcode)
457 continue;
458 else
460 tree decl = builtin_decl_implicit (tsan_atomic_table[i].tsan_fcode);
461 if (decl == NULL_TREE)
462 return;
463 switch (tsan_atomic_table[i].action)
465 case check_last:
466 case fetch_op:
467 last_arg = gimple_call_arg (stmt, num - 1);
468 if (!tree_fits_uhwi_p (last_arg)
469 || tree_to_uhwi (last_arg) > MEMMODEL_SEQ_CST)
470 return;
471 gimple_call_set_fndecl (stmt, decl);
472 update_stmt (stmt);
473 if (tsan_atomic_table[i].action == fetch_op)
475 args[1] = gimple_call_arg (stmt, 1);
476 goto adjust_result;
478 return;
479 case add_seq_cst:
480 case add_acquire:
481 case fetch_op_seq_cst:
482 gcc_assert (num <= 2);
483 for (j = 0; j < num; j++)
484 args[j] = gimple_call_arg (stmt, j);
485 for (; j < 2; j++)
486 args[j] = NULL_TREE;
487 args[num] = build_int_cst (NULL_TREE,
488 tsan_atomic_table[i].action
489 != add_acquire
490 ? MEMMODEL_SEQ_CST
491 : MEMMODEL_ACQUIRE);
492 update_gimple_call (gsi, decl, num + 1, args[0], args[1], args[2]);
493 stmt = gsi_stmt (*gsi);
494 if (tsan_atomic_table[i].action == fetch_op_seq_cst)
496 adjust_result:
497 lhs = gimple_call_lhs (stmt);
498 if (lhs == NULL_TREE)
499 return;
500 if (!useless_type_conversion_p (TREE_TYPE (lhs),
501 TREE_TYPE (args[1])))
503 tree var = make_ssa_name (TREE_TYPE (lhs), NULL);
504 g = gimple_build_assign_with_ops (NOP_EXPR, var,
505 args[1], NULL_TREE);
506 gsi_insert_after (gsi, g, GSI_NEW_STMT);
507 args[1] = var;
509 gimple_call_set_lhs (stmt,
510 make_ssa_name (TREE_TYPE (lhs), NULL));
511 /* BIT_NOT_EXPR stands for NAND. */
512 if (tsan_atomic_table[i].code == BIT_NOT_EXPR)
514 tree var = make_ssa_name (TREE_TYPE (lhs), NULL);
515 g = gimple_build_assign_with_ops (BIT_AND_EXPR, var,
516 gimple_call_lhs (stmt),
517 args[1]);
518 gsi_insert_after (gsi, g, GSI_NEW_STMT);
519 g = gimple_build_assign_with_ops (BIT_NOT_EXPR, lhs, var,
520 NULL_TREE);
522 else
523 g = gimple_build_assign_with_ops (tsan_atomic_table[i].code,
524 lhs,
525 gimple_call_lhs (stmt),
526 args[1]);
527 update_stmt (stmt);
528 gsi_insert_after (gsi, g, GSI_NEW_STMT);
530 return;
531 case weak_cas:
532 if (!integer_nonzerop (gimple_call_arg (stmt, 3)))
533 continue;
534 /* FALLTHRU */
535 case strong_cas:
536 gcc_assert (num == 6);
537 for (j = 0; j < 6; j++)
538 args[j] = gimple_call_arg (stmt, j);
539 if (!tree_fits_uhwi_p (args[4])
540 || tree_to_uhwi (args[4]) > MEMMODEL_SEQ_CST)
541 return;
542 if (!tree_fits_uhwi_p (args[5])
543 || tree_to_uhwi (args[5]) > MEMMODEL_SEQ_CST)
544 return;
545 update_gimple_call (gsi, decl, 5, args[0], args[1], args[2],
546 args[4], args[5]);
547 return;
548 case bool_cas:
549 case val_cas:
550 gcc_assert (num == 3);
551 for (j = 0; j < 3; j++)
552 args[j] = gimple_call_arg (stmt, j);
553 t = TYPE_ARG_TYPES (TREE_TYPE (decl));
554 t = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (t)));
555 t = create_tmp_var (t, NULL);
556 mark_addressable (t);
557 if (!useless_type_conversion_p (TREE_TYPE (t),
558 TREE_TYPE (args[1])))
560 g = gimple_build_assign_with_ops (NOP_EXPR,
561 make_ssa_name (TREE_TYPE (t),
562 NULL),
563 args[1], NULL_TREE);
564 gsi_insert_before (gsi, g, GSI_SAME_STMT);
565 args[1] = gimple_assign_lhs (g);
567 g = gimple_build_assign (t, args[1]);
568 gsi_insert_before (gsi, g, GSI_SAME_STMT);
569 lhs = gimple_call_lhs (stmt);
570 update_gimple_call (gsi, decl, 5, args[0],
571 build_fold_addr_expr (t), args[2],
572 build_int_cst (NULL_TREE,
573 MEMMODEL_SEQ_CST),
574 build_int_cst (NULL_TREE,
575 MEMMODEL_SEQ_CST));
576 if (tsan_atomic_table[i].action == val_cas && lhs)
578 tree cond;
579 stmt = gsi_stmt (*gsi);
580 g = gimple_build_assign (make_ssa_name (TREE_TYPE (t), NULL),
582 gsi_insert_after (gsi, g, GSI_NEW_STMT);
583 t = make_ssa_name (TREE_TYPE (TREE_TYPE (decl)), stmt);
584 cond = build2 (NE_EXPR, boolean_type_node, t,
585 build_int_cst (TREE_TYPE (t), 0));
586 g = gimple_build_assign_with_ops (COND_EXPR, lhs, cond,
587 args[1],
588 gimple_assign_lhs (g));
589 gimple_call_set_lhs (stmt, t);
590 update_stmt (stmt);
591 gsi_insert_after (gsi, g, GSI_NEW_STMT);
593 return;
594 case lock_release:
595 gcc_assert (num == 1);
596 t = TYPE_ARG_TYPES (TREE_TYPE (decl));
597 t = TREE_VALUE (TREE_CHAIN (t));
598 update_gimple_call (gsi, decl, 3, gimple_call_arg (stmt, 0),
599 build_int_cst (t, 0),
600 build_int_cst (NULL_TREE,
601 MEMMODEL_RELEASE));
602 return;
603 default:
604 continue;
609 /* Instruments the gimple pointed to by GSI. Return
610 true if func entry/exit should be instrumented. */
612 static bool
613 instrument_gimple (gimple_stmt_iterator *gsi)
615 gimple stmt;
616 tree rhs, lhs;
617 bool instrumented = false;
619 stmt = gsi_stmt (*gsi);
620 if (is_gimple_call (stmt)
621 && (gimple_call_fndecl (stmt)
622 != builtin_decl_implicit (BUILT_IN_TSAN_INIT)))
624 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
625 instrument_builtin_call (gsi);
626 return true;
628 else if (is_gimple_assign (stmt)
629 && !gimple_clobber_p (stmt))
631 if (gimple_store_p (stmt))
633 lhs = gimple_assign_lhs (stmt);
634 instrumented = instrument_expr (*gsi, lhs, true);
636 if (gimple_assign_load_p (stmt))
638 rhs = gimple_assign_rhs1 (stmt);
639 instrumented = instrument_expr (*gsi, rhs, false);
642 return instrumented;
645 /* Instruments all interesting memory accesses in the current function.
646 Return true if func entry/exit should be instrumented. */
648 static bool
649 instrument_memory_accesses (void)
651 basic_block bb;
652 gimple_stmt_iterator gsi;
653 bool fentry_exit_instrument = false;
655 FOR_EACH_BB_FN (bb, cfun)
656 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
657 fentry_exit_instrument |= instrument_gimple (&gsi);
658 return fentry_exit_instrument;
661 /* Instruments function entry. */
663 static void
664 instrument_func_entry (void)
666 basic_block succ_bb;
667 gimple_stmt_iterator gsi;
668 tree ret_addr, builtin_decl;
669 gimple g;
671 succ_bb = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
672 gsi = gsi_after_labels (succ_bb);
674 builtin_decl = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
675 g = gimple_build_call (builtin_decl, 1, integer_zero_node);
676 ret_addr = make_ssa_name (ptr_type_node, NULL);
677 gimple_call_set_lhs (g, ret_addr);
678 gimple_set_location (g, cfun->function_start_locus);
679 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
681 builtin_decl = builtin_decl_implicit (BUILT_IN_TSAN_FUNC_ENTRY);
682 g = gimple_build_call (builtin_decl, 1, ret_addr);
683 gimple_set_location (g, cfun->function_start_locus);
684 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
687 /* Instruments function exits. */
689 static void
690 instrument_func_exit (void)
692 location_t loc;
693 basic_block exit_bb;
694 gimple_stmt_iterator gsi;
695 gimple stmt, g;
696 tree builtin_decl;
697 edge e;
698 edge_iterator ei;
700 /* Find all function exits. */
701 exit_bb = EXIT_BLOCK_PTR_FOR_FN (cfun);
702 FOR_EACH_EDGE (e, ei, exit_bb->preds)
704 gsi = gsi_last_bb (e->src);
705 stmt = gsi_stmt (gsi);
706 gcc_assert (gimple_code (stmt) == GIMPLE_RETURN
707 || gimple_call_builtin_p (stmt, BUILT_IN_RETURN));
708 loc = gimple_location (stmt);
709 builtin_decl = builtin_decl_implicit (BUILT_IN_TSAN_FUNC_EXIT);
710 g = gimple_build_call (builtin_decl, 0);
711 gimple_set_location (g, loc);
712 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
716 /* ThreadSanitizer instrumentation pass. */
718 static unsigned
719 tsan_pass (void)
721 initialize_sanitizer_builtins ();
722 if (instrument_memory_accesses ())
724 instrument_func_entry ();
725 instrument_func_exit ();
727 return 0;
730 /* Inserts __tsan_init () into the list of CTORs. */
732 void
733 tsan_finish_file (void)
735 tree ctor_statements = NULL_TREE;
737 initialize_sanitizer_builtins ();
738 tree init_decl = builtin_decl_implicit (BUILT_IN_TSAN_INIT);
739 append_to_statement_list (build_call_expr (init_decl, 0),
740 &ctor_statements);
741 cgraph_build_static_cdtor ('I', ctor_statements,
742 MAX_RESERVED_INIT_PRIORITY - 1);
745 /* The pass descriptor. */
747 namespace {
749 const pass_data pass_data_tsan =
751 GIMPLE_PASS, /* type */
752 "tsan", /* name */
753 OPTGROUP_NONE, /* optinfo_flags */
754 TV_NONE, /* tv_id */
755 ( PROP_ssa | PROP_cfg ), /* properties_required */
756 0, /* properties_provided */
757 0, /* properties_destroyed */
758 0, /* todo_flags_start */
759 TODO_update_ssa, /* todo_flags_finish */
762 class pass_tsan : public gimple_opt_pass
764 public:
765 pass_tsan (gcc::context *ctxt)
766 : gimple_opt_pass (pass_data_tsan, ctxt)
769 /* opt_pass methods: */
770 opt_pass * clone () { return new pass_tsan (m_ctxt); }
771 virtual bool gate (function *)
773 return (flag_sanitize & SANITIZE_THREAD) != 0;
776 virtual unsigned int execute (function *) { return tsan_pass (); }
778 }; // class pass_tsan
780 } // anon namespace
782 gimple_opt_pass *
783 make_pass_tsan (gcc::context *ctxt)
785 return new pass_tsan (ctxt);
788 namespace {
790 const pass_data pass_data_tsan_O0 =
792 GIMPLE_PASS, /* type */
793 "tsan0", /* name */
794 OPTGROUP_NONE, /* optinfo_flags */
795 TV_NONE, /* tv_id */
796 ( PROP_ssa | PROP_cfg ), /* properties_required */
797 0, /* properties_provided */
798 0, /* properties_destroyed */
799 0, /* todo_flags_start */
800 TODO_update_ssa, /* todo_flags_finish */
803 class pass_tsan_O0 : public gimple_opt_pass
805 public:
806 pass_tsan_O0 (gcc::context *ctxt)
807 : gimple_opt_pass (pass_data_tsan_O0, ctxt)
810 /* opt_pass methods: */
811 virtual bool gate (function *)
813 return (flag_sanitize & SANITIZE_THREAD) != 0 && !optimize;
816 virtual unsigned int execute (function *) { return tsan_pass (); }
818 }; // class pass_tsan_O0
820 } // anon namespace
822 gimple_opt_pass *
823 make_pass_tsan_O0 (gcc::context *ctxt)
825 return new pass_tsan_O0 (ctxt);