summaryrefslogtreecommitdiff
path: root/packaging/0031-Fix-preventing-memory-allocation-in-signal-handler.patch
blob: 8520d2608d613d61f2247334de92312c92e2f9b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
From 53c2a4bb7536cb6273e5ba58ee15f007978ba742 Mon Sep 17 00:00:00 2001
From: Jan Vorlicek <janvorli@microsoft.com>
Date: Thu, 22 Feb 2018 01:48:43 +0100
Subject: [PATCH 31/47] Fix preventing memory allocation in signal handler

There was a subtle bug. When the hardware exception handler returns back
to the signal handler, the exception's CONTEXT record may contain
modified registers and so the changes need to be propagated back to the
signal context. But the recent change #16384 was restoring the signal
context from the originally grabbed context instead of the one that's
pointed to by the exception, which is different.

I have also added a little optimization - the contextRecord that was
added is not needed, since the signalContextRecord can be used as the
initial context record for the exception. So we can save the
contextRecord and also copying to the signalContextRecord from it.
---
 src/pal/src/exception/signal.cpp | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/src/pal/src/exception/signal.cpp b/src/pal/src/exception/signal.cpp
index 3b4bec8..430cd05 100644
--- a/src/pal/src/exception/signal.cpp
+++ b/src/pal/src/exception/signal.cpp
@@ -801,7 +801,6 @@ static bool common_signal_handler(int code, siginfo_t *siginfo, void *sigcontext
 {
     sigset_t signal_set;
     CONTEXT signalContextRecord;
-    CONTEXT contextRecord;
     EXCEPTION_RECORD exceptionRecord;
     native_context_t *ucontext;
 
@@ -824,7 +823,7 @@ static bool common_signal_handler(int code, siginfo_t *siginfo, void *sigcontext
 
     // Pre-populate context with data from current frame, because ucontext doesn't have some data (e.g. SS register)
     // which is required for restoring context
-    RtlCaptureContext(&contextRecord);
+    RtlCaptureContext(&signalContextRecord);
 
     ULONG contextFlags = CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_FLOATING_POINT;
 
@@ -835,7 +834,7 @@ static bool common_signal_handler(int code, siginfo_t *siginfo, void *sigcontext
     // Fill context record with required information. from pal.h:
     // On non-Win32 platforms, the CONTEXT pointer in the
     // PEXCEPTION_POINTERS will contain at least the CONTEXT_CONTROL registers.
-    CONTEXTFromNativeContext(ucontext, &contextRecord, contextFlags);
+    CONTEXTFromNativeContext(ucontext, &signalContextRecord, contextFlags);
 
     /* Unmask signal so we can receive it again */
     sigemptyset(&signal_set);
@@ -846,17 +845,15 @@ static bool common_signal_handler(int code, siginfo_t *siginfo, void *sigcontext
         ASSERT("pthread_sigmask failed; error number is %d\n", sigmaskRet);
     }
 
-    contextRecord.ContextFlags |= CONTEXT_EXCEPTION_ACTIVE;
-
-    memcpy_s(&signalContextRecord, sizeof(CONTEXT), &contextRecord, sizeof(CONTEXT));
+    signalContextRecord.ContextFlags |= CONTEXT_EXCEPTION_ACTIVE;
 
     // The exception object takes ownership of the exceptionRecord and contextRecord
-    PAL_SEHException exception(&exceptionRecord, &contextRecord, true);
+    PAL_SEHException exception(&exceptionRecord, &signalContextRecord, true);
 
     if (SEHProcessException(&exception))
     {
         // Exception handling may have modified the context, so update it.
-        CONTEXTToNativeContext(&contextRecord, ucontext);
+        CONTEXTToNativeContext(exception.ExceptionPointers.ContextRecord, ucontext);
         return true;
     }
 
-- 
2.7.4