70 lines · cpp
1// Regression test 1:2// When the stack size is 1<<16, SizeRequiredForFlags(16) == 2KB. This forces3// FakeStack's GetFrame() out of alignment if the FakeStack isn't padded.4// RUN: %clangxx_asan -pthread -fsanitize-address-use-after-return=always -O0 -DALIGNMENT=4096 -DTHREAD_COUNT=1 -DTHREAD_STACK_SIZE=65536 %s -o %t && %run %t 2>&15 6// Regression test 2:7// Check that the FakeStack frame is aligned, beyond the typical 4KB page8// alignment. Alignment can happen by chance, so try this on many threads.9// RUN: %clangxx_asan -pthread -fsanitize-address-use-after-return=always -O0 -DALIGNMENT=8192 -DTHREAD_COUNT=32 -DTHREAD_STACK_SIZE=131072 %s -o %t && %run %t 2>&110// RUN: %clangxx_asan -pthread -fsanitize-address-use-after-return=always -O0 -DALIGNMENT=16384 -DTHREAD_COUNT=32 -DTHREAD_STACK_SIZE=131072 %s -o %t && %run %t 2>&111 12// Extra tests:13// RUN: %clangxx_asan -pthread -fsanitize-address-use-after-return=always -O0 -DALIGNMENT=4096 -DTHREAD_COUNT=32 -DTHREAD_STACK_SIZE=65536 %s -o %t && %run %t 2>&114// RUN: %clangxx_asan -pthread -fsanitize-address-use-after-return=always -O0 -DALIGNMENT=8192 -DTHREAD_COUNT=32 -DTHREAD_STACK_SIZE=65536 %s -o %t && %run %t 2>&115// RUN: %clangxx_asan -pthread -fsanitize-address-use-after-return=always -O0 -DALIGNMENT=16384 -DTHREAD_COUNT=32 -DTHREAD_STACK_SIZE=65536 %s -o %t && %run %t 2>&116// RUN: %clangxx_asan -pthread -fsanitize-address-use-after-return=always -O0 -DALIGNMENT=4096 -DTHREAD_COUNT=32 -DTHREAD_STACK_SIZE=131072 %s -o %t && %run %t 2>&117// RUN: %clangxx_asan -pthread -fsanitize-address-use-after-return=always -O0 -DALIGNMENT=8192 -DTHREAD_COUNT=32 -DTHREAD_STACK_SIZE=131072 %s -o %t && %run %t 2>&118// RUN: %clangxx_asan -pthread -fsanitize-address-use-after-return=always -O0 -DALIGNMENT=16384 -DTHREAD_COUNT=32 -DTHREAD_STACK_SIZE=131072 %s -o %t && %run %t 2>&119 20// UNSUPPORTED: android21 22#include <assert.h>23#include <pthread.h>24#include <stdint.h>25#include <stdio.h>26#include <stdlib.h>27#include <string.h>28 29struct alignas(ALIGNMENT) big_object {30 int x;31};32 33bool misaligned = false;34 35// Check whether the FakeStack frame is sufficiently aligned. Alignment can36// happen by chance, so try this on many threads.37void *Thread(void *unused) {38 big_object x;39 uintptr_t alignment = (uintptr_t)&x % alignof(big_object);40 41 if (alignment != 0)42 misaligned = true;43 44 return nullptr;45}46 47int main(int argc, char **argv) {48 pthread_attr_t attr;49 pthread_attr_init(&attr);50#ifdef THREAD_STACK_SIZE51 pthread_attr_setstacksize(&attr, THREAD_STACK_SIZE);52#endif53 54 pthread_t threads[THREAD_COUNT];55 for (pthread_t &t : threads)56 pthread_create(&t, &attr, Thread, 0);57 58 pthread_attr_destroy(&attr);59 60 for (pthread_t &t : threads)61 pthread_join(t, 0);62 63 if (misaligned) {64 printf("Test failed: not perfectly aligned\n");65 exit(1);66 }67 68 return 0;69}70