[llvm] [llvm][ELF]Add Shdr check for getBuildID (PR #126537)

Ruoyu Qiu via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 25 03:33:25 PST 2025


================
@@ -0,0 +1,61 @@
+//===- BuildIDTest.cpp - Tests for getBuildID ----------------===//
+//
+// 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/BuildID.h"
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Object/ELFObjectFile.h"
+#include "llvm/ObjectYAML/yaml2obj.h"
+#include "llvm/Support/YAMLTraits.h"
+#include "llvm/Testing/Support/Error.h"
+
+#include "gtest/gtest.h"
+
+using namespace llvm;
+using namespace llvm::object;
+
+template <class ELFT>
+static Expected<ELFObjectFile<ELFT>> toBinary(SmallVectorImpl<char> &Storage,
+                                              StringRef Yaml) {
+  raw_svector_ostream OS(Storage);
+  yaml::Input YIn(Yaml);
+  if (!yaml::convertYAML(YIn, OS, [](const Twine &Msg) {}))
+    return createStringError(std::errc::invalid_argument,
+                             "unable to convert YAML");
+  return ELFObjectFile<ELFT>::create(MemoryBufferRef(OS.str(), "dummyELF"));
+}
+
+TEST(BuildIDTest, InvalidNoteFileSizeTest) {
----------------
cabbaken wrote:

> This is good as a starting point. I think you also need at least the following test cases:
> 
> 1. A test case where you have an invalid program header and no section headers to rely on. You can do this using the same YAML, except also including a `SectionHeaderTable` chunk that uses the `NoHeaders` field (see https://github.com/llvm/llvm-project/blob/c5f40bf024ee2d62478c8036fb174d75ecabe51f/llvm/test/tools/yaml2obj/ELF/section-headers.yaml#L108
>     for an example). (Rather than duplicating the input string, you could add a helper function to construct it with/without the `NoHeaders` value set to true, for example).

I’m not sure if this aligns with your idea, but I’ve implemented it in the latest commit. Let me know if I misunderstood anything.

> 2. This may be a little trickier to achieve, but if possible, identify the max file size that would be considered "valid" and have cases with it specified and not specified (where section headers are missing) to show that the correct edge value is used.

If I did not misunderstand, I should know the generated ELF file size before I generate it? I think the file size may change according to `yaml2obj`.
Please let me know if I misunderstood anything...

> 3. A test case where the section header is present but invalid for reading the build-id, e.g. it has an invalid `sh_offset` value. You can use the `SHOffset` field in the YAML to specify a section header with an offset that is invalid, without affecting the actual location of the section data. Your program header in this case should point at the section correctly, to show that the data is read properly.

I've implemented this in the latest commit. Please let me know if I misunderstood anything.

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


More information about the llvm-commits mailing list