xref: /llvm-project/lldb/unittests/Symbol/JSONSymbolTest.cpp (revision cf3524a5746f9498280b3a9180b75575c0065d1a)
1*cf3524a5SJonas Devlieghere //===-- JSONSymbolTest.cpp ------------------------------------------------===//
2*cf3524a5SJonas Devlieghere //
3*cf3524a5SJonas Devlieghere // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*cf3524a5SJonas Devlieghere // See https://llvm.org/LICENSE.txt for license information.
5*cf3524a5SJonas Devlieghere // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*cf3524a5SJonas Devlieghere //
7*cf3524a5SJonas Devlieghere //===----------------------------------------------------------------------===//
8*cf3524a5SJonas Devlieghere 
9*cf3524a5SJonas Devlieghere #include "lldb/Core/Section.h"
10*cf3524a5SJonas Devlieghere #include "lldb/Symbol/Symbol.h"
11*cf3524a5SJonas Devlieghere #include "llvm/Testing/Support/Error.h"
12*cf3524a5SJonas Devlieghere 
13*cf3524a5SJonas Devlieghere #include "gtest/gtest.h"
14*cf3524a5SJonas Devlieghere 
15*cf3524a5SJonas Devlieghere using namespace lldb;
16*cf3524a5SJonas Devlieghere using namespace llvm;
17*cf3524a5SJonas Devlieghere using namespace lldb_private;
18*cf3524a5SJonas Devlieghere 
19*cf3524a5SJonas Devlieghere static std::string g_error_no_section_list = "no section list provided";
20*cf3524a5SJonas Devlieghere static std::string g_error_both_value_and_address =
21*cf3524a5SJonas Devlieghere     "symbol cannot contain both a value and an address";
22*cf3524a5SJonas Devlieghere static std::string g_error_neither_value_or_address =
23*cf3524a5SJonas Devlieghere     "symbol must contain either a value or an address";
24*cf3524a5SJonas Devlieghere 
TEST(JSONSymbolTest,DeserializeCodeAddress)25*cf3524a5SJonas Devlieghere TEST(JSONSymbolTest, DeserializeCodeAddress) {
26*cf3524a5SJonas Devlieghere   std::string text = R"(
27*cf3524a5SJonas Devlieghere {
28*cf3524a5SJonas Devlieghere   "name": "foo",
29*cf3524a5SJonas Devlieghere   "type": "code",
30*cf3524a5SJonas Devlieghere   "size": 32,
31*cf3524a5SJonas Devlieghere   "address": 4096
32*cf3524a5SJonas Devlieghere })";
33*cf3524a5SJonas Devlieghere 
34*cf3524a5SJonas Devlieghere   Expected<json::Value> json = json::parse(text);
35*cf3524a5SJonas Devlieghere   ASSERT_TRUE(static_cast<bool>(json));
36*cf3524a5SJonas Devlieghere 
37*cf3524a5SJonas Devlieghere   json::Path::Root root;
38*cf3524a5SJonas Devlieghere   JSONSymbol json_symbol;
39*cf3524a5SJonas Devlieghere   ASSERT_TRUE(fromJSON(*json, json_symbol, root));
40*cf3524a5SJonas Devlieghere 
41*cf3524a5SJonas Devlieghere   SectionSP sect_sp(new Section(
42*cf3524a5SJonas Devlieghere       /*module_sp=*/ModuleSP(),
43*cf3524a5SJonas Devlieghere       /*obj_file=*/nullptr,
44*cf3524a5SJonas Devlieghere       /*sect_id=*/1,
45*cf3524a5SJonas Devlieghere       /*name=*/ConstString(".text"),
46*cf3524a5SJonas Devlieghere       /*sect_type=*/eSectionTypeCode,
47*cf3524a5SJonas Devlieghere       /*file_vm_addr=*/0x1000,
48*cf3524a5SJonas Devlieghere       /*vm_size=*/0x1000,
49*cf3524a5SJonas Devlieghere       /*file_offset=*/0,
50*cf3524a5SJonas Devlieghere       /*file_size=*/0,
51*cf3524a5SJonas Devlieghere       /*log2align=*/5,
52*cf3524a5SJonas Devlieghere       /*flags=*/0x10203040));
53*cf3524a5SJonas Devlieghere   SectionList sect_list;
54*cf3524a5SJonas Devlieghere   sect_list.AddSection(sect_sp);
55*cf3524a5SJonas Devlieghere 
56*cf3524a5SJonas Devlieghere   Expected<Symbol> symbol = Symbol::FromJSON(json_symbol, &sect_list);
57*cf3524a5SJonas Devlieghere   EXPECT_THAT_EXPECTED(symbol, llvm::Succeeded());
58*cf3524a5SJonas Devlieghere   EXPECT_EQ(symbol->GetName(), ConstString("foo"));
59*cf3524a5SJonas Devlieghere   EXPECT_EQ(symbol->GetFileAddress(), static_cast<lldb::addr_t>(0x1000));
60*cf3524a5SJonas Devlieghere   EXPECT_EQ(symbol->GetType(), eSymbolTypeCode);
61*cf3524a5SJonas Devlieghere }
62*cf3524a5SJonas Devlieghere 
TEST(JSONSymbolTest,DeserializeCodeValue)63*cf3524a5SJonas Devlieghere TEST(JSONSymbolTest, DeserializeCodeValue) {
64*cf3524a5SJonas Devlieghere   std::string text = R"(
65*cf3524a5SJonas Devlieghere {
66*cf3524a5SJonas Devlieghere   "name": "foo",
67*cf3524a5SJonas Devlieghere   "type": "code",
68*cf3524a5SJonas Devlieghere   "size": 32,
69*cf3524a5SJonas Devlieghere   "value": 4096
70*cf3524a5SJonas Devlieghere })";
71*cf3524a5SJonas Devlieghere 
72*cf3524a5SJonas Devlieghere   Expected<json::Value> json = json::parse(text);
73*cf3524a5SJonas Devlieghere   EXPECT_THAT_EXPECTED(json, llvm::Succeeded());
74*cf3524a5SJonas Devlieghere 
75*cf3524a5SJonas Devlieghere   json::Path::Root root;
76*cf3524a5SJonas Devlieghere   JSONSymbol json_symbol;
77*cf3524a5SJonas Devlieghere   ASSERT_TRUE(fromJSON(*json, json_symbol, root));
78*cf3524a5SJonas Devlieghere 
79*cf3524a5SJonas Devlieghere   SectionList sect_list;
80*cf3524a5SJonas Devlieghere 
81*cf3524a5SJonas Devlieghere   Expected<Symbol> symbol = Symbol::FromJSON(json_symbol, &sect_list);
82*cf3524a5SJonas Devlieghere   EXPECT_THAT_EXPECTED(symbol, llvm::Succeeded());
83*cf3524a5SJonas Devlieghere   EXPECT_EQ(symbol->GetName(), ConstString("foo"));
84*cf3524a5SJonas Devlieghere   EXPECT_EQ(symbol->GetRawValue(), static_cast<lldb::addr_t>(0x1000));
85*cf3524a5SJonas Devlieghere   EXPECT_EQ(symbol->GetType(), eSymbolTypeCode);
86*cf3524a5SJonas Devlieghere }
87*cf3524a5SJonas Devlieghere 
TEST(JSONSymbolTest,JSONInvalidValueAndAddress)88*cf3524a5SJonas Devlieghere TEST(JSONSymbolTest, JSONInvalidValueAndAddress) {
89*cf3524a5SJonas Devlieghere   std::string text = R"(
90*cf3524a5SJonas Devlieghere {
91*cf3524a5SJonas Devlieghere   "name": "foo",
92*cf3524a5SJonas Devlieghere   "type": "code",
93*cf3524a5SJonas Devlieghere   "size": 32,
94*cf3524a5SJonas Devlieghere   "value": 4096,
95*cf3524a5SJonas Devlieghere   "address": 4096
96*cf3524a5SJonas Devlieghere })";
97*cf3524a5SJonas Devlieghere 
98*cf3524a5SJonas Devlieghere   Expected<json::Value> json = json::parse(text);
99*cf3524a5SJonas Devlieghere   EXPECT_THAT_EXPECTED(json, llvm::Succeeded());
100*cf3524a5SJonas Devlieghere 
101*cf3524a5SJonas Devlieghere   json::Path::Root root;
102*cf3524a5SJonas Devlieghere   JSONSymbol json_symbol;
103*cf3524a5SJonas Devlieghere   ASSERT_FALSE(fromJSON(*json, json_symbol, root));
104*cf3524a5SJonas Devlieghere }
105*cf3524a5SJonas Devlieghere 
TEST(JSONSymbolTest,JSONInvalidNoValueOrAddress)106*cf3524a5SJonas Devlieghere TEST(JSONSymbolTest, JSONInvalidNoValueOrAddress) {
107*cf3524a5SJonas Devlieghere   std::string text = R"(
108*cf3524a5SJonas Devlieghere {
109*cf3524a5SJonas Devlieghere   "name": "foo",
110*cf3524a5SJonas Devlieghere   "type": "code",
111*cf3524a5SJonas Devlieghere   "size": 32
112*cf3524a5SJonas Devlieghere })";
113*cf3524a5SJonas Devlieghere 
114*cf3524a5SJonas Devlieghere   Expected<json::Value> json = json::parse(text);
115*cf3524a5SJonas Devlieghere   EXPECT_THAT_EXPECTED(json, llvm::Succeeded());
116*cf3524a5SJonas Devlieghere 
117*cf3524a5SJonas Devlieghere   json::Path::Root root;
118*cf3524a5SJonas Devlieghere   JSONSymbol json_symbol;
119*cf3524a5SJonas Devlieghere   ASSERT_FALSE(fromJSON(*json, json_symbol, root));
120*cf3524a5SJonas Devlieghere }
121*cf3524a5SJonas Devlieghere 
TEST(JSONSymbolTest,JSONInvalidType)122*cf3524a5SJonas Devlieghere TEST(JSONSymbolTest, JSONInvalidType) {
123*cf3524a5SJonas Devlieghere   std::string text = R"(
124*cf3524a5SJonas Devlieghere {
125*cf3524a5SJonas Devlieghere   "name": "foo",
126*cf3524a5SJonas Devlieghere   "type": "bogus",
127*cf3524a5SJonas Devlieghere   "value": 4096,
128*cf3524a5SJonas Devlieghere   "size": 32
129*cf3524a5SJonas Devlieghere })";
130*cf3524a5SJonas Devlieghere 
131*cf3524a5SJonas Devlieghere   Expected<json::Value> json = json::parse(text);
132*cf3524a5SJonas Devlieghere   EXPECT_THAT_EXPECTED(json, llvm::Succeeded());
133*cf3524a5SJonas Devlieghere 
134*cf3524a5SJonas Devlieghere   json::Path::Root root;
135*cf3524a5SJonas Devlieghere   JSONSymbol json_symbol;
136*cf3524a5SJonas Devlieghere   ASSERT_FALSE(fromJSON(*json, json_symbol, root));
137*cf3524a5SJonas Devlieghere }
138*cf3524a5SJonas Devlieghere 
TEST(JSONSymbolTest,SymbolInvalidNoSectionList)139*cf3524a5SJonas Devlieghere TEST(JSONSymbolTest, SymbolInvalidNoSectionList) {
140*cf3524a5SJonas Devlieghere   JSONSymbol json_symbol;
141*cf3524a5SJonas Devlieghere   json_symbol.value = 0x1;
142*cf3524a5SJonas Devlieghere 
143*cf3524a5SJonas Devlieghere   Expected<Symbol> symbol = Symbol::FromJSON(json_symbol, nullptr);
144*cf3524a5SJonas Devlieghere   EXPECT_THAT_EXPECTED(symbol,
145*cf3524a5SJonas Devlieghere                        llvm::FailedWithMessage(g_error_no_section_list));
146*cf3524a5SJonas Devlieghere }
147*cf3524a5SJonas Devlieghere 
TEST(JSONSymbolTest,SymbolInvalidValueAndAddress)148*cf3524a5SJonas Devlieghere TEST(JSONSymbolTest, SymbolInvalidValueAndAddress) {
149*cf3524a5SJonas Devlieghere   JSONSymbol json_symbol;
150*cf3524a5SJonas Devlieghere   json_symbol.value = 0x1;
151*cf3524a5SJonas Devlieghere   json_symbol.address = 0x2;
152*cf3524a5SJonas Devlieghere 
153*cf3524a5SJonas Devlieghere   SectionList sect_list;
154*cf3524a5SJonas Devlieghere 
155*cf3524a5SJonas Devlieghere   Expected<Symbol> symbol = Symbol::FromJSON(json_symbol, &sect_list);
156*cf3524a5SJonas Devlieghere   EXPECT_THAT_EXPECTED(symbol,
157*cf3524a5SJonas Devlieghere                        llvm::FailedWithMessage(g_error_both_value_and_address));
158*cf3524a5SJonas Devlieghere }
159*cf3524a5SJonas Devlieghere 
TEST(JSONSymbolTest,SymbolInvalidNoValueOrAddress)160*cf3524a5SJonas Devlieghere TEST(JSONSymbolTest, SymbolInvalidNoValueOrAddress) {
161*cf3524a5SJonas Devlieghere   JSONSymbol json_symbol;
162*cf3524a5SJonas Devlieghere 
163*cf3524a5SJonas Devlieghere   SectionList sect_list;
164*cf3524a5SJonas Devlieghere 
165*cf3524a5SJonas Devlieghere   Expected<Symbol> symbol = Symbol::FromJSON(json_symbol, &sect_list);
166*cf3524a5SJonas Devlieghere   EXPECT_THAT_EXPECTED(
167*cf3524a5SJonas Devlieghere       symbol, llvm::FailedWithMessage(g_error_neither_value_or_address));
168*cf3524a5SJonas Devlieghere }
169*cf3524a5SJonas Devlieghere 
TEST(JSONSymbolTest,SymbolInvalidAddressNotInSection)170*cf3524a5SJonas Devlieghere TEST(JSONSymbolTest, SymbolInvalidAddressNotInSection) {
171*cf3524a5SJonas Devlieghere   JSONSymbol json_symbol;
172*cf3524a5SJonas Devlieghere   json_symbol.address = 0x0fff;
173*cf3524a5SJonas Devlieghere 
174*cf3524a5SJonas Devlieghere   SectionSP sect_sp(new Section(
175*cf3524a5SJonas Devlieghere       /*module_sp=*/ModuleSP(),
176*cf3524a5SJonas Devlieghere       /*obj_file=*/nullptr,
177*cf3524a5SJonas Devlieghere       /*sect_id=*/1,
178*cf3524a5SJonas Devlieghere       /*name=*/ConstString(".text"),
179*cf3524a5SJonas Devlieghere       /*sect_type=*/eSectionTypeCode,
180*cf3524a5SJonas Devlieghere       /*file_vm_addr=*/0x1000,
181*cf3524a5SJonas Devlieghere       /*vm_size=*/0x1000,
182*cf3524a5SJonas Devlieghere       /*file_offset=*/0,
183*cf3524a5SJonas Devlieghere       /*file_size=*/0,
184*cf3524a5SJonas Devlieghere       /*log2align=*/5,
185*cf3524a5SJonas Devlieghere       /*flags=*/0x10203040));
186*cf3524a5SJonas Devlieghere   SectionList sect_list;
187*cf3524a5SJonas Devlieghere   sect_list.AddSection(sect_sp);
188*cf3524a5SJonas Devlieghere 
189*cf3524a5SJonas Devlieghere   Expected<Symbol> symbol = Symbol::FromJSON(json_symbol, &sect_list);
190*cf3524a5SJonas Devlieghere   EXPECT_THAT_EXPECTED(
191*cf3524a5SJonas Devlieghere       symbol, llvm::FailedWithMessage("no section found for address: 0xfff"));
192*cf3524a5SJonas Devlieghere }
193