Skip to content

Commit 5d71de0

Browse files
committed
notify about death
1 parent 1093751 commit 5d71de0

2 files changed

Lines changed: 80 additions & 1 deletion

File tree

‎Modules/_remote_debugging/_remote_debugging.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,10 @@ typedef struct {
276276

277277
typedef struct {
278278
AliasPageEntry pages[MAX_ALIAS_PAGES];
279+
mach_port_t notify_port;
279280
uint64_t access_seq;
280281
uint32_t probe_counter;
282+
int notify_armed;
281283
int disabled;
282284
} AliasReadCache;
283285
#endif

‎Modules/_remote_debugging/alias_read.c‎

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#if defined(__APPLE__) && TARGET_OS_OSX
44

5+
#include <mach/notify.h>
6+
57
#ifndef VM_FLAGS_RETURN_DATA_ADDR
68
# define VM_FLAGS_RETURN_DATA_ADDR 0
79
#endif
@@ -37,6 +39,12 @@ _Py_RemoteDebug_AliasCacheClear(RemoteUnwinderObject *unwinder)
3739
for (int i = 0; i < MAX_ALIAS_PAGES; i++) {
3840
alias_deallocate_entry(&cache->pages[i]);
3941
}
42+
if (cache->notify_port != MACH_PORT_NULL) {
43+
(void)mach_port_mod_refs(mach_task_self(), cache->notify_port,
44+
MACH_PORT_RIGHT_RECEIVE, -1);
45+
cache->notify_port = MACH_PORT_NULL;
46+
}
47+
cache->notify_armed = 0;
4048
}
4149

4250
static void
@@ -64,10 +72,38 @@ _Py_RemoteDebug_AliasCacheInit(RemoteUnwinderObject *unwinder)
6472
{
6573
AliasReadCache *cache = &unwinder->alias_cache;
6674
memset(cache, 0, sizeof(*cache));
75+
76+
mach_port_t notify_port = MACH_PORT_NULL;
77+
kern_return_t kr = mach_port_allocate(mach_task_self(),
78+
MACH_PORT_RIGHT_RECEIVE,
79+
&notify_port);
80+
if (kr != KERN_SUCCESS) {
81+
return;
82+
}
83+
84+
mach_port_t previous = MACH_PORT_NULL;
85+
kr = mach_port_request_notification(mach_task_self(),
86+
unwinder->handle.task,
87+
MACH_NOTIFY_DEAD_NAME,
88+
1,
89+
notify_port,
90+
MACH_MSG_TYPE_MAKE_SEND_ONCE,
91+
&previous);
92+
if (previous != MACH_PORT_NULL) {
93+
(void)mach_port_deallocate(mach_task_self(), previous);
94+
}
95+
if (kr != KERN_SUCCESS) {
96+
(void)mach_port_mod_refs(mach_task_self(), notify_port,
97+
MACH_PORT_RIGHT_RECEIVE, -1);
98+
return;
99+
}
100+
101+
cache->notify_port = notify_port;
102+
cache->notify_armed = 1;
67103
}
68104

69105
static int
70-
alias_identity_matches(RemoteUnwinderObject *unwinder)
106+
alias_identity_matches_fallback(RemoteUnwinderObject *unwinder)
71107
{
72108
mach_port_type_t type = 0;
73109
kern_return_t kr = mach_port_type(mach_task_self(),
@@ -79,6 +115,47 @@ alias_identity_matches(RemoteUnwinderObject *unwinder)
79115
!(type & MACH_PORT_TYPE_DEAD_NAME)) ? 1 : 0;
80116
}
81117

118+
static int
119+
alias_identity_matches(RemoteUnwinderObject *unwinder)
120+
{
121+
AliasReadCache *cache = &unwinder->alias_cache;
122+
if (!cache->notify_armed) {
123+
return alias_identity_matches_fallback(unwinder);
124+
}
125+
126+
union {
127+
mach_dead_name_notification_t dead_name;
128+
struct {
129+
mach_msg_header_t header;
130+
mach_msg_trailer_t trailer;
131+
} msg;
132+
char storage[sizeof(mach_dead_name_notification_t) +
133+
sizeof(mach_msg_trailer_t)];
134+
} msg;
135+
memset(&msg, 0, sizeof(msg));
136+
137+
mach_msg_return_t mr = mach_msg(&msg.msg.header,
138+
MACH_RCV_MSG | MACH_RCV_TIMEOUT,
139+
0,
140+
(mach_msg_size_t)sizeof(msg),
141+
cache->notify_port,
142+
0,
143+
MACH_PORT_NULL);
144+
if (mr == MACH_RCV_TIMED_OUT) {
145+
return 1;
146+
}
147+
if (mr == MACH_MSG_SUCCESS &&
148+
msg.msg.header.msgh_id == MACH_NOTIFY_DEAD_NAME)
149+
{
150+
if (msg.dead_name.not_port != MACH_PORT_NULL) {
151+
(void)mach_port_deallocate(mach_task_self(),
152+
msg.dead_name.not_port);
153+
}
154+
return 0;
155+
}
156+
return 0;
157+
}
158+
82159
static int
83160
alias_maybe_probe_identity(RemoteUnwinderObject *unwinder)
84161
{

0 commit comments

Comments
 (0)