2018-12-04 03:30:52 +08:00
|
|
|
//===- llvm/unittests/TextAPI/YAMLTest.cpp --------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2018-12-04 03:30:52 +08:00
|
|
|
//
|
|
|
|
//===-----------------------------------------------------------------------===/
|
|
|
|
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
2021-04-08 06:50:12 +08:00
|
|
|
#include "llvm/BinaryFormat/ELF.h"
|
|
|
|
#include "llvm/InterfaceStub/IFSHandler.h"
|
|
|
|
#include "llvm/InterfaceStub/IFSStub.h"
|
2018-12-04 03:30:52 +08:00
|
|
|
#include "llvm/Support/Error.h"
|
2018-12-10 10:36:33 +08:00
|
|
|
#include "llvm/Testing/Support/Error.h"
|
2018-12-04 03:30:52 +08:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::ELF;
|
2021-04-02 11:55:11 +08:00
|
|
|
using namespace llvm::ifs;
|
2018-12-04 03:30:52 +08:00
|
|
|
|
|
|
|
void compareByLine(StringRef LHS, StringRef RHS) {
|
|
|
|
StringRef Line1;
|
|
|
|
StringRef Line2;
|
|
|
|
while (LHS.size() > 0 && RHS.size() > 0) {
|
|
|
|
std::tie(Line1, LHS) = LHS.split('\n');
|
|
|
|
std::tie(Line2, RHS) = RHS.split('\n');
|
|
|
|
// Comparing StringRef objects works, but has messy output when not equal.
|
|
|
|
// Using STREQ on StringRef.data() doesn't work since these substrings are
|
|
|
|
// not null terminated.
|
|
|
|
// This is inefficient, but forces null terminated strings that can be
|
|
|
|
// cleanly compared.
|
|
|
|
EXPECT_STREQ(Line1.str().data(), Line2.str().data());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ElfYamlTextAPI, YAMLReadableTBE) {
|
2021-04-01 07:48:56 +08:00
|
|
|
const char Data[] = "--- !ifs-v1\n"
|
2021-04-08 06:50:12 +08:00
|
|
|
"IfsVersion: 1.0\n"
|
2021-04-01 07:48:56 +08:00
|
|
|
"Target: { ObjectFormat: ELF, Arch: x86_64, Endianness: "
|
|
|
|
"little, BitWidth: 64 }\n"
|
2018-12-04 03:30:52 +08:00
|
|
|
"NeededLibs: [libc.so, libfoo.so, libbar.so]\n"
|
|
|
|
"Symbols:\n"
|
2021-04-01 07:48:56 +08:00
|
|
|
" - { Name: foo, Type: Func, Undefined: true }\n"
|
2018-12-04 03:30:52 +08:00
|
|
|
"...\n";
|
2021-04-08 06:50:12 +08:00
|
|
|
Expected<std::unique_ptr<IFSStub>> StubOrErr = readIFSFromBuffer(Data);
|
2018-12-10 10:36:33 +08:00
|
|
|
ASSERT_THAT_ERROR(StubOrErr.takeError(), Succeeded());
|
2021-04-08 06:50:12 +08:00
|
|
|
std::unique_ptr<IFSStub> Stub = std::move(StubOrErr.get());
|
2018-12-04 03:30:52 +08:00
|
|
|
EXPECT_NE(Stub.get(), nullptr);
|
2018-12-11 09:00:16 +08:00
|
|
|
EXPECT_FALSE(Stub->SoName.hasValue());
|
2021-04-01 07:48:56 +08:00
|
|
|
EXPECT_TRUE(Stub->Target.Arch.hasValue());
|
|
|
|
EXPECT_EQ(Stub->Target.Arch.getValue(), (uint16_t)llvm::ELF::EM_X86_64);
|
2018-12-04 03:30:52 +08:00
|
|
|
EXPECT_EQ(Stub->NeededLibs.size(), 3u);
|
|
|
|
EXPECT_STREQ(Stub->NeededLibs[0].c_str(), "libc.so");
|
|
|
|
EXPECT_STREQ(Stub->NeededLibs[1].c_str(), "libfoo.so");
|
|
|
|
EXPECT_STREQ(Stub->NeededLibs[2].c_str(), "libbar.so");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ElfYamlTextAPI, YAMLReadsTBESymbols) {
|
2021-04-01 07:48:56 +08:00
|
|
|
const char Data[] =
|
|
|
|
"--- !ifs-v1\n"
|
2021-04-08 06:50:12 +08:00
|
|
|
"IfsVersion: 1.0\n"
|
2021-04-01 07:48:56 +08:00
|
|
|
"SoName: test.so\n"
|
|
|
|
"Target: { ObjectFormat: ELF, Arch: x86_64, Endianness: little, "
|
|
|
|
"BitWidth: 64 }\n"
|
|
|
|
"Symbols:\n"
|
|
|
|
" - { Name: bar, Type: Object, Size: 42 }\n"
|
|
|
|
" - { Name: baz, Type: TLS, Size: 3 }\n"
|
|
|
|
" - { Name: foo, Type: Func, Warning: \"Deprecated!\" }\n"
|
|
|
|
" - { Name: nor, Type: NoType, Undefined: true }\n"
|
|
|
|
" - { Name: not, Type: File, Undefined: true, Size: 111, "
|
|
|
|
"Weak: true, Warning: \'All fields populated!\' }\n"
|
|
|
|
"...\n";
|
2021-04-08 06:50:12 +08:00
|
|
|
Expected<std::unique_ptr<IFSStub>> StubOrErr = readIFSFromBuffer(Data);
|
2018-12-10 10:36:33 +08:00
|
|
|
ASSERT_THAT_ERROR(StubOrErr.takeError(), Succeeded());
|
2021-04-08 06:50:12 +08:00
|
|
|
std::unique_ptr<IFSStub> Stub = std::move(StubOrErr.get());
|
2018-12-10 10:36:33 +08:00
|
|
|
EXPECT_NE(Stub.get(), nullptr);
|
2018-12-11 09:00:16 +08:00
|
|
|
EXPECT_TRUE(Stub->SoName.hasValue());
|
|
|
|
EXPECT_STREQ(Stub->SoName->c_str(), "test.so");
|
2018-12-04 03:30:52 +08:00
|
|
|
EXPECT_EQ(Stub->Symbols.size(), 5u);
|
|
|
|
|
|
|
|
auto Iterator = Stub->Symbols.begin();
|
2021-04-08 06:50:12 +08:00
|
|
|
IFSSymbol const &SymBar = *Iterator++;
|
2018-12-04 03:30:52 +08:00
|
|
|
EXPECT_STREQ(SymBar.Name.c_str(), "bar");
|
|
|
|
EXPECT_EQ(SymBar.Size, 42u);
|
2021-04-08 06:50:12 +08:00
|
|
|
EXPECT_EQ(SymBar.Type, IFSSymbolType::Object);
|
2018-12-04 03:30:52 +08:00
|
|
|
EXPECT_FALSE(SymBar.Undefined);
|
2018-12-22 04:45:58 +08:00
|
|
|
EXPECT_FALSE(SymBar.Weak);
|
2018-12-04 03:30:52 +08:00
|
|
|
EXPECT_FALSE(SymBar.Warning.hasValue());
|
|
|
|
|
2021-04-08 06:50:12 +08:00
|
|
|
IFSSymbol const &SymBaz = *Iterator++;
|
2018-12-04 03:30:52 +08:00
|
|
|
EXPECT_STREQ(SymBaz.Name.c_str(), "baz");
|
|
|
|
EXPECT_EQ(SymBaz.Size, 3u);
|
2021-04-08 06:50:12 +08:00
|
|
|
EXPECT_EQ(SymBaz.Type, IFSSymbolType::TLS);
|
2018-12-04 03:30:52 +08:00
|
|
|
EXPECT_FALSE(SymBaz.Undefined);
|
2018-12-22 04:45:58 +08:00
|
|
|
EXPECT_FALSE(SymBaz.Weak);
|
2018-12-04 03:30:52 +08:00
|
|
|
EXPECT_FALSE(SymBaz.Warning.hasValue());
|
|
|
|
|
2021-04-08 06:50:12 +08:00
|
|
|
IFSSymbol const &SymFoo = *Iterator++;
|
2018-12-04 03:30:52 +08:00
|
|
|
EXPECT_STREQ(SymFoo.Name.c_str(), "foo");
|
|
|
|
EXPECT_EQ(SymFoo.Size, 0u);
|
2021-04-08 06:50:12 +08:00
|
|
|
EXPECT_EQ(SymFoo.Type, IFSSymbolType::Func);
|
2018-12-04 03:30:52 +08:00
|
|
|
EXPECT_FALSE(SymFoo.Undefined);
|
2018-12-22 04:45:58 +08:00
|
|
|
EXPECT_FALSE(SymFoo.Weak);
|
2018-12-04 03:30:52 +08:00
|
|
|
EXPECT_TRUE(SymFoo.Warning.hasValue());
|
|
|
|
EXPECT_STREQ(SymFoo.Warning->c_str(), "Deprecated!");
|
|
|
|
|
2021-04-08 06:50:12 +08:00
|
|
|
IFSSymbol const &SymNor = *Iterator++;
|
2018-12-04 03:30:52 +08:00
|
|
|
EXPECT_STREQ(SymNor.Name.c_str(), "nor");
|
|
|
|
EXPECT_EQ(SymNor.Size, 0u);
|
2021-04-08 06:50:12 +08:00
|
|
|
EXPECT_EQ(SymNor.Type, IFSSymbolType::NoType);
|
2018-12-04 03:30:52 +08:00
|
|
|
EXPECT_TRUE(SymNor.Undefined);
|
2018-12-22 04:45:58 +08:00
|
|
|
EXPECT_FALSE(SymNor.Weak);
|
2018-12-04 03:30:52 +08:00
|
|
|
EXPECT_FALSE(SymNor.Warning.hasValue());
|
|
|
|
|
2021-04-08 06:50:12 +08:00
|
|
|
IFSSymbol const &SymNot = *Iterator++;
|
2018-12-04 03:30:52 +08:00
|
|
|
EXPECT_STREQ(SymNot.Name.c_str(), "not");
|
|
|
|
EXPECT_EQ(SymNot.Size, 111u);
|
2021-04-08 06:50:12 +08:00
|
|
|
EXPECT_EQ(SymNot.Type, IFSSymbolType::Unknown);
|
2018-12-04 03:30:52 +08:00
|
|
|
EXPECT_TRUE(SymNot.Undefined);
|
2018-12-22 04:45:58 +08:00
|
|
|
EXPECT_TRUE(SymNot.Weak);
|
2018-12-04 03:30:52 +08:00
|
|
|
EXPECT_TRUE(SymNot.Warning.hasValue());
|
|
|
|
EXPECT_STREQ(SymNot.Warning->c_str(), "All fields populated!");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ElfYamlTextAPI, YAMLReadsNoTBESyms) {
|
2021-04-01 07:48:56 +08:00
|
|
|
const char Data[] = "--- !ifs-v1\n"
|
2021-04-08 06:50:12 +08:00
|
|
|
"IfsVersion: 1.0\n"
|
2018-12-04 03:30:52 +08:00
|
|
|
"SoName: test.so\n"
|
2021-04-01 07:48:56 +08:00
|
|
|
"Target: { ObjectFormat: ELF, Arch: x86_64, Endianness: "
|
|
|
|
"little, BitWidth: 64 }\n"
|
|
|
|
"Symbols: []\n"
|
2018-12-04 03:30:52 +08:00
|
|
|
"...\n";
|
2021-04-08 06:50:12 +08:00
|
|
|
Expected<std::unique_ptr<IFSStub>> StubOrErr = readIFSFromBuffer(Data);
|
2018-12-10 10:36:33 +08:00
|
|
|
ASSERT_THAT_ERROR(StubOrErr.takeError(), Succeeded());
|
2021-04-08 06:50:12 +08:00
|
|
|
std::unique_ptr<IFSStub> Stub = std::move(StubOrErr.get());
|
2018-12-10 10:36:33 +08:00
|
|
|
EXPECT_NE(Stub.get(), nullptr);
|
2018-12-04 03:30:52 +08:00
|
|
|
EXPECT_EQ(0u, Stub->Symbols.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ElfYamlTextAPI, YAMLUnreadableTBE) {
|
|
|
|
// Can't read: wrong format/version.
|
|
|
|
const char Data[] = "--- !tapi-tbz\n"
|
2021-04-08 06:50:12 +08:00
|
|
|
"IfsVersion: z.3\n"
|
2018-12-04 03:30:52 +08:00
|
|
|
"SoName: test.so\n"
|
2021-04-01 07:48:56 +08:00
|
|
|
"Target: { ObjectFormat: ELF, Arch: x86_64, Endianness: "
|
|
|
|
"little, BitWidth: 64 }\n"
|
2018-12-04 03:30:52 +08:00
|
|
|
"Symbols:\n"
|
|
|
|
" foo: { Type: Func, Undefined: true }\n";
|
2021-04-08 06:50:12 +08:00
|
|
|
Expected<std::unique_ptr<IFSStub>> StubOrErr = readIFSFromBuffer(Data);
|
2018-12-10 10:36:33 +08:00
|
|
|
ASSERT_THAT_ERROR(StubOrErr.takeError(), Failed());
|
2018-12-04 03:30:52 +08:00
|
|
|
}
|
|
|
|
|
2020-10-21 08:50:29 +08:00
|
|
|
TEST(ElfYamlTextAPI, YAMLUnsupportedVersion) {
|
2021-04-01 07:48:56 +08:00
|
|
|
const char Data[] = "--- !ifs-v1\n"
|
2021-04-08 06:50:12 +08:00
|
|
|
"IfsVersion: 9.9.9\n"
|
2020-10-21 08:50:29 +08:00
|
|
|
"SoName: test.so\n"
|
2021-04-01 07:48:56 +08:00
|
|
|
"Target: { ObjectFormat: ELF, Arch: x86_64, Endianness: "
|
|
|
|
"little, BitWidth: 64 }\n"
|
|
|
|
"Symbols: []\n"
|
2020-10-21 08:50:29 +08:00
|
|
|
"...\n";
|
2021-04-08 06:50:12 +08:00
|
|
|
Expected<std::unique_ptr<IFSStub>> StubOrErr = readIFSFromBuffer(Data);
|
2020-10-21 08:50:29 +08:00
|
|
|
std::string ErrorMessage = toString(StubOrErr.takeError());
|
2021-04-08 06:50:12 +08:00
|
|
|
EXPECT_EQ("IFS version 9.9.9 is unsupported.", ErrorMessage);
|
2020-10-21 08:50:29 +08:00
|
|
|
}
|
|
|
|
|
2018-12-04 03:30:52 +08:00
|
|
|
TEST(ElfYamlTextAPI, YAMLWritesTBESymbols) {
|
|
|
|
const char Expected[] =
|
2021-04-01 07:48:56 +08:00
|
|
|
"--- !ifs-v1\n"
|
2021-04-08 06:50:12 +08:00
|
|
|
"IfsVersion: 1.0\n"
|
2021-04-01 07:48:56 +08:00
|
|
|
"Target: { ObjectFormat: ELF, Arch: AArch64, Endianness: "
|
|
|
|
"little, BitWidth: 64 }\n"
|
2019-07-12 12:51:31 +08:00
|
|
|
"Symbols:\n"
|
2021-04-01 07:48:56 +08:00
|
|
|
" - { Name: bar, Type: Func, Weak: true }\n"
|
|
|
|
" - { Name: foo, Type: NoType, Size: 99, Warning: Does nothing }\n"
|
|
|
|
" - { Name: nor, Type: Func, Undefined: true }\n"
|
|
|
|
" - { Name: not, Type: Unknown, Size: 12345678901234 }\n"
|
2018-12-04 03:30:52 +08:00
|
|
|
"...\n";
|
2021-04-08 06:50:12 +08:00
|
|
|
IFSStub Stub;
|
|
|
|
Stub.IfsVersion = VersionTuple(1, 0);
|
2021-04-01 07:48:56 +08:00
|
|
|
Stub.Target.Arch = ELF::EM_AARCH64;
|
2021-04-08 06:50:12 +08:00
|
|
|
Stub.Target.BitWidth = IFSBitWidthType::IFS64;
|
|
|
|
Stub.Target.Endianness = IFSEndiannessType::Little;
|
2021-04-01 07:48:56 +08:00
|
|
|
Stub.Target.ObjectFormat = "ELF";
|
|
|
|
|
2021-04-08 06:50:12 +08:00
|
|
|
IFSSymbol SymBar("bar");
|
2021-04-01 07:48:56 +08:00
|
|
|
SymBar.Size = 128u;
|
2021-04-08 06:50:12 +08:00
|
|
|
SymBar.Type = IFSSymbolType::Func;
|
2021-04-01 07:48:56 +08:00
|
|
|
SymBar.Undefined = false;
|
|
|
|
SymBar.Weak = true;
|
2018-12-04 03:30:52 +08:00
|
|
|
|
2021-04-08 06:50:12 +08:00
|
|
|
IFSSymbol SymFoo("foo");
|
2018-12-04 03:30:52 +08:00
|
|
|
SymFoo.Size = 99u;
|
2021-04-08 06:50:12 +08:00
|
|
|
SymFoo.Type = IFSSymbolType::NoType;
|
2018-12-04 03:30:52 +08:00
|
|
|
SymFoo.Undefined = false;
|
2018-12-22 04:45:58 +08:00
|
|
|
SymFoo.Weak = false;
|
2018-12-04 03:30:52 +08:00
|
|
|
SymFoo.Warning = "Does nothing";
|
|
|
|
|
2021-04-08 06:50:12 +08:00
|
|
|
IFSSymbol SymNor("nor");
|
2018-12-22 05:44:09 +08:00
|
|
|
SymNor.Size = 1234u;
|
2021-04-08 06:50:12 +08:00
|
|
|
SymNor.Type = IFSSymbolType::Func;
|
2018-12-04 03:30:52 +08:00
|
|
|
SymNor.Undefined = true;
|
2018-12-22 04:45:58 +08:00
|
|
|
SymNor.Weak = false;
|
2018-12-04 03:30:52 +08:00
|
|
|
|
2021-04-08 06:50:12 +08:00
|
|
|
IFSSymbol SymNot("not");
|
2018-12-04 03:30:52 +08:00
|
|
|
SymNot.Size = 12345678901234u;
|
2021-04-08 06:50:12 +08:00
|
|
|
SymNot.Type = IFSSymbolType::Unknown;
|
2018-12-04 03:30:52 +08:00
|
|
|
SymNot.Undefined = false;
|
2018-12-22 04:45:58 +08:00
|
|
|
SymNot.Weak = false;
|
2018-12-04 03:30:52 +08:00
|
|
|
|
2021-04-01 07:48:56 +08:00
|
|
|
// Symbol order is preserved instead of being sorted.
|
|
|
|
Stub.Symbols.push_back(SymBar);
|
|
|
|
Stub.Symbols.push_back(SymFoo);
|
|
|
|
Stub.Symbols.push_back(SymNor);
|
|
|
|
Stub.Symbols.push_back(SymNot);
|
2018-12-04 03:30:52 +08:00
|
|
|
|
|
|
|
// Ensure move constructor works as expected.
|
2021-04-08 06:50:12 +08:00
|
|
|
IFSStub Moved = std::move(Stub);
|
2018-12-04 03:30:52 +08:00
|
|
|
|
|
|
|
std::string Result;
|
|
|
|
raw_string_ostream OS(Result);
|
2021-04-08 06:50:12 +08:00
|
|
|
ASSERT_THAT_ERROR(writeIFSToOutputStream(OS, Moved), Succeeded());
|
2018-12-04 03:30:52 +08:00
|
|
|
Result = OS.str();
|
|
|
|
compareByLine(Result.c_str(), Expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ElfYamlTextAPI, YAMLWritesNoTBESyms) {
|
2021-04-01 07:48:56 +08:00
|
|
|
const char Expected[] = "--- !ifs-v1\n"
|
2021-04-08 06:50:12 +08:00
|
|
|
"IfsVersion: 1.0\n"
|
2018-12-04 03:30:52 +08:00
|
|
|
"SoName: nosyms.so\n"
|
2021-04-01 07:48:56 +08:00
|
|
|
"Target: { ObjectFormat: ELF, Arch: x86_64, "
|
|
|
|
"Endianness: little, BitWidth: 64 }\n"
|
2019-07-12 12:51:31 +08:00
|
|
|
"NeededLibs:\n"
|
2018-12-08 03:29:00 +08:00
|
|
|
" - libc.so\n"
|
|
|
|
" - libfoo.so\n"
|
|
|
|
" - libbar.so\n"
|
2021-04-01 07:48:56 +08:00
|
|
|
"Symbols: []\n"
|
2018-12-04 03:30:52 +08:00
|
|
|
"...\n";
|
2021-04-08 06:50:12 +08:00
|
|
|
IFSStub Stub;
|
|
|
|
Stub.IfsVersion = VersionTuple(1, 0);
|
2018-12-04 03:30:52 +08:00
|
|
|
Stub.SoName = "nosyms.so";
|
2021-04-01 07:48:56 +08:00
|
|
|
Stub.Target.Arch = ELF::EM_X86_64;
|
2021-04-08 06:50:12 +08:00
|
|
|
Stub.Target.BitWidth = IFSBitWidthType::IFS64;
|
|
|
|
Stub.Target.Endianness = IFSEndiannessType::Little;
|
2021-04-01 07:48:56 +08:00
|
|
|
Stub.Target.ObjectFormat = "ELF";
|
2018-12-04 03:30:52 +08:00
|
|
|
Stub.NeededLibs.push_back("libc.so");
|
|
|
|
Stub.NeededLibs.push_back("libfoo.so");
|
|
|
|
Stub.NeededLibs.push_back("libbar.so");
|
|
|
|
|
|
|
|
std::string Result;
|
|
|
|
raw_string_ostream OS(Result);
|
2021-04-08 06:50:12 +08:00
|
|
|
ASSERT_THAT_ERROR(writeIFSToOutputStream(OS, Stub), Succeeded());
|
2018-12-04 03:30:52 +08:00
|
|
|
Result = OS.str();
|
|
|
|
compareByLine(Result.c_str(), Expected);
|
|
|
|
}
|