blob: 87bd2ecde97be2dea568a77f60c4f975dd88252c [file] [log] [blame]
Austin Schuh745610d2015-09-06 18:19:50 -07001/* Copyright (c) 2008-2009, Google Inc.
2 * All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * ---
31 * Author: Kostya Serebryany
32 */
33
34#ifdef __cplusplus
35# error "This file should be built as pure C to avoid name mangling"
36#endif
37
38#include "config.h"
39#include <stdlib.h>
40#include <string.h>
41
42#include "base/dynamic_annotations.h"
43#include "getenv_safe.h" // for TCMallocGetenvSafe
44
45#ifdef __GNUC__
46/* valgrind.h uses gcc extensions so it won't build with other compilers */
47# ifdef HAVE_VALGRIND_H /* prefer the user's copy if they have it */
48# include <valgrind.h>
49# else /* otherwise just use the copy that we have */
50# include "third_party/valgrind.h"
51# endif
52#endif
53
54/* Compiler-based ThreadSanitizer defines
55 DYNAMIC_ANNOTATIONS_EXTERNAL_IMPL = 1
56 and provides its own definitions of the functions. */
57
58#ifndef DYNAMIC_ANNOTATIONS_EXTERNAL_IMPL
59# define DYNAMIC_ANNOTATIONS_EXTERNAL_IMPL 0
60#endif
61
62/* Each function is empty and called (via a macro) only in debug mode.
63 The arguments are captured by dynamic tools at runtime. */
64
65#if DYNAMIC_ANNOTATIONS_ENABLED == 1 \
66 && DYNAMIC_ANNOTATIONS_EXTERNAL_IMPL == 0
67
68void AnnotateRWLockCreate(const char *file, int line,
69 const volatile void *lock){}
70void AnnotateRWLockDestroy(const char *file, int line,
71 const volatile void *lock){}
72void AnnotateRWLockAcquired(const char *file, int line,
73 const volatile void *lock, long is_w){}
74void AnnotateRWLockReleased(const char *file, int line,
75 const volatile void *lock, long is_w){}
76void AnnotateBarrierInit(const char *file, int line,
77 const volatile void *barrier, long count,
78 long reinitialization_allowed) {}
79void AnnotateBarrierWaitBefore(const char *file, int line,
80 const volatile void *barrier) {}
81void AnnotateBarrierWaitAfter(const char *file, int line,
82 const volatile void *barrier) {}
83void AnnotateBarrierDestroy(const char *file, int line,
84 const volatile void *barrier) {}
85
86void AnnotateCondVarWait(const char *file, int line,
87 const volatile void *cv,
88 const volatile void *lock){}
89void AnnotateCondVarSignal(const char *file, int line,
90 const volatile void *cv){}
91void AnnotateCondVarSignalAll(const char *file, int line,
92 const volatile void *cv){}
93void AnnotatePublishMemoryRange(const char *file, int line,
94 const volatile void *address,
95 long size){}
96void AnnotateUnpublishMemoryRange(const char *file, int line,
97 const volatile void *address,
98 long size){}
99void AnnotatePCQCreate(const char *file, int line,
100 const volatile void *pcq){}
101void AnnotatePCQDestroy(const char *file, int line,
102 const volatile void *pcq){}
103void AnnotatePCQPut(const char *file, int line,
104 const volatile void *pcq){}
105void AnnotatePCQGet(const char *file, int line,
106 const volatile void *pcq){}
107void AnnotateNewMemory(const char *file, int line,
108 const volatile void *mem,
109 long size){}
110void AnnotateExpectRace(const char *file, int line,
111 const volatile void *mem,
112 const char *description){}
113void AnnotateBenignRace(const char *file, int line,
114 const volatile void *mem,
115 const char *description){}
116void AnnotateBenignRaceSized(const char *file, int line,
117 const volatile void *mem,
118 long size,
119 const char *description) {}
120void AnnotateMutexIsUsedAsCondVar(const char *file, int line,
121 const volatile void *mu){}
122void AnnotateTraceMemory(const char *file, int line,
123 const volatile void *arg){}
124void AnnotateThreadName(const char *file, int line,
125 const char *name){}
126void AnnotateIgnoreReadsBegin(const char *file, int line){}
127void AnnotateIgnoreReadsEnd(const char *file, int line){}
128void AnnotateIgnoreWritesBegin(const char *file, int line){}
129void AnnotateIgnoreWritesEnd(const char *file, int line){}
130void AnnotateEnableRaceDetection(const char *file, int line, int enable){}
131void AnnotateNoOp(const char *file, int line,
132 const volatile void *arg){}
133void AnnotateFlushState(const char *file, int line){}
134
135#endif /* DYNAMIC_ANNOTATIONS_ENABLED == 1
136 && DYNAMIC_ANNOTATIONS_EXTERNAL_IMPL == 0 */
137
138#if DYNAMIC_ANNOTATIONS_EXTERNAL_IMPL == 0
139
140static int GetRunningOnValgrind(void) {
141#ifdef RUNNING_ON_VALGRIND
142 if (RUNNING_ON_VALGRIND) return 1;
143#endif
144 const char *running_on_valgrind_str = TCMallocGetenvSafe("RUNNING_ON_VALGRIND");
145 if (running_on_valgrind_str) {
146 return strcmp(running_on_valgrind_str, "0") != 0;
147 }
148 return 0;
149}
150
151/* See the comments in dynamic_annotations.h */
152int RunningOnValgrind(void) {
153 static volatile int running_on_valgrind = -1;
154 int local_running_on_valgrind = running_on_valgrind;
155 /* C doesn't have thread-safe initialization of statics, and we
156 don't want to depend on pthread_once here, so hack it. */
157 ANNOTATE_BENIGN_RACE(&running_on_valgrind, "safe hack");
158 if (local_running_on_valgrind == -1)
159 running_on_valgrind = local_running_on_valgrind = GetRunningOnValgrind();
160 return local_running_on_valgrind;
161}
162
163#endif /* DYNAMIC_ANNOTATIONS_EXTERNAL_IMPL == 0 */
164
165/* See the comments in dynamic_annotations.h */
166double ValgrindSlowdown(void) {
167 /* Same initialization hack as in RunningOnValgrind(). */
168 static volatile double slowdown = 0.0;
169 double local_slowdown = slowdown;
170 ANNOTATE_BENIGN_RACE(&slowdown, "safe hack");
171 if (RunningOnValgrind() == 0) {
172 return 1.0;
173 }
174 if (local_slowdown == 0.0) {
175 char *env = getenv("VALGRIND_SLOWDOWN");
176 slowdown = local_slowdown = env ? atof(env) : 50.0;
177 }
178 return local_slowdown;
179}