/ Check-in [f53b8a57]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Fix a data race causing a tsan complaint with SQLITE_ENABLE_API_ARMOR builds on unix. The race condition is not dangerous in practice, it just upsets tsan.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: f53b8a573bfbb48780243d56ca8372165adb1b979731c43d46cd1f8eb7e593f3
User & Date: dan 2018-02-17 07:38:50
References
2018-02-18
00:54
Port mutex enhancments from check-in [f53b8a573b] to the Win32 implementation. check-in: 74bb7225 user: mistachkin tags: trunk
Context
2018-02-18
00:54
Port mutex enhancments from check-in [f53b8a573b] to the Win32 implementation. check-in: 74bb7225 user: mistachkin tags: trunk
2018-02-17
07:38
Fix a data race causing a tsan complaint with SQLITE_ENABLE_API_ARMOR builds on unix. The race condition is not dangerous in practice, it just upsets tsan. check-in: f53b8a57 user: dan tags: trunk
2018-02-15
21:00
Do not allow parameters or schema references inside of WITH clause of triggers and views. This fixes a bug discovered by OSSFuzz and present since common-table-expressions were first added in 2014-02-03. check-in: b918d4b4 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/mutex_unix.c.

    46     46   #if SQLITE_MUTEX_NREF
    47     47     volatile int nRef;         /* Number of entrances */
    48     48     volatile pthread_t owner;  /* Thread that is within this mutex */
    49     49     int trace;                 /* True to trace changes */
    50     50   #endif
    51     51   };
    52     52   #if SQLITE_MUTEX_NREF
    53         -#define SQLITE3_MUTEX_INITIALIZER {PTHREAD_MUTEX_INITIALIZER,0,0,(pthread_t)0,0}
           53  +# define SQLITE3_MUTEX_INITIALIZER(id) \
           54  +     {PTHREAD_MUTEX_INITIALIZER,id,0,(pthread_t)0,0}
    54     55   #elif defined(SQLITE_ENABLE_API_ARMOR)
    55         -#define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0 }
           56  +# define SQLITE3_MUTEX_INITIALIZER(id) { PTHREAD_MUTEX_INITIALIZER, id }
    56     57   #else
    57         -#define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER }
           58  +#define SQLITE3_MUTEX_INITIALIZER(id) { PTHREAD_MUTEX_INITIALIZER }
    58     59   #endif
    59     60   
    60     61   /*
    61     62   ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
    62     63   ** intended for use only inside assert() statements.  On some platforms,
    63     64   ** there might be race conditions that can cause these routines to
    64     65   ** deliver incorrect results.  In particular, if pthread_equal() is
................................................................................
   147    148   ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
   148    149   ** returns a different mutex on every call.  But for the static 
   149    150   ** mutex types, the same mutex is returned on every call that has
   150    151   ** the same type number.
   151    152   */
   152    153   static sqlite3_mutex *pthreadMutexAlloc(int iType){
   153    154     static sqlite3_mutex staticMutexes[] = {
   154         -    SQLITE3_MUTEX_INITIALIZER,
   155         -    SQLITE3_MUTEX_INITIALIZER,
   156         -    SQLITE3_MUTEX_INITIALIZER,
   157         -    SQLITE3_MUTEX_INITIALIZER,
   158         -    SQLITE3_MUTEX_INITIALIZER,
   159         -    SQLITE3_MUTEX_INITIALIZER,
   160         -    SQLITE3_MUTEX_INITIALIZER,
   161         -    SQLITE3_MUTEX_INITIALIZER,
   162         -    SQLITE3_MUTEX_INITIALIZER,
   163         -    SQLITE3_MUTEX_INITIALIZER,
   164         -    SQLITE3_MUTEX_INITIALIZER,
   165         -    SQLITE3_MUTEX_INITIALIZER
          155  +    SQLITE3_MUTEX_INITIALIZER(2),
          156  +    SQLITE3_MUTEX_INITIALIZER(3),
          157  +    SQLITE3_MUTEX_INITIALIZER(4),
          158  +    SQLITE3_MUTEX_INITIALIZER(5),
          159  +    SQLITE3_MUTEX_INITIALIZER(6),
          160  +    SQLITE3_MUTEX_INITIALIZER(7),
          161  +    SQLITE3_MUTEX_INITIALIZER(8),
          162  +    SQLITE3_MUTEX_INITIALIZER(9),
          163  +    SQLITE3_MUTEX_INITIALIZER(10),
          164  +    SQLITE3_MUTEX_INITIALIZER(11),
          165  +    SQLITE3_MUTEX_INITIALIZER(12),
          166  +    SQLITE3_MUTEX_INITIALIZER(13)
   166    167     };
   167    168     sqlite3_mutex *p;
   168    169     switch( iType ){
   169    170       case SQLITE_MUTEX_RECURSIVE: {
   170    171         p = sqlite3MallocZero( sizeof(*p) );
   171    172         if( p ){
   172    173   #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
................................................................................
   177    178           /* Use a recursive mutex if it is available */
   178    179           pthread_mutexattr_t recursiveAttr;
   179    180           pthread_mutexattr_init(&recursiveAttr);
   180    181           pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
   181    182           pthread_mutex_init(&p->mutex, &recursiveAttr);
   182    183           pthread_mutexattr_destroy(&recursiveAttr);
   183    184   #endif
          185  +#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
          186  +        p->id = SQLITE_MUTEX_RECURSIVE;
          187  +#endif
   184    188         }
   185    189         break;
   186    190       }
   187    191       case SQLITE_MUTEX_FAST: {
   188    192         p = sqlite3MallocZero( sizeof(*p) );
   189    193         if( p ){
   190    194           pthread_mutex_init(&p->mutex, 0);
          195  +#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
          196  +        p->id = SQLITE_MUTEX_FAST;
          197  +#endif
   191    198         }
   192    199         break;
   193    200       }
   194    201       default: {
   195    202   #ifdef SQLITE_ENABLE_API_ARMOR
   196    203         if( iType-2<0 || iType-2>=ArraySize(staticMutexes) ){
   197    204           (void)SQLITE_MISUSE_BKPT;
................................................................................
   199    206         }
   200    207   #endif
   201    208         p = &staticMutexes[iType-2];
   202    209         break;
   203    210       }
   204    211     }
   205    212   #if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
   206         -  if( p ) p->id = iType;
          213  +  assert( p==0 || p->id==iType );
   207    214   #endif
   208    215     return p;
   209    216   }
   210    217   
   211    218   
   212    219   /*
   213    220   ** This routine deallocates a previously