Skip to content

Commit 5927388

Browse files
committed
Ensure we can build with older SDKs
Ensure we can keep building debugserver with `memory tag read <addr-expr>` support with older SDKs.
1 parent 2b82f3f commit 5927388

File tree

3 files changed

+48
-14
lines changed

3 files changed

+48
-14
lines changed

lldb/tools/debugserver/source/MacOSX/MachVMMemory.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "MachVMMemory.h"
1414
#include "DNBLog.h"
1515
#include "MachVMRegion.h"
16+
#include <cassert>
1617
#include <dlfcn.h>
1718
#include <mach/mach_vm.h>
1819
#include <mach/shared_region.h>
@@ -157,12 +158,23 @@ nub_bool_t MachVMMemory::GetMemoryRegionInfo(task_t task, nub_addr_t address,
157158
#ifndef VM_OFFSET_LIST_MAX
158159
#define VM_OFFSET_LIST_MAX 512
159160
#endif
161+
using mach_vm_offset_list_t = mach_vm_offset_t *;
162+
using mach_vm_update_pointers_with_remote_tags_t = kern_return_t(
163+
mach_port_name_t target, mach_vm_offset_list_t in_pointer_list,
164+
mach_msg_type_number_t in_pointer_listCnt,
165+
mach_vm_offset_list_t out_pointer_list,
166+
mach_msg_type_number_t *out_pointer_listCnt);
167+
160168
nub_bool_t MachVMMemory::GetMemoryTags(task_t task, nub_addr_t address,
161169
nub_size_t size,
162170
std::vector<uint8_t> &tags) {
171+
static auto mach_vm_update_pointers_with_remote_tags =
172+
(mach_vm_update_pointers_with_remote_tags_t *)dlsym(
173+
RTLD_DEFAULT, "mach_vm_update_pointers_with_remote_tags");
174+
assert(mach_vm_update_pointers_with_remote_tags);
175+
163176
// Max batch size supported by mach_vm_update_pointers_with_remote_tags()
164177
constexpr uint32_t max_ptr_count = VM_OFFSET_LIST_MAX;
165-
166178
constexpr uint32_t tag_shift = 56;
167179
constexpr nub_addr_t tag_mask =
168180
((nub_addr_t)0x0f << tag_shift); // Lower half of top byte
@@ -177,7 +189,7 @@ nub_bool_t MachVMMemory::GetMemoryTags(task_t task, nub_addr_t address,
177189
ptr_arr[i] = (address + i * tag_granule);
178190

179191
mach_msg_type_number_t ptr_count_out = ptr_count;
180-
m_err = ::mach_vm_update_pointers_with_remote_tags(
192+
m_err = mach_vm_update_pointers_with_remote_tags(
181193
task, ptr_arr.get(), ptr_count, ptr_arr.get(), &ptr_count_out);
182194

183195
const bool failed = (m_err.Fail() || (ptr_count != ptr_count_out));

lldb/tools/debugserver/source/MacOSX/MachVMRegion.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ bool MachVMRegion::RestoreProtections() {
114114
return false;
115115
}
116116

117+
#ifdef VM_REGION_FLAG_JIT_ENABLED
118+
#define VM_REGION_HAS_FLAGS 1
119+
#else
120+
#define VM_REGION_HAS_FLAGS 0
121+
#endif
117122
bool MachVMRegion::GetRegionForAddress(nub_addr_t addr) {
118123
// Restore any original protections and clear our vars
119124
Clear();
@@ -140,6 +145,7 @@ bool MachVMRegion::GetRegionForAddress(nub_addr_t addr) {
140145
if (failed)
141146
return false;
142147
if (log_protections) {
148+
#if VM_REGION_HAS_FLAGS
143149
DNBLogThreaded("info = { prot = %u, "
144150
"max_prot = %u, "
145151
"inheritance = 0x%8.8x, "
@@ -159,6 +165,29 @@ bool MachVMRegion::GetRegionForAddress(nub_addr_t addr) {
159165
m_data.shadow_depth, m_data.external_pager,
160166
m_data.share_mode, m_data.is_submap, m_data.behavior,
161167
m_data.object_id, m_data.user_wired_count, m_data.flags);
168+
#else
169+
// Duplicate log call instead of #if-defing printing of flags to avoid
170+
// compiler warning: 'embedding a directive within macro arguments has
171+
// undefined behavior'
172+
DNBLogThreaded("info = { prot = %u, "
173+
"max_prot = %u, "
174+
"inheritance = 0x%8.8x, "
175+
"offset = 0x%8.8llx, "
176+
"user_tag = 0x%8.8x, "
177+
"ref_count = %u, "
178+
"shadow_depth = %u, "
179+
"ext_pager = %u, "
180+
"share_mode = %u, "
181+
"is_submap = %d, "
182+
"behavior = %d, "
183+
"object_id = 0x%8.8x, "
184+
"user_wired_count = 0x%4.4x }",
185+
m_data.protection, m_data.max_protection, m_data.inheritance,
186+
(uint64_t)m_data.offset, m_data.user_tag, m_data.ref_count,
187+
m_data.shadow_depth, m_data.external_pager,
188+
m_data.share_mode, m_data.is_submap, m_data.behavior,
189+
m_data.object_id, m_data.user_wired_count);
190+
#endif
162191
}
163192
m_curr_protection = m_data.protection;
164193

@@ -189,12 +218,14 @@ uint32_t MachVMRegion::GetDNBPermissions() const {
189218
#endif
190219
std::vector<std::string> MachVMRegion::GetFlags() const {
191220
std::vector<std::string> flags;
221+
#if VM_REGION_HAS_FLAGS
192222
if (m_data.flags & VM_REGION_FLAG_JIT_ENABLED)
193223
flags.push_back("jit");
194224
if (m_data.flags & VM_REGION_FLAG_TPRO_ENABLED)
195225
flags.push_back("tpro");
196226
if (m_data.flags & VM_REGION_FLAG_MTE_ENABLED)
197227
flags.push_back("mt");
228+
#endif
198229
return flags;
199230
}
200231

lldb/tools/debugserver/source/RNBRemote.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include <mach/mach_vm.h>
2323
#include <mach/task_info.h>
2424
#include <memory>
25-
#if __has_include(<os/security_config.h>) // macOS 26.1
25+
#if __has_include(<os/security_config.h>)
2626
#include <os/security_config.h>
2727
#endif
2828
#include <pwd.h>
@@ -6247,18 +6247,8 @@ GetCPUTypesFromHost(nub_process_t pid) {
62476247
return {cputype, cpusubtype};
62486248
}
62496249

6250-
#if !__has_include(<os/security_config.h>) // macOS 26.1
6251-
extern "C" {
6252-
using os_security_config_t = uint64_t;
6253-
#define OS_SECURITY_CONFIG_MTE 0x4
6254-
6255-
API_AVAILABLE(macos(26.0), ios(26.0), tvos(26.0), watchos(26.0), visionos(26.0),
6256-
driverkit(25.0))
6257-
OS_EXPORT OS_NOTHROW OS_NONNULL_ALL int
6258-
os_security_config_get_for_proc(pid_t pid, os_security_config_t *config);
6259-
}
6260-
#endif
62616250
static bool ProcessRunningWithMemoryTagging(pid_t pid) {
6251+
#if __has_include(<os/security_config.h>)
62626252
if (__builtin_available(macOS 26.0, iOS 26.0, tvOS 26.0, watchOS 26.0,
62636253
visionOS 26.0, driverkit 25.0, *)) {
62646254
os_security_config_t config;
@@ -6268,6 +6258,7 @@ static bool ProcessRunningWithMemoryTagging(pid_t pid) {
62686258

62696259
return (config & OS_SECURITY_CONFIG_MTE);
62706260
}
6261+
#endif
62716262
return false;
62726263
}
62736264

0 commit comments

Comments
 (0)