[llvm] 4a12722 - [ORC] Expose a non-destructive check-macho-buffer overload.

Lang Hames via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 23 04:10:16 PDT 2024


Author: Lang Hames
Date: 2024-08-23T21:02:56+10:00
New Revision: 4a12722110abb2ccb98173c82a7d7b96a5c098e0

URL: https://github.com/llvm/llvm-project/commit/4a12722110abb2ccb98173c82a7d7b96a5c098e0
DIFF: https://github.com/llvm/llvm-project/commit/4a12722110abb2ccb98173c82a7d7b96a5c098e0.diff

LOG: [ORC] Expose a non-destructive check-macho-buffer overload.

This allows clients to check buffers that they don't own.

rdar://133536831

Added: 
    

Modified: 
    llvm/include/llvm/ExecutionEngine/Orc/MachO.h
    llvm/lib/ExecutionEngine/Orc/MachO.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ExecutionEngine/Orc/MachO.h b/llvm/include/llvm/ExecutionEngine/Orc/MachO.h
index fdaa2f73cda6a3..8bf2550d2d4f06 100644
--- a/llvm/include/llvm/ExecutionEngine/Orc/MachO.h
+++ b/llvm/include/llvm/ExecutionEngine/Orc/MachO.h
@@ -31,6 +31,13 @@ namespace orc {
 /// given triple.
 /// ObjIsSlice should be set to true if Obj is a slice of a universal binary
 /// (that fact will then be reported in the error messages).
+Error checkMachORelocatableObject(MemoryBufferRef Obj, const Triple &TT,
+                                  bool ObjIsSlice);
+
+/// Check that the given buffer contains a MachO object file compatible with the
+/// given triple.
+/// This convenience overload returns the buffer if it passes all checks,
+/// otherwise it returns an error.
 Expected<std::unique_ptr<MemoryBuffer>>
 checkMachORelocatableObject(std::unique_ptr<MemoryBuffer> Obj, const Triple &TT,
                             bool ObjIsSlice);

diff  --git a/llvm/lib/ExecutionEngine/Orc/MachO.cpp b/llvm/lib/ExecutionEngine/Orc/MachO.cpp
index 8fc262220bf892..7fab56f393c506 100644
--- a/llvm/lib/ExecutionEngine/Orc/MachO.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/MachO.cpp
@@ -17,7 +17,7 @@
 namespace llvm {
 namespace orc {
 
-static std::string objDesc(MemoryBuffer &Obj, const Triple &TT,
+static std::string objDesc(const MemoryBufferRef &Obj, const Triple &TT,
                            bool ObjIsSlice) {
   std::string Desc;
   if (ObjIsSlice)
@@ -27,11 +27,10 @@ static std::string objDesc(MemoryBuffer &Obj, const Triple &TT,
 }
 
 template <typename HeaderType>
-static Expected<std::unique_ptr<MemoryBuffer>>
-checkMachORelocatableObject(std::unique_ptr<MemoryBuffer> Obj,
-                            bool SwapEndianness, const Triple &TT,
-                            bool ObjIsSlice) {
-  StringRef Data = Obj->getBuffer();
+static Error checkMachORelocatableObject(MemoryBufferRef Obj,
+                                         bool SwapEndianness, const Triple &TT,
+                                         bool ObjIsSlice) {
+  StringRef Data = Obj.getBuffer();
 
   HeaderType Hdr;
   memcpy(&Hdr, Data.data(), sizeof(HeaderType));
@@ -40,28 +39,27 @@ checkMachORelocatableObject(std::unique_ptr<MemoryBuffer> Obj,
     swapStruct(Hdr);
 
   if (Hdr.filetype != MachO::MH_OBJECT)
-    return make_error<StringError>(objDesc(*Obj, TT, ObjIsSlice) +
+    return make_error<StringError>(objDesc(Obj, TT, ObjIsSlice) +
                                        " is not a MachO relocatable object",
                                    inconvertibleErrorCode());
 
   auto ObjArch = object::MachOObjectFile::getArch(Hdr.cputype, Hdr.cpusubtype);
   if (ObjArch != TT.getArch())
     return make_error<StringError>(
-        objDesc(*Obj, TT, ObjIsSlice) + Triple::getArchTypeName(ObjArch) +
+        objDesc(Obj, TT, ObjIsSlice) + Triple::getArchTypeName(ObjArch) +
             ", cannot be loaded into " + TT.str() + " process",
         inconvertibleErrorCode());
 
-  return std::move(Obj);
+  return Error::success();
 }
 
-Expected<std::unique_ptr<MemoryBuffer>>
-checkMachORelocatableObject(std::unique_ptr<MemoryBuffer> Obj, const Triple &TT,
-                            bool ObjIsSlice) {
-  StringRef Data = Obj->getBuffer();
+Error checkMachORelocatableObject(MemoryBufferRef Obj, const Triple &TT,
+                                  bool ObjIsSlice) {
+  StringRef Data = Obj.getBuffer();
 
   if (Data.size() < 4)
     return make_error<StringError>(
-        objDesc(*Obj, TT, ObjIsSlice) +
+        objDesc(Obj, TT, ObjIsSlice) +
             " is not a valid MachO relocatable object file (truncated header)",
         inconvertibleErrorCode());
 
@@ -79,12 +77,21 @@ checkMachORelocatableObject(std::unique_ptr<MemoryBuffer> Obj, const Triple &TT,
         std::move(Obj), Magic == MachO::MH_CIGAM_64, TT, ObjIsSlice);
   default:
     return make_error<StringError>(
-        objDesc(*Obj, TT, ObjIsSlice) +
+        objDesc(Obj, TT, ObjIsSlice) +
             " is not a valid MachO relocatable object (bad magic value)",
         inconvertibleErrorCode());
   }
 }
 
+Expected<std::unique_ptr<MemoryBuffer>>
+checkMachORelocatableObject(std::unique_ptr<MemoryBuffer> Obj, const Triple &TT,
+                            bool ObjIsSlice) {
+  if (auto Err =
+          checkMachORelocatableObject(Obj->getMemBufferRef(), TT, ObjIsSlice))
+    return std::move(Err);
+  return std::move(Obj);
+}
+
 Expected<std::unique_ptr<MemoryBuffer>>
 loadMachORelocatableObject(StringRef Path, const Triple &TT,
                            std::optional<StringRef> IdentifierOverride) {


        


More information about the llvm-commits mailing list