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