[llvm] [llvm-objdump] Error with relevant message when adding invalid notes (PR #90458)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 29 14:29:50 PDT 2024


================
@@ -623,6 +623,58 @@ handleUserSection(const NewSectionInfo &NewSection,
   return F(NewSection.SectionName, Data);
 }
 
+static Error verifyNoteSection(StringRef Name, llvm::endianness E,
+                               ArrayRef<uint8_t> Data) {
+
+  // An ELF note have the following structure:
+  // Name Size: 4 bytes (integer)
+  // Desc Size: 4 bytes (integer)
+  // Type     : 4 bytes
+  // Name     : variable size, padded to a 4 byte boundary
+  // Desc     : variable size, padded to a 4 byte boundary
+
+  if (Data.empty())
+    return Error::success();
+
+  if (Data.size() < 12) {
+    std::string msg;
+    raw_string_ostream(msg)
+        << Name << " data must be either empty or at least 12 bytes long.";
+    return createStringError(errc::invalid_argument, msg);
+  }
+  if (Data.size() % 4 != 0) {
+    std::string msg;
+    raw_string_ostream(msg)
+        << Name << " data size must be a  multiple of 4 bytes.";
+    return createStringError(errc::invalid_argument, msg);
+  }
+  ArrayRef<uint8_t> NameSize = Data.slice(0, 4);
+  ArrayRef<uint8_t> DescSize = Data.slice(4, 4);
+
+  uint32_t NameSizeValue, DescSizeValue;
+  std::memcpy(&NameSizeValue, NameSize.data(), 4);
+  std::memcpy(&DescSizeValue, DescSize.data(), 4);
+
+  if (llvm::endianness::native != E) {
+    NameSizeValue = byteswap(NameSizeValue);
+    DescSizeValue = byteswap(DescSizeValue);
+  }
+  uint64_t ExpectedDataSize =
+      4 + 4 + 4 + (NameSizeValue + 3) / 4 * 4 + (DescSizeValue + 3) / 4 * 4;
+  uint64_t ActualDataSize = Data.size();
+  if (ActualDataSize != ExpectedDataSize) {
+    std::string msg;
+    raw_string_ostream(msg) << Name
----------------
MaskRay wrote:

@bulbazord's #80493  (reverted due to compile time increase) could simplify this.

https://github.com/llvm/llvm-project/pull/90458


More information about the llvm-commits mailing list