[PATCH] D103546: Added ELFObjectFileBase::checkMagic() for checking ELF magic word.
Vyacheslav Zakharin via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri Jun 4 11:08:42 PDT 2021
vzakhari updated this revision to Diff 349923.
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D103546/new/
https://reviews.llvm.org/D103546
Files:
llvm/include/llvm/Object/ELFObjectFile.h
llvm/unittests/Object/ELFObjectFileTest.cpp
Index: llvm/unittests/Object/ELFObjectFileTest.cpp
===================================================================
--- llvm/unittests/Object/ELFObjectFileTest.cpp
+++ llvm/unittests/Object/ELFObjectFileTest.cpp
@@ -23,8 +23,8 @@
std::vector<uint8_t> Data;
template <typename T>
- std::vector<uint8_t> makeElfData(uint8_t Class, uint8_t Encoding,
- uint16_t Machine) {
+ static std::vector<uint8_t> makeElfData(uint8_t Class, uint8_t Encoding,
+ uint16_t Machine) {
T Ehdr{}; // Zero-initialise the header.
Ehdr.e_ident[ELF::EI_MAG0] = 0x7f;
Ehdr.e_ident[ELF::EI_MAG1] = 'E';
@@ -590,3 +590,25 @@
DoCheck(OverLimitNumBlocks,
"ULEB128 value at offset 0x8 exceeds UINT32_MAX (0x100000000)");
}
+
+TEST(ELFObjectFileTest, InvalidMagicTest) {
+ std::vector<uint8_t> Data = DataForTest::makeElfData<ELF::Elf64_Ehdr>(
+ ELF::ELFCLASS32, ELF::ELFDATA2LSB, ELF::EM_NONE);
+ // Replace the ELF with an ORC.
+ Data[ELF::EI_MAG1] = 'O';
+ Data[ELF::EI_MAG2] = 'R';
+ Data[ELF::EI_MAG3] = 'C';
+
+ // In the current implementation the data is still recognized
+ // as an ELF even though it is an ORC.
+ Expected<std::unique_ptr<ObjectFile>> ELFObjOrErr =
+ object::ObjectFile::createELFObjectFile(
+ MemoryBufferRef(toStringRef(Data), "dummyELF"));
+ ASSERT_THAT_EXPECTED(ELFObjOrErr, Succeeded());
+
+ auto *Object = dyn_cast<const ELFObjectFileBase>(ELFObjOrErr->get());
+ // It should be a valid ELFObjectFileBase object.
+ EXPECT_TRUE(Object != nullptr);
+ // checkMagic() recognizes a real ELF.
+ EXPECT_TRUE(!Object->checkMagic());
+}
Index: llvm/include/llvm/Object/ELFObjectFile.h
===================================================================
--- llvm/include/llvm/Object/ELFObjectFile.h
+++ llvm/include/llvm/Object/ELFObjectFile.h
@@ -96,6 +96,8 @@
std::vector<std::pair<Optional<DataRefImpl>, uint64_t>>
getPltAddresses() const;
+
+ virtual bool checkMagic() const = 0;
};
class ELFSectionRef : public SectionRef {
@@ -229,6 +231,7 @@
uint16_t getEMachine() const override;
uint16_t getEType() const override;
uint64_t getSymbolSize(DataRefImpl Sym) const override;
+ bool checkMagic() const override;
public:
LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
@@ -623,6 +626,10 @@
return (*SymOrErr)->st_size;
}
+template <class ELFT> bool ELFObjectFile<ELFT>::checkMagic() const {
+ return EF.getHeader().checkMagic();
+}
+
template <class ELFT>
uint64_t ELFObjectFile<ELFT>::getCommonSymbolSizeImpl(DataRefImpl Symb) const {
return getSymbolSize(Symb);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D103546.349923.patch
Type: text/x-patch
Size: 2635 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210604/a9a44e63/attachment.bin>
More information about the llvm-commits
mailing list