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
18namespace __sanitizer {
19
20TEST(SanitizerCommon, StackDepotBasic) {
21  uptr s1[] = {1, 2, 3, 4, 5};
22  u32 i1 = StackDepotPut(s1, ARRAY_SIZE(s1));
23  uptr sz1 = 0;
24  const uptr *sp1 = StackDepotGet(i1, &sz1);
25  EXPECT_NE(sp1, (uptr*)0);
26  EXPECT_EQ(sz1, ARRAY_SIZE(s1));
27  EXPECT_EQ(internal_memcmp(sp1, s1, sizeof(s1)), 0);
28}
29
30TEST(SanitizerCommon, StackDepotAbsent) {
31  uptr sz1 = 0;
32  const uptr *sp1 = StackDepotGet((1 << 30) - 1, &sz1);
33  EXPECT_EQ(sp1, (uptr*)0);
34}
35
36TEST(SanitizerCommon, StackDepotEmptyStack) {
37  u32 i1 = StackDepotPut(0, 0);
38  uptr sz1 = 0;
39  const uptr *sp1 = StackDepotGet(i1, &sz1);
40  EXPECT_EQ(sp1, (uptr*)0);
41}
42
43TEST(SanitizerCommon, StackDepotZeroId) {
44  uptr sz1 = 0;
45  const uptr *sp1 = StackDepotGet(0, &sz1);
46  EXPECT_EQ(sp1, (uptr*)0);
47}
48
49TEST(SanitizerCommon, StackDepotSame) {
50  uptr s1[] = {1, 2, 3, 4, 6};
51  u32 i1 = StackDepotPut(s1, ARRAY_SIZE(s1));
52  u32 i2 = StackDepotPut(s1, ARRAY_SIZE(s1));
53  EXPECT_EQ(i1, i2);
54  uptr sz1 = 0;
55  const uptr *sp1 = StackDepotGet(i1, &sz1);
56  EXPECT_NE(sp1, (uptr*)0);
57  EXPECT_EQ(sz1, ARRAY_SIZE(s1));
58  EXPECT_EQ(internal_memcmp(sp1, s1, sizeof(s1)), 0);
59}
60
61TEST(SanitizerCommon, StackDepotSeveral) {
62  uptr s1[] = {1, 2, 3, 4, 7};
63  u32 i1 = StackDepotPut(s1, ARRAY_SIZE(s1));
64  uptr s2[] = {1, 2, 3, 4, 8, 9};
65  u32 i2 = StackDepotPut(s2, ARRAY_SIZE(s2));
66  EXPECT_NE(i1, i2);
67}
68
69TEST(SanitizerCommon, StackDepotReverseMap) {
70  uptr s1[] = {1, 2, 3, 4, 5};
71  uptr s2[] = {7, 1, 3, 0};
72  uptr s3[] = {10, 2, 5, 3};
73  uptr s4[] = {1, 3, 2, 5};
74  u32 ids[4] = {0};
75  ids[0] = StackDepotPut(s1, ARRAY_SIZE(s1));
76  ids[1] = StackDepotPut(s2, ARRAY_SIZE(s2));
77  ids[2] = StackDepotPut(s3, ARRAY_SIZE(s3));
78  ids[3] = StackDepotPut(s4, ARRAY_SIZE(s4));
79
80  StackDepotReverseMap map;
81
82  for (uptr i = 0; i < 4; i++) {
83    uptr sz_depot, sz_map;
84    const uptr *sp_depot, *sp_map;
85    sp_depot = StackDepotGet(ids[i], &sz_depot);
86    sp_map = map.Get(ids[i], &sz_map);
87    EXPECT_EQ(sz_depot, sz_map);
88    EXPECT_EQ(sp_depot, sp_map);
89  }
90}
91
92}  // namespace __sanitizer
93