xref: /openbsd-src/gnu/llvm/compiler-rt/lib/sanitizer_common/tests/sanitizer_chained_origin_depot_test.cpp (revision 810390e339a5425391477d5d41c78d7cab2424ac)
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 
20 namespace __sanitizer {
21 
22 static ChainedOriginDepot chainedOriginDepot;
23 
TEST(SanitizerCommon,ChainedOriginDepotBasic)24 TEST(SanitizerCommon, ChainedOriginDepotBasic) {
25   u32 new_id;
26   EXPECT_TRUE(chainedOriginDepot.Put(1, 2, &new_id));
27   u32 prev_id;
28   EXPECT_EQ(chainedOriginDepot.Get(new_id, &prev_id), 1U);
29   EXPECT_EQ(prev_id, 2U);
30 }
31 
TEST(SanitizerCommon,ChainedOriginDepotAbsent)32 TEST(SanitizerCommon, ChainedOriginDepotAbsent) {
33   u32 prev_id;
34   EXPECT_EQ(0U, chainedOriginDepot.Get(99, &prev_id));
35   EXPECT_EQ(0U, prev_id);
36 }
37 
TEST(SanitizerCommon,ChainedOriginDepotZeroId)38 TEST(SanitizerCommon, ChainedOriginDepotZeroId) {
39   u32 prev_id;
40   EXPECT_EQ(0U, chainedOriginDepot.Get(0, &prev_id));
41   EXPECT_EQ(0U, prev_id);
42 }
43 
TEST(SanitizerCommon,ChainedOriginDepotSame)44 TEST(SanitizerCommon, ChainedOriginDepotSame) {
45   u32 new_id1;
46   EXPECT_TRUE(chainedOriginDepot.Put(11, 12, &new_id1));
47   u32 new_id2;
48   EXPECT_FALSE(chainedOriginDepot.Put(11, 12, &new_id2));
49   EXPECT_EQ(new_id1, new_id2);
50 
51   u32 prev_id;
52   EXPECT_EQ(chainedOriginDepot.Get(new_id1, &prev_id), 11U);
53   EXPECT_EQ(prev_id, 12U);
54 }
55 
TEST(SanitizerCommon,ChainedOriginDepotDifferent)56 TEST(SanitizerCommon, ChainedOriginDepotDifferent) {
57   u32 new_id1;
58   EXPECT_TRUE(chainedOriginDepot.Put(21, 22, &new_id1));
59   u32 new_id2;
60   EXPECT_TRUE(chainedOriginDepot.Put(21, 23, &new_id2));
61   EXPECT_NE(new_id1, new_id2);
62 
63   u32 prev_id;
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);
68 }
69 
TEST(SanitizerCommon,ChainedOriginDepotStats)70 TEST(SanitizerCommon, ChainedOriginDepotStats) {
71   chainedOriginDepot.TestOnlyUnmap();
72   StackDepotStats stats0 = chainedOriginDepot.GetStats();
73 
74   u32 new_id;
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);
79 
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);
84 
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);
89   }
90   EXPECT_GT(chainedOriginDepot.GetStats().allocated, stats2.allocated);
91 }
92 
93 }  // namespace __sanitizer
94