[llvm-jitlink] Fix a bug in llvm-jitlink's Slab allocator.
authorLang Hames <lhames@gmail.com>
Fri, 11 Feb 2022 07:45:45 +0000 (23:45 -0800)
committerLang Hames <lhames@gmail.com>
Mon, 14 Feb 2022 03:28:38 +0000 (19:28 -0800)
The slab delta (used to link as if allocated at a specified address) should
remain constant.The update to the delta was accidentally introduced in
962a2479b57f5, but hasn't caused any failures as it only breaks in an obvious
way for multi-file exec uses (our regression tests are all -noexec, and tend to
be single-file).

No testcase here: this is an obscure utility for testing support, and an
uncommon use-case. If the slab allocator is ever moved into LLVM we could add
a unit test to catch this.

llvm/tools/llvm-jitlink/llvm-jitlink.cpp

index 580ebda..f3443fa 100644 (file)
@@ -557,7 +557,7 @@ public:
                << "\n";
       });
       Seg.WorkingMem = SegAddr.toPtr<char *>();
-      Seg.Addr = SegAddr + NextSlabDelta;
+      Seg.Addr = SegAddr + SlabDelta;
 
       SegAddr += alignTo(Seg.ContentSize + Seg.ZeroFillSize, PageSize);
 
@@ -566,8 +566,6 @@ public:
         memset(Seg.WorkingMem + Seg.ContentSize, 0, Seg.ZeroFillSize);
     }
 
-    NextSlabDelta += SegsSizes->total();
-
     if (auto Err = BL.apply()) {
       OnAllocated(std::move(Err));
       return;
@@ -637,7 +635,7 @@ private:
     // Calculate the target address delta to link as-if slab were at
     // SlabAddress.
     if (SlabAddress != ~0ULL)
-      NextSlabDelta = ExecutorAddr(SlabAddress) -
+      SlabDelta = ExecutorAddr(SlabAddress) -
                       ExecutorAddr::fromPtr(SlabRemaining.base());
   }
 
@@ -649,7 +647,7 @@ private:
   std::mutex SlabMutex;
   sys::MemoryBlock SlabRemaining;
   uint64_t PageSize = 0;
-  int64_t NextSlabDelta = 0;
+  int64_t SlabDelta = 0;
 };
 
 Expected<uint64_t> getSlabAllocSize(StringRef SizeString) {