blob: 1797c0b2d796878623b51455bc912ccffbb48db4 [file] [log] [blame]
Armando Montanez1e4b3702018-12-03 19:30:52 +00001//===- llvm/unittests/TextAPI/YAMLTest.cpp --------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===-----------------------------------------------------------------------===/
9
10#include "llvm/ADT/StringRef.h"
11#include "llvm/TextAPI/ELF/ELFStub.h"
12#include "llvm/TextAPI/ELF/TBEHandler.h"
13#include "llvm/Support/Error.h"
Armando Montaneze3534592018-12-10 02:36:33 +000014#include "llvm/Testing/Support/Error.h"
Armando Montanez1e4b3702018-12-03 19:30:52 +000015#include "gtest/gtest.h"
16#include <string>
17
18using namespace llvm;
19using namespace llvm::ELF;
20using namespace llvm::elfabi;
21
Armando Montanez1e4b3702018-12-03 19:30:52 +000022void compareByLine(StringRef LHS, StringRef RHS) {
23 StringRef Line1;
24 StringRef Line2;
25 while (LHS.size() > 0 && RHS.size() > 0) {
26 std::tie(Line1, LHS) = LHS.split('\n');
27 std::tie(Line2, RHS) = RHS.split('\n');
28 // Comparing StringRef objects works, but has messy output when not equal.
29 // Using STREQ on StringRef.data() doesn't work since these substrings are
30 // not null terminated.
31 // This is inefficient, but forces null terminated strings that can be
32 // cleanly compared.
33 EXPECT_STREQ(Line1.str().data(), Line2.str().data());
34 }
35}
36
37TEST(ElfYamlTextAPI, YAMLReadableTBE) {
38 const char Data[] = "--- !tapi-tbe\n"
39 "TbeVersion: 1.0\n"
40 "SoName: test.so\n"
41 "Arch: x86_64\n"
42 "NeededLibs: [libc.so, libfoo.so, libbar.so]\n"
43 "Symbols:\n"
44 " foo: { Type: Func, Undefined: true }\n"
45 "...\n";
Armando Montaneze3534592018-12-10 02:36:33 +000046 Expected<std::unique_ptr<ELFStub>> StubOrErr = readTBEFromBuffer(Data);
47 ASSERT_THAT_ERROR(StubOrErr.takeError(), Succeeded());
48 std::unique_ptr<ELFStub> Stub = std::move(StubOrErr.get());
Armando Montanez1e4b3702018-12-03 19:30:52 +000049 EXPECT_NE(Stub.get(), nullptr);
50 EXPECT_EQ(Stub->Arch, (uint16_t)llvm::ELF::EM_X86_64);
51 EXPECT_STREQ(Stub->SoName.c_str(), "test.so");
52 EXPECT_EQ(Stub->NeededLibs.size(), 3u);
53 EXPECT_STREQ(Stub->NeededLibs[0].c_str(), "libc.so");
54 EXPECT_STREQ(Stub->NeededLibs[1].c_str(), "libfoo.so");
55 EXPECT_STREQ(Stub->NeededLibs[2].c_str(), "libbar.so");
56}
57
58TEST(ElfYamlTextAPI, YAMLReadsTBESymbols) {
59 const char Data[] = "--- !tapi-tbe\n"
60 "TbeVersion: 1.0\n"
61 "SoName: test.so\n"
62 "Arch: x86_64\n"
63 "Symbols:\n"
64 " bar: { Type: Object, Size: 42 }\n"
65 " baz: { Type: TLS, Size: 3 }\n"
66 " foo: { Type: Func, Warning: \"Deprecated!\" }\n"
67 " nor: { Type: NoType, Undefined: true }\n"
68 " not: { Type: File, Undefined: true, Size: 111, "
69 "Warning: \'All fields populated!\' }\n"
70 "...\n";
Armando Montaneze3534592018-12-10 02:36:33 +000071 Expected<std::unique_ptr<ELFStub>> StubOrErr = readTBEFromBuffer(Data);
72 ASSERT_THAT_ERROR(StubOrErr.takeError(), Succeeded());
73 std::unique_ptr<ELFStub> Stub = std::move(StubOrErr.get());
74 EXPECT_NE(Stub.get(), nullptr);
Armando Montanez1e4b3702018-12-03 19:30:52 +000075 EXPECT_EQ(Stub->Symbols.size(), 5u);
76
77 auto Iterator = Stub->Symbols.begin();
78 ELFSymbol const &SymBar = *Iterator++;
79 EXPECT_STREQ(SymBar.Name.c_str(), "bar");
80 EXPECT_EQ(SymBar.Size, 42u);
81 EXPECT_EQ(SymBar.Type, ELFSymbolType::Object);
82 EXPECT_FALSE(SymBar.Undefined);
83 EXPECT_FALSE(SymBar.Warning.hasValue());
84
85 ELFSymbol const &SymBaz = *Iterator++;
86 EXPECT_STREQ(SymBaz.Name.c_str(), "baz");
87 EXPECT_EQ(SymBaz.Size, 3u);
88 EXPECT_EQ(SymBaz.Type, ELFSymbolType::TLS);
89 EXPECT_FALSE(SymBaz.Undefined);
90 EXPECT_FALSE(SymBaz.Warning.hasValue());
91
92 ELFSymbol const &SymFoo = *Iterator++;
93 EXPECT_STREQ(SymFoo.Name.c_str(), "foo");
94 EXPECT_EQ(SymFoo.Size, 0u);
95 EXPECT_EQ(SymFoo.Type, ELFSymbolType::Func);
96 EXPECT_FALSE(SymFoo.Undefined);
97 EXPECT_TRUE(SymFoo.Warning.hasValue());
98 EXPECT_STREQ(SymFoo.Warning->c_str(), "Deprecated!");
99
100 ELFSymbol const &SymNor = *Iterator++;
101 EXPECT_STREQ(SymNor.Name.c_str(), "nor");
102 EXPECT_EQ(SymNor.Size, 0u);
103 EXPECT_EQ(SymNor.Type, ELFSymbolType::NoType);
104 EXPECT_TRUE(SymNor.Undefined);
105 EXPECT_FALSE(SymNor.Warning.hasValue());
106
107 ELFSymbol const &SymNot = *Iterator++;
108 EXPECT_STREQ(SymNot.Name.c_str(), "not");
109 EXPECT_EQ(SymNot.Size, 111u);
110 EXPECT_EQ(SymNot.Type, ELFSymbolType::Unknown);
111 EXPECT_TRUE(SymNot.Undefined);
112 EXPECT_TRUE(SymNot.Warning.hasValue());
113 EXPECT_STREQ(SymNot.Warning->c_str(), "All fields populated!");
114}
115
116TEST(ElfYamlTextAPI, YAMLReadsNoTBESyms) {
117 const char Data[] = "--- !tapi-tbe\n"
118 "TbeVersion: 1.0\n"
119 "SoName: test.so\n"
120 "Arch: x86_64\n"
121 "Symbols: {}\n"
122 "...\n";
Armando Montaneze3534592018-12-10 02:36:33 +0000123 Expected<std::unique_ptr<ELFStub>> StubOrErr = readTBEFromBuffer(Data);
124 ASSERT_THAT_ERROR(StubOrErr.takeError(), Succeeded());
125 std::unique_ptr<ELFStub> Stub = std::move(StubOrErr.get());
126 EXPECT_NE(Stub.get(), nullptr);
Armando Montanez1e4b3702018-12-03 19:30:52 +0000127 EXPECT_EQ(0u, Stub->Symbols.size());
128}
129
130TEST(ElfYamlTextAPI, YAMLUnreadableTBE) {
Armando Montanez1e4b3702018-12-03 19:30:52 +0000131 // Can't read: wrong format/version.
132 const char Data[] = "--- !tapi-tbz\n"
133 "TbeVersion: z.3\n"
134 "SoName: test.so\n"
135 "Arch: x86_64\n"
136 "Symbols:\n"
137 " foo: { Type: Func, Undefined: true }\n";
Armando Montaneze3534592018-12-10 02:36:33 +0000138 Expected<std::unique_ptr<ELFStub>> StubOrErr = readTBEFromBuffer(Data);
139 ASSERT_THAT_ERROR(StubOrErr.takeError(), Failed());
Armando Montanez1e4b3702018-12-03 19:30:52 +0000140}
141
142TEST(ElfYamlTextAPI, YAMLWritesTBESymbols) {
143 const char Expected[] =
144 "--- !tapi-tbe\n"
145 "TbeVersion: 1.0\n"
146 "SoName: test.so\n"
147 "Arch: AArch64\n"
148 "Symbols: \n"
149 " foo: { Type: NoType, Size: 99, Warning: Does nothing }\n"
150 " nor: { Type: Func, Undefined: true }\n"
151 " not: { Type: Unknown, Size: 12345678901234 }\n"
152 "...\n";
153 ELFStub Stub;
154 Stub.TbeVersion = VersionTuple(1, 0);
155 Stub.SoName = "test.so";
156 Stub.Arch = ELF::EM_AARCH64;
157
158 ELFSymbol SymFoo("foo");
159 SymFoo.Size = 99u;
160 SymFoo.Type = ELFSymbolType::NoType;
161 SymFoo.Undefined = false;
162 SymFoo.Warning = "Does nothing";
163
164 ELFSymbol SymNor("nor");
165 SymNor.Type = ELFSymbolType::Func;
166 SymNor.Undefined = true;
167
168 ELFSymbol SymNot("not");
169 SymNot.Size = 12345678901234u;
170 SymNot.Type = ELFSymbolType::Unknown;
171 SymNot.Undefined = false;
172
173 // Deliberately not in order to check that result is sorted.
174 Stub.Symbols.insert(SymNot);
175 Stub.Symbols.insert(SymFoo);
176 Stub.Symbols.insert(SymNor);
177
178 // Ensure move constructor works as expected.
179 ELFStub Moved = std::move(Stub);
180
181 std::string Result;
182 raw_string_ostream OS(Result);
Armando Montaneze3534592018-12-10 02:36:33 +0000183 ASSERT_THAT_ERROR(writeTBEToOutputStream(OS, Moved), Succeeded());
Armando Montanez1e4b3702018-12-03 19:30:52 +0000184 Result = OS.str();
185 compareByLine(Result.c_str(), Expected);
186}
187
188TEST(ElfYamlTextAPI, YAMLWritesNoTBESyms) {
189 const char Expected[] = "--- !tapi-tbe\n"
190 "TbeVersion: 1.0\n"
191 "SoName: nosyms.so\n"
192 "Arch: x86_64\n"
Sam Clegg992fc882018-12-07 19:29:00 +0000193 "NeededLibs: \n"
194 " - libc.so\n"
195 " - libfoo.so\n"
196 " - libbar.so\n"
Armando Montanez1e4b3702018-12-03 19:30:52 +0000197 "Symbols: {}\n"
198 "...\n";
199 ELFStub Stub;
200 Stub.TbeVersion = VersionTuple(1, 0);
201 Stub.SoName = "nosyms.so";
202 Stub.Arch = ELF::EM_X86_64;
203 Stub.NeededLibs.push_back("libc.so");
204 Stub.NeededLibs.push_back("libfoo.so");
205 Stub.NeededLibs.push_back("libbar.so");
206
207 std::string Result;
208 raw_string_ostream OS(Result);
Armando Montaneze3534592018-12-10 02:36:33 +0000209 ASSERT_THAT_ERROR(writeTBEToOutputStream(OS, Stub), Succeeded());
Armando Montanez1e4b3702018-12-03 19:30:52 +0000210 Result = OS.str();
211 compareByLine(Result.c_str(), Expected);
212}