https://github.com/JDevlieghere created 
https://github.com/llvm/llvm-project/pull/186322

When LLDB_ENABLE_MTE is set to ON, we should always run the driver with MTE by 
signing with the checked-allocations entitlement.

>From b70e864ef394d975fa22c26a622c3156c66f5a68 Mon Sep 17 00:00:00 2001
From: Jonas Devlieghere <[email protected]>
Date: Thu, 12 Mar 2026 23:20:01 -0700
Subject: [PATCH] [lldb] When LLDB_ENABLE_MTE is ON always run the driver with
 MTE

When LLDB_ENABLE_MTE is set to ON, we should always run the driver with
MTE by signing with the checked-allocations entitlement.
---
 lldb/include/lldb/Host/Config.h.cmake            |  2 ++
 .../Python/ScriptInterpreterPython.cpp           | 10 ++++++++++
 lldb/tools/driver/CMakeLists.txt                 | 16 ++++++++++++++--
 lldb/tools/driver/lldb-mte-entitlements.plist    | 10 ++++++++++
 4 files changed, 36 insertions(+), 2 deletions(-)
 create mode 100644 lldb/tools/driver/lldb-mte-entitlements.plist

diff --git a/lldb/include/lldb/Host/Config.h.cmake 
b/lldb/include/lldb/Host/Config.h.cmake
index f165d99830d8d..06ab9b9a7775c 100644
--- a/lldb/include/lldb/Host/Config.h.cmake
+++ b/lldb/include/lldb/Host/Config.h.cmake
@@ -51,6 +51,8 @@
 
 #cmakedefine01 LLDB_ENABLE_TREESITTER
 
+#cmakedefine01 LLDB_ENABLE_MTE
+
 #cmakedefine LLDB_PYTHON_HOME R"(${LLDB_PYTHON_HOME})"
 
 #define LLDB_INSTALL_LIBDIR_BASENAME "${LLDB_INSTALL_LIBDIR_BASENAME}"
diff --git 
a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp 
b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
index de8be43aa35fd..42f6f0748f82a 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
+++ b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
@@ -1,3 +1,4 @@
+
 //===-- ScriptInterpreterPython.cpp 
---------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
@@ -25,6 +26,7 @@
 #include "lldb/Core/PluginManager.h"
 #include "lldb/Core/ThreadedCommunication.h"
 #include "lldb/DataFormatters/TypeSummary.h"
+#include "lldb/Host/Config.h"
 #include "lldb/Host/FileSystem.h"
 #include "lldb/Host/HostInfo.h"
 #include "lldb/Host/Pipe.h"
@@ -49,6 +51,7 @@
 #include <cstdlib>
 #include <memory>
 #include <optional>
+#include <stdlib.h>
 #include <string>
 
 using namespace lldb;
@@ -289,6 +292,13 @@ llvm::StringRef 
ScriptInterpreterPython::GetPluginDescriptionStatic() {
 }
 
 void ScriptInterpreterPython::Initialize() {
+#if LLDB_ENABLE_MTE
+  // Python's allocator (pymalloc) is not aware of Memory Tagging Extension
+  // (MTE) and crashes.
+  // https://bugs.python.org/issue43593
+  setenv("PYTHONMALLOC", "malloc", /*overwrite=*/true);
+#endif
+
   HostInfo::SetSharedLibraryDirectoryHelper(
       ScriptInterpreterPython::SharedLibraryDirectoryHelper);
   PluginManager::RegisterPlugin(
diff --git a/lldb/tools/driver/CMakeLists.txt b/lldb/tools/driver/CMakeLists.txt
index 7043f648518d6..0500d01e9df34 100644
--- a/lldb/tools/driver/CMakeLists.txt
+++ b/lldb/tools/driver/CMakeLists.txt
@@ -9,11 +9,21 @@ if(APPLE)
     )
   # Inline info plist in binary (use target_link_options for this as soon as 
CMake 3.13 is available)
   set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} 
-Wl,-sectcreate,__TEXT,__info_plist,${CMAKE_CURRENT_BINARY_DIR}/lldb-Info.plist")
+
+  if(LLDB_CODESIGN_IDENTITY)
+    set(LLVM_CODESIGNING_IDENTITY ${LLDB_CODESIGN_IDENTITY})
+  elseif(NOT LLVM_CODESIGNING_IDENTITY)
+    set(LLVM_CODESIGNING_IDENTITY "-")
+  endif()
+
+  if (LLDB_ENABLE_MTE)
+    set(ENTITLEMENTS ENTITLEMENTS 
${CMAKE_CURRENT_SOURCE_DIR}/lldb-mte-entitlements.plist)
+  endif()
 endif()
 
 if (UNIX AND "${CMAKE_SYSTEM_NAME}" MATCHES "AIX")
-  remove_definitions("-D_XOPEN_SOURCE=700")                                    
-  add_definitions("-D_ALL_SOURCE")                                             
+  remove_definitions("-D_XOPEN_SOURCE=700")
+  add_definitions("-D_ALL_SOURCE")
 endif()
 
 set(LLDB_DRIVER_LINK_LIBS
@@ -30,6 +40,8 @@ add_lldb_tool(lldb
   Driver.cpp
   Platform.cpp
 
+  ${ENTITLEMENTS}
+
   LINK_COMPONENTS
     Option
     Support
diff --git a/lldb/tools/driver/lldb-mte-entitlements.plist 
b/lldb/tools/driver/lldb-mte-entitlements.plist
new file mode 100644
index 0000000000000..7ab4dd621bd53
--- /dev/null
+++ b/lldb/tools/driver/lldb-mte-entitlements.plist
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" 
"http://www.apple.com/DTDs/PropertyList-1.0.dtd";>
+<plist version="1.0">
+<dict>
+       <key>com.apple.developer.hardened-process</key>
+       <true/>
+       <key>com.apple.developer.hardened-process.checked-allocations</key>
+       <true/>
+</dict>
+</plist>

_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to