Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 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 <assert.h> |
| 6 | #include <semaphore.h> |
| 7 | #include <string.h> |
| 8 | |
| 9 | void my_sem_init(bool priv, int value, unsigned *a, unsigned char *b) { |
| 10 | sem_t sem; |
| 11 | memset(&sem, 0xAB, sizeof(sem)); |
| 12 | sem_init(&sem, priv, value); |
| 13 | |
| 14 | char *p = (char *)&sem; |
| 15 | memcpy(a, p, sizeof(unsigned)); |
| 16 | memcpy(b, p + sizeof(unsigned), sizeof(char)); |
| 17 | |
| 18 | sem_destroy(&sem); |
| 19 | } |
| 20 | |
| 21 | int main() { |
| 22 | unsigned a; |
| 23 | unsigned char b; |
| 24 | |
| 25 | my_sem_init(false, 42, &a, &b); |
| 26 | assert(a == 42); |
| 27 | assert(b != 0xAB); |
| 28 | |
| 29 | my_sem_init(true, 43, &a, &b); |
| 30 | assert(a == 43); |
| 31 | assert(b != 0xAB); |
| 32 | } |