From 17e3979bd2cad997c853ea6ceae5879ac977e014 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Tue, 27 Aug 2024 08:58:48 +1200
Subject: [PATCH] XXX replace LLVM SectionMemoryManager

---
 src/backend/jit/llvm/llvmjit.c        |  3 ++-
 src/backend/jit/llvm/llvmjit_wrap.cpp | 23 +++++++++++++++++++++++
 src/include/jit/llvmjit.h             |  2 ++
 3 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/src/backend/jit/llvm/llvmjit.c b/src/backend/jit/llvm/llvmjit.c
index 0f6cec52496..23803172575 100644
--- a/src/backend/jit/llvm/llvmjit.c
+++ b/src/backend/jit/llvm/llvmjit.c
@@ -1279,7 +1279,8 @@ static LLVMOrcObjectLayerRef
 llvm_create_object_layer(void *Ctx, LLVMOrcExecutionSessionRef ES, const char *Triple)
 {
 	LLVMOrcObjectLayerRef objlayer =
-		LLVMOrcCreateRTDyldObjectLinkingLayerWithSectionMemoryManager(ES);
+		LLVMOrcCreateRTDyldObjectLinkingLayerWithSafeSectionMemoryManager(ES);
+
 
 #if defined(HAVE_DECL_LLVMCREATEGDBREGISTRATIONLISTENER) && HAVE_DECL_LLVMCREATEGDBREGISTRATIONLISTENER
 	if (jit_debugging_support)
diff --git a/src/backend/jit/llvm/llvmjit_wrap.cpp b/src/backend/jit/llvm/llvmjit_wrap.cpp
index 641c8841ca3..f4a35325482 100644
--- a/src/backend/jit/llvm/llvmjit_wrap.cpp
+++ b/src/backend/jit/llvm/llvmjit_wrap.cpp
@@ -17,11 +17,15 @@ extern "C"
 }
 
 #include <llvm-c/Core.h>
+#include <llvm-c/OrcEE.h>
 
 /* Avoid macro clash with LLVM's C++ headers */
 #undef Min
 
+#include <llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h>
+#include <llvm/ExecutionEngine/SectionMemoryManager.h>
 #include <llvm/IR/Function.h>
+#include <llvm/Support/CBindingWrapping.h>
 
 #include "jit/llvmjit.h"
 
@@ -41,3 +45,22 @@ LLVMGetFunctionType(LLVMValueRef r)
 {
 	return llvm::wrap(llvm::unwrap<llvm::Function>(r)->getFunctionType());
 }
+
+struct SafeSectionMemoryManager : llvm::SectionMemoryManager {
+	uint8_t *allocateCodeSection (uintptr_t Size, unsigned Alignment, unsigned SectionID, llvm::StringRef SectionName) override {
+		uint8_t *result;
+		result = SectionMemoryManager::allocateCodeSection(Size, Alignment, SectionID, SectionName);
+		elog(NOTICE, "allocateCodeSection returning %p", result);
+		return result;
+	}
+};
+
+DEFINE_SIMPLE_CONVERSION_FUNCTIONS(llvm::orc::ExecutionSession, LLVMOrcExecutionSessionRef)
+DEFINE_SIMPLE_CONVERSION_FUNCTIONS(llvm::orc::ObjectLayer, LLVMOrcObjectLayerRef);
+
+LLVMOrcObjectLayerRef
+LLVMOrcCreateRTDyldObjectLinkingLayerWithSafeSectionMemoryManager(LLVMOrcExecutionSessionRef ES)
+{
+	return wrap(new llvm::orc::RTDyldObjectLinkingLayer(
+		*unwrap(ES), [] { return std::make_unique<SafeSectionMemoryManager>(); }));
+}
diff --git a/src/include/jit/llvmjit.h b/src/include/jit/llvmjit.h
index 420775b1899..99c6fd15849 100644
--- a/src/include/jit/llvmjit.h
+++ b/src/include/jit/llvmjit.h
@@ -18,6 +18,7 @@
 #ifdef USE_LLVM
 
 #include <llvm-c/Types.h>
+#include <llvm-c/OrcEE.h>
 
 
 /*
@@ -135,6 +136,7 @@ extern LLVMValueRef slot_compile_deform(struct LLVMJitContext *context, TupleDes
  */
 extern LLVMTypeRef LLVMGetFunctionReturnType(LLVMValueRef r);
 extern LLVMTypeRef LLVMGetFunctionType(LLVMValueRef r);
+extern LLVMOrcObjectLayerRef LLVMOrcCreateRTDyldObjectLinkingLayerWithSafeSectionMemoryManager(LLVMOrcExecutionSessionRef RS);
 
 #ifdef __cplusplus
 } /* extern "C" */
-- 
2.39.3 (Apple Git-146)

