2014-03-18 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / tsan.c
blob2e32cd2b32f7bb46fd79472e1b4f228607146a17
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 "basic-block.h"
30 #include "tree-ssa-alias.h"
31 #include "internal-fn.h"
32 #include "gimple-expr.h"
33 #include "is-a.h"
34 #include "gimple.h"
35 #include "gimplify.h"
36 #include "gimple-iterator.h"
37 #include "function.h"
38 #include "gimple-ssa.h"
39 #include "cgraph.h"
40 #include "tree-cfg.h"
41 #include "stringpool.h"
42 #include "tree-ssanames.h"
43 #include "tree-pass.h"
44 #include "tree-iterator.h"
45 #include "langhooks.h"
46 #include "output.h"
47 #include "options.h"
48 #include "target.h"
49 #include "diagnostic.h"
50 #include "tree-ssa-propagate.h"
51 #include "tsan.h"
52 #include "asan.h"
54 /* Number of instrumented memory accesses in the current function. */
56 /* Builds the following decl
57 void __tsan_read/writeX (void *addr); */
59 static tree
60 get_memory_access_decl (bool is_write, unsigned size)
62 enum built_in_function fcode;
64 if (size <= 1)
65 fcode = is_write ? BUILT_IN_TSAN_WRITE1
66 : BUILT_IN_TSAN_READ1;
67 else if (size <= 3)
68 fcode = is_write ? BUILT_IN_TSAN_WRITE2
69 : BUILT_IN_TSAN_READ2;
70 else if (size <= 7)
71 fcode = is_write ? BUILT_IN_TSAN_WRITE4
72 : BUILT_IN_TSAN_READ4;
73 else if (size <= 15)
74 fcode = is_write ? BUILT_IN_TSAN_WRITE8
75 : BUILT_IN_TSAN_READ8;
76 else
77 fcode = is_write ? BUILT_IN_TSAN_WRITE16
78 : BUILT_IN_TSAN_READ16;
80 return builtin_decl_implicit (fcode);
83 /* Check as to whether EXPR refers to a store to vptr. */
85 static tree
86 is_vptr_store (gimple stmt, tree expr, bool is_write)
88 if (is_write == true
89 && gimple_assign_single_p (stmt)
90 && TREE_CODE (expr) == COMPONENT_REF)
92 tree field = TREE_OPERAND (expr, 1);
93 if (TREE_CODE (field) == FIELD_DECL
94 && DECL_VIRTUAL_P (field))
95 return gimple_assign_rhs1 (stmt);
97 return NULL;
100 /* Instruments EXPR if needed. If any instrumentation is inserted,
101 return true. */
103 static bool
104 instrument_expr (gimple_stmt_iterator gsi, tree expr, bool is_write)
106 tree base, rhs, expr_ptr, builtin_decl;
107 basic_block bb;
108 HOST_WIDE_INT size;
109 gimple stmt, g;
110 gimple_seq seq;
111 location_t loc;
113 size = int_size_in_bytes (TREE_TYPE (expr));
114 if (size == -1)
115 return false;
117 /* For now just avoid instrumenting bit field acceses.
118 TODO: handle bit-fields as if touching the whole field. */
119 HOST_WIDE_INT bitsize, bitpos;
120 tree offset;
121 enum machine_mode mode;
122 int volatilep = 0, unsignedp = 0;
123 base = get_inner_reference (expr, &bitsize, &bitpos, &offset,
124 &mode, &unsignedp, &volatilep, false);
126 /* No need to instrument accesses to decls that don't escape,
127 they can't escape to other threads then. */
128 if (DECL_P (base))
130 struct pt_solution pt;
131 memset (&pt, 0, sizeof (pt));
132 pt.escaped = 1;
133 pt.ipa_escaped = flag_ipa_pta != 0;
134 pt.nonlocal = 1;
135 if (!pt_solution_includes (&pt, base))
136 return false;
137 if (!is_global_var (base) && !may_be_aliased (base))
138 return false;
141 if (TREE_READONLY (base)
142 || (TREE_CODE (base) == VAR_DECL
143 && DECL_HARD_REGISTER (base)))
144 return false;
146 if (size == 0
147 || bitpos % (size * BITS_PER_UNIT)
148 || bitsize != size * BITS_PER_UNIT)
149 return false;
151 stmt = gsi_stmt (gsi);
152 loc = gimple_location (stmt);
153 rhs = is_vptr_store (stmt, expr, is_write);
154 gcc_checking_assert (rhs != NULL || is_gimple_addressable (expr));
155 expr_ptr = build_fold_addr_expr (unshare_expr (expr));
156 seq = NULL;
157 if (!is_gimple_val (expr_ptr))
159 g = gimple_build_assign (make_ssa_name (TREE_TYPE (expr_ptr), NULL),
160 expr_ptr);
161 expr_ptr = gimple_assign_lhs (g);
162 gimple_set_location (g, loc);
163 gimple_seq_add_stmt_without_update (&seq, g);
165 if (rhs == NULL)
166 g = gimple_build_call (get_memory_access_decl (is_write, size),
167 1, expr_ptr);
168 else
170 builtin_decl = builtin_decl_implicit (BUILT_IN_TSAN_VPTR_UPDATE);
171 g = gimple_build_call (builtin_decl, 1, expr_ptr);
173 gimple_set_location (g, loc);
174 gimple_seq_add_stmt_without_update (&seq, g);
175 /* Instrumentation for assignment of a function result
176 must be inserted after the call. Instrumentation for
177 reads of function arguments must be inserted before the call.
178 That's because the call can contain synchronization. */
179 if (is_gimple_call (stmt) && is_write)
181 /* If the call can throw, it must be the last stmt in
182 a basic block, so the instrumented stmts need to be
183 inserted in successor bbs. */
184 if (is_ctrl_altering_stmt (stmt))
186 edge e;
188 bb = gsi_bb (gsi);
189 e = find_fallthru_edge (bb->succs);
190 if (e)
191 gsi_insert_seq_on_edge_immediate (e, seq);
193 else
194 gsi_insert_seq_after (&gsi, seq, GSI_NEW_STMT);
196 else
197 gsi_insert_seq_before (&gsi, seq, GSI_SAME_STMT);
199 return true;
202 /* Actions for sync/atomic builtin transformations. */
203 enum tsan_atomic_action
205 check_last, add_seq_cst, add_acquire, weak_cas, strong_cas,
206 bool_cas, val_cas, lock_release, fetch_op, fetch_op_seq_cst
209 /* Table how to map sync/atomic builtins to their corresponding
210 tsan equivalents. */
211 static const struct tsan_map_atomic
213 enum built_in_function fcode, tsan_fcode;
214 enum tsan_atomic_action action;
215 enum tree_code code;
216 } tsan_atomic_table[] =
218 #define TRANSFORM(fcode, tsan_fcode, action, code) \
219 { BUILT_IN_##fcode, BUILT_IN_##tsan_fcode, action, code }
220 #define CHECK_LAST(fcode, tsan_fcode) \
221 TRANSFORM (fcode, tsan_fcode, check_last, ERROR_MARK)
222 #define ADD_SEQ_CST(fcode, tsan_fcode) \
223 TRANSFORM (fcode, tsan_fcode, add_seq_cst, ERROR_MARK)
224 #define ADD_ACQUIRE(fcode, tsan_fcode) \
225 TRANSFORM (fcode, tsan_fcode, add_acquire, ERROR_MARK)
226 #define WEAK_CAS(fcode, tsan_fcode) \
227 TRANSFORM (fcode, tsan_fcode, weak_cas, ERROR_MARK)
228 #define STRONG_CAS(fcode, tsan_fcode) \
229 TRANSFORM (fcode, tsan_fcode, strong_cas, ERROR_MARK)
230 #define BOOL_CAS(fcode, tsan_fcode) \
231 TRANSFORM (fcode, tsan_fcode, bool_cas, ERROR_MARK)
232 #define VAL_CAS(fcode, tsan_fcode) \
233 TRANSFORM (fcode, tsan_fcode, val_cas, ERROR_MARK)
234 #define LOCK_RELEASE(fcode, tsan_fcode) \
235 TRANSFORM (fcode, tsan_fcode, lock_release, ERROR_MARK)
236 #define FETCH_OP(fcode, tsan_fcode, code) \
237 TRANSFORM (fcode, tsan_fcode, fetch_op, code)
238 #define FETCH_OPS(fcode, tsan_fcode, code) \
239 TRANSFORM (fcode, tsan_fcode, fetch_op_seq_cst, code)
241 CHECK_LAST (ATOMIC_LOAD_1, TSAN_ATOMIC8_LOAD),
242 CHECK_LAST (ATOMIC_LOAD_2, TSAN_ATOMIC16_LOAD),
243 CHECK_LAST (ATOMIC_LOAD_4, TSAN_ATOMIC32_LOAD),
244 CHECK_LAST (ATOMIC_LOAD_8, TSAN_ATOMIC64_LOAD),
245 CHECK_LAST (ATOMIC_LOAD_16, TSAN_ATOMIC128_LOAD),
246 CHECK_LAST (ATOMIC_STORE_1, TSAN_ATOMIC8_STORE),
247 CHECK_LAST (ATOMIC_STORE_2, TSAN_ATOMIC16_STORE),
248 CHECK_LAST (ATOMIC_STORE_4, TSAN_ATOMIC32_STORE),
249 CHECK_LAST (ATOMIC_STORE_8, TSAN_ATOMIC64_STORE),
250 CHECK_LAST (ATOMIC_STORE_16, TSAN_ATOMIC128_STORE),
251 CHECK_LAST (ATOMIC_EXCHANGE_1, TSAN_ATOMIC8_EXCHANGE),
252 CHECK_LAST (ATOMIC_EXCHANGE_2, TSAN_ATOMIC16_EXCHANGE),
253 CHECK_LAST (ATOMIC_EXCHANGE_4, TSAN_ATOMIC32_EXCHANGE),
254 CHECK_LAST (ATOMIC_EXCHANGE_8, TSAN_ATOMIC64_EXCHANGE),
255 CHECK_LAST (ATOMIC_EXCHANGE_16, TSAN_ATOMIC128_EXCHANGE),
256 CHECK_LAST (ATOMIC_FETCH_ADD_1, TSAN_ATOMIC8_FETCH_ADD),
257 CHECK_LAST (ATOMIC_FETCH_ADD_2, TSAN_ATOMIC16_FETCH_ADD),
258 CHECK_LAST (ATOMIC_FETCH_ADD_4, TSAN_ATOMIC32_FETCH_ADD),
259 CHECK_LAST (ATOMIC_FETCH_ADD_8, TSAN_ATOMIC64_FETCH_ADD),
260 CHECK_LAST (ATOMIC_FETCH_ADD_16, TSAN_ATOMIC128_FETCH_ADD),
261 CHECK_LAST (ATOMIC_FETCH_SUB_1, TSAN_ATOMIC8_FETCH_SUB),
262 CHECK_LAST (ATOMIC_FETCH_SUB_2, TSAN_ATOMIC16_FETCH_SUB),
263 CHECK_LAST (ATOMIC_FETCH_SUB_4, TSAN_ATOMIC32_FETCH_SUB),
264 CHECK_LAST (ATOMIC_FETCH_SUB_8, TSAN_ATOMIC64_FETCH_SUB),
265 CHECK_LAST (ATOMIC_FETCH_SUB_16, TSAN_ATOMIC128_FETCH_SUB),
266 CHECK_LAST (ATOMIC_FETCH_AND_1, TSAN_ATOMIC8_FETCH_AND),
267 CHECK_LAST (ATOMIC_FETCH_AND_2, TSAN_ATOMIC16_FETCH_AND),
268 CHECK_LAST (ATOMIC_FETCH_AND_4, TSAN_ATOMIC32_FETCH_AND),
269 CHECK_LAST (ATOMIC_FETCH_AND_8, TSAN_ATOMIC64_FETCH_AND),
270 CHECK_LAST (ATOMIC_FETCH_AND_16, TSAN_ATOMIC128_FETCH_AND),
271 CHECK_LAST (ATOMIC_FETCH_OR_1, TSAN_ATOMIC8_FETCH_OR),
272 CHECK_LAST (ATOMIC_FETCH_OR_2, TSAN_ATOMIC16_FETCH_OR),
273 CHECK_LAST (ATOMIC_FETCH_OR_4, TSAN_ATOMIC32_FETCH_OR),
274 CHECK_LAST (ATOMIC_FETCH_OR_8, TSAN_ATOMIC64_FETCH_OR),
275 CHECK_LAST (ATOMIC_FETCH_OR_16, TSAN_ATOMIC128_FETCH_OR),
276 CHECK_LAST (ATOMIC_FETCH_XOR_1, TSAN_ATOMIC8_FETCH_XOR),
277 CHECK_LAST (ATOMIC_FETCH_XOR_2, TSAN_ATOMIC16_FETCH_XOR),
278 CHECK_LAST (ATOMIC_FETCH_XOR_4, TSAN_ATOMIC32_FETCH_XOR),
279 CHECK_LAST (ATOMIC_FETCH_XOR_8, TSAN_ATOMIC64_FETCH_XOR),
280 CHECK_LAST (ATOMIC_FETCH_XOR_16, TSAN_ATOMIC128_FETCH_XOR),
281 CHECK_LAST (ATOMIC_FETCH_NAND_1, TSAN_ATOMIC8_FETCH_NAND),
282 CHECK_LAST (ATOMIC_FETCH_NAND_2, TSAN_ATOMIC16_FETCH_NAND),
283 CHECK_LAST (ATOMIC_FETCH_NAND_4, TSAN_ATOMIC32_FETCH_NAND),
284 CHECK_LAST (ATOMIC_FETCH_NAND_8, TSAN_ATOMIC64_FETCH_NAND),
285 CHECK_LAST (ATOMIC_FETCH_NAND_16, TSAN_ATOMIC128_FETCH_NAND),
287 CHECK_LAST (ATOMIC_THREAD_FENCE, TSAN_ATOMIC_THREAD_FENCE),
288 CHECK_LAST (ATOMIC_SIGNAL_FENCE, TSAN_ATOMIC_SIGNAL_FENCE),
290 FETCH_OP (ATOMIC_ADD_FETCH_1, TSAN_ATOMIC8_FETCH_ADD, PLUS_EXPR),
291 FETCH_OP (ATOMIC_ADD_FETCH_2, TSAN_ATOMIC16_FETCH_ADD, PLUS_EXPR),
292 FETCH_OP (ATOMIC_ADD_FETCH_4, TSAN_ATOMIC32_FETCH_ADD, PLUS_EXPR),
293 FETCH_OP (ATOMIC_ADD_FETCH_8, TSAN_ATOMIC64_FETCH_ADD, PLUS_EXPR),
294 FETCH_OP (ATOMIC_ADD_FETCH_16, TSAN_ATOMIC128_FETCH_ADD, PLUS_EXPR),
295 FETCH_OP (ATOMIC_SUB_FETCH_1, TSAN_ATOMIC8_FETCH_SUB, MINUS_EXPR),
296 FETCH_OP (ATOMIC_SUB_FETCH_2, TSAN_ATOMIC16_FETCH_SUB, MINUS_EXPR),
297 FETCH_OP (ATOMIC_SUB_FETCH_4, TSAN_ATOMIC32_FETCH_SUB, MINUS_EXPR),
298 FETCH_OP (ATOMIC_SUB_FETCH_8, TSAN_ATOMIC64_FETCH_SUB, MINUS_EXPR),
299 FETCH_OP (ATOMIC_SUB_FETCH_16, TSAN_ATOMIC128_FETCH_SUB, MINUS_EXPR),
300 FETCH_OP (ATOMIC_AND_FETCH_1, TSAN_ATOMIC8_FETCH_AND, BIT_AND_EXPR),
301 FETCH_OP (ATOMIC_AND_FETCH_2, TSAN_ATOMIC16_FETCH_AND, BIT_AND_EXPR),
302 FETCH_OP (ATOMIC_AND_FETCH_4, TSAN_ATOMIC32_FETCH_AND, BIT_AND_EXPR),
303 FETCH_OP (ATOMIC_AND_FETCH_8, TSAN_ATOMIC64_FETCH_AND, BIT_AND_EXPR),
304 FETCH_OP (ATOMIC_AND_FETCH_16, TSAN_ATOMIC128_FETCH_AND, BIT_AND_EXPR),
305 FETCH_OP (ATOMIC_OR_FETCH_1, TSAN_ATOMIC8_FETCH_OR, BIT_IOR_EXPR),
306 FETCH_OP (ATOMIC_OR_FETCH_2, TSAN_ATOMIC16_FETCH_OR, BIT_IOR_EXPR),
307 FETCH_OP (ATOMIC_OR_FETCH_4, TSAN_ATOMIC32_FETCH_OR, BIT_IOR_EXPR),
308 FETCH_OP (ATOMIC_OR_FETCH_8, TSAN_ATOMIC64_FETCH_OR, BIT_IOR_EXPR),
309 FETCH_OP (ATOMIC_OR_FETCH_16, TSAN_ATOMIC128_FETCH_OR, BIT_IOR_EXPR),
310 FETCH_OP (ATOMIC_XOR_FETCH_1, TSAN_ATOMIC8_FETCH_XOR, BIT_XOR_EXPR),
311 FETCH_OP (ATOMIC_XOR_FETCH_2, TSAN_ATOMIC16_FETCH_XOR, BIT_XOR_EXPR),
312 FETCH_OP (ATOMIC_XOR_FETCH_4, TSAN_ATOMIC32_FETCH_XOR, BIT_XOR_EXPR),
313 FETCH_OP (ATOMIC_XOR_FETCH_8, TSAN_ATOMIC64_FETCH_XOR, BIT_XOR_EXPR),
314 FETCH_OP (ATOMIC_XOR_FETCH_16, TSAN_ATOMIC128_FETCH_XOR, BIT_XOR_EXPR),
315 FETCH_OP (ATOMIC_NAND_FETCH_1, TSAN_ATOMIC8_FETCH_NAND, BIT_NOT_EXPR),
316 FETCH_OP (ATOMIC_NAND_FETCH_2, TSAN_ATOMIC16_FETCH_NAND, BIT_NOT_EXPR),
317 FETCH_OP (ATOMIC_NAND_FETCH_4, TSAN_ATOMIC32_FETCH_NAND, BIT_NOT_EXPR),
318 FETCH_OP (ATOMIC_NAND_FETCH_8, TSAN_ATOMIC64_FETCH_NAND, BIT_NOT_EXPR),
319 FETCH_OP (ATOMIC_NAND_FETCH_16, TSAN_ATOMIC128_FETCH_NAND, BIT_NOT_EXPR),
321 ADD_ACQUIRE (SYNC_LOCK_TEST_AND_SET_1, TSAN_ATOMIC8_EXCHANGE),
322 ADD_ACQUIRE (SYNC_LOCK_TEST_AND_SET_2, TSAN_ATOMIC16_EXCHANGE),
323 ADD_ACQUIRE (SYNC_LOCK_TEST_AND_SET_4, TSAN_ATOMIC32_EXCHANGE),
324 ADD_ACQUIRE (SYNC_LOCK_TEST_AND_SET_8, TSAN_ATOMIC64_EXCHANGE),
325 ADD_ACQUIRE (SYNC_LOCK_TEST_AND_SET_16, TSAN_ATOMIC128_EXCHANGE),
327 ADD_SEQ_CST (SYNC_FETCH_AND_ADD_1, TSAN_ATOMIC8_FETCH_ADD),
328 ADD_SEQ_CST (SYNC_FETCH_AND_ADD_2, TSAN_ATOMIC16_FETCH_ADD),
329 ADD_SEQ_CST (SYNC_FETCH_AND_ADD_4, TSAN_ATOMIC32_FETCH_ADD),
330 ADD_SEQ_CST (SYNC_FETCH_AND_ADD_8, TSAN_ATOMIC64_FETCH_ADD),
331 ADD_SEQ_CST (SYNC_FETCH_AND_ADD_16, TSAN_ATOMIC128_FETCH_ADD),
332 ADD_SEQ_CST (SYNC_FETCH_AND_SUB_1, TSAN_ATOMIC8_FETCH_SUB),
333 ADD_SEQ_CST (SYNC_FETCH_AND_SUB_2, TSAN_ATOMIC16_FETCH_SUB),
334 ADD_SEQ_CST (SYNC_FETCH_AND_SUB_4, TSAN_ATOMIC32_FETCH_SUB),
335 ADD_SEQ_CST (SYNC_FETCH_AND_SUB_8, TSAN_ATOMIC64_FETCH_SUB),
336 ADD_SEQ_CST (SYNC_FETCH_AND_SUB_16, TSAN_ATOMIC128_FETCH_SUB),
337 ADD_SEQ_CST (SYNC_FETCH_AND_AND_1, TSAN_ATOMIC8_FETCH_AND),
338 ADD_SEQ_CST (SYNC_FETCH_AND_AND_2, TSAN_ATOMIC16_FETCH_AND),
339 ADD_SEQ_CST (SYNC_FETCH_AND_AND_4, TSAN_ATOMIC32_FETCH_AND),
340 ADD_SEQ_CST (SYNC_FETCH_AND_AND_8, TSAN_ATOMIC64_FETCH_AND),
341 ADD_SEQ_CST (SYNC_FETCH_AND_AND_16, TSAN_ATOMIC128_FETCH_AND),
342 ADD_SEQ_CST (SYNC_FETCH_AND_OR_1, TSAN_ATOMIC8_FETCH_OR),
343 ADD_SEQ_CST (SYNC_FETCH_AND_OR_2, TSAN_ATOMIC16_FETCH_OR),
344 ADD_SEQ_CST (SYNC_FETCH_AND_OR_4, TSAN_ATOMIC32_FETCH_OR),
345 ADD_SEQ_CST (SYNC_FETCH_AND_OR_8, TSAN_ATOMIC64_FETCH_OR),
346 ADD_SEQ_CST (SYNC_FETCH_AND_OR_16, TSAN_ATOMIC128_FETCH_OR),
347 ADD_SEQ_CST (SYNC_FETCH_AND_XOR_1, TSAN_ATOMIC8_FETCH_XOR),
348 ADD_SEQ_CST (SYNC_FETCH_AND_XOR_2, TSAN_ATOMIC16_FETCH_XOR),
349 ADD_SEQ_CST (SYNC_FETCH_AND_XOR_4, TSAN_ATOMIC32_FETCH_XOR),
350 ADD_SEQ_CST (SYNC_FETCH_AND_XOR_8, TSAN_ATOMIC64_FETCH_XOR),
351 ADD_SEQ_CST (SYNC_FETCH_AND_XOR_16, TSAN_ATOMIC128_FETCH_XOR),
352 ADD_SEQ_CST (SYNC_FETCH_AND_NAND_1, TSAN_ATOMIC8_FETCH_NAND),
353 ADD_SEQ_CST (SYNC_FETCH_AND_NAND_2, TSAN_ATOMIC16_FETCH_NAND),
354 ADD_SEQ_CST (SYNC_FETCH_AND_NAND_4, TSAN_ATOMIC32_FETCH_NAND),
355 ADD_SEQ_CST (SYNC_FETCH_AND_NAND_8, TSAN_ATOMIC64_FETCH_NAND),
356 ADD_SEQ_CST (SYNC_FETCH_AND_NAND_16, TSAN_ATOMIC128_FETCH_NAND),
358 ADD_SEQ_CST (SYNC_SYNCHRONIZE, TSAN_ATOMIC_THREAD_FENCE),
360 FETCH_OPS (SYNC_ADD_AND_FETCH_1, TSAN_ATOMIC8_FETCH_ADD, PLUS_EXPR),
361 FETCH_OPS (SYNC_ADD_AND_FETCH_2, TSAN_ATOMIC16_FETCH_ADD, PLUS_EXPR),
362 FETCH_OPS (SYNC_ADD_AND_FETCH_4, TSAN_ATOMIC32_FETCH_ADD, PLUS_EXPR),
363 FETCH_OPS (SYNC_ADD_AND_FETCH_8, TSAN_ATOMIC64_FETCH_ADD, PLUS_EXPR),
364 FETCH_OPS (SYNC_ADD_AND_FETCH_16, TSAN_ATOMIC128_FETCH_ADD, PLUS_EXPR),
365 FETCH_OPS (SYNC_SUB_AND_FETCH_1, TSAN_ATOMIC8_FETCH_SUB, MINUS_EXPR),
366 FETCH_OPS (SYNC_SUB_AND_FETCH_2, TSAN_ATOMIC16_FETCH_SUB, MINUS_EXPR),
367 FETCH_OPS (SYNC_SUB_AND_FETCH_4, TSAN_ATOMIC32_FETCH_SUB, MINUS_EXPR),
368 FETCH_OPS (SYNC_SUB_AND_FETCH_8, TSAN_ATOMIC64_FETCH_SUB, MINUS_EXPR),
369 FETCH_OPS (SYNC_SUB_AND_FETCH_16, TSAN_ATOMIC128_FETCH_SUB, MINUS_EXPR),
370 FETCH_OPS (SYNC_AND_AND_FETCH_1, TSAN_ATOMIC8_FETCH_AND, BIT_AND_EXPR),
371 FETCH_OPS (SYNC_AND_AND_FETCH_2, TSAN_ATOMIC16_FETCH_AND, BIT_AND_EXPR),
372 FETCH_OPS (SYNC_AND_AND_FETCH_4, TSAN_ATOMIC32_FETCH_AND, BIT_AND_EXPR),
373 FETCH_OPS (SYNC_AND_AND_FETCH_8, TSAN_ATOMIC64_FETCH_AND, BIT_AND_EXPR),
374 FETCH_OPS (SYNC_AND_AND_FETCH_16, TSAN_ATOMIC128_FETCH_AND, BIT_AND_EXPR),
375 FETCH_OPS (SYNC_OR_AND_FETCH_1, TSAN_ATOMIC8_FETCH_OR, BIT_IOR_EXPR),
376 FETCH_OPS (SYNC_OR_AND_FETCH_2, TSAN_ATOMIC16_FETCH_OR, BIT_IOR_EXPR),
377 FETCH_OPS (SYNC_OR_AND_FETCH_4, TSAN_ATOMIC32_FETCH_OR, BIT_IOR_EXPR),
378 FETCH_OPS (SYNC_OR_AND_FETCH_8, TSAN_ATOMIC64_FETCH_OR, BIT_IOR_EXPR),
379 FETCH_OPS (SYNC_OR_AND_FETCH_16, TSAN_ATOMIC128_FETCH_OR, BIT_IOR_EXPR),
380 FETCH_OPS (SYNC_XOR_AND_FETCH_1, TSAN_ATOMIC8_FETCH_XOR, BIT_XOR_EXPR),
381 FETCH_OPS (SYNC_XOR_AND_FETCH_2, TSAN_ATOMIC16_FETCH_XOR, BIT_XOR_EXPR),
382 FETCH_OPS (SYNC_XOR_AND_FETCH_4, TSAN_ATOMIC32_FETCH_XOR, BIT_XOR_EXPR),
383 FETCH_OPS (SYNC_XOR_AND_FETCH_8, TSAN_ATOMIC64_FETCH_XOR, BIT_XOR_EXPR),
384 FETCH_OPS (SYNC_XOR_AND_FETCH_16, TSAN_ATOMIC128_FETCH_XOR, BIT_XOR_EXPR),
385 FETCH_OPS (SYNC_NAND_AND_FETCH_1, TSAN_ATOMIC8_FETCH_NAND, BIT_NOT_EXPR),
386 FETCH_OPS (SYNC_NAND_AND_FETCH_2, TSAN_ATOMIC16_FETCH_NAND, BIT_NOT_EXPR),
387 FETCH_OPS (SYNC_NAND_AND_FETCH_4, TSAN_ATOMIC32_FETCH_NAND, BIT_NOT_EXPR),
388 FETCH_OPS (SYNC_NAND_AND_FETCH_8, TSAN_ATOMIC64_FETCH_NAND, BIT_NOT_EXPR),
389 FETCH_OPS (SYNC_NAND_AND_FETCH_16, TSAN_ATOMIC128_FETCH_NAND, BIT_NOT_EXPR),
391 WEAK_CAS (ATOMIC_COMPARE_EXCHANGE_1, TSAN_ATOMIC8_COMPARE_EXCHANGE_WEAK),
392 WEAK_CAS (ATOMIC_COMPARE_EXCHANGE_2, TSAN_ATOMIC16_COMPARE_EXCHANGE_WEAK),
393 WEAK_CAS (ATOMIC_COMPARE_EXCHANGE_4, TSAN_ATOMIC32_COMPARE_EXCHANGE_WEAK),
394 WEAK_CAS (ATOMIC_COMPARE_EXCHANGE_8, TSAN_ATOMIC64_COMPARE_EXCHANGE_WEAK),
395 WEAK_CAS (ATOMIC_COMPARE_EXCHANGE_16, TSAN_ATOMIC128_COMPARE_EXCHANGE_WEAK),
397 STRONG_CAS (ATOMIC_COMPARE_EXCHANGE_1, TSAN_ATOMIC8_COMPARE_EXCHANGE_STRONG),
398 STRONG_CAS (ATOMIC_COMPARE_EXCHANGE_2,
399 TSAN_ATOMIC16_COMPARE_EXCHANGE_STRONG),
400 STRONG_CAS (ATOMIC_COMPARE_EXCHANGE_4,
401 TSAN_ATOMIC32_COMPARE_EXCHANGE_STRONG),
402 STRONG_CAS (ATOMIC_COMPARE_EXCHANGE_8,
403 TSAN_ATOMIC64_COMPARE_EXCHANGE_STRONG),
404 STRONG_CAS (ATOMIC_COMPARE_EXCHANGE_16,
405 TSAN_ATOMIC128_COMPARE_EXCHANGE_STRONG),
407 BOOL_CAS (SYNC_BOOL_COMPARE_AND_SWAP_1,
408 TSAN_ATOMIC8_COMPARE_EXCHANGE_STRONG),
409 BOOL_CAS (SYNC_BOOL_COMPARE_AND_SWAP_2,
410 TSAN_ATOMIC16_COMPARE_EXCHANGE_STRONG),
411 BOOL_CAS (SYNC_BOOL_COMPARE_AND_SWAP_4,
412 TSAN_ATOMIC32_COMPARE_EXCHANGE_STRONG),
413 BOOL_CAS (SYNC_BOOL_COMPARE_AND_SWAP_8,
414 TSAN_ATOMIC64_COMPARE_EXCHANGE_STRONG),
415 BOOL_CAS (SYNC_BOOL_COMPARE_AND_SWAP_16,
416 TSAN_ATOMIC128_COMPARE_EXCHANGE_STRONG),
418 VAL_CAS (SYNC_VAL_COMPARE_AND_SWAP_1, TSAN_ATOMIC8_COMPARE_EXCHANGE_STRONG),
419 VAL_CAS (SYNC_VAL_COMPARE_AND_SWAP_2, TSAN_ATOMIC16_COMPARE_EXCHANGE_STRONG),
420 VAL_CAS (SYNC_VAL_COMPARE_AND_SWAP_4, TSAN_ATOMIC32_COMPARE_EXCHANGE_STRONG),
421 VAL_CAS (SYNC_VAL_COMPARE_AND_SWAP_8, TSAN_ATOMIC64_COMPARE_EXCHANGE_STRONG),
422 VAL_CAS (SYNC_VAL_COMPARE_AND_SWAP_16,
423 TSAN_ATOMIC128_COMPARE_EXCHANGE_STRONG),
425 LOCK_RELEASE (SYNC_LOCK_RELEASE_1, TSAN_ATOMIC8_STORE),
426 LOCK_RELEASE (SYNC_LOCK_RELEASE_2, TSAN_ATOMIC16_STORE),
427 LOCK_RELEASE (SYNC_LOCK_RELEASE_4, TSAN_ATOMIC32_STORE),
428 LOCK_RELEASE (SYNC_LOCK_RELEASE_8, TSAN_ATOMIC64_STORE),
429 LOCK_RELEASE (SYNC_LOCK_RELEASE_16, TSAN_ATOMIC128_STORE)
432 /* Instrument an atomic builtin. */
434 static void
435 instrument_builtin_call (gimple_stmt_iterator *gsi)
437 gimple stmt = gsi_stmt (*gsi), g;
438 tree callee = gimple_call_fndecl (stmt), last_arg, args[6], t, lhs;
439 enum built_in_function fcode = DECL_FUNCTION_CODE (callee);
440 unsigned int i, num = gimple_call_num_args (stmt), j;
441 for (j = 0; j < 6 && j < num; j++)
442 args[j] = gimple_call_arg (stmt, j);
443 for (i = 0; i < ARRAY_SIZE (tsan_atomic_table); i++)
444 if (fcode != tsan_atomic_table[i].fcode)
445 continue;
446 else
448 tree decl = builtin_decl_implicit (tsan_atomic_table[i].tsan_fcode);
449 if (decl == NULL_TREE)
450 return;
451 switch (tsan_atomic_table[i].action)
453 case check_last:
454 case fetch_op:
455 last_arg = gimple_call_arg (stmt, num - 1);
456 if (!tree_fits_uhwi_p (last_arg)
457 || tree_to_uhwi (last_arg) > MEMMODEL_SEQ_CST)
458 return;
459 gimple_call_set_fndecl (stmt, decl);
460 update_stmt (stmt);
461 if (tsan_atomic_table[i].action == fetch_op)
463 args[1] = gimple_call_arg (stmt, 1);
464 goto adjust_result;
466 return;
467 case add_seq_cst:
468 case add_acquire:
469 case fetch_op_seq_cst:
470 gcc_assert (num <= 2);
471 for (j = 0; j < num; j++)
472 args[j] = gimple_call_arg (stmt, j);
473 for (; j < 2; j++)
474 args[j] = NULL_TREE;
475 args[num] = build_int_cst (NULL_TREE,
476 tsan_atomic_table[i].action
477 != add_acquire
478 ? MEMMODEL_SEQ_CST
479 : MEMMODEL_ACQUIRE);
480 update_gimple_call (gsi, decl, num + 1, args[0], args[1], args[2]);
481 stmt = gsi_stmt (*gsi);
482 if (tsan_atomic_table[i].action == fetch_op_seq_cst)
484 adjust_result:
485 lhs = gimple_call_lhs (stmt);
486 if (lhs == NULL_TREE)
487 return;
488 if (!useless_type_conversion_p (TREE_TYPE (lhs),
489 TREE_TYPE (args[1])))
491 tree var = make_ssa_name (TREE_TYPE (lhs), NULL);
492 g = gimple_build_assign_with_ops (NOP_EXPR, var,
493 args[1], NULL_TREE);
494 gsi_insert_after (gsi, g, GSI_NEW_STMT);
495 args[1] = var;
497 gimple_call_set_lhs (stmt,
498 make_ssa_name (TREE_TYPE (lhs), NULL));
499 /* BIT_NOT_EXPR stands for NAND. */
500 if (tsan_atomic_table[i].code == BIT_NOT_EXPR)
502 tree var = make_ssa_name (TREE_TYPE (lhs), NULL);
503 g = gimple_build_assign_with_ops (BIT_AND_EXPR, var,
504 gimple_call_lhs (stmt),
505 args[1]);
506 gsi_insert_after (gsi, g, GSI_NEW_STMT);
507 g = gimple_build_assign_with_ops (BIT_NOT_EXPR, lhs, var,
508 NULL_TREE);
510 else
511 g = gimple_build_assign_with_ops (tsan_atomic_table[i].code,
512 lhs,
513 gimple_call_lhs (stmt),
514 args[1]);
515 update_stmt (stmt);
516 gsi_insert_after (gsi, g, GSI_NEW_STMT);
518 return;
519 case weak_cas:
520 if (!integer_nonzerop (gimple_call_arg (stmt, 3)))
521 continue;
522 /* FALLTHRU */
523 case strong_cas:
524 gcc_assert (num == 6);
525 for (j = 0; j < 6; j++)
526 args[j] = gimple_call_arg (stmt, j);
527 if (!tree_fits_uhwi_p (args[4])
528 || tree_to_uhwi (args[4]) > MEMMODEL_SEQ_CST)
529 return;
530 if (!tree_fits_uhwi_p (args[5])
531 || tree_to_uhwi (args[5]) > MEMMODEL_SEQ_CST)
532 return;
533 update_gimple_call (gsi, decl, 5, args[0], args[1], args[2],
534 args[4], args[5]);
535 return;
536 case bool_cas:
537 case val_cas:
538 gcc_assert (num == 3);
539 for (j = 0; j < 3; j++)
540 args[j] = gimple_call_arg (stmt, j);
541 t = TYPE_ARG_TYPES (TREE_TYPE (decl));
542 t = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (t)));
543 t = create_tmp_var (t, NULL);
544 mark_addressable (t);
545 if (!useless_type_conversion_p (TREE_TYPE (t),
546 TREE_TYPE (args[1])))
548 g = gimple_build_assign_with_ops (NOP_EXPR,
549 make_ssa_name (TREE_TYPE (t),
550 NULL),
551 args[1], NULL_TREE);
552 gsi_insert_before (gsi, g, GSI_SAME_STMT);
553 args[1] = gimple_assign_lhs (g);
555 g = gimple_build_assign (t, args[1]);
556 gsi_insert_before (gsi, g, GSI_SAME_STMT);
557 lhs = gimple_call_lhs (stmt);
558 update_gimple_call (gsi, decl, 5, args[0],
559 build_fold_addr_expr (t), args[2],
560 build_int_cst (NULL_TREE,
561 MEMMODEL_SEQ_CST),
562 build_int_cst (NULL_TREE,
563 MEMMODEL_SEQ_CST));
564 if (tsan_atomic_table[i].action == val_cas && lhs)
566 tree cond;
567 stmt = gsi_stmt (*gsi);
568 g = gimple_build_assign (make_ssa_name (TREE_TYPE (t), NULL),
570 gsi_insert_after (gsi, g, GSI_NEW_STMT);
571 t = make_ssa_name (TREE_TYPE (TREE_TYPE (decl)), stmt);
572 cond = build2 (NE_EXPR, boolean_type_node, t,
573 build_int_cst (TREE_TYPE (t), 0));
574 g = gimple_build_assign_with_ops (COND_EXPR, lhs, cond,
575 args[1],
576 gimple_assign_lhs (g));
577 gimple_call_set_lhs (stmt, t);
578 update_stmt (stmt);
579 gsi_insert_after (gsi, g, GSI_NEW_STMT);
581 return;
582 case lock_release:
583 gcc_assert (num == 1);
584 t = TYPE_ARG_TYPES (TREE_TYPE (decl));
585 t = TREE_VALUE (TREE_CHAIN (t));
586 update_gimple_call (gsi, decl, 3, gimple_call_arg (stmt, 0),
587 build_int_cst (t, 0),
588 build_int_cst (NULL_TREE,
589 MEMMODEL_RELEASE));
590 return;
591 default:
592 continue;
597 /* Instruments the gimple pointed to by GSI. Return
598 true if func entry/exit should be instrumented. */
600 static bool
601 instrument_gimple (gimple_stmt_iterator *gsi)
603 gimple stmt;
604 tree rhs, lhs;
605 bool instrumented = false;
607 stmt = gsi_stmt (*gsi);
608 if (is_gimple_call (stmt)
609 && (gimple_call_fndecl (stmt)
610 != builtin_decl_implicit (BUILT_IN_TSAN_INIT)))
612 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
613 instrument_builtin_call (gsi);
614 return true;
616 else if (is_gimple_assign (stmt)
617 && !gimple_clobber_p (stmt))
619 if (gimple_store_p (stmt))
621 lhs = gimple_assign_lhs (stmt);
622 instrumented = instrument_expr (*gsi, lhs, true);
624 if (gimple_assign_load_p (stmt))
626 rhs = gimple_assign_rhs1 (stmt);
627 instrumented = instrument_expr (*gsi, rhs, false);
630 return instrumented;
633 /* Instruments all interesting memory accesses in the current function.
634 Return true if func entry/exit should be instrumented. */
636 static bool
637 instrument_memory_accesses (void)
639 basic_block bb;
640 gimple_stmt_iterator gsi;
641 bool fentry_exit_instrument = false;
643 FOR_EACH_BB_FN (bb, cfun)
644 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
645 fentry_exit_instrument |= instrument_gimple (&gsi);
646 return fentry_exit_instrument;
649 /* Instruments function entry. */
651 static void
652 instrument_func_entry (void)
654 basic_block succ_bb;
655 gimple_stmt_iterator gsi;
656 tree ret_addr, builtin_decl;
657 gimple g;
659 succ_bb = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
660 gsi = gsi_after_labels (succ_bb);
662 builtin_decl = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
663 g = gimple_build_call (builtin_decl, 1, integer_zero_node);
664 ret_addr = make_ssa_name (ptr_type_node, NULL);
665 gimple_call_set_lhs (g, ret_addr);
666 gimple_set_location (g, cfun->function_start_locus);
667 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
669 builtin_decl = builtin_decl_implicit (BUILT_IN_TSAN_FUNC_ENTRY);
670 g = gimple_build_call (builtin_decl, 1, ret_addr);
671 gimple_set_location (g, cfun->function_start_locus);
672 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
675 /* Instruments function exits. */
677 static void
678 instrument_func_exit (void)
680 location_t loc;
681 basic_block exit_bb;
682 gimple_stmt_iterator gsi;
683 gimple stmt, g;
684 tree builtin_decl;
685 edge e;
686 edge_iterator ei;
688 /* Find all function exits. */
689 exit_bb = EXIT_BLOCK_PTR_FOR_FN (cfun);
690 FOR_EACH_EDGE (e, ei, exit_bb->preds)
692 gsi = gsi_last_bb (e->src);
693 stmt = gsi_stmt (gsi);
694 gcc_assert (gimple_code (stmt) == GIMPLE_RETURN
695 || gimple_call_builtin_p (stmt, BUILT_IN_RETURN));
696 loc = gimple_location (stmt);
697 builtin_decl = builtin_decl_implicit (BUILT_IN_TSAN_FUNC_EXIT);
698 g = gimple_build_call (builtin_decl, 0);
699 gimple_set_location (g, loc);
700 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
704 /* ThreadSanitizer instrumentation pass. */
706 static unsigned
707 tsan_pass (void)
709 initialize_sanitizer_builtins ();
710 if (instrument_memory_accesses ())
712 instrument_func_entry ();
713 instrument_func_exit ();
715 return 0;
718 /* The pass's gate. */
720 static bool
721 tsan_gate (void)
723 return (flag_sanitize & SANITIZE_THREAD) != 0;
726 /* Inserts __tsan_init () into the list of CTORs. */
728 void
729 tsan_finish_file (void)
731 tree ctor_statements = NULL_TREE;
733 initialize_sanitizer_builtins ();
734 tree init_decl = builtin_decl_implicit (BUILT_IN_TSAN_INIT);
735 append_to_statement_list (build_call_expr (init_decl, 0),
736 &ctor_statements);
737 cgraph_build_static_cdtor ('I', ctor_statements,
738 MAX_RESERVED_INIT_PRIORITY - 1);
741 /* The pass descriptor. */
743 namespace {
745 const pass_data pass_data_tsan =
747 GIMPLE_PASS, /* type */
748 "tsan", /* name */
749 OPTGROUP_NONE, /* optinfo_flags */
750 true, /* has_gate */
751 true, /* has_execute */
752 TV_NONE, /* tv_id */
753 ( PROP_ssa | PROP_cfg ), /* properties_required */
754 0, /* properties_provided */
755 0, /* properties_destroyed */
756 0, /* todo_flags_start */
757 ( TODO_verify_all | TODO_update_ssa ), /* todo_flags_finish */
760 class pass_tsan : public gimple_opt_pass
762 public:
763 pass_tsan (gcc::context *ctxt)
764 : gimple_opt_pass (pass_data_tsan, ctxt)
767 /* opt_pass methods: */
768 opt_pass * clone () { return new pass_tsan (m_ctxt); }
769 bool gate () { return tsan_gate (); }
770 unsigned int execute () { return tsan_pass (); }
772 }; // class pass_tsan
774 } // anon namespace
776 gimple_opt_pass *
777 make_pass_tsan (gcc::context *ctxt)
779 return new pass_tsan (ctxt);
782 static bool
783 tsan_gate_O0 (void)
785 return (flag_sanitize & SANITIZE_THREAD) != 0 && !optimize;
788 namespace {
790 const pass_data pass_data_tsan_O0 =
792 GIMPLE_PASS, /* type */
793 "tsan0", /* name */
794 OPTGROUP_NONE, /* optinfo_flags */
795 true, /* has_gate */
796 true, /* has_execute */
797 TV_NONE, /* tv_id */
798 ( PROP_ssa | PROP_cfg ), /* properties_required */
799 0, /* properties_provided */
800 0, /* properties_destroyed */
801 0, /* todo_flags_start */
802 ( TODO_verify_all | TODO_update_ssa ), /* todo_flags_finish */
805 class pass_tsan_O0 : public gimple_opt_pass
807 public:
808 pass_tsan_O0 (gcc::context *ctxt)
809 : gimple_opt_pass (pass_data_tsan_O0, ctxt)
812 /* opt_pass methods: */
813 bool gate () { return tsan_gate_O0 (); }
814 unsigned int execute () { return tsan_pass (); }
816 }; // class pass_tsan_O0
818 } // anon namespace
820 gimple_opt_pass *
821 make_pass_tsan_O0 (gcc::context *ctxt)
823 return new pass_tsan_O0 (ctxt);