From 68390c6547e07b2e285cb3d714ab45f0f90c458f Mon Sep 17 00:00:00 2001 From: Dineshkumar Bhaskaran Date: Tue, 16 Jun 2020 10:00:46 +0000 Subject: [PATCH] [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 --- include/llvm/Object/ELFTypes.h | 2 +- unittests/Object/CMakeLists.txt | 1 + unittests/Object/ELFTypesTest.cpp | 63 +++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 unittests/Object/ELFTypesTest.cpp diff --git a/include/llvm/Object/ELFTypes.h b/include/llvm/Object/ELFTypes.h index d0909f5b882..ac9eb48575b 100644 --- a/include/llvm/Object/ELFTypes.h +++ b/include/llvm/Object/ELFTypes.h @@ -617,7 +617,7 @@ public: /// Get the note's descriptor as StringRef StringRef getDescAsStringRef() const { - auto &Desc = getDesc(); + ArrayRef Desc = getDesc(); return StringRef(reinterpret_cast(Desc.data()), Desc.size()); } diff --git a/unittests/Object/CMakeLists.txt b/unittests/Object/CMakeLists.txt index 1d419eb187d..6fee67732ae 100644 --- a/unittests/Object/CMakeLists.txt +++ b/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/unittests/Object/ELFTypesTest.cpp b/unittests/Object/ELFTypesTest.cpp new file mode 100644 index 00000000000..183ef3e97da --- /dev/null +++ b/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 + +using namespace llvm; +using namespace llvm::object; + +template using Elf_Note = typename ELFT::Note; + +template struct NoteTestData { + std::vector Data; + + const Elf_Note_Impl getElfNote(StringRef Name, uint32_t Type, + ArrayRef Desc) { + Data.resize(sizeof(Elf_Nhdr_Impl) + + alignTo::Align>(Name.size()) + + alignTo::Align>(Desc.size()), + 0); + + Elf_Nhdr_Impl *Nhdr = + reinterpret_cast *>(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::Align>(Nhdr->n_namesz); + std::copy(Desc.begin(), Desc.end(), DescOffset); + + return Elf_Note_Impl(*Nhdr); + } +}; + +TEST(ELFTypesTest, NoteTest) { + static const uint8_t Random[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + ArrayRef RandomData = makeArrayRef(Random); + NoteTestData 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(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()); + EXPECT_EQ(Note3.getDescAsStringRef(), StringRef("")); +}