Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame^] | 1 | /* 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 | |
| 68 | void AnnotateRWLockCreate(const char *file, int line, |
| 69 | const volatile void *lock){} |
| 70 | void AnnotateRWLockDestroy(const char *file, int line, |
| 71 | const volatile void *lock){} |
| 72 | void AnnotateRWLockAcquired(const char *file, int line, |
| 73 | const volatile void *lock, long is_w){} |
| 74 | void AnnotateRWLockReleased(const char *file, int line, |
| 75 | const volatile void *lock, long is_w){} |
| 76 | void AnnotateBarrierInit(const char *file, int line, |
| 77 | const volatile void *barrier, long count, |
| 78 | long reinitialization_allowed) {} |
| 79 | void AnnotateBarrierWaitBefore(const char *file, int line, |
| 80 | const volatile void *barrier) {} |
| 81 | void AnnotateBarrierWaitAfter(const char *file, int line, |
| 82 | const volatile void *barrier) {} |
| 83 | void AnnotateBarrierDestroy(const char *file, int line, |
| 84 | const volatile void *barrier) {} |
| 85 | |
| 86 | void AnnotateCondVarWait(const char *file, int line, |
| 87 | const volatile void *cv, |
| 88 | const volatile void *lock){} |
| 89 | void AnnotateCondVarSignal(const char *file, int line, |
| 90 | const volatile void *cv){} |
| 91 | void AnnotateCondVarSignalAll(const char *file, int line, |
| 92 | const volatile void *cv){} |
| 93 | void AnnotatePublishMemoryRange(const char *file, int line, |
| 94 | const volatile void *address, |
| 95 | long size){} |
| 96 | void AnnotateUnpublishMemoryRange(const char *file, int line, |
| 97 | const volatile void *address, |
| 98 | long size){} |
| 99 | void AnnotatePCQCreate(const char *file, int line, |
| 100 | const volatile void *pcq){} |
| 101 | void AnnotatePCQDestroy(const char *file, int line, |
| 102 | const volatile void *pcq){} |
| 103 | void AnnotatePCQPut(const char *file, int line, |
| 104 | const volatile void *pcq){} |
| 105 | void AnnotatePCQGet(const char *file, int line, |
| 106 | const volatile void *pcq){} |
| 107 | void AnnotateNewMemory(const char *file, int line, |
| 108 | const volatile void *mem, |
| 109 | long size){} |
| 110 | void AnnotateExpectRace(const char *file, int line, |
| 111 | const volatile void *mem, |
| 112 | const char *description){} |
| 113 | void AnnotateBenignRace(const char *file, int line, |
| 114 | const volatile void *mem, |
| 115 | const char *description){} |
| 116 | void AnnotateBenignRaceSized(const char *file, int line, |
| 117 | const volatile void *mem, |
| 118 | long size, |
| 119 | const char *description) {} |
| 120 | void AnnotateMutexIsUsedAsCondVar(const char *file, int line, |
| 121 | const volatile void *mu){} |
| 122 | void AnnotateTraceMemory(const char *file, int line, |
| 123 | const volatile void *arg){} |
| 124 | void AnnotateThreadName(const char *file, int line, |
| 125 | const char *name){} |
| 126 | void AnnotateIgnoreReadsBegin(const char *file, int line){} |
| 127 | void AnnotateIgnoreReadsEnd(const char *file, int line){} |
| 128 | void AnnotateIgnoreWritesBegin(const char *file, int line){} |
| 129 | void AnnotateIgnoreWritesEnd(const char *file, int line){} |
| 130 | void AnnotateEnableRaceDetection(const char *file, int line, int enable){} |
| 131 | void AnnotateNoOp(const char *file, int line, |
| 132 | const volatile void *arg){} |
| 133 | void 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 | |
| 140 | static 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 */ |
| 152 | int 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 */ |
| 166 | double 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 | } |