/* Copyright (c) 2008-2009, Google Inc.1* All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions are5* met:6*7* * Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* * Neither the name of Google Inc. nor the names of its10* contributors may be used to endorse or promote products derived from11* this software without specific prior written permission.12*13* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS14* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT15* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR16* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT17* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,18* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT19* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,20* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY21* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT22* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE23* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.24*25* ---26* Author: Kostya Serebryany27* Copied to CPython by Jeffrey Yasskin, with all macros renamed to28* start with _Py_ to avoid colliding with users embedding Python, and29* with deprecated macros removed.30*/3132/* This file defines dynamic annotations for use with dynamic analysis33tool such as valgrind, PIN, etc.3435Dynamic annotation is a source code annotation that affects36the generated code (that is, the annotation is not a comment).37Each such annotation is attached to a particular38instruction and/or to a particular object (address) in the program.3940The annotations that should be used by users are macros in all upper-case41(e.g., _Py_ANNOTATE_NEW_MEMORY).4243Actual implementation of these macros may differ depending on the44dynamic analysis tool being used.4546See https://code.google.com/p/data-race-test/ for more information.4748This file supports the following dynamic analysis tools:49- None (DYNAMIC_ANNOTATIONS_ENABLED is not defined or zero).50Macros are defined empty.51- ThreadSanitizer, Helgrind, DRD (DYNAMIC_ANNOTATIONS_ENABLED is 1).52Macros are defined as calls to non-inlinable empty functions53that are intercepted by Valgrind. */5455#ifndef __DYNAMIC_ANNOTATIONS_H__56#define __DYNAMIC_ANNOTATIONS_H__5758#ifndef DYNAMIC_ANNOTATIONS_ENABLED59# define DYNAMIC_ANNOTATIONS_ENABLED 060#endif6162#if DYNAMIC_ANNOTATIONS_ENABLED != 06364/* -------------------------------------------------------------65Annotations useful when implementing condition variables such as CondVar,66using conditional critical sections (Await/LockWhen) and when constructing67user-defined synchronization mechanisms.6869The annotations _Py_ANNOTATE_HAPPENS_BEFORE() and70_Py_ANNOTATE_HAPPENS_AFTER() can be used to define happens-before arcs in71user-defined synchronization mechanisms: the race detector will infer an72arc from the former to the latter when they share the same argument73pointer.7475Example 1 (reference counting):7677void Unref() {78_Py_ANNOTATE_HAPPENS_BEFORE(&refcount_);79if (AtomicDecrementByOne(&refcount_) == 0) {80_Py_ANNOTATE_HAPPENS_AFTER(&refcount_);81delete this;82}83}8485Example 2 (message queue):8687void MyQueue::Put(Type *e) {88MutexLock lock(&mu_);89_Py_ANNOTATE_HAPPENS_BEFORE(e);90PutElementIntoMyQueue(e);91}9293Type *MyQueue::Get() {94MutexLock lock(&mu_);95Type *e = GetElementFromMyQueue();96_Py_ANNOTATE_HAPPENS_AFTER(e);97return e;98}99100Note: when possible, please use the existing reference counting and message101queue implementations instead of inventing new ones. */102103/* Report that wait on the condition variable at address "cv" has succeeded104and the lock at address "lock" is held. */105#define _Py_ANNOTATE_CONDVAR_LOCK_WAIT(cv, lock) \106AnnotateCondVarWait(__FILE__, __LINE__, cv, lock)107108/* Report that wait on the condition variable at "cv" has succeeded. Variant109w/o lock. */110#define _Py_ANNOTATE_CONDVAR_WAIT(cv) \111AnnotateCondVarWait(__FILE__, __LINE__, cv, NULL)112113/* Report that we are about to signal on the condition variable at address114"cv". */115#define _Py_ANNOTATE_CONDVAR_SIGNAL(cv) \116AnnotateCondVarSignal(__FILE__, __LINE__, cv)117118/* Report that we are about to signal_all on the condition variable at "cv". */119#define _Py_ANNOTATE_CONDVAR_SIGNAL_ALL(cv) \120AnnotateCondVarSignalAll(__FILE__, __LINE__, cv)121122/* Annotations for user-defined synchronization mechanisms. */123#define _Py_ANNOTATE_HAPPENS_BEFORE(obj) _Py_ANNOTATE_CONDVAR_SIGNAL(obj)124#define _Py_ANNOTATE_HAPPENS_AFTER(obj) _Py_ANNOTATE_CONDVAR_WAIT(obj)125126/* Report that the bytes in the range [pointer, pointer+size) are about127to be published safely. The race checker will create a happens-before128arc from the call _Py_ANNOTATE_PUBLISH_MEMORY_RANGE(pointer, size) to129subsequent accesses to this memory.130Note: this annotation may not work properly if the race detector uses131sampling, i.e. does not observe all memory accesses.132*/133#define _Py_ANNOTATE_PUBLISH_MEMORY_RANGE(pointer, size) \134AnnotatePublishMemoryRange(__FILE__, __LINE__, pointer, size)135136/* Instruct the tool to create a happens-before arc between mu->Unlock() and137mu->Lock(). This annotation may slow down the race detector and hide real138races. Normally it is used only when it would be difficult to annotate each139of the mutex's critical sections individually using the annotations above.140This annotation makes sense only for hybrid race detectors. For pure141happens-before detectors this is a no-op. For more details see142https://code.google.com/p/data-race-test/wiki/PureHappensBeforeVsHybrid . */143#define _Py_ANNOTATE_PURE_HAPPENS_BEFORE_MUTEX(mu) \144AnnotateMutexIsUsedAsCondVar(__FILE__, __LINE__, mu)145146/* -------------------------------------------------------------147Annotations useful when defining memory allocators, or when memory that148was protected in one way starts to be protected in another. */149150/* Report that a new memory at "address" of size "size" has been allocated.151This might be used when the memory has been retrieved from a free list and152is about to be reused, or when the locking discipline for a variable153changes. */154#define _Py_ANNOTATE_NEW_MEMORY(address, size) \155AnnotateNewMemory(__FILE__, __LINE__, address, size)156157/* -------------------------------------------------------------158Annotations useful when defining FIFO queues that transfer data between159threads. */160161/* Report that the producer-consumer queue (such as ProducerConsumerQueue) at162address "pcq" has been created. The _Py_ANNOTATE_PCQ_* annotations should163be used only for FIFO queues. For non-FIFO queues use164_Py_ANNOTATE_HAPPENS_BEFORE (for put) and _Py_ANNOTATE_HAPPENS_AFTER (for165get). */166#define _Py_ANNOTATE_PCQ_CREATE(pcq) \167AnnotatePCQCreate(__FILE__, __LINE__, pcq)168169/* Report that the queue at address "pcq" is about to be destroyed. */170#define _Py_ANNOTATE_PCQ_DESTROY(pcq) \171AnnotatePCQDestroy(__FILE__, __LINE__, pcq)172173/* Report that we are about to put an element into a FIFO queue at address174"pcq". */175#define _Py_ANNOTATE_PCQ_PUT(pcq) \176AnnotatePCQPut(__FILE__, __LINE__, pcq)177178/* Report that we've just got an element from a FIFO queue at address "pcq". */179#define _Py_ANNOTATE_PCQ_GET(pcq) \180AnnotatePCQGet(__FILE__, __LINE__, pcq)181182/* -------------------------------------------------------------183Annotations that suppress errors. It is usually better to express the184program's synchronization using the other annotations, but these can185be used when all else fails. */186187/* Report that we may have a benign race at "pointer", with size188"sizeof(*(pointer))". "pointer" must be a non-void* pointer. Insert at the189point where "pointer" has been allocated, preferably close to the point190where the race happens. See also _Py_ANNOTATE_BENIGN_RACE_STATIC. */191#define _Py_ANNOTATE_BENIGN_RACE(pointer, description) \192AnnotateBenignRaceSized(__FILE__, __LINE__, pointer, \193sizeof(*(pointer)), description)194195/* Same as _Py_ANNOTATE_BENIGN_RACE(address, description), but applies to196the memory range [address, address+size). */197#define _Py_ANNOTATE_BENIGN_RACE_SIZED(address, size, description) \198AnnotateBenignRaceSized(__FILE__, __LINE__, address, size, description)199200/* Request the analysis tool to ignore all reads in the current thread201until _Py_ANNOTATE_IGNORE_READS_END is called.202Useful to ignore intentional racey reads, while still checking203other reads and all writes.204See also _Py_ANNOTATE_UNPROTECTED_READ. */205#define _Py_ANNOTATE_IGNORE_READS_BEGIN() \206AnnotateIgnoreReadsBegin(__FILE__, __LINE__)207208/* Stop ignoring reads. */209#define _Py_ANNOTATE_IGNORE_READS_END() \210AnnotateIgnoreReadsEnd(__FILE__, __LINE__)211212/* Similar to _Py_ANNOTATE_IGNORE_READS_BEGIN, but ignore writes. */213#define _Py_ANNOTATE_IGNORE_WRITES_BEGIN() \214AnnotateIgnoreWritesBegin(__FILE__, __LINE__)215216/* Stop ignoring writes. */217#define _Py_ANNOTATE_IGNORE_WRITES_END() \218AnnotateIgnoreWritesEnd(__FILE__, __LINE__)219220/* Start ignoring all memory accesses (reads and writes). */221#define _Py_ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN() \222do {\223_Py_ANNOTATE_IGNORE_READS_BEGIN();\224_Py_ANNOTATE_IGNORE_WRITES_BEGIN();\225}while(0)\226227/* Stop ignoring all memory accesses. */228#define _Py_ANNOTATE_IGNORE_READS_AND_WRITES_END() \229do {\230_Py_ANNOTATE_IGNORE_WRITES_END();\231_Py_ANNOTATE_IGNORE_READS_END();\232}while(0)\233234/* Similar to _Py_ANNOTATE_IGNORE_READS_BEGIN, but ignore synchronization events:235RWLOCK* and CONDVAR*. */236#define _Py_ANNOTATE_IGNORE_SYNC_BEGIN() \237AnnotateIgnoreSyncBegin(__FILE__, __LINE__)238239/* Stop ignoring sync events. */240#define _Py_ANNOTATE_IGNORE_SYNC_END() \241AnnotateIgnoreSyncEnd(__FILE__, __LINE__)242243244/* Enable (enable!=0) or disable (enable==0) race detection for all threads.245This annotation could be useful if you want to skip expensive race analysis246during some period of program execution, e.g. during initialization. */247#define _Py_ANNOTATE_ENABLE_RACE_DETECTION(enable) \248AnnotateEnableRaceDetection(__FILE__, __LINE__, enable)249250/* -------------------------------------------------------------251Annotations useful for debugging. */252253/* Request to trace every access to "address". */254#define _Py_ANNOTATE_TRACE_MEMORY(address) \255AnnotateTraceMemory(__FILE__, __LINE__, address)256257/* Report the current thread name to a race detector. */258#define _Py_ANNOTATE_THREAD_NAME(name) \259AnnotateThreadName(__FILE__, __LINE__, name)260261/* -------------------------------------------------------------262Annotations useful when implementing locks. They are not263normally needed by modules that merely use locks.264The "lock" argument is a pointer to the lock object. */265266/* Report that a lock has been created at address "lock". */267#define _Py_ANNOTATE_RWLOCK_CREATE(lock) \268AnnotateRWLockCreate(__FILE__, __LINE__, lock)269270/* Report that the lock at address "lock" is about to be destroyed. */271#define _Py_ANNOTATE_RWLOCK_DESTROY(lock) \272AnnotateRWLockDestroy(__FILE__, __LINE__, lock)273274/* Report that the lock at address "lock" has been acquired.275is_w=1 for writer lock, is_w=0 for reader lock. */276#define _Py_ANNOTATE_RWLOCK_ACQUIRED(lock, is_w) \277AnnotateRWLockAcquired(__FILE__, __LINE__, lock, is_w)278279/* Report that the lock at address "lock" is about to be released. */280#define _Py_ANNOTATE_RWLOCK_RELEASED(lock, is_w) \281AnnotateRWLockReleased(__FILE__, __LINE__, lock, is_w)282283/* -------------------------------------------------------------284Annotations useful when implementing barriers. They are not285normally needed by modules that merely use barriers.286The "barrier" argument is a pointer to the barrier object. */287288/* Report that the "barrier" has been initialized with initial "count".289If 'reinitialization_allowed' is true, initialization is allowed to happen290multiple times w/o calling barrier_destroy() */291#define _Py_ANNOTATE_BARRIER_INIT(barrier, count, reinitialization_allowed) \292AnnotateBarrierInit(__FILE__, __LINE__, barrier, count, \293reinitialization_allowed)294295/* Report that we are about to enter barrier_wait("barrier"). */296#define _Py_ANNOTATE_BARRIER_WAIT_BEFORE(barrier) \297AnnotateBarrierWaitBefore(__FILE__, __LINE__, barrier)298299/* Report that we just exited barrier_wait("barrier"). */300#define _Py_ANNOTATE_BARRIER_WAIT_AFTER(barrier) \301AnnotateBarrierWaitAfter(__FILE__, __LINE__, barrier)302303/* Report that the "barrier" has been destroyed. */304#define _Py_ANNOTATE_BARRIER_DESTROY(barrier) \305AnnotateBarrierDestroy(__FILE__, __LINE__, barrier)306307/* -------------------------------------------------------------308Annotations useful for testing race detectors. */309310/* Report that we expect a race on the variable at "address".311Use only in unit tests for a race detector. */312#define _Py_ANNOTATE_EXPECT_RACE(address, description) \313AnnotateExpectRace(__FILE__, __LINE__, address, description)314315/* A no-op. Insert where you like to test the interceptors. */316#define _Py_ANNOTATE_NO_OP(arg) \317AnnotateNoOp(__FILE__, __LINE__, arg)318319/* Force the race detector to flush its state. The actual effect depends on320* the implementation of the detector. */321#define _Py_ANNOTATE_FLUSH_STATE() \322AnnotateFlushState(__FILE__, __LINE__)323324325#else /* DYNAMIC_ANNOTATIONS_ENABLED == 0 */326327#define _Py_ANNOTATE_RWLOCK_CREATE(lock) /* empty */328#define _Py_ANNOTATE_RWLOCK_DESTROY(lock) /* empty */329#define _Py_ANNOTATE_RWLOCK_ACQUIRED(lock, is_w) /* empty */330#define _Py_ANNOTATE_RWLOCK_RELEASED(lock, is_w) /* empty */331#define _Py_ANNOTATE_BARRIER_INIT(barrier, count, reinitialization_allowed) /* */332#define _Py_ANNOTATE_BARRIER_WAIT_BEFORE(barrier) /* empty */333#define _Py_ANNOTATE_BARRIER_WAIT_AFTER(barrier) /* empty */334#define _Py_ANNOTATE_BARRIER_DESTROY(barrier) /* empty */335#define _Py_ANNOTATE_CONDVAR_LOCK_WAIT(cv, lock) /* empty */336#define _Py_ANNOTATE_CONDVAR_WAIT(cv) /* empty */337#define _Py_ANNOTATE_CONDVAR_SIGNAL(cv) /* empty */338#define _Py_ANNOTATE_CONDVAR_SIGNAL_ALL(cv) /* empty */339#define _Py_ANNOTATE_HAPPENS_BEFORE(obj) /* empty */340#define _Py_ANNOTATE_HAPPENS_AFTER(obj) /* empty */341#define _Py_ANNOTATE_PUBLISH_MEMORY_RANGE(address, size) /* empty */342#define _Py_ANNOTATE_UNPUBLISH_MEMORY_RANGE(address, size) /* empty */343#define _Py_ANNOTATE_SWAP_MEMORY_RANGE(address, size) /* empty */344#define _Py_ANNOTATE_PCQ_CREATE(pcq) /* empty */345#define _Py_ANNOTATE_PCQ_DESTROY(pcq) /* empty */346#define _Py_ANNOTATE_PCQ_PUT(pcq) /* empty */347#define _Py_ANNOTATE_PCQ_GET(pcq) /* empty */348#define _Py_ANNOTATE_NEW_MEMORY(address, size) /* empty */349#define _Py_ANNOTATE_EXPECT_RACE(address, description) /* empty */350#define _Py_ANNOTATE_BENIGN_RACE(address, description) /* empty */351#define _Py_ANNOTATE_BENIGN_RACE_SIZED(address, size, description) /* empty */352#define _Py_ANNOTATE_PURE_HAPPENS_BEFORE_MUTEX(mu) /* empty */353#define _Py_ANNOTATE_MUTEX_IS_USED_AS_CONDVAR(mu) /* empty */354#define _Py_ANNOTATE_TRACE_MEMORY(arg) /* empty */355#define _Py_ANNOTATE_THREAD_NAME(name) /* empty */356#define _Py_ANNOTATE_IGNORE_READS_BEGIN() /* empty */357#define _Py_ANNOTATE_IGNORE_READS_END() /* empty */358#define _Py_ANNOTATE_IGNORE_WRITES_BEGIN() /* empty */359#define _Py_ANNOTATE_IGNORE_WRITES_END() /* empty */360#define _Py_ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN() /* empty */361#define _Py_ANNOTATE_IGNORE_READS_AND_WRITES_END() /* empty */362#define _Py_ANNOTATE_IGNORE_SYNC_BEGIN() /* empty */363#define _Py_ANNOTATE_IGNORE_SYNC_END() /* empty */364#define _Py_ANNOTATE_ENABLE_RACE_DETECTION(enable) /* empty */365#define _Py_ANNOTATE_NO_OP(arg) /* empty */366#define _Py_ANNOTATE_FLUSH_STATE() /* empty */367368#endif /* DYNAMIC_ANNOTATIONS_ENABLED */369370/* Use the macros above rather than using these functions directly. */371#ifdef __cplusplus372extern "C" {373#endif374void AnnotateRWLockCreate(const char *file, int line,375const volatile void *lock);376void AnnotateRWLockDestroy(const char *file, int line,377const volatile void *lock);378void AnnotateRWLockAcquired(const char *file, int line,379const volatile void *lock, long is_w);380void AnnotateRWLockReleased(const char *file, int line,381const volatile void *lock, long is_w);382void AnnotateBarrierInit(const char *file, int line,383const volatile void *barrier, long count,384long reinitialization_allowed);385void AnnotateBarrierWaitBefore(const char *file, int line,386const volatile void *barrier);387void AnnotateBarrierWaitAfter(const char *file, int line,388const volatile void *barrier);389void AnnotateBarrierDestroy(const char *file, int line,390const volatile void *barrier);391void AnnotateCondVarWait(const char *file, int line,392const volatile void *cv,393const volatile void *lock);394void AnnotateCondVarSignal(const char *file, int line,395const volatile void *cv);396void AnnotateCondVarSignalAll(const char *file, int line,397const volatile void *cv);398void AnnotatePublishMemoryRange(const char *file, int line,399const volatile void *address,400long size);401void AnnotateUnpublishMemoryRange(const char *file, int line,402const volatile void *address,403long size);404void AnnotatePCQCreate(const char *file, int line,405const volatile void *pcq);406void AnnotatePCQDestroy(const char *file, int line,407const volatile void *pcq);408void AnnotatePCQPut(const char *file, int line,409const volatile void *pcq);410void AnnotatePCQGet(const char *file, int line,411const volatile void *pcq);412void AnnotateNewMemory(const char *file, int line,413const volatile void *address,414long size);415void AnnotateExpectRace(const char *file, int line,416const volatile void *address,417const char *description);418void AnnotateBenignRace(const char *file, int line,419const volatile void *address,420const char *description);421void AnnotateBenignRaceSized(const char *file, int line,422const volatile void *address,423long size,424const char *description);425void AnnotateMutexIsUsedAsCondVar(const char *file, int line,426const volatile void *mu);427void AnnotateTraceMemory(const char *file, int line,428const volatile void *arg);429void AnnotateThreadName(const char *file, int line,430const char *name);431void AnnotateIgnoreReadsBegin(const char *file, int line);432void AnnotateIgnoreReadsEnd(const char *file, int line);433void AnnotateIgnoreWritesBegin(const char *file, int line);434void AnnotateIgnoreWritesEnd(const char *file, int line);435void AnnotateEnableRaceDetection(const char *file, int line, int enable);436void AnnotateNoOp(const char *file, int line,437const volatile void *arg);438void AnnotateFlushState(const char *file, int line);439440/* Return non-zero value if running under valgrind.441442If "valgrind.h" is included into dynamic_annotations.c,443the regular valgrind mechanism will be used.444See http://valgrind.org/docs/manual/manual-core-adv.html about445RUNNING_ON_VALGRIND and other valgrind "client requests".446The file "valgrind.h" may be obtained by doing447svn co svn://svn.valgrind.org/valgrind/trunk/include448449If for some reason you can't use "valgrind.h" or want to fake valgrind,450there are two ways to make this function return non-zero:451- Use environment variable: export RUNNING_ON_VALGRIND=1452- Make your tool intercept the function RunningOnValgrind() and453change its return value.454*/455int RunningOnValgrind(void);456457#ifdef __cplusplus458}459#endif460461#if DYNAMIC_ANNOTATIONS_ENABLED != 0 && defined(__cplusplus)462463/* _Py_ANNOTATE_UNPROTECTED_READ is the preferred way to annotate racey reads.464465Instead of doing466_Py_ANNOTATE_IGNORE_READS_BEGIN();467... = x;468_Py_ANNOTATE_IGNORE_READS_END();469one can use470... = _Py_ANNOTATE_UNPROTECTED_READ(x); */471template <class T>472inline T _Py_ANNOTATE_UNPROTECTED_READ(const volatile T &x) {473_Py_ANNOTATE_IGNORE_READS_BEGIN();474T res = x;475_Py_ANNOTATE_IGNORE_READS_END();476return res;477}478/* Apply _Py_ANNOTATE_BENIGN_RACE_SIZED to a static variable. */479#define _Py_ANNOTATE_BENIGN_RACE_STATIC(static_var, description) \480namespace { \481class static_var ## _annotator { \482public: \483static_var ## _annotator() { \484_Py_ANNOTATE_BENIGN_RACE_SIZED(&static_var, \485sizeof(static_var), \486# static_var ": " description); \487} \488}; \489static static_var ## _annotator the ## static_var ## _annotator;\490}491#else /* DYNAMIC_ANNOTATIONS_ENABLED == 0 */492493#define _Py_ANNOTATE_UNPROTECTED_READ(x) (x)494#define _Py_ANNOTATE_BENIGN_RACE_STATIC(static_var, description) /* empty */495496#endif /* DYNAMIC_ANNOTATIONS_ENABLED */497498#endif /* __DYNAMIC_ANNOTATIONS_H__ */499500501