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

Saiyedul Islam via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 16 03:17:39 PDT 2020


This revision was automatically updated to reflect the committed changes.
Closed by commit rGb7084d8ede1b: [ELF] Fixing an issue in Elf_Note_Impl::getDescAsStringRef (authored by dineshkb-amd, committed by saiislam).

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.271013.patch
Type: text/x-patch
Size: 3399 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200616/c1737ed8/attachment.bin>


More information about the llvm-commits mailing list