1 //===-- sanitizer_symbolizer_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 // Tests for sanitizer_symbolizer.h and sanitizer_symbolizer_internal.h
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "sanitizer_common/sanitizer_allocator_internal.h"
14 #include "sanitizer_common/sanitizer_symbolizer_internal.h"
15 #include "gtest/gtest.h"
16
17 namespace __sanitizer {
18
TEST(Symbolizer,ExtractToken)19 TEST(Symbolizer, ExtractToken) {
20 char *token;
21 const char *rest;
22
23 rest = ExtractToken("a;b;c", ";", &token);
24 EXPECT_STREQ("a", token);
25 EXPECT_STREQ("b;c", rest);
26 InternalFree(token);
27
28 rest = ExtractToken("aaa-bbb.ccc", ";.-*", &token);
29 EXPECT_STREQ("aaa", token);
30 EXPECT_STREQ("bbb.ccc", rest);
31 InternalFree(token);
32 }
33
TEST(Symbolizer,ExtractInt)34 TEST(Symbolizer, ExtractInt) {
35 int token;
36 const char *rest = ExtractInt("123,456;789", ";,", &token);
37 EXPECT_EQ(123, token);
38 EXPECT_STREQ("456;789", rest);
39 }
40
TEST(Symbolizer,ExtractUptr)41 TEST(Symbolizer, ExtractUptr) {
42 uptr token;
43 const char *rest = ExtractUptr("123,456;789", ";,", &token);
44 EXPECT_EQ(123U, token);
45 EXPECT_STREQ("456;789", rest);
46 }
47
TEST(Symbolizer,ExtractTokenUpToDelimiter)48 TEST(Symbolizer, ExtractTokenUpToDelimiter) {
49 char *token;
50 const char *rest =
51 ExtractTokenUpToDelimiter("aaa-+-bbb-+-ccc", "-+-", &token);
52 EXPECT_STREQ("aaa", token);
53 EXPECT_STREQ("bbb-+-ccc", rest);
54 InternalFree(token);
55 }
56
57 #if !SANITIZER_WINDOWS
TEST(Symbolizer,DemangleSwiftAndCXX)58 TEST(Symbolizer, DemangleSwiftAndCXX) {
59 // Swift names are not demangled in default llvm build because Swift
60 // runtime is not linked in.
61 EXPECT_STREQ(nullptr, DemangleSwiftAndCXX("_TtSd"));
62 // Check that the rest demangles properly.
63 EXPECT_STREQ("f1(char*, int)", DemangleSwiftAndCXX("_Z2f1Pci"));
64 #if !SANITIZER_FREEBSD // QoI issue with libcxxrt on FreeBSD
65 EXPECT_STREQ(nullptr, DemangleSwiftAndCXX("foo"));
66 #endif
67 EXPECT_STREQ(nullptr, DemangleSwiftAndCXX(""));
68 }
69 #endif
70
71 } // namespace __sanitizer
72