1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
| //===-- tsan_stack_test.cpp -----------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file is a part of ThreadSanitizer (TSan), a race detector.
//
//===----------------------------------------------------------------------===//
#include "tsan_sync.h"
#include "tsan_rtl.h"
#include "gtest/gtest.h"
#include <string.h>
namespace __tsan {
template <typename StackTraceTy>
static void TestStackTrace(StackTraceTy *trace) {
ThreadState thr(0, 0, 0, 0, 0, 0, 0, 0, 0);
uptr stack[128];
thr.shadow_stack = &stack[0];
thr.shadow_stack_pos = &stack[0];
thr.shadow_stack_end = &stack[128];
ObtainCurrentStack(&thr, 0, trace);
EXPECT_EQ(0U, trace->size);
ObtainCurrentStack(&thr, 42, trace);
EXPECT_EQ(1U, trace->size);
EXPECT_EQ(42U, trace->trace[0]);
*thr.shadow_stack_pos++ = 100;
*thr.shadow_stack_pos++ = 101;
ObtainCurrentStack(&thr, 0, trace);
EXPECT_EQ(2U, trace->size);
EXPECT_EQ(100U, trace->trace[0]);
EXPECT_EQ(101U, trace->trace[1]);
ObtainCurrentStack(&thr, 42, trace);
EXPECT_EQ(3U, trace->size);
EXPECT_EQ(100U, trace->trace[0]);
EXPECT_EQ(101U, trace->trace[1]);
EXPECT_EQ(42U, trace->trace[2]);
}
template<typename StackTraceTy>
static void TestTrim(StackTraceTy *trace) {
ThreadState thr(0, 0, 0, 0, 0, 0, 0, 0, 0);
const uptr kShadowStackSize = 2 * kStackTraceMax;
uptr stack[kShadowStackSize];
thr.shadow_stack = &stack[0];
thr.shadow_stack_pos = &stack[0];
thr.shadow_stack_end = &stack[kShadowStackSize];
for (uptr i = 0; i < kShadowStackSize; ++i)
*thr.shadow_stack_pos++ = 100 + i;
ObtainCurrentStack(&thr, 0, trace);
EXPECT_EQ(kStackTraceMax, trace->size);
for (uptr i = 0; i < kStackTraceMax; i++) {
EXPECT_EQ(100 + kStackTraceMax + i, trace->trace[i]);
}
ObtainCurrentStack(&thr, 42, trace);
EXPECT_EQ(kStackTraceMax, trace->size);
for (uptr i = 0; i < kStackTraceMax - 1; i++) {
EXPECT_EQ(101 + kStackTraceMax + i, trace->trace[i]);
}
EXPECT_EQ(42U, trace->trace[kStackTraceMax - 1]);
}
TEST(StackTrace, BasicVarSize) {
VarSizeStackTrace trace;
TestStackTrace(&trace);
}
TEST(StackTrace, BasicBuffered) {
BufferedStackTrace trace;
TestStackTrace(&trace);
}
TEST(StackTrace, TrimVarSize) {
VarSizeStackTrace trace;
TestTrim(&trace);
}
TEST(StackTrace, TrimBuffered) {
BufferedStackTrace trace;
TestTrim(&trace);
}
} // namespace __tsan
|