Bartleby 0.1.0
A symbol renaming toolkit
Loading...
Searching...
No Matches
Macros | Functions
Tests.cpp File Reference

Unit tests for Bartleby. More...

#include "Bartleby/Bartleby.h"
#include "Bartleby-c/Bartleby.h"
#include "llvm/ADT/Triple.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Object/Binary.h"
#include "llvm/ObjectYAML/yaml2obj.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/SmallVectorMemoryBuffer.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/YAMLParser.h"
#include "llvm/Support/YAMLTraits.h"
#include <unistd.h>
#include "gtest/gtest.h"

Macros

#define TEST_DATA_BASE_DIR   "bartleby/tests/Bartleby/testdata/yaml"
 Base directory for test data based on yaml files.
 
#define ASSERT_SYM_RESOLVE(_b_, _name_)
 Resolves a symbol in the map of symbols.
 
#define ASSERT_SYM_GLOBALNESS(_b_, _name_, _exp_)
 Asserts the globalness of a symbol.
 
#define ASSERT_SYM_LOCAL(_b_, _name_)    ASSERT_SYM_GLOBALNESS((_b_), (_name_), false)
 Asserts that a symbol is local.
 
#define ASSERT_SYM_GLOBAL(_b_, _name_)    ASSERT_SYM_GLOBALNESS((_b_), (_name_), true)
 Asserts that a symbol is local.
 
#define ASSERT_SYM_DEFINEDNESS(_b_, _name_, _exp_)
 Asserts the definedness of a symbol.
 
#define ASSERT_SYM_UNDEFINED(_b_, _name_)    ASSERT_SYM_DEFINEDNESS((_b_), (_name_), false)
 Asserts that a symbol is undefined.
 
#define ASSERT_SYM_DEFINED(_b_, _name_)    ASSERT_SYM_DEFINEDNESS((_b_), (_name_), true)
 Asserts that a symbol is defined.
 
#define ASSERT_SYM_OVERWRITTEN(_b_, _name_, _exp_)
 Asserts the overwrittenness of a symbol.
 
#define ASSERT_SYM_WILL_BE_RENAMED(_b_, _name_)    ASSERT_SYM_OVERWRITTEN((_b_), (_name_), true)
 Asserts that a symbol name will be overwritten.
 
#define ASSERT_SYM_WILL_NOT_BE_RENAMED(_b_, _name_)    ASSERT_SYM_OVERWRITTEN((_b_), (_name_), false)
 Asserts that a symbol name will NOT be overwritten.
 

Functions

 TEST (BartleByObjectYamlELF, Object386)
 
 TEST (BartleByObjectYamlError, ObjectTypeMisMatch)
 Test that passing two objects with different format types is an error.
 
 TEST (BartleByObjectYamlELF, SymbolInBSS)
 Test that a symbol in a BSS section is well renamed.
 
 TEST (BartlebyCAPI, CAPI)
 Test the C API.
 
 TEST (BartlebyCAPI, CAPI_Invalid_Input)
 Test the C API with invalid inputs.
 

Detailed Description

Unit tests for Bartleby.

Author
thb-sb

Macro Definition Documentation

◆ ASSERT_SYM_DEFINED

#define ASSERT_SYM_DEFINED (   _b_,
  _name_ 
)     ASSERT_SYM_DEFINEDNESS((_b_), (_name_), true)

Asserts that a symbol is defined.

Parameters
_b_Bartleby handle.
_name_Name of the symbol.

◆ ASSERT_SYM_DEFINEDNESS

#define ASSERT_SYM_DEFINEDNESS (   _b_,
  _name_,
  _exp_ 
)
Value:
do { \
ASSERT_SYM_RESOLVE((_b_), (_name_)); \
ASSERT_EQ(_s_->getValue().isDefined(), (_exp_)); \
} while (0)

Asserts the definedness of a symbol.

Parameters
_b_Bartleby handle.
_name_Name of the symbol.

◆ ASSERT_SYM_GLOBAL

#define ASSERT_SYM_GLOBAL (   _b_,
  _name_ 
)     ASSERT_SYM_GLOBALNESS((_b_), (_name_), true)

Asserts that a symbol is local.

Parameters
_b_Bartleby handle.
_name_Name of the symbol.

◆ ASSERT_SYM_GLOBALNESS

#define ASSERT_SYM_GLOBALNESS (   _b_,
  _name_,
  _exp_ 
)
Value:
do { \
ASSERT_SYM_RESOLVE((_b_), (_name_)); \
ASSERT_EQ(_s_->getValue().isGlobal(), (_exp_)); \
} while (0)

Asserts the globalness of a symbol.

Parameters
_b_Bartleby handle.
_name_Name of the symbol.

◆ ASSERT_SYM_LOCAL

#define ASSERT_SYM_LOCAL (   _b_,
  _name_ 
)     ASSERT_SYM_GLOBALNESS((_b_), (_name_), false)

Asserts that a symbol is local.

Parameters
_b_Bartleby handle.
_name_Name of the symbol.

◆ ASSERT_SYM_OVERWRITTEN

#define ASSERT_SYM_OVERWRITTEN (   _b_,
  _name_,
  _exp_ 
)
Value:
do { \
ASSERT_SYM_RESOLVE((_b_), (_name_)); \
ASSERT_TRUE(_s_->getValue().getOverwriteName().has_value() == _exp_); \
} while (0)

Asserts the overwrittenness of a symbol.

Parameters
_b_Bartleby handle.
_name_Name of the symbol.

◆ ASSERT_SYM_RESOLVE

#define ASSERT_SYM_RESOLVE (   _b_,
  _name_ 
)
Value:
const auto &_s_ = (_b_).getSymbols().find((_name_)); \
ASSERT_NE(_s_, (_b_).getSymbols().end()) \
<< "Symbol " << (_name_) << " not found"

Resolves a symbol in the map of symbols.

Parameters
_b_Bartleby handle.
_name_Name of the symbol.
Returns
The symbol entry.

◆ ASSERT_SYM_UNDEFINED

#define ASSERT_SYM_UNDEFINED (   _b_,
  _name_ 
)     ASSERT_SYM_DEFINEDNESS((_b_), (_name_), false)

Asserts that a symbol is undefined.

Parameters
_b_Bartleby handle.
_name_Name of the symbol.

◆ ASSERT_SYM_WILL_BE_RENAMED

#define ASSERT_SYM_WILL_BE_RENAMED (   _b_,
  _name_ 
)     ASSERT_SYM_OVERWRITTEN((_b_), (_name_), true)

Asserts that a symbol name will be overwritten.

Parameters
_b_Bartleby handle.
_name_Name of the symbol.

◆ ASSERT_SYM_WILL_NOT_BE_RENAMED

#define ASSERT_SYM_WILL_NOT_BE_RENAMED (   _b_,
  _name_ 
)     ASSERT_SYM_OVERWRITTEN((_b_), (_name_), false)

Asserts that a symbol name will NOT be overwritten.

Parameters
_b_Bartleby handle.
_name_Name of the symbol.

Function Documentation

◆ TEST() [1/5]

TEST ( BartlebyCAPI  ,
CAPI   
)

Test the C API.

355 {
356 llvm::SmallVector<llvm::object::OwningBinary<llvm::object::Binary>, 2>
357 Objects;
358 ASSERT_TRUE(YAML2Objects("symbols_visibility.yaml",
359 llvm::Triple::ObjectFormatType::ELF, Objects, 2));
360
361 auto* bh = ::saq_bartleby_new();
362 ASSERT_NE(bh, nullptr);
363
364 const auto data = Objects[0].getBinary()->getData();
365
366 ASSERT_EQ(::saq_bartleby_add_binary(bh, data.data(), data.size()), 0);
367
368 ASSERT_EQ(::saq_bartleby_set_prefix(bh, "prefix_"), 0);
369
370 void* out = nullptr;
371 size_t out_n = 0;
372
373 ASSERT_EQ(::saq_bartleby_build_archive(bh, &out, &out_n), 0);
374 ASSERT_TRUE(out_n > 0);
375 ASSERT_NE(out, nullptr);
376
377 ::free(out);
378}
SAQ_BARTLEBY_API struct BartlebyHandle * saq_bartleby_new(void)
Allocates a new Bartleby handle.
Definition: Bartleby-c.cpp:47
SAQ_BARTLEBY_API int saq_bartleby_build_archive(struct BartlebyHandle *bh, void **s, size_t *n)
Builds the final archive and writes its content to a buffer.
Definition: Bartleby-c.cpp:98
SAQ_BARTLEBY_API int saq_bartleby_set_prefix(struct BartlebyHandle *bh, const char *prefix)
Applies a prefix to all global and defined symbols.
Definition: Bartleby-c.cpp:55
SAQ_BARTLEBY_API int saq_bartleby_add_binary(struct BartlebyHandle *bh, const void *s, const size_t n)
Adds a new binary to Bartleby.
Definition: Bartleby-c.cpp:69

◆ TEST() [2/5]

TEST ( BartlebyCAPI  ,
CAPI_Invalid_Input   
)

Test the C API with invalid inputs.

381 {
382 auto* bh = ::saq_bartleby_new();
383 ASSERT_NE(bh, nullptr);
384 char c = 0x42;
385
386 ASSERT_EQ(::saq_bartleby_add_binary(nullptr, &c, 1), EINVAL);
387 ASSERT_EQ(::saq_bartleby_add_binary(bh, nullptr, 1), EINVAL);
388 ASSERT_EQ(::saq_bartleby_add_binary(bh, &c, 0), EINVAL);
389 ASSERT_EQ(::saq_bartleby_add_binary(nullptr, nullptr, 0), EINVAL);
390
391 void* out = nullptr;
392 size_t n = 0;
393 ASSERT_EQ(::saq_bartleby_build_archive(nullptr, &out, &n), EINVAL);
394 ASSERT_EQ(::saq_bartleby_build_archive(bh, nullptr, nullptr), EINVAL);
395 bh = ::saq_bartleby_new();
396 ASSERT_EQ(::saq_bartleby_build_archive(bh, &out, nullptr), EINVAL);
397 bh = ::saq_bartleby_new();
398 ASSERT_EQ(::saq_bartleby_build_archive(bh, nullptr, &n), EINVAL);
399}

◆ TEST() [3/5]

TEST ( BartleByObjectYamlELF  ,
Object386   
)
241 {
242 llvm::SmallVector<llvm::object::OwningBinary<llvm::object::Binary>, 2>
243 Objects;
244 ASSERT_TRUE(YAML2Objects("symbols_visibility.yaml",
245 llvm::Triple::ObjectFormatType::ELF, Objects, 2));
246
247 llvm::DebugFlag = true;
248 Bartleby B;
249 auto Err = B.addBinary(std::move(Objects[0]));
250 ASSERT_FALSE(Err);
251
252 ASSERT_SYM_DEFINED(B, "defined_local_symbol");
253 ASSERT_SYM_LOCAL(B, "defined_local_symbol");
254
255 ASSERT_SYM_DEFINED(B, "defined_global_symbol");
256 ASSERT_SYM_GLOBAL(B, "defined_global_symbol");
257
258 ASSERT_SYM_UNDEFINED(B, "undefined_symbol");
259 ASSERT_SYM_GLOBAL(B, "undefined_symbol");
260
261 ASSERT_SYM_UNDEFINED(B, "weak_symbol");
262 ASSERT_SYM_LOCAL(B, "weak_symbol");
263
265
266 ASSERT_SYM_WILL_NOT_BE_RENAMED(B, "defined_local_symbol");
267 ASSERT_SYM_WILL_BE_RENAMED(B, "defined_global_symbol");
268 ASSERT_SYM_WILL_NOT_BE_RENAMED(B, "undefined_symbol");
269 ASSERT_SYM_WILL_NOT_BE_RENAMED(B, "weak_symbol");
270
271 Err = B.addBinary(std::move(Objects[1]));
272 ASSERT_FALSE(Err);
273
274 ASSERT_SYM_DEFINED(B, "defined_local_symbol");
275 ASSERT_SYM_LOCAL(B, "defined_local_symbol");
276
277 ASSERT_SYM_DEFINED(B, "defined_global_symbol");
278 ASSERT_SYM_GLOBAL(B, "defined_global_symbol");
279
280 // Object 2 defined `undefined_symbol`.
281 ASSERT_SYM_DEFINED(B, "undefined_symbol");
282 ASSERT_SYM_GLOBAL(B, "undefined_symbol");
283
284 ASSERT_SYM_UNDEFINED(B, "weak_symbol");
285 ASSERT_SYM_LOCAL(B, "weak_symbol");
286
288
289 ASSERT_SYM_WILL_NOT_BE_RENAMED(B, "defined_local_symbol");
290 ASSERT_SYM_WILL_BE_RENAMED(B, "defined_global_symbol");
291 ASSERT_SYM_WILL_BE_RENAMED(B, "undefined_symbol");
292 ASSERT_SYM_WILL_NOT_BE_RENAMED(B, "weak_symbol");
293
294 auto ArOrErr = Bartleby::buildFinalArchive(std::move(B));
295 ASSERT_TRUE(!!ArOrErr);
296 auto ArContent = std::move(*ArOrErr);
297
298 auto Ar = llvm::object::createBinary(*ArContent);
299 ASSERT_TRUE(!!Ar);
300
301 auto OwningAr = llvm::object::OwningBinary<llvm::object::Binary>(
302 std::move(*Ar), std::move(ArContent));
303
304 B = Bartleby();
305 ASSERT_FALSE(B.addBinary(std::move(OwningAr)));
306
307 ASSERT_SYM_DEFINED(B, "defined_local_symbol");
308 ASSERT_SYM_LOCAL(B, "defined_local_symbol");
309
310 ASSERT_SYM_DEFINED(B, "prefix_defined_global_symbol");
311 ASSERT_SYM_GLOBAL(B, "prefix_defined_global_symbol");
312
313 ASSERT_SYM_DEFINED(B, "prefix_undefined_symbol");
314 ASSERT_SYM_GLOBAL(B, "prefix_undefined_symbol");
315
316 ASSERT_SYM_UNDEFINED(B, "weak_symbol");
317 ASSERT_SYM_LOCAL(B, "weak_symbol");
318}
#define ASSERT_SYM_UNDEFINED(_b_, _name_)
Asserts that a symbol is undefined.
Definition: Tests.cpp:203
#define ASSERT_SYM_WILL_NOT_BE_RENAMED(_b_, _name_)
Asserts that a symbol name will NOT be overwritten.
Definition: Tests.cpp:234
#define ASSERT_SYM_WILL_BE_RENAMED(_b_, _name_)
Asserts that a symbol name will be overwritten.
Definition: Tests.cpp:227
#define ASSERT_SYM_GLOBAL(_b_, _name_)
Asserts that a symbol is local.
Definition: Tests.cpp:186
#define ASSERT_SYM_DEFINED(_b_, _name_)
Asserts that a symbol is defined.
Definition: Tests.cpp:210
#define ASSERT_SYM_LOCAL(_b_, _name_)
Asserts that a symbol is local.
Definition: Tests.cpp:179
Bartleby handle.
Definition: Bartleby.h:85
llvm::Error addBinary(llvm::object::OwningBinary< llvm::object::Binary > Binary) noexcept
Adds a new binary to Bartleby.
Definition: Bartleby.cpp:152
size_t prefixGlobalAndDefinedSymbols(llvm::StringRef Prefix) noexcept
Applies a prefix to all global and defined symbols.
Definition: Bartleby.cpp:218
static llvm::Error buildFinalArchive(Bartleby &&B, llvm::StringRef OutFilepath) noexcept
Builds the final archive and writes its content to a file.
Definition: ArchiveWriter.cpp:327

◆ TEST() [4/5]

TEST ( BartleByObjectYamlELF  ,
SymbolInBSS   
)

Test that a symbol in a BSS section is well renamed.

338 {
339 llvm::SmallVector<llvm::object::OwningBinary<llvm::object::Binary>, 1>
340 Objects;
341 ASSERT_TRUE(YAML2Objects("uninit_symbol_in_BSS.yaml",
342 llvm::Triple::ObjectFormatType::ELF, Objects));
343 Bartleby B;
344 auto Err = B.addBinary(std::move(Objects[0]));
345 ASSERT_FALSE(Err);
346
347 ASSERT_SYM_DEFINED(B, "thread_local_var");
348 ASSERT_SYM_GLOBAL(B, "thread_local_var");
349
351 ASSERT_SYM_WILL_BE_RENAMED(B, "thread_local_var");
352}

◆ TEST() [5/5]

TEST ( BartleByObjectYamlError  ,
ObjectTypeMisMatch   
)

Test that passing two objects with different format types is an error.

322 {
323 llvm::SmallVector<llvm::object::OwningBinary<llvm::object::Binary>, 2>
324 Objects;
325 ASSERT_TRUE(YAML2Objects("arm64.yaml", llvm::Triple::ObjectFormatType::MachO,
326 Objects));
327
328 ASSERT_TRUE(YAML2Objects("simple_x86_64.yaml",
329 llvm::Triple::ObjectFormatType::ELF, Objects));
330
331 Bartleby B;
332 ASSERT_FALSE(B.addBinary(std::move(Objects[0])));
333 auto Err = B.addBinary(std::move(Objects[1]));
334 ASSERT_TRUE(!!Err);
335}