dynamic_annotations.cc
Go to the documentation of this file.
00001 // Copyright 2017 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #include <stdlib.h>
00016 #include <string.h>
00017 
00018 #include "absl/base/dynamic_annotations.h"
00019 
00020 #ifndef __has_feature
00021 #define __has_feature(x) 0
00022 #endif
00023 
00024 /* Compiler-based ThreadSanitizer defines
00025    DYNAMIC_ANNOTATIONS_EXTERNAL_IMPL = 1
00026    and provides its own definitions of the functions. */
00027 
00028 #ifndef DYNAMIC_ANNOTATIONS_EXTERNAL_IMPL
00029 # define DYNAMIC_ANNOTATIONS_EXTERNAL_IMPL 0
00030 #endif
00031 
00032 /* Each function is empty and called (via a macro) only in debug mode.
00033    The arguments are captured by dynamic tools at runtime. */
00034 
00035 #if DYNAMIC_ANNOTATIONS_EXTERNAL_IMPL == 0 && !defined(__native_client__)
00036 
00037 #if __has_feature(memory_sanitizer)
00038 #include <sanitizer/msan_interface.h>
00039 #endif
00040 
00041 #ifdef __cplusplus
00042 extern "C" {
00043 #endif
00044 
00045 void AnnotateRWLockCreate(const char *, int,
00046                           const volatile void *){}
00047 void AnnotateRWLockDestroy(const char *, int,
00048                            const volatile void *){}
00049 void AnnotateRWLockAcquired(const char *, int,
00050                             const volatile void *, long){}
00051 void AnnotateRWLockReleased(const char *, int,
00052                             const volatile void *, long){}
00053 void AnnotateBenignRace(const char *, int,
00054                         const volatile void *,
00055                         const char *){}
00056 void AnnotateBenignRaceSized(const char *, int,
00057                              const volatile void *,
00058                              size_t,
00059                              const char *) {}
00060 void AnnotateThreadName(const char *, int,
00061                         const char *){}
00062 void AnnotateIgnoreReadsBegin(const char *, int){}
00063 void AnnotateIgnoreReadsEnd(const char *, int){}
00064 void AnnotateIgnoreWritesBegin(const char *, int){}
00065 void AnnotateIgnoreWritesEnd(const char *, int){}
00066 void AnnotateEnableRaceDetection(const char *, int, int){}
00067 void AnnotateMemoryIsInitialized(const char *, int,
00068                                  const volatile void *mem, size_t size) {
00069 #if __has_feature(memory_sanitizer)
00070   __msan_unpoison(mem, size);
00071 #else
00072   (void)mem;
00073   (void)size;
00074 #endif
00075 }
00076 
00077 void AnnotateMemoryIsUninitialized(const char *, int,
00078                                    const volatile void *mem, size_t size) {
00079 #if __has_feature(memory_sanitizer)
00080   __msan_allocated_memory(mem, size);
00081 #else
00082   (void)mem;
00083   (void)size;
00084 #endif
00085 }
00086 
00087 static int GetRunningOnValgrind(void) {
00088 #ifdef RUNNING_ON_VALGRIND
00089   if (RUNNING_ON_VALGRIND) return 1;
00090 #endif
00091   char *running_on_valgrind_str = getenv("RUNNING_ON_VALGRIND");
00092   if (running_on_valgrind_str) {
00093     return strcmp(running_on_valgrind_str, "0") != 0;
00094   }
00095   return 0;
00096 }
00097 
00098 /* See the comments in dynamic_annotations.h */
00099 int RunningOnValgrind(void) {
00100   static volatile int running_on_valgrind = -1;
00101   int local_running_on_valgrind = running_on_valgrind;
00102   /* C doesn't have thread-safe initialization of statics, and we
00103      don't want to depend on pthread_once here, so hack it. */
00104   ANNOTATE_BENIGN_RACE(&running_on_valgrind, "safe hack");
00105   if (local_running_on_valgrind == -1)
00106     running_on_valgrind = local_running_on_valgrind = GetRunningOnValgrind();
00107   return local_running_on_valgrind;
00108 }
00109 
00110 /* See the comments in dynamic_annotations.h */
00111 double ValgrindSlowdown(void) {
00112   /* Same initialization hack as in RunningOnValgrind(). */
00113   static volatile double slowdown = 0.0;
00114   double local_slowdown = slowdown;
00115   ANNOTATE_BENIGN_RACE(&slowdown, "safe hack");
00116   if (RunningOnValgrind() == 0) {
00117     return 1.0;
00118   }
00119   if (local_slowdown == 0.0) {
00120     char *env = getenv("VALGRIND_SLOWDOWN");
00121     slowdown = local_slowdown = env ? atof(env) : 50.0;
00122   }
00123   return local_slowdown;
00124 }
00125 
00126 #ifdef __cplusplus
00127 }  // extern "C"
00128 #endif
00129 #endif  /* DYNAMIC_ANNOTATIONS_EXTERNAL_IMPL == 0 */


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:14