[Clang][SME2] Enable multi-vector loads & stores for SME2 (#75821)
[llvm-project.git] / compiler-rt / lib / asan / tests / asan_fake_stack_test.cpp
blob504b0aaf30143abecbf5d14f34c3dbc69c13672e
1 //===-- asan_fake_stack_test.cpp ------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a part of AddressSanitizer, an address sanity checker.
11 // Tests for FakeStack.
12 // This test file should be compiled w/o asan instrumentation.
13 //===----------------------------------------------------------------------===//
15 #include "asan_fake_stack.h"
16 #include "asan_test_utils.h"
17 #include "sanitizer_common/sanitizer_common.h"
19 #include <assert.h>
20 #include <stdlib.h>
21 #include <stdio.h>
23 #include <map>
25 namespace __asan {
27 TEST(FakeStack, FlagsSize) {
28 EXPECT_EQ(FakeStack::SizeRequiredForFlags(10), 1U << 5);
29 EXPECT_EQ(FakeStack::SizeRequiredForFlags(11), 1U << 6);
30 EXPECT_EQ(FakeStack::SizeRequiredForFlags(20), 1U << 15);
33 TEST(FakeStack, RequiredSize) {
34 // for (int i = 15; i < 20; i++) {
35 // uptr alloc_size = FakeStack::RequiredSize(i);
36 // printf("%zdK ==> %zd\n", 1 << (i - 10), alloc_size);
37 // }
38 EXPECT_EQ(FakeStack::RequiredSize(15), 365568U);
39 EXPECT_EQ(FakeStack::RequiredSize(16), 727040U);
40 EXPECT_EQ(FakeStack::RequiredSize(17), 1449984U);
41 EXPECT_EQ(FakeStack::RequiredSize(18), 2895872U);
42 EXPECT_EQ(FakeStack::RequiredSize(19), 5787648U);
45 TEST(FakeStack, FlagsOffset) {
46 for (uptr stack_size_log = 15; stack_size_log <= 20; stack_size_log++) {
47 uptr stack_size = 1UL << stack_size_log;
48 uptr offset = 0;
49 for (uptr class_id = 0; class_id < FakeStack::kNumberOfSizeClasses;
50 class_id++) {
51 uptr frame_size = FakeStack::BytesInSizeClass(class_id);
52 uptr num_flags = stack_size / frame_size;
53 EXPECT_EQ(offset, FakeStack::FlagsOffset(stack_size_log, class_id));
54 // printf("%zd: %zd => %zd %zd\n", stack_size_log, class_id, offset,
55 // FakeStack::FlagsOffset(stack_size_log, class_id));
56 offset += num_flags;
61 #if !defined(_WIN32) // FIXME: Fails due to OOM on Windows.
62 TEST(FakeStack, CreateDestroy) {
63 for (int i = 0; i < 1000; i++) {
64 for (uptr stack_size_log = 20; stack_size_log <= 22; stack_size_log++) {
65 FakeStack *fake_stack = FakeStack::Create(stack_size_log);
66 fake_stack->Destroy(0);
70 #endif
72 TEST(FakeStack, ModuloNumberOfFrames) {
73 EXPECT_EQ(FakeStack::ModuloNumberOfFrames(15, 0, 0), 0U);
74 EXPECT_EQ(FakeStack::ModuloNumberOfFrames(15, 0, (1<<15)), 0U);
75 EXPECT_EQ(FakeStack::ModuloNumberOfFrames(15, 0, (1<<10)), 0U);
76 EXPECT_EQ(FakeStack::ModuloNumberOfFrames(15, 0, (1<<9)), 0U);
77 EXPECT_EQ(FakeStack::ModuloNumberOfFrames(15, 0, (1<<8)), 1U<<8);
78 EXPECT_EQ(FakeStack::ModuloNumberOfFrames(15, 0, (1<<15) + 1), 1U);
80 EXPECT_EQ(FakeStack::ModuloNumberOfFrames(15, 1, 0), 0U);
81 EXPECT_EQ(FakeStack::ModuloNumberOfFrames(15, 1, 1<<9), 0U);
82 EXPECT_EQ(FakeStack::ModuloNumberOfFrames(15, 1, 1<<8), 0U);
83 EXPECT_EQ(FakeStack::ModuloNumberOfFrames(15, 1, 1<<7), 1U<<7);
85 EXPECT_EQ(FakeStack::ModuloNumberOfFrames(15, 5, 0), 0U);
86 EXPECT_EQ(FakeStack::ModuloNumberOfFrames(15, 5, 1), 1U);
87 EXPECT_EQ(FakeStack::ModuloNumberOfFrames(15, 5, 15), 15U);
88 EXPECT_EQ(FakeStack::ModuloNumberOfFrames(15, 5, 16), 0U);
89 EXPECT_EQ(FakeStack::ModuloNumberOfFrames(15, 5, 17), 1U);
92 TEST(FakeStack, GetFrame) {
93 const uptr stack_size_log = 20;
94 const uptr stack_size = 1 << stack_size_log;
95 FakeStack *fs = FakeStack::Create(stack_size_log);
96 u8 *base = fs->GetFrame(stack_size_log, 0, 0);
97 EXPECT_EQ(base, reinterpret_cast<u8 *>(fs) +
98 fs->SizeRequiredForFlags(stack_size_log) + 4096);
99 EXPECT_EQ(base + 0*stack_size + 64 * 7, fs->GetFrame(stack_size_log, 0, 7U));
100 EXPECT_EQ(base + 1*stack_size + 128 * 3, fs->GetFrame(stack_size_log, 1, 3U));
101 EXPECT_EQ(base + 2*stack_size + 256 * 5, fs->GetFrame(stack_size_log, 2, 5U));
102 fs->Destroy(0);
105 TEST(FakeStack, Allocate) {
106 const uptr stack_size_log = 19;
107 FakeStack *fs = FakeStack::Create(stack_size_log);
108 std::map<FakeFrame *, uptr> s;
109 for (int iter = 0; iter < 2; iter++) {
110 s.clear();
111 for (uptr cid = 0; cid < FakeStack::kNumberOfSizeClasses; cid++) {
112 uptr n = FakeStack::NumberOfFrames(stack_size_log, cid);
113 uptr bytes_in_class = FakeStack::BytesInSizeClass(cid);
114 for (uptr j = 0; j < n; j++) {
115 FakeFrame *ff = fs->Allocate(stack_size_log, cid, 0);
116 uptr x = reinterpret_cast<uptr>(ff);
117 EXPECT_TRUE(s.insert(std::make_pair(ff, cid)).second);
118 EXPECT_EQ(x, fs->AddrIsInFakeStack(x));
119 EXPECT_EQ(x, fs->AddrIsInFakeStack(x + 1));
120 EXPECT_EQ(x, fs->AddrIsInFakeStack(x + bytes_in_class - 1));
121 EXPECT_NE(x, fs->AddrIsInFakeStack(x + bytes_in_class));
123 // We are out of fake stack, so Allocate should return 0.
124 EXPECT_EQ(0UL, fs->Allocate(stack_size_log, cid, 0));
126 for (std::map<FakeFrame *, uptr>::iterator it = s.begin(); it != s.end();
127 ++it) {
128 fs->Deallocate(reinterpret_cast<uptr>(it->first), it->second);
131 fs->Destroy(0);
134 static void RecursiveFunction(FakeStack *fs, int depth) {
135 uptr class_id = depth / 3;
136 FakeFrame *ff = fs->Allocate(fs->stack_size_log(), class_id, 0);
137 if (depth) {
138 RecursiveFunction(fs, depth - 1);
139 RecursiveFunction(fs, depth - 1);
141 fs->Deallocate(reinterpret_cast<uptr>(ff), class_id);
144 TEST(FakeStack, RecursiveStressTest) {
145 const uptr stack_size_log = 16;
146 FakeStack *fs = FakeStack::Create(stack_size_log);
147 RecursiveFunction(fs, 22); // with 26 runs for 2-3 seconds.
148 fs->Destroy(0);
151 } // namespace __asan