[llvm] b7084d8 - [ELF] Fixing an issue in Elf_Note_Impl::getDescAsStringRef
Saiyedul Islam via llvm-commits
llvm-commits at lists.llvm.org
Tue Jun 16 03:08:49 PDT 2020
Author: Dineshkumar Bhaskaran
Date: 2020-06-16T10:08:31Z
New Revision: b7084d8ede1b0c16d3426dede53ed5ce62242c42
URL: https://github.com/llvm/llvm-project/commit/b7084d8ede1b0c16d3426dede53ed5ce62242c42
DIFF: https://github.com/llvm/llvm-project/commit/b7084d8ede1b0c16d3426dede53ed5ce62242c42.diff
LOG: [ELF] Fixing an issue in Elf_Note_Impl::getDescAsStringRef
Summary:
Fix in getDescAsStringRef to not use a reference to a
temporary type and added a testcase.
Reviewers: arsenm, saiislam, scott.linder
Reviewed By: scott.linder
Subscribers: wdng, mgorny, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D81653
Added:
llvm/unittests/Object/ELFTypesTest.cpp
Modified:
llvm/include/llvm/Object/ELFTypes.h
llvm/unittests/Object/CMakeLists.txt
Removed:
################################################################################
diff --git a/llvm/include/llvm/Object/ELFTypes.h b/llvm/include/llvm/Object/ELFTypes.h
index d0909f5b8823..ac9eb48575b2 100644
--- a/llvm/include/llvm/Object/ELFTypes.h
+++ b/llvm/include/llvm/Object/ELFTypes.h
@@ -617,7 +617,7 @@ class Elf_Note_Impl {
/// Get the note's descriptor as StringRef
StringRef getDescAsStringRef() const {
- auto &Desc = getDesc();
+ ArrayRef<uint8_t> Desc = getDesc();
return StringRef(reinterpret_cast<const char *>(Desc.data()), Desc.size());
}
diff --git a/llvm/unittests/Object/CMakeLists.txt b/llvm/unittests/Object/CMakeLists.txt
index 1d419eb187d8..6fee67732ae5 100644
--- a/llvm/unittests/Object/CMakeLists.txt
+++ b/llvm/unittests/Object/CMakeLists.txt
@@ -6,6 +6,7 @@ set(LLVM_LINK_COMPONENTS
add_llvm_unittest(ObjectTests
ArchiveTest.cpp
ELFObjectFileTest.cpp
+ ELFTypesTest.cpp
ELFTest.cpp
MinidumpTest.cpp
ObjectFileTest.cpp
diff --git a/llvm/unittests/Object/ELFTypesTest.cpp b/llvm/unittests/Object/ELFTypesTest.cpp
new file mode 100644
index 000000000000..183ef3e97daf
--- /dev/null
+++ b/llvm/unittests/Object/ELFTypesTest.cpp
@@ -0,0 +1,63 @@
+//===----------------------- ELFTypesTest.cpp -----------------------------===//
+//
+// 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/ELFTypes.h"
+#include "gtest/gtest.h"
+#include <iostream>
+
+using namespace llvm;
+using namespace llvm::object;
+
+template <typename ELFT> using Elf_Note = typename ELFT::Note;
+
+template <class ELFT> struct NoteTestData {
+ std::vector<uint8_t> Data;
+
+ const Elf_Note_Impl<ELFT> getElfNote(StringRef Name, uint32_t Type,
+ ArrayRef<uint8_t> Desc) {
+ Data.resize(sizeof(Elf_Nhdr_Impl<ELFT>) +
+ alignTo<Elf_Nhdr_Impl<ELFT>::Align>(Name.size()) +
+ alignTo<Elf_Nhdr_Impl<ELFT>::Align>(Desc.size()),
+ 0);
+
+ Elf_Nhdr_Impl<ELFT> *Nhdr =
+ reinterpret_cast<Elf_Nhdr_Impl<ELFT> *>(Data.data());
+ Nhdr->n_namesz = (Name == "") ? 0 : Name.size() + 1;
+ Nhdr->n_descsz = Desc.size();
+ Nhdr->n_type = Type;
+
+ auto NameOffset = Data.begin() + sizeof(*Nhdr);
+ std::copy(Name.begin(), Name.end(), NameOffset);
+
+ auto DescOffset =
+ NameOffset + alignTo<Elf_Nhdr_Impl<ELFT>::Align>(Nhdr->n_namesz);
+ std::copy(Desc.begin(), Desc.end(), DescOffset);
+
+ return Elf_Note_Impl<ELFT>(*Nhdr);
+ }
+};
+
+TEST(ELFTypesTest, NoteTest) {
+ static const uint8_t Random[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
+ ArrayRef<uint8_t> RandomData = makeArrayRef(Random);
+ NoteTestData<ELF64LE> TestData;
+
+ auto Note1 = TestData.getElfNote(StringRef("AMD"), ELF::NT_AMDGPU_METADATA,
+ RandomData);
+ EXPECT_EQ(Note1.getName(), "AMD");
+ EXPECT_EQ(Note1.getType(), ELF::NT_AMDGPU_METADATA);
+ EXPECT_EQ(Note1.getDesc(), RandomData);
+ EXPECT_EQ(Note1.getDescAsStringRef(),
+ StringRef(reinterpret_cast<const char *>(Random), sizeof(Random)));
+
+ auto Note2 = TestData.getElfNote("", ELF::NT_AMDGPU_METADATA, RandomData);
+ EXPECT_EQ(Note2.getName(), "");
+
+ auto Note3 =
+ TestData.getElfNote("AMD", ELF::NT_AMDGPU_METADATA, ArrayRef<uint8_t>());
+ EXPECT_EQ(Note3.getDescAsStringRef(), StringRef(""));
+}
More information about the llvm-commits
mailing list