1//===-- sanitizer_chained_origin_depot_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 Sanitizer runtime.
10// Tests for sanitizer_chained_origin_depot.h.
11//
12//===----------------------------------------------------------------------===//
13
14#include "sanitizer_common/sanitizer_chained_origin_depot.h"
15
16#include "gtest/gtest.h"
17#include "sanitizer_common/sanitizer_internal_defs.h"
18#include "sanitizer_common/sanitizer_libc.h"
19
20namespace __sanitizer {
21
22static ChainedOriginDepot chainedOriginDepot;
23
24TEST(SanitizerCommon, ChainedOriginDepotBasic) {
25 u32 new_id;
26 EXPECT_TRUE(chainedOriginDepot.Put(here_id: 1, prev_id: 2, new_id: &new_id));
27 u32 prev_id;
28 EXPECT_EQ(chainedOriginDepot.Get(id: new_id, other: &prev_id), 1U);
29 EXPECT_EQ(prev_id, 2U);
30}
31
32TEST(SanitizerCommon, ChainedOriginDepotAbsent) {
33 u32 prev_id;
34 EXPECT_EQ(0U, chainedOriginDepot.Get(id: 99, other: &prev_id));
35 EXPECT_EQ(0U, prev_id);
36}
37
38TEST(SanitizerCommon, ChainedOriginDepotZeroId) {
39 u32 prev_id;
40 EXPECT_EQ(0U, chainedOriginDepot.Get(id: 0, other: &prev_id));
41 EXPECT_EQ(0U, prev_id);
42}
43
44TEST(SanitizerCommon, ChainedOriginDepotSame) {
45 u32 new_id1;
46 EXPECT_TRUE(chainedOriginDepot.Put(here_id: 11, prev_id: 12, new_id: &new_id1));
47 u32 new_id2;
48 EXPECT_FALSE(chainedOriginDepot.Put(here_id: 11, prev_id: 12, new_id: &new_id2));
49 EXPECT_EQ(new_id1, new_id2);
50
51 u32 prev_id;
52 EXPECT_EQ(chainedOriginDepot.Get(id: new_id1, other: &prev_id), 11U);
53 EXPECT_EQ(prev_id, 12U);
54}
55
56TEST(SanitizerCommon, ChainedOriginDepotDifferent) {
57 u32 new_id1;
58 EXPECT_TRUE(chainedOriginDepot.Put(here_id: 21, prev_id: 22, new_id: &new_id1));
59 u32 new_id2;
60 EXPECT_TRUE(chainedOriginDepot.Put(here_id: 21, prev_id: 23, new_id: &new_id2));
61 EXPECT_NE(new_id1, new_id2);
62
63 u32 prev_id;
64 EXPECT_EQ(chainedOriginDepot.Get(id: new_id1, other: &prev_id), 21U);
65 EXPECT_EQ(prev_id, 22U);
66 EXPECT_EQ(chainedOriginDepot.Get(id: new_id2, other: &prev_id), 21U);
67 EXPECT_EQ(prev_id, 23U);
68}
69
70TEST(SanitizerCommon, ChainedOriginDepotStats) {
71 chainedOriginDepot.TestOnlyUnmap();
72 StackDepotStats stats0 = chainedOriginDepot.GetStats();
73
74 u32 new_id;
75 EXPECT_TRUE(chainedOriginDepot.Put(here_id: 33, prev_id: 34, new_id: &new_id));
76 StackDepotStats stats1 = chainedOriginDepot.GetStats();
77 EXPECT_EQ(stats1.n_uniq_ids, stats0.n_uniq_ids + 1);
78 EXPECT_GT(stats1.allocated, stats0.allocated);
79
80 EXPECT_FALSE(chainedOriginDepot.Put(here_id: 33, prev_id: 34, new_id: &new_id));
81 StackDepotStats stats2 = chainedOriginDepot.GetStats();
82 EXPECT_EQ(stats2.n_uniq_ids, stats1.n_uniq_ids);
83 EXPECT_EQ(stats2.allocated, stats1.allocated);
84
85 for (int i = 0; i < 100000; ++i) {
86 ASSERT_TRUE(chainedOriginDepot.Put(here_id: 35, prev_id: i, new_id: &new_id));
87 StackDepotStats stats3 = chainedOriginDepot.GetStats();
88 ASSERT_EQ(stats3.n_uniq_ids, stats2.n_uniq_ids + 1 + i);
89 }
90 EXPECT_GT(chainedOriginDepot.GetStats().allocated, stats2.allocated);
91}
92
93} // namespace __sanitizer
94

source code of compiler-rt/lib/sanitizer_common/tests/sanitizer_chained_origin_depot_test.cpp