1 //===-- sanitizer_chained_origin_depot_test.cpp ---------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is a part of Sanitizer runtime.
10 // Tests for sanitizer_chained_origin_depot.h.
12 //===----------------------------------------------------------------------===//
14 #include "sanitizer_common/sanitizer_chained_origin_depot.h"
16 #include "gtest/gtest.h"
17 #include "sanitizer_common/sanitizer_internal_defs.h"
18 #include "sanitizer_common/sanitizer_libc.h"
20 namespace __sanitizer
{
22 static ChainedOriginDepot chainedOriginDepot
;
24 TEST(SanitizerCommon
, ChainedOriginDepotBasic
) {
26 EXPECT_TRUE(chainedOriginDepot
.Put(1, 2, &new_id
));
28 EXPECT_EQ(chainedOriginDepot
.Get(new_id
, &prev_id
), 1U);
29 EXPECT_EQ(prev_id
, 2U);
32 TEST(SanitizerCommon
, ChainedOriginDepotAbsent
) {
34 EXPECT_EQ(0U, chainedOriginDepot
.Get(123456, &prev_id
));
35 EXPECT_EQ(0U, prev_id
);
38 TEST(SanitizerCommon
, ChainedOriginDepotZeroId
) {
40 EXPECT_EQ(0U, chainedOriginDepot
.Get(0, &prev_id
));
41 EXPECT_EQ(0U, prev_id
);
44 TEST(SanitizerCommon
, ChainedOriginDepotSame
) {
46 EXPECT_TRUE(chainedOriginDepot
.Put(11, 12, &new_id1
));
48 EXPECT_FALSE(chainedOriginDepot
.Put(11, 12, &new_id2
));
49 EXPECT_EQ(new_id1
, new_id2
);
52 EXPECT_EQ(chainedOriginDepot
.Get(new_id1
, &prev_id
), 11U);
53 EXPECT_EQ(prev_id
, 12U);
56 TEST(SanitizerCommon
, ChainedOriginDepotDifferent
) {
58 EXPECT_TRUE(chainedOriginDepot
.Put(21, 22, &new_id1
));
60 EXPECT_TRUE(chainedOriginDepot
.Put(21, 23, &new_id2
));
61 EXPECT_NE(new_id1
, new_id2
);
64 EXPECT_EQ(chainedOriginDepot
.Get(new_id1
, &prev_id
), 21U);
65 EXPECT_EQ(prev_id
, 22U);
66 EXPECT_EQ(chainedOriginDepot
.Get(new_id2
, &prev_id
), 21U);
67 EXPECT_EQ(prev_id
, 23U);
70 TEST(SanitizerCommon
, ChainedOriginDepotStats
) {
71 chainedOriginDepot
.TestOnlyUnmap();
72 StackDepotStats stats0
= chainedOriginDepot
.GetStats();
75 EXPECT_TRUE(chainedOriginDepot
.Put(33, 34, &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
);
80 EXPECT_FALSE(chainedOriginDepot
.Put(33, 34, &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
);
85 for (int i
= 0; i
< 100000; ++i
) {
86 ASSERT_TRUE(chainedOriginDepot
.Put(35, i
, &new_id
));
87 StackDepotStats stats3
= chainedOriginDepot
.GetStats();
88 ASSERT_EQ(stats3
.n_uniq_ids
, stats2
.n_uniq_ids
+ 1 + i
);
90 EXPECT_GT(chainedOriginDepot
.GetStats().allocated
, stats2
.allocated
);
93 } // namespace __sanitizer