[llvm] [llvm][ELF]Add Shdr check for getBuildID (PR #126537)
Ruoyu Qiu via llvm-commits
llvm-commits at lists.llvm.org
Tue Feb 25 03:24:56 PST 2025
https://github.com/cabbaken updated https://github.com/llvm/llvm-project/pull/126537
>From a19e1a05cde04298aee6732811353d1814d21eb8 Mon Sep 17 00:00:00 2001
From: Ruoyu Qiu <cabbaken at outlook.com>
Date: Mon, 10 Feb 2025 15:56:31 +0000
Subject: [PATCH 01/10] [llvm-objdump][ELF]Add Shdr buildID check(#126418)
Add Section Header check for getBuildID, fix crash
with invalid Program Header.
Signed-off-by: Ruoyu Qiu <cabbaken at outlook.com>
---
llvm/lib/Object/BuildID.cpp | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/llvm/lib/Object/BuildID.cpp b/llvm/lib/Object/BuildID.cpp
index 89d6bc3ab550d..d446057129827 100644
--- a/llvm/lib/Object/BuildID.cpp
+++ b/llvm/lib/Object/BuildID.cpp
@@ -24,6 +24,20 @@ using namespace llvm::object;
namespace {
template <typename ELFT> BuildIDRef getBuildID(const ELFFile<ELFT> &Obj) {
+ auto Sections = cantFail(Obj.sections());
+ if (!Sections.empty()) {
+ for (const auto &S : Sections) {
+ if (S.sh_type != ELF::SHT_NOTE)
+ continue;
+ Error Err = Error::success();
+ for (auto N : Obj.notes(S, Err))
+ if (N.getType() == ELF::NT_GNU_BUILD_ID &&
+ N.getName() == ELF::ELF_NOTE_GNU)
+ return N.getDesc(S.sh_addralign);
+ consumeError(std::move(Err));
+ }
+ }
+
auto PhdrsOrErr = Obj.program_headers();
if (!PhdrsOrErr) {
consumeError(PhdrsOrErr.takeError());
>From 169f9db3abaf765d58a4f7fd3e8cbc44469fafbe Mon Sep 17 00:00:00 2001
From: Ruoyu Qiu <cabbaken at outlook.com>
Date: Tue, 11 Feb 2025 08:29:19 +0000
Subject: [PATCH 02/10] [LLVM][ELF]Add Align check for getDesc()
Add Aligh check to avoid crashing if Aligh is 0.
Signed-off-by: Ruoyu Qiu <cabbaken at outlook.com>
---
llvm/include/llvm/Object/ELFTypes.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/include/llvm/Object/ELFTypes.h b/llvm/include/llvm/Object/ELFTypes.h
index 87e4dbe448091..8441009900316 100644
--- a/llvm/include/llvm/Object/ELFTypes.h
+++ b/llvm/include/llvm/Object/ELFTypes.h
@@ -660,7 +660,7 @@ class Elf_Note_Impl {
/// Get the note's descriptor.
ArrayRef<uint8_t> getDesc(size_t Align) const {
- if (!Nhdr.n_descsz)
+ if (!Nhdr.n_descsz || !Align)
return ArrayRef<uint8_t>();
return ArrayRef<uint8_t>(
reinterpret_cast<const uint8_t *>(&Nhdr) +
>From 0c6d355e70a8d563c42a53b33dc87854d91b4cba Mon Sep 17 00:00:00 2001
From: Ruoyu Qiu <cabbaken at outlook.com>
Date: Wed, 12 Feb 2025 10:49:06 +0000
Subject: [PATCH 03/10] Optimize getBuildID() code style
Signed-off-by: Ruoyu Qiu <cabbaken at outlook.com>
---
llvm/lib/Object/BuildID.cpp | 30 +++++++++++++++++-------------
1 file changed, 17 insertions(+), 13 deletions(-)
diff --git a/llvm/lib/Object/BuildID.cpp b/llvm/lib/Object/BuildID.cpp
index d446057129827..08f1502b362b0 100644
--- a/llvm/lib/Object/BuildID.cpp
+++ b/llvm/lib/Object/BuildID.cpp
@@ -24,20 +24,27 @@ using namespace llvm::object;
namespace {
template <typename ELFT> BuildIDRef getBuildID(const ELFFile<ELFT> &Obj) {
+ auto findBuildID = [&Obj](const auto &ShdrOrPhdr,
+ uint64_t Alignment) -> std::optional<BuildIDRef> {
+ Error Err = Error::success();
+ for (auto N : Obj.notes(ShdrOrPhdr, Err))
+ if (N.getType() == ELF::NT_GNU_BUILD_ID &&
+ N.getName() == ELF::ELF_NOTE_GNU)
+ return N.getDesc(Alignment);
+ consumeError(std::move(Err));
+ return std::nullopt;
+ };
+
auto Sections = cantFail(Obj.sections());
if (!Sections.empty()) {
for (const auto &S : Sections) {
if (S.sh_type != ELF::SHT_NOTE)
continue;
- Error Err = Error::success();
- for (auto N : Obj.notes(S, Err))
- if (N.getType() == ELF::NT_GNU_BUILD_ID &&
- N.getName() == ELF::ELF_NOTE_GNU)
- return N.getDesc(S.sh_addralign);
- consumeError(std::move(Err));
+ auto ShdrRes = findBuildID(S, S.sh_addralign);
+ if (ShdrRes)
+ return ShdrRes.value();
}
}
-
auto PhdrsOrErr = Obj.program_headers();
if (!PhdrsOrErr) {
consumeError(PhdrsOrErr.takeError());
@@ -46,12 +53,9 @@ template <typename ELFT> BuildIDRef getBuildID(const ELFFile<ELFT> &Obj) {
for (const auto &P : *PhdrsOrErr) {
if (P.p_type != ELF::PT_NOTE)
continue;
- Error Err = Error::success();
- for (auto N : Obj.notes(P, Err))
- if (N.getType() == ELF::NT_GNU_BUILD_ID &&
- N.getName() == ELF::ELF_NOTE_GNU)
- return N.getDesc(P.p_align);
- consumeError(std::move(Err));
+ auto PhdrRes = findBuildID(P, P.p_align);
+ if (PhdrRes)
+ return PhdrRes.value();
}
return {};
}
>From 40ff1ffaf89597089763fa99c3cf6024bb8895da Mon Sep 17 00:00:00 2001
From: Ruoyu Qiu <cabbaken at outlook.com>
Date: Thu, 13 Feb 2025 02:34:01 +0000
Subject: [PATCH 04/10] Fix alignment judge of getDesc()
Signed-off-by: Ruoyu Qiu <cabbaken at outlook.com>
---
llvm/include/llvm/Object/ELFTypes.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/include/llvm/Object/ELFTypes.h b/llvm/include/llvm/Object/ELFTypes.h
index 8441009900316..87e4dbe448091 100644
--- a/llvm/include/llvm/Object/ELFTypes.h
+++ b/llvm/include/llvm/Object/ELFTypes.h
@@ -660,7 +660,7 @@ class Elf_Note_Impl {
/// Get the note's descriptor.
ArrayRef<uint8_t> getDesc(size_t Align) const {
- if (!Nhdr.n_descsz || !Align)
+ if (!Nhdr.n_descsz)
return ArrayRef<uint8_t>();
return ArrayRef<uint8_t>(
reinterpret_cast<const uint8_t *>(&Nhdr) +
>From 0a502cd7fbc1c92d81efd82fb3fc06ee5c494ea9 Mon Sep 17 00:00:00 2001
From: Ruoyu Qiu <cabbaken at outlook.com>
Date: Thu, 13 Feb 2025 02:46:54 +0000
Subject: [PATCH 05/10] Add AddressAlign to symbolize-build-id.test to avoid
crashing of getDesc().
Signed-off-by: Ruoyu Qiu <cabbaken at outlook.com>
---
llvm/test/DebugInfo/symbolize-build-id.test | 1 +
1 file changed, 1 insertion(+)
diff --git a/llvm/test/DebugInfo/symbolize-build-id.test b/llvm/test/DebugInfo/symbolize-build-id.test
index d63f43ff859e6..2620718293320 100644
--- a/llvm/test/DebugInfo/symbolize-build-id.test
+++ b/llvm/test/DebugInfo/symbolize-build-id.test
@@ -21,6 +21,7 @@ Sections:
Type: SHT_NOTE
Flags: [ SHF_ALLOC ]
Content: 040000000800000003000000474e5500abb50d82b6bdc861
+ AddressAlign: 4
ProgramHeaders:
- Type: PT_NOTE
Flags: [ PF_R ]
>From 719333976b47e4b2fc60df5de32d8641fdf4e382 Mon Sep 17 00:00:00 2001
From: Ruoyu Qiu <cabbaken at outlook.com>
Date: Thu, 13 Feb 2025 09:02:09 +0000
Subject: [PATCH 06/10] Optimize code style.
Signed-off-by: Ruoyu Qiu <cabbaken at outlook.com>
---
llvm/lib/Object/BuildID.cpp | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/llvm/lib/Object/BuildID.cpp b/llvm/lib/Object/BuildID.cpp
index 08f1502b362b0..860f13f921fd6 100644
--- a/llvm/lib/Object/BuildID.cpp
+++ b/llvm/lib/Object/BuildID.cpp
@@ -40,8 +40,7 @@ template <typename ELFT> BuildIDRef getBuildID(const ELFFile<ELFT> &Obj) {
for (const auto &S : Sections) {
if (S.sh_type != ELF::SHT_NOTE)
continue;
- auto ShdrRes = findBuildID(S, S.sh_addralign);
- if (ShdrRes)
+ if (std::optional<BuildIDRef> ShdrRes = findBuildID(S, S.sh_addralign))
return ShdrRes.value();
}
}
@@ -53,8 +52,7 @@ template <typename ELFT> BuildIDRef getBuildID(const ELFFile<ELFT> &Obj) {
for (const auto &P : *PhdrsOrErr) {
if (P.p_type != ELF::PT_NOTE)
continue;
- auto PhdrRes = findBuildID(P, P.p_align);
- if (PhdrRes)
+ if (std::optional<BuildIDRef> PhdrRes = findBuildID(P, P.p_align))
return PhdrRes.value();
}
return {};
>From c388efa7804702b6decc726eef8149e8d594cbfb Mon Sep 17 00:00:00 2001
From: Ruoyu Qiu <cabbaken at outlook.com>
Date: Thu, 13 Feb 2025 11:40:10 +0000
Subject: [PATCH 07/10] remove unneeded empty check
Signed-off-by: Ruoyu Qiu <cabbaken at outlook.com>
---
llvm/lib/Object/BuildID.cpp | 12 +++++-------
1 file changed, 5 insertions(+), 7 deletions(-)
diff --git a/llvm/lib/Object/BuildID.cpp b/llvm/lib/Object/BuildID.cpp
index 860f13f921fd6..d1ee597a11327 100644
--- a/llvm/lib/Object/BuildID.cpp
+++ b/llvm/lib/Object/BuildID.cpp
@@ -36,13 +36,11 @@ template <typename ELFT> BuildIDRef getBuildID(const ELFFile<ELFT> &Obj) {
};
auto Sections = cantFail(Obj.sections());
- if (!Sections.empty()) {
- for (const auto &S : Sections) {
- if (S.sh_type != ELF::SHT_NOTE)
- continue;
- if (std::optional<BuildIDRef> ShdrRes = findBuildID(S, S.sh_addralign))
- return ShdrRes.value();
- }
+ for (const auto &S : Sections) {
+ if (S.sh_type != ELF::SHT_NOTE)
+ continue;
+ if (std::optional<BuildIDRef> ShdrRes = findBuildID(S, S.sh_addralign))
+ return ShdrRes.value();
}
auto PhdrsOrErr = Obj.program_headers();
if (!PhdrsOrErr) {
>From 9393e975b90b288d014d3ee693ddb7c20e526d34 Mon Sep 17 00:00:00 2001
From: Ruoyu Qiu <cabbaken at outlook.com>
Date: Fri, 14 Feb 2025 15:01:21 +0000
Subject: [PATCH 08/10] Add invalid filesz of program header(PT_NOTE)
Signed-off-by: Ruoyu Qiu <cabbaken at outlook.com>
---
.../tools/llvm-objdump/ELF/invalid-phdr.test | 23 +++++++++++++++++++
1 file changed, 23 insertions(+)
diff --git a/llvm/test/tools/llvm-objdump/ELF/invalid-phdr.test b/llvm/test/tools/llvm-objdump/ELF/invalid-phdr.test
index 188c8eeeff491..91f8581d788bd 100644
--- a/llvm/test/tools/llvm-objdump/ELF/invalid-phdr.test
+++ b/llvm/test/tools/llvm-objdump/ELF/invalid-phdr.test
@@ -15,3 +15,26 @@ FileHeader:
Type: ET_EXEC
Machine: EM_X86_64
EPhOff: 0xffffff
+
+
+# RUN: yaml2obj --docnum=2 %s -o %t.note.err
+# RUN: llvm-objdump -D %t.note.err 2>&1 | \
+# RUN: FileCheck %s --check-prefix=PFILESIZE
+
+# PFILESIZE: Disassembly of section .note.gnu.build-id:
+
+--- !ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_EXEC
+ Machine: EM_X86_64
+Sections:
+ - Name: .note.gnu.build-id
+ Type: SHT_NOTE
+ Content: 040000000800000003000000474e5500abb50d82b6bdc861
+ AddressAlign: 0x04
+ProgramHeaders:
+ - Type: PT_NOTE
+ FileSize: 0xffffffffffffffff
+ Offset: 0x100
>From e07a49b24fffc39fec3d6a9d32d8a3d7ab33fe5b Mon Sep 17 00:00:00 2001
From: Ruoyu Qiu <cabbaken at outlook.com>
Date: Fri, 21 Feb 2025 16:12:00 +0000
Subject: [PATCH 09/10] add unit test of getBuildId()
Signed-off-by: Ruoyu Qiu <cabbaken at outlook.com>
---
.../tools/llvm-objdump/ELF/invalid-phdr.test | 23 -------
llvm/unittests/Object/BuildIDTest.cpp | 61 +++++++++++++++++++
llvm/unittests/Object/CMakeLists.txt | 1 +
3 files changed, 62 insertions(+), 23 deletions(-)
create mode 100644 llvm/unittests/Object/BuildIDTest.cpp
diff --git a/llvm/test/tools/llvm-objdump/ELF/invalid-phdr.test b/llvm/test/tools/llvm-objdump/ELF/invalid-phdr.test
index 91f8581d788bd..188c8eeeff491 100644
--- a/llvm/test/tools/llvm-objdump/ELF/invalid-phdr.test
+++ b/llvm/test/tools/llvm-objdump/ELF/invalid-phdr.test
@@ -15,26 +15,3 @@ FileHeader:
Type: ET_EXEC
Machine: EM_X86_64
EPhOff: 0xffffff
-
-
-# RUN: yaml2obj --docnum=2 %s -o %t.note.err
-# RUN: llvm-objdump -D %t.note.err 2>&1 | \
-# RUN: FileCheck %s --check-prefix=PFILESIZE
-
-# PFILESIZE: Disassembly of section .note.gnu.build-id:
-
---- !ELF
-FileHeader:
- Class: ELFCLASS64
- Data: ELFDATA2LSB
- Type: ET_EXEC
- Machine: EM_X86_64
-Sections:
- - Name: .note.gnu.build-id
- Type: SHT_NOTE
- Content: 040000000800000003000000474e5500abb50d82b6bdc861
- AddressAlign: 0x04
-ProgramHeaders:
- - Type: PT_NOTE
- FileSize: 0xffffffffffffffff
- Offset: 0x100
diff --git a/llvm/unittests/Object/BuildIDTest.cpp b/llvm/unittests/Object/BuildIDTest.cpp
new file mode 100644
index 0000000000000..34b8116f7067f
--- /dev/null
+++ b/llvm/unittests/Object/BuildIDTest.cpp
@@ -0,0 +1,61 @@
+//===- BuildIDTest.cpp - Tests for getBuildID ----------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Object/BuildID.h"
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Object/ELFObjectFile.h"
+#include "llvm/ObjectYAML/yaml2obj.h"
+#include "llvm/Support/YAMLTraits.h"
+#include "llvm/Testing/Support/Error.h"
+
+#include "gtest/gtest.h"
+
+using namespace llvm;
+using namespace llvm::object;
+
+template <class ELFT>
+static Expected<ELFObjectFile<ELFT>> toBinary(SmallVectorImpl<char> &Storage,
+ StringRef Yaml) {
+ raw_svector_ostream OS(Storage);
+ yaml::Input YIn(Yaml);
+ if (!yaml::convertYAML(YIn, OS, [](const Twine &Msg) {}))
+ return createStringError(std::errc::invalid_argument,
+ "unable to convert YAML");
+ return ELFObjectFile<ELFT>::create(MemoryBufferRef(OS.str(), "dummyELF"));
+}
+
+TEST(BuildIDTest, InvalidNoteFileSizeTest) {
+ SmallString<0> Storage;
+ Expected<ELFObjectFile<ELF64LE>> ElfOrErr = toBinary<ELF64LE>(Storage, R"(
+--- !ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_EXEC
+ Machine: EM_X86_64
+Sections:
+ - Name: .note.gnu.build-id
+ Type: SHT_NOTE
+ AddressAlign: 0x04
+ Notes:
+ - Name: "GNU"
+ Desc: "abb50d82b6bdc861"
+ Type: 3
+ProgramHeaders:
+ - Type: PT_NOTE
+ FileSize: 0xffffffffffffffff
+ Offset: 0x100
+)");
+ ASSERT_THAT_EXPECTED(ElfOrErr, Succeeded());
+ BuildIDRef BuildID = getBuildID(&ElfOrErr.get());
+ EXPECT_EQ(
+ StringRef(reinterpret_cast<const char *>(BuildID.data()), BuildID.size()),
+ "\xAB\xB5\x0D\x82\xB6\xBD\xC8\x61");
+}
\ No newline at end of file
diff --git a/llvm/unittests/Object/CMakeLists.txt b/llvm/unittests/Object/CMakeLists.txt
index 81bc4a5577e68..6f51ce4303213 100644
--- a/llvm/unittests/Object/CMakeLists.txt
+++ b/llvm/unittests/Object/CMakeLists.txt
@@ -7,6 +7,7 @@ set(LLVM_LINK_COMPONENTS
add_llvm_unittest(ObjectTests
ArchiveTest.cpp
+ BuildIDTest.cpp
COFFObjectFileTest.cpp
DXContainerTest.cpp
ELFObjectFileTest.cpp
>From 4e83f8a1dcc108fca38190f73fd0a169c9f12451 Mon Sep 17 00:00:00 2001
From: Ruoyu Qiu <cabbaken at outlook.com>
Date: Tue, 25 Feb 2025 11:24:04 +0000
Subject: [PATCH 10/10] add test cases for BuildIDTest.
Signed-off-by: Ruoyu Qiu <cabbaken at outlook.com>
---
llvm/unittests/Object/BuildIDTest.cpp | 66 ++++++++++++++++++++++++---
1 file changed, 60 insertions(+), 6 deletions(-)
diff --git a/llvm/unittests/Object/BuildIDTest.cpp b/llvm/unittests/Object/BuildIDTest.cpp
index 34b8116f7067f..6a60ded1406f1 100644
--- a/llvm/unittests/Object/BuildIDTest.cpp
+++ b/llvm/unittests/Object/BuildIDTest.cpp
@@ -7,7 +7,6 @@
//===----------------------------------------------------------------------===//
#include "llvm/Object/BuildID.h"
-#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Object/ELFObjectFile.h"
@@ -31,7 +30,61 @@ static Expected<ELFObjectFile<ELFT>> toBinary(SmallVectorImpl<char> &Storage,
return ELFObjectFile<ELFT>::create(MemoryBufferRef(OS.str(), "dummyELF"));
}
+static StringRef optionalSectionHeaderELF(bool WithSec) {
+ static std::string WithSection(R"(
+--- !ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_EXEC
+ Machine: EM_X86_64
+ProgramHeaders:
+ - Type: PT_NOTE
+ FileSize: 0xffffffffffffffff
+ FirstSec: .note.gnu.build-id
+ LastSec: .note.gnu.build-id
+Sections:
+ - Name: .note.gnu.build-id
+ Type: SHT_NOTE
+ AddressAlign: 0x04
+ Notes:
+ - Name: "GNU"
+ Desc: "abb50d82b6bdc861"
+ Type: 3
+)");
+ static std::string WithoutSection(WithSection + R"(
+ - Type: SectionHeaderTable
+ NoHeaders: true
+)");
+ if (WithSec)
+ return WithSection;
+ else
+ return WithoutSection;
+}
+
TEST(BuildIDTest, InvalidNoteFileSizeTest) {
+ SmallString<0> Storage;
+ Expected<ELFObjectFile<ELF64LE>> ElfOrErr =
+ toBinary<ELF64LE>(Storage, optionalSectionHeaderELF(true));
+ ASSERT_THAT_EXPECTED(ElfOrErr, Succeeded());
+ BuildIDRef BuildID = getBuildID(&ElfOrErr.get());
+ EXPECT_EQ(
+ StringRef(reinterpret_cast<const char *>(BuildID.data()), BuildID.size()),
+ "\xAB\xB5\x0D\x82\xB6\xBD\xC8\x61");
+}
+
+TEST(BuildIDTest, OnlyInvalidProgramHeader) {
+ SmallString<0> Storage;
+ Expected<ELFObjectFile<ELF64LE>> ElfOrErr =
+ toBinary<ELF64LE>(Storage, optionalSectionHeaderELF(false));
+ ASSERT_THAT_EXPECTED(ElfOrErr, Succeeded());
+ BuildIDRef BuildID = getBuildID(&ElfOrErr.get());
+ EXPECT_EQ(
+ StringRef(reinterpret_cast<const char *>(BuildID.data()), BuildID.size()),
+ "\xAB\xB5\x0D\x82\xB6\xBD\xC8\x61");
+}
+
+TEST(BuildIDTest, InvalidSectionHeader) {
SmallString<0> Storage;
Expected<ELFObjectFile<ELF64LE>> ElfOrErr = toBinary<ELF64LE>(Storage, R"(
--- !ELF
@@ -40,22 +93,23 @@ TEST(BuildIDTest, InvalidNoteFileSizeTest) {
Data: ELFDATA2LSB
Type: ET_EXEC
Machine: EM_X86_64
+ProgramHeaders:
+ - Type: PT_NOTE
+ FirstSec: .note.gnu.build-id
+ LastSec: .note.gnu.build-id
Sections:
- Name: .note.gnu.build-id
Type: SHT_NOTE
AddressAlign: 0x04
+ Offset: 0x8000
Notes:
- Name: "GNU"
Desc: "abb50d82b6bdc861"
Type: 3
-ProgramHeaders:
- - Type: PT_NOTE
- FileSize: 0xffffffffffffffff
- Offset: 0x100
)");
ASSERT_THAT_EXPECTED(ElfOrErr, Succeeded());
BuildIDRef BuildID = getBuildID(&ElfOrErr.get());
EXPECT_EQ(
StringRef(reinterpret_cast<const char *>(BuildID.data()), BuildID.size()),
"\xAB\xB5\x0D\x82\xB6\xBD\xC8\x61");
-}
\ No newline at end of file
+}
More information about the llvm-commits
mailing list