1 //===-- ModuleSpecTest.cpp ------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "TestingSupport/SubsystemRAII.h"
10 #include "TestingSupport/TestUtilities.h"
12 #include "lldb/Core/Module.h"
13 #include "lldb/Core/ModuleSpec.h"
14 #include "lldb/Utility/DataBuffer.h"
16 #include "Plugins/ObjectFile/ELF/ObjectFileELF.h"
17 #include "Plugins/ObjectFile/Mach-O/ObjectFileMachO.h"
18 #include "Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.h"
20 #include "gtest/gtest.h"
23 using namespace lldb_private
;
25 extern const char *TestMainArgv0
;
27 // This test file intentionally doesn't initialize the FileSystem.
28 // Everything in this file should be able to run without requiring
29 // any interaction with the FileSystem class; by keeping it
30 // uninitialized, it will assert if anything tries to interact with
33 TEST(ModuleSpecTest
, InvalidInMemoryBuffer
) {
34 uint8_t Invalid
[] = "This is not a binary file.";
35 DataBufferSP InvalidBufferSP
=
36 std::make_shared
<DataBufferUnowned
>(Invalid
, sizeof(Invalid
));
37 ModuleSpec
Spec(FileSpec(), UUID(), InvalidBufferSP
);
39 auto InvalidModuleSP
= std::make_shared
<Module
>(Spec
);
40 ASSERT_EQ(InvalidModuleSP
->GetObjectFile(), nullptr);
43 TEST(ModuleSpecTest
, InvalidInMemoryBufferValidFile
) {
44 uint8_t Invalid
[] = "This is not a binary file.";
45 DataBufferSP InvalidBufferSP
=
46 std::make_shared
<DataBufferUnowned
>(Invalid
, sizeof(Invalid
));
47 ModuleSpec
Spec(FileSpec(TestMainArgv0
), UUID(), InvalidBufferSP
);
49 auto InvalidModuleSP
= std::make_shared
<Module
>(Spec
);
50 ASSERT_EQ(InvalidModuleSP
->GetObjectFile(), nullptr);
53 TEST(ModuleSpecTest
, TestELFFile
) {
54 SubsystemRAII
<ObjectFileELF
> subsystems
;
56 auto ExpectedFile
= TestFile::fromYaml(R
"(
66 Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
67 AddressAlign: 0x0000000000000010
70 ASSERT_THAT_EXPECTED(ExpectedFile
, llvm::Succeeded());
72 auto M
= std::make_shared
<Module
>(ExpectedFile
->moduleSpec());
73 ObjectFile
*OF
= M
->GetObjectFile();
75 ASSERT_EQ(llvm::isa
<ObjectFileELF
>(OF
), true);
78 TEST(ModuleSpecTest
, TestCOFFFile
) {
79 SubsystemRAII
<ObjectFilePECOFF
> subsystems
;
81 auto ExpectedFile
= TestFile::fromYaml(R
"(
84 AddressOfEntryPoint: 0
86 SectionAlignment: 4096
88 MajorOperatingSystemVersion: 6
89 MinorOperatingSystemVersion: 0
92 MajorSubsystemVersion: 6
93 MinorSubsystemVersion: 0
94 Subsystem: IMAGE_SUBSYSTEM_WINDOWS_CUI
95 DLLCharacteristics: [ IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA, IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE, IMAGE_DLL_CHARACTERISTICS_NX_COMPAT ]
96 SizeOfStackReserve: 1048576
97 SizeOfStackCommit: 4096
98 SizeOfHeapReserve: 1048576
99 SizeOfHeapCommit: 4096
101 Machine: IMAGE_FILE_MACHINE_AMD64
102 Characteristics: [ IMAGE_FILE_EXECUTABLE_IMAGE, IMAGE_FILE_LARGE_ADDRESS_AWARE ]
105 Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
111 ASSERT_THAT_EXPECTED(ExpectedFile
, llvm::Succeeded());
113 auto M
= std::make_shared
<Module
>(ExpectedFile
->moduleSpec());
114 ObjectFile
*OF
= M
->GetObjectFile();
116 ASSERT_EQ(llvm::isa
<ObjectFilePECOFF
>(OF
), true);
119 TEST(ModuleSpecTest
, TestMachOFile
) {
120 SubsystemRAII
<ObjectFileMachO
> subsystems
;
122 auto ExpectedFile
= TestFile::fromYaml(R
"(
127 cpusubtype: 0x00000000
148 addr: 0x0000000000000000
155 reserved1: 0x00000000
156 reserved2: 0x00000000
157 reserved3: 0x00000000
160 ASSERT_THAT_EXPECTED(ExpectedFile
, llvm::Succeeded());
162 auto M
= std::make_shared
<Module
>(ExpectedFile
->moduleSpec());
163 ObjectFile
*OF
= M
->GetObjectFile();
165 ASSERT_EQ(llvm::isa
<ObjectFileMachO
>(OF
), true);