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