1 //===-- sanitizer_stackdepot_test.cc --------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of ThreadSanitizer/AddressSanitizer runtime.
11 //
12 //===----------------------------------------------------------------------===//
13 #include "sanitizer_common/sanitizer_stackdepot.h"
14 #include "sanitizer_common/sanitizer_internal_defs.h"
15 #include "sanitizer_common/sanitizer_libc.h"
16 #include "gtest/gtest.h"
17
18 namespace __sanitizer {
19
TEST(SanitizerCommon,StackDepotBasic)20 TEST(SanitizerCommon, StackDepotBasic) {
21 uptr array[] = {1, 2, 3, 4, 5};
22 StackTrace s1(array, ARRAY_SIZE(array));
23 u32 i1 = StackDepotPut(s1);
24 StackTrace stack = StackDepotGet(i1);
25 EXPECT_NE(stack.trace, (uptr*)0);
26 EXPECT_EQ(ARRAY_SIZE(array), stack.size);
27 EXPECT_EQ(0, internal_memcmp(stack.trace, array, sizeof(array)));
28 }
29
TEST(SanitizerCommon,StackDepotAbsent)30 TEST(SanitizerCommon, StackDepotAbsent) {
31 StackTrace stack = StackDepotGet((1 << 30) - 1);
32 EXPECT_EQ((uptr*)0, stack.trace);
33 }
34
TEST(SanitizerCommon,StackDepotEmptyStack)35 TEST(SanitizerCommon, StackDepotEmptyStack) {
36 u32 i1 = StackDepotPut(StackTrace());
37 StackTrace stack = StackDepotGet(i1);
38 EXPECT_EQ((uptr*)0, stack.trace);
39 }
40
TEST(SanitizerCommon,StackDepotZeroId)41 TEST(SanitizerCommon, StackDepotZeroId) {
42 StackTrace stack = StackDepotGet(0);
43 EXPECT_EQ((uptr*)0, stack.trace);
44 }
45
TEST(SanitizerCommon,StackDepotSame)46 TEST(SanitizerCommon, StackDepotSame) {
47 uptr array[] = {1, 2, 3, 4, 6};
48 StackTrace s1(array, ARRAY_SIZE(array));
49 u32 i1 = StackDepotPut(s1);
50 u32 i2 = StackDepotPut(s1);
51 EXPECT_EQ(i1, i2);
52 StackTrace stack = StackDepotGet(i1);
53 EXPECT_NE(stack.trace, (uptr*)0);
54 EXPECT_EQ(ARRAY_SIZE(array), stack.size);
55 EXPECT_EQ(0, internal_memcmp(stack.trace, array, sizeof(array)));
56 }
57
TEST(SanitizerCommon,StackDepotSeveral)58 TEST(SanitizerCommon, StackDepotSeveral) {
59 uptr array1[] = {1, 2, 3, 4, 7};
60 StackTrace s1(array1, ARRAY_SIZE(array1));
61 u32 i1 = StackDepotPut(s1);
62 uptr array2[] = {1, 2, 3, 4, 8, 9};
63 StackTrace s2(array2, ARRAY_SIZE(array2));
64 u32 i2 = StackDepotPut(s2);
65 EXPECT_NE(i1, i2);
66 }
67
TEST(SanitizerCommon,StackDepotReverseMap)68 TEST(SanitizerCommon, StackDepotReverseMap) {
69 uptr array1[] = {1, 2, 3, 4, 5};
70 uptr array2[] = {7, 1, 3, 0};
71 uptr array3[] = {10, 2, 5, 3};
72 uptr array4[] = {1, 3, 2, 5};
73 u32 ids[4] = {0};
74 StackTrace s1(array1, ARRAY_SIZE(array1));
75 StackTrace s2(array2, ARRAY_SIZE(array2));
76 StackTrace s3(array3, ARRAY_SIZE(array3));
77 StackTrace s4(array4, ARRAY_SIZE(array4));
78 ids[0] = StackDepotPut(s1);
79 ids[1] = StackDepotPut(s2);
80 ids[2] = StackDepotPut(s3);
81 ids[3] = StackDepotPut(s4);
82
83 StackDepotReverseMap map;
84
85 for (uptr i = 0; i < 4; i++) {
86 StackTrace stack = StackDepotGet(ids[i]);
87 StackTrace from_map = map.Get(ids[i]);
88 EXPECT_EQ(stack.size, from_map.size);
89 EXPECT_EQ(stack.trace, from_map.trace);
90 }
91 }
92
93 } // namespace __sanitizer
94