[PATCH] D81653: [ELF] Fixing an issue in Elf_Note_Impl::getDescAsStringRef

DineshKumar Bhaskaran via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 15 08:41:08 PDT 2020


dbhaskaran updated this revision to Diff 270765.
dbhaskaran added a comment.

Rebased


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D81653/new/

https://reviews.llvm.org/D81653

Files:
  llvm/include/llvm/Object/ELFTypes.h
  llvm/unittests/Object/CMakeLists.txt
  llvm/unittests/Object/ELFTypesTest.cpp


Index: llvm/unittests/Object/ELFTypesTest.cpp
===================================================================
--- /dev/null
+++ 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(""));
+}
Index: llvm/unittests/Object/CMakeLists.txt
===================================================================
--- llvm/unittests/Object/CMakeLists.txt
+++ llvm/unittests/Object/CMakeLists.txt
@@ -6,6 +6,7 @@
 add_llvm_unittest(ObjectTests
   ArchiveTest.cpp
   ELFObjectFileTest.cpp
+  ELFTypesTest.cpp
   ELFTest.cpp
   MinidumpTest.cpp
   ObjectFileTest.cpp
Index: llvm/include/llvm/Object/ELFTypes.h
===================================================================
--- llvm/include/llvm/Object/ELFTypes.h
+++ llvm/include/llvm/Object/ELFTypes.h
@@ -617,7 +617,7 @@
 
   /// 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());
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D81653.270765.patch
Type: text/x-patch
Size: 3399 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200615/98340281/attachment.bin>


More information about the llvm-commits mailing list