llvm-project/lldb/unittests/Symbol/JSONSymbolTest.cpp
Jonas Devlieghere cf3524a574
[lldb] Introduce new SymbolFileJSON and ObjectFileJSON
Introduce a new object and symbol file format with the goal of mapping
addresses to symbol names. I'd like to think of is as an extremely
simple textual symtab. The file format consists of a triple, a UUID and
a list of symbols. JSON is used for the encoding, but that's mostly an
implementation detail. The goal of the format was to be simple and human
readable.

The new file format is motivated by two use cases:

 - Stripped binaries: when a binary is stripped, you lose the ability to
   do thing like setting symbolic breakpoints. You can keep the
   unstripped binary around, but if all you need is the stripped
   symbols then that's a lot of overhead. Instead, we could save the
   stripped symbols to a file and load them in the debugger when
   needed. I want to extend llvm-strip to have a mode where it emits
   this new file format.

 - Interactive crashlogs: with interactive crashlogs, if we don't have
   the binary or the dSYM for a particular module, we currently show an
   unnamed symbol for those frames. This is a regression compared to the
   textual format, that has these frames pre-symbolicated. Given that
   this information is available in the JSON crashlog, we need a way to
   tell LLDB about it. With the new symbol file format, we can easily
   synthesize a symbol file for each of those modules and load them to
   symbolicate those frames.

Here's an example of the file format:

 {
     "triple": "arm64-apple-macosx13.0.0",
     "uuid": "36D0CCE7-8ED2-3CA3-96B0-48C1764DA908",
     "symbols": [
         {
             "name": "main",
             "type": "code",
             "size": 32,
             "address": 4294983568
         },
         {
             "name": "foo",
             "type": "code",
             "size": 8,
             "address": 4294983560
         }
     ]
 }

Differential revision: https://reviews.llvm.org/D145180
2023-03-08 20:56:11 -08:00

193 lines
5.1 KiB
C++

//===-- JSONSymbolTest.cpp ------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "lldb/Core/Section.h"
#include "lldb/Symbol/Symbol.h"
#include "llvm/Testing/Support/Error.h"
#include "gtest/gtest.h"
using namespace lldb;
using namespace llvm;
using namespace lldb_private;
static std::string g_error_no_section_list = "no section list provided";
static std::string g_error_both_value_and_address =
"symbol cannot contain both a value and an address";
static std::string g_error_neither_value_or_address =
"symbol must contain either a value or an address";
TEST(JSONSymbolTest, DeserializeCodeAddress) {
std::string text = R"(
{
"name": "foo",
"type": "code",
"size": 32,
"address": 4096
})";
Expected<json::Value> json = json::parse(text);
ASSERT_TRUE(static_cast<bool>(json));
json::Path::Root root;
JSONSymbol json_symbol;
ASSERT_TRUE(fromJSON(*json, json_symbol, root));
SectionSP sect_sp(new Section(
/*module_sp=*/ModuleSP(),
/*obj_file=*/nullptr,
/*sect_id=*/1,
/*name=*/ConstString(".text"),
/*sect_type=*/eSectionTypeCode,
/*file_vm_addr=*/0x1000,
/*vm_size=*/0x1000,
/*file_offset=*/0,
/*file_size=*/0,
/*log2align=*/5,
/*flags=*/0x10203040));
SectionList sect_list;
sect_list.AddSection(sect_sp);
Expected<Symbol> symbol = Symbol::FromJSON(json_symbol, &sect_list);
EXPECT_THAT_EXPECTED(symbol, llvm::Succeeded());
EXPECT_EQ(symbol->GetName(), ConstString("foo"));
EXPECT_EQ(symbol->GetFileAddress(), static_cast<lldb::addr_t>(0x1000));
EXPECT_EQ(symbol->GetType(), eSymbolTypeCode);
}
TEST(JSONSymbolTest, DeserializeCodeValue) {
std::string text = R"(
{
"name": "foo",
"type": "code",
"size": 32,
"value": 4096
})";
Expected<json::Value> json = json::parse(text);
EXPECT_THAT_EXPECTED(json, llvm::Succeeded());
json::Path::Root root;
JSONSymbol json_symbol;
ASSERT_TRUE(fromJSON(*json, json_symbol, root));
SectionList sect_list;
Expected<Symbol> symbol = Symbol::FromJSON(json_symbol, &sect_list);
EXPECT_THAT_EXPECTED(symbol, llvm::Succeeded());
EXPECT_EQ(symbol->GetName(), ConstString("foo"));
EXPECT_EQ(symbol->GetRawValue(), static_cast<lldb::addr_t>(0x1000));
EXPECT_EQ(symbol->GetType(), eSymbolTypeCode);
}
TEST(JSONSymbolTest, JSONInvalidValueAndAddress) {
std::string text = R"(
{
"name": "foo",
"type": "code",
"size": 32,
"value": 4096,
"address": 4096
})";
Expected<json::Value> json = json::parse(text);
EXPECT_THAT_EXPECTED(json, llvm::Succeeded());
json::Path::Root root;
JSONSymbol json_symbol;
ASSERT_FALSE(fromJSON(*json, json_symbol, root));
}
TEST(JSONSymbolTest, JSONInvalidNoValueOrAddress) {
std::string text = R"(
{
"name": "foo",
"type": "code",
"size": 32
})";
Expected<json::Value> json = json::parse(text);
EXPECT_THAT_EXPECTED(json, llvm::Succeeded());
json::Path::Root root;
JSONSymbol json_symbol;
ASSERT_FALSE(fromJSON(*json, json_symbol, root));
}
TEST(JSONSymbolTest, JSONInvalidType) {
std::string text = R"(
{
"name": "foo",
"type": "bogus",
"value": 4096,
"size": 32
})";
Expected<json::Value> json = json::parse(text);
EXPECT_THAT_EXPECTED(json, llvm::Succeeded());
json::Path::Root root;
JSONSymbol json_symbol;
ASSERT_FALSE(fromJSON(*json, json_symbol, root));
}
TEST(JSONSymbolTest, SymbolInvalidNoSectionList) {
JSONSymbol json_symbol;
json_symbol.value = 0x1;
Expected<Symbol> symbol = Symbol::FromJSON(json_symbol, nullptr);
EXPECT_THAT_EXPECTED(symbol,
llvm::FailedWithMessage(g_error_no_section_list));
}
TEST(JSONSymbolTest, SymbolInvalidValueAndAddress) {
JSONSymbol json_symbol;
json_symbol.value = 0x1;
json_symbol.address = 0x2;
SectionList sect_list;
Expected<Symbol> symbol = Symbol::FromJSON(json_symbol, &sect_list);
EXPECT_THAT_EXPECTED(symbol,
llvm::FailedWithMessage(g_error_both_value_and_address));
}
TEST(JSONSymbolTest, SymbolInvalidNoValueOrAddress) {
JSONSymbol json_symbol;
SectionList sect_list;
Expected<Symbol> symbol = Symbol::FromJSON(json_symbol, &sect_list);
EXPECT_THAT_EXPECTED(
symbol, llvm::FailedWithMessage(g_error_neither_value_or_address));
}
TEST(JSONSymbolTest, SymbolInvalidAddressNotInSection) {
JSONSymbol json_symbol;
json_symbol.address = 0x0fff;
SectionSP sect_sp(new Section(
/*module_sp=*/ModuleSP(),
/*obj_file=*/nullptr,
/*sect_id=*/1,
/*name=*/ConstString(".text"),
/*sect_type=*/eSectionTypeCode,
/*file_vm_addr=*/0x1000,
/*vm_size=*/0x1000,
/*file_offset=*/0,
/*file_size=*/0,
/*log2align=*/5,
/*flags=*/0x10203040));
SectionList sect_list;
sect_list.AddSection(sect_sp);
Expected<Symbol> symbol = Symbol::FromJSON(json_symbol, &sect_list);
EXPECT_THAT_EXPECTED(
symbol, llvm::FailedWithMessage("no section found for address: 0xfff"));
}