Skip to content
53 changes: 53 additions & 0 deletions compiler-rt/test/asan/TestCases/fakestack_alignment.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// RUN: %clangxx_asan -fsanitize-address-use-after-return=always -O0 %s -o %t && %run %t 2>&1
// XFAIL: *

#include <assert.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct alignas(4096) page {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is unused? Can we merge this with the other test, and make the alignas / thread count a define instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merged and #define'd

int x;
};

struct alignas(16384) larry {
int x;
};

bool misaligned = false;

// Check whether the FakeStack frame is sufficiently aligned. Alignment can
// happen by chance, so try this on many threads.
void *Thread(void *unused) {
larry l1;
uint alignment = (unsigned long)&l1 % alignof(larry);
printf("Thread: address modulo alignment is %u\n", alignment);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to print this 32 times?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed

if (alignment != 0)
misaligned = true;

child();

return NULL;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is C++, use nullptr

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}

int main(int argc, char **argv) {
pthread_attr_t attr;
pthread_attr_init(&attr);

pthread_t t[32];
for (int i = 0; i < 32; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

braces

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed

pthread_create(&t[i], &attr, Thread, 0);
}
pthread_attr_destroy(&attr);
for (int i = 0; i < 32; i++) {
pthread_join(t[i], 0);
}

if (misaligned) {
printf("Test failed: not perfectly aligned\n");
exit(1);
}

return 0;
}
Loading