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.
10//
11// Tests for FakeStack.
12// This test file should be compiled w/o asan instrumentation.
13//===----------------------------------------------------------------------===//
14
15#include "asan_fake_stack.h"
16#include "asan_test_utils.h"
17#include "sanitizer_common/sanitizer_common.h"
18
19#include <assert.h>
20#include <stdlib.h>
21#include <stdio.h>
22
23#include <map>
24
25namespace __asan {
26
27TEST(FakeStack, FlagsSize) {
28 EXPECT_EQ(FakeStack::SizeRequiredForFlags(stack_size_log: 10), 1U << 5);
29 EXPECT_EQ(FakeStack::SizeRequiredForFlags(stack_size_log: 11), 1U << 6);
30 EXPECT_EQ(FakeStack::SizeRequiredForFlags(stack_size_log: 20), 1U << 15);
31}
32
33TEST(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(stack_size_log: 15), 365568U);
39 EXPECT_EQ(FakeStack::RequiredSize(stack_size_log: 16), 727040U);
40 EXPECT_EQ(FakeStack::RequiredSize(stack_size_log: 17), 1449984U);
41 EXPECT_EQ(FakeStack::RequiredSize(stack_size_log: 18), 2895872U);
42 EXPECT_EQ(FakeStack::RequiredSize(stack_size_log: 19), 5787648U);
43}
44
45TEST(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;
57 }
58 }
59}
60
61#if !defined(_WIN32) // FIXME: Fails due to OOM on Windows.
62TEST(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(tid: 0);
67 }
68 }
69}
70#endif
71
72TEST(FakeStack, ModuloNumberOfFrames) {
73 EXPECT_EQ(FakeStack::ModuloNumberOfFrames(stack_size_log: 15, class_id: 0, n: 0), 0U);
74 EXPECT_EQ(FakeStack::ModuloNumberOfFrames(stack_size_log: 15, class_id: 0, n: (1<<15)), 0U);
75 EXPECT_EQ(FakeStack::ModuloNumberOfFrames(stack_size_log: 15, class_id: 0, n: (1<<10)), 0U);
76 EXPECT_EQ(FakeStack::ModuloNumberOfFrames(stack_size_log: 15, class_id: 0, n: (1<<9)), 0U);
77 EXPECT_EQ(FakeStack::ModuloNumberOfFrames(stack_size_log: 15, class_id: 0, n: (1<<8)), 1U<<8);
78 EXPECT_EQ(FakeStack::ModuloNumberOfFrames(stack_size_log: 15, class_id: 0, n: (1<<15) + 1), 1U);
79
80 EXPECT_EQ(FakeStack::ModuloNumberOfFrames(stack_size_log: 15, class_id: 1, n: 0), 0U);
81 EXPECT_EQ(FakeStack::ModuloNumberOfFrames(stack_size_log: 15, class_id: 1, n: 1<<9), 0U);
82 EXPECT_EQ(FakeStack::ModuloNumberOfFrames(stack_size_log: 15, class_id: 1, n: 1<<8), 0U);
83 EXPECT_EQ(FakeStack::ModuloNumberOfFrames(stack_size_log: 15, class_id: 1, n: 1<<7), 1U<<7);
84
85 EXPECT_EQ(FakeStack::ModuloNumberOfFrames(stack_size_log: 15, class_id: 5, n: 0), 0U);
86 EXPECT_EQ(FakeStack::ModuloNumberOfFrames(stack_size_log: 15, class_id: 5, n: 1), 1U);
87 EXPECT_EQ(FakeStack::ModuloNumberOfFrames(stack_size_log: 15, class_id: 5, n: 15), 15U);
88 EXPECT_EQ(FakeStack::ModuloNumberOfFrames(stack_size_log: 15, class_id: 5, n: 16), 0U);
89 EXPECT_EQ(FakeStack::ModuloNumberOfFrames(stack_size_log: 15, class_id: 5, n: 17), 1U);
90}
91
92TEST(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, class_id: 0, pos: 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, class_id: 0, pos: 7U));
100 EXPECT_EQ(base + 1*stack_size + 128 * 3, fs->GetFrame(stack_size_log, class_id: 1, pos: 3U));
101 EXPECT_EQ(base + 2*stack_size + 256 * 5, fs->GetFrame(stack_size_log, class_id: 2, pos: 5U));
102 fs->Destroy(tid: 0);
103}
104
105TEST(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, class_id: cid);
113 uptr bytes_in_class = FakeStack::BytesInSizeClass(class_id: cid);
114 for (uptr j = 0; j < n; j++) {
115 FakeFrame *ff = fs->Allocate(stack_size_log, class_id: cid, real_stack: 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(addr: x));
119 EXPECT_EQ(x, fs->AddrIsInFakeStack(addr: x + 1));
120 EXPECT_EQ(x, fs->AddrIsInFakeStack(addr: x + bytes_in_class - 1));
121 EXPECT_NE(x, fs->AddrIsInFakeStack(addr: x + bytes_in_class));
122 }
123 // We are out of fake stack, so Allocate should return 0.
124 EXPECT_EQ(0UL, fs->Allocate(stack_size_log, class_id: cid, real_stack: 0));
125 }
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);
129 }
130 }
131 fs->Destroy(tid: 0);
132}
133
134static void RecursiveFunction(FakeStack *fs, int depth) {
135 uptr class_id = depth / 3;
136 FakeFrame *ff = fs->Allocate(stack_size_log: fs->stack_size_log(), class_id, real_stack: 0);
137 if (depth) {
138 RecursiveFunction(fs, depth: depth - 1);
139 RecursiveFunction(fs, depth: depth - 1);
140 }
141 fs->Deallocate(x: reinterpret_cast<uptr>(ff), class_id);
142}
143
144TEST(FakeStack, RecursiveStressTest) {
145 const uptr stack_size_log = 16;
146 FakeStack *fs = FakeStack::Create(stack_size_log);
147 RecursiveFunction(fs, depth: 22); // with 26 runs for 2-3 seconds.
148 fs->Destroy(tid: 0);
149}
150
151} // namespace __asan
152

source code of compiler-rt/lib/asan/tests/asan_fake_stack_test.cpp