Home | History | Annotate | Download | only in Linux
      1 // RUN: %clangxx -O0 -g %s -lutil -o %t && %run %t
      2 // This test depends on the glibc layout of struct sem_t and checks that we
      3 // don't leave sem_t::private uninitialized.
      4 // UNSUPPORTED: android
      5 #include <features.h>
      6 #include <assert.h>
      7 #include <semaphore.h>
      8 #include <string.h>
      9 #include <stdint.h>
     10 
     11 // This condition needs to correspond to __HAVE_64B_ATOMICS macro in glibc.
     12 #if (defined(__x86_64__) || defined(__aarch64__) || defined(__powerpc64__) || \
     13      defined(__s390x__) || defined(__sparc64__) || defined(__alpha__) || \
     14      defined(__ia64__) || defined(__m68k__)) && __GLIBC_PREREQ(2, 21)
     15 typedef uint64_t semval_t;
     16 #else
     17 typedef unsigned semval_t;
     18 #endif
     19 
     20 void my_sem_init(bool priv, int value, semval_t *a, unsigned char *b) {
     21   sem_t sem;
     22   memset(&sem, 0xAB, sizeof(sem));
     23   sem_init(&sem, priv, value);
     24 
     25   char *p = (char *)&sem;
     26   memcpy(a, p, sizeof(semval_t));
     27   memcpy(b, p + sizeof(semval_t), sizeof(char));
     28 
     29   sem_destroy(&sem);
     30 }
     31 
     32 int main() {
     33   semval_t a;
     34   unsigned char b;
     35 
     36   my_sem_init(false, 42, &a, &b);
     37   assert(a == 42);
     38   assert(b != 0xAB);
     39 
     40   my_sem_init(true, 43, &a, &b);
     41   assert(a == 43);
     42   assert(b != 0xAB);
     43 }
     44