(root)/
Python-3.12.0/
Python/
dynamic_annotations.c
       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   *     * Neither the name of Google Inc. nor the names of its
      11   * contributors may be used to endorse or promote products derived from
      12   * this software without specific prior written permission.
      13   *
      14   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
      15   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
      16   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
      17   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
      18   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
      19   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
      20   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
      21   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      22   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      23   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
      24   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      25   *
      26   * ---
      27   * Author: Kostya Serebryany
      28   */
      29  
      30  #ifdef _MSC_VER
      31  # include <windows.h>
      32  #endif
      33  
      34  #ifdef __cplusplus
      35  # error "This file should be built as pure C to avoid name mangling"
      36  #endif
      37  
      38  #include <stdlib.h>
      39  #include <string.h>
      40  
      41  #include "dynamic_annotations.h"
      42  
      43  /* Each function is empty and called (via a macro) only in debug mode.
      44     The arguments are captured by dynamic tools at runtime. */
      45  
      46  #if DYNAMIC_ANNOTATIONS_ENABLED == 1
      47  
      48  void AnnotateRWLockCreate(const char *file, int line,
      49                            const volatile void *lock){}
      50  void AnnotateRWLockDestroy(const char *file, int line,
      51                             const volatile void *lock){}
      52  void AnnotateRWLockAcquired(const char *file, int line,
      53                              const volatile void *lock, long is_w){}
      54  void AnnotateRWLockReleased(const char *file, int line,
      55                              const volatile void *lock, long is_w){}
      56  void AnnotateBarrierInit(const char *file, int line,
      57                           const volatile void *barrier, long count,
      58                           long reinitialization_allowed) {}
      59  void AnnotateBarrierWaitBefore(const char *file, int line,
      60                                 const volatile void *barrier) {}
      61  void AnnotateBarrierWaitAfter(const char *file, int line,
      62                                const volatile void *barrier) {}
      63  void AnnotateBarrierDestroy(const char *file, int line,
      64                              const volatile void *barrier) {}
      65  
      66  void AnnotateCondVarWait(const char *file, int line,
      67                           const volatile void *cv,
      68                           const volatile void *lock){}
      69  void AnnotateCondVarSignal(const char *file, int line,
      70                             const volatile void *cv){}
      71  void AnnotateCondVarSignalAll(const char *file, int line,
      72                                const volatile void *cv){}
      73  void AnnotatePublishMemoryRange(const char *file, int line,
      74                                  const volatile void *address,
      75                                  long size){}
      76  void AnnotateUnpublishMemoryRange(const char *file, int line,
      77                                    const volatile void *address,
      78                                    long size){}
      79  void AnnotatePCQCreate(const char *file, int line,
      80                         const volatile void *pcq){}
      81  void AnnotatePCQDestroy(const char *file, int line,
      82                          const volatile void *pcq){}
      83  void AnnotatePCQPut(const char *file, int line,
      84                      const volatile void *pcq){}
      85  void AnnotatePCQGet(const char *file, int line,
      86                      const volatile void *pcq){}
      87  void AnnotateNewMemory(const char *file, int line,
      88                         const volatile void *mem,
      89                         long size){}
      90  void AnnotateExpectRace(const char *file, int line,
      91                          const volatile void *mem,
      92                          const char *description){}
      93  void AnnotateBenignRace(const char *file, int line,
      94                          const volatile void *mem,
      95                          const char *description){}
      96  void AnnotateBenignRaceSized(const char *file, int line,
      97                               const volatile void *mem,
      98                               long size,
      99                               const char *description) {}
     100  void AnnotateMutexIsUsedAsCondVar(const char *file, int line,
     101                                    const volatile void *mu){}
     102  void AnnotateTraceMemory(const char *file, int line,
     103                           const volatile void *arg){}
     104  void AnnotateThreadName(const char *file, int line,
     105                          const char *name){}
     106  void AnnotateIgnoreReadsBegin(const char *file, int line){}
     107  void AnnotateIgnoreReadsEnd(const char *file, int line){}
     108  void AnnotateIgnoreWritesBegin(const char *file, int line){}
     109  void AnnotateIgnoreWritesEnd(const char *file, int line){}
     110  void AnnotateIgnoreSyncBegin(const char *file, int line){}
     111  void AnnotateIgnoreSyncEnd(const char *file, int line){}
     112  void AnnotateEnableRaceDetection(const char *file, int line, int enable){}
     113  void AnnotateNoOp(const char *file, int line,
     114                    const volatile void *arg){}
     115  void AnnotateFlushState(const char *file, int line){}
     116  
     117  static int GetRunningOnValgrind(void) {
     118  #ifdef RUNNING_ON_VALGRIND
     119    if (RUNNING_ON_VALGRIND) return 1;
     120  #endif
     121  
     122  #ifndef _MSC_VER
     123    const char *running_on_valgrind_str = getenv("RUNNING_ON_VALGRIND");
     124    if (running_on_valgrind_str) {
     125      return strcmp(running_on_valgrind_str, "0") != 0;
     126    }
     127  #else
     128    /* Visual Studio issues warnings if we use getenv,
     129     * so we use GetEnvironmentVariableA instead.
     130     */
     131    char value[100] = "1";
     132    int res = GetEnvironmentVariableA("RUNNING_ON_VALGRIND",
     133                                      value, sizeof(value));
     134    /* value will remain "1" if res == 0 or res >= sizeof(value). The latter
     135     * can happen only if the given value is long, in this case it can't be "0".
     136     */
     137    if (res > 0 && !strcmp(value, "0"))
     138      return 1;
     139  #endif
     140    return 0;
     141  }
     142  
     143  /* See the comments in dynamic_annotations.h */
     144  int RunningOnValgrind(void) {
     145    static volatile int running_on_valgrind = -1;
     146    /* C doesn't have thread-safe initialization of statics, and we
     147       don't want to depend on pthread_once here, so hack it. */
     148    int local_running_on_valgrind = running_on_valgrind;
     149    if (local_running_on_valgrind == -1)
     150      running_on_valgrind = local_running_on_valgrind = GetRunningOnValgrind();
     151    return local_running_on_valgrind;
     152  }
     153  
     154  #endif  /* DYNAMIC_ANNOTATIONS_ENABLED == 1 */