summaryrefslogtreecommitdiff
path: root/packaging/0029-Move-exception-allocation-to-PAL_SEHException.patch
blob: 67e9f9e5e49941e4798e4961fe870caf7a9ba7a6 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
From d7ea540ec5ffa7e44627b61f0cfa480341ec64ec Mon Sep 17 00:00:00 2001
From: Mikhail Labiuk <m.labiuk@samsung.com>
Date: Tue, 20 Feb 2018 14:26:35 +0300
Subject: [PATCH 29/47] Move exception allocation to PAL_SEHException

PAL_SEHException::EnsureExceptionRecordsOnHeap() moves exception record
to heap if needed.

fix https://github.com/dotnet/coreclr/issues/16338
---
 src/pal/inc/pal.h                | 35 ++++++++++++++++++++++++++++++-----
 src/pal/src/exception/seh.cpp    | 25 ++-----------------------
 src/pal/src/exception/signal.cpp |  6 ++----
 3 files changed, 34 insertions(+), 32 deletions(-)

diff --git a/src/pal/inc/pal.h b/src/pal/inc/pal.h
index e3bfa40..e241219 100644
--- a/src/pal/inc/pal.h
+++ b/src/pal/inc/pal.h
@@ -3888,8 +3888,6 @@ PAL_BindResources(IN LPCSTR lpDomain);
 
 #define EXCEPTION_IS_SIGNAL 0x100
 
-#define EXCEPTION_ON_STACK 0x400
-
 #define EXCEPTION_MAXIMUM_PARAMETERS 15
 
 // Index in the ExceptionInformation array where we will keep the reference
@@ -5796,6 +5794,11 @@ PAL_FreeExceptionRecords(
   IN EXCEPTION_RECORD *exceptionRecord, 
   IN CONTEXT *contextRecord);
 
+VOID
+AllocateExceptionRecords(
+    EXCEPTION_RECORD** exceptionRecord,
+    CONTEXT** contextRecord);
+
 #define EXCEPTION_CONTINUE_SEARCH   0
 #define EXCEPTION_EXECUTE_HANDLER   1
 #define EXCEPTION_CONTINUE_EXECUTION -1
@@ -5810,14 +5813,14 @@ private:
         ExceptionPointers.ExceptionRecord = ex.ExceptionPointers.ExceptionRecord;
         ExceptionPointers.ContextRecord = ex.ExceptionPointers.ContextRecord;
         TargetFrameSp = ex.TargetFrameSp;
+        RecordsOnStack = ex.RecordsOnStack;
 
         ex.Clear();
     }
 
     void FreeRecords()
     {
-        if (ExceptionPointers.ExceptionRecord != NULL &&
-            ! (ExceptionPointers.ExceptionRecord->ExceptionFlags | EXCEPTION_ON_STACK) )
+        if (ExceptionPointers.ExceptionRecord != NULL && !RecordsOnStack )
         {
             PAL_FreeExceptionRecords(ExceptionPointers.ExceptionRecord, ExceptionPointers.ContextRecord);
             ExceptionPointers.ExceptionRecord = NULL;
@@ -5829,12 +5832,14 @@ public:
     EXCEPTION_POINTERS ExceptionPointers;
     // Target frame stack pointer set before the 2nd pass.
     SIZE_T TargetFrameSp;
+    bool RecordsOnStack;
 
-    PAL_SEHException(EXCEPTION_RECORD *pExceptionRecord, CONTEXT *pContextRecord)
+    PAL_SEHException(EXCEPTION_RECORD *pExceptionRecord, CONTEXT *pContextRecord, bool onStack = false)
     {
         ExceptionPointers.ExceptionRecord = pExceptionRecord;
         ExceptionPointers.ContextRecord = pContextRecord;
         TargetFrameSp = NoTargetFrameSp;
+        RecordsOnStack = onStack;
     }
 
     PAL_SEHException()
@@ -5870,6 +5875,26 @@ public:
         ExceptionPointers.ExceptionRecord = NULL;
         ExceptionPointers.ContextRecord = NULL;
         TargetFrameSp = NoTargetFrameSp;
+        RecordsOnStack = false;
+    }
+
+    void EnsureExceptionRecordsOnHeap()
+    {
+        if( !RecordsOnStack || ExceptionPointers.ExceptionRecord == NULL)
+        {
+            return;
+        }
+
+        CONTEXT* contextRecordCopy;
+        EXCEPTION_RECORD* exceptionRecordCopy;
+        AllocateExceptionRecords(&exceptionRecordCopy, &contextRecordCopy);
+
+        *exceptionRecordCopy = *ExceptionPointers.ExceptionRecord;
+        ExceptionPointers.ExceptionRecord = exceptionRecordCopy;
+        *contextRecordCopy = *ExceptionPointers.ContextRecord;
+        ExceptionPointers.ContextRecord = contextRecordCopy;
+
+        RecordsOnStack = false;
     }
 
     CONTEXT* GetContextRecord()
diff --git a/src/pal/src/exception/seh.cpp b/src/pal/src/exception/seh.cpp
index a7d4ad9..27766f2 100644
--- a/src/pal/src/exception/seh.cpp
+++ b/src/pal/src/exception/seh.cpp
@@ -232,23 +232,6 @@ void ThrowExceptionHelper(PAL_SEHException* ex)
     throw std::move(*ex);
 }
 
-static PAL_SEHException copyPAL_SEHException(PAL_SEHException* src)
-{
-    CONTEXT* contextRecord = src->GetContextRecord();
-    EXCEPTION_RECORD* exceptionRecord = src->GetExceptionRecord();
-
-    CONTEXT* contextRecordCopy;
-    EXCEPTION_RECORD* exceptionRecordCopy;
-    AllocateExceptionRecords(&exceptionRecordCopy, &contextRecordCopy);
-
-    *exceptionRecordCopy = *exceptionRecord;
-    exceptionRecordCopy->ExceptionFlags &= ~EXCEPTION_ON_STACK;
-    *contextRecordCopy = *contextRecord;
-    return PAL_SEHException(exceptionRecordCopy, contextRecordCopy);
-} 
-
-
-
 /*++
 Function:
     SEHProcessException
@@ -296,10 +279,8 @@ SEHProcessException(PAL_SEHException* exception)
                         PROCAbort();
                     }
                 }
-                
-                if(exceptionRecord->ExceptionFlags | EXCEPTION_ON_STACK)
-                    *exception = copyPAL_SEHException(exception);
 
+                exception->EnsureExceptionRecordsOnHeap();
                 if (g_hardwareExceptionHandler(exception))
                 {
                     // The exception happened in managed code and the execution should continue.
@@ -312,9 +293,7 @@ SEHProcessException(PAL_SEHException* exception)
 
         if (CatchHardwareExceptionHolder::IsEnabled())
         {
-            if(exceptionRecord->ExceptionFlags | EXCEPTION_ON_STACK)
-                *exception = copyPAL_SEHException(exception);
-
+            exception->EnsureExceptionRecordsOnHeap();
             PAL_ThrowExceptionFromContext(exception->GetContextRecord(), exception);
         }
     }
diff --git a/src/pal/src/exception/signal.cpp b/src/pal/src/exception/signal.cpp
index d42ba38..3b4bec8 100644
--- a/src/pal/src/exception/signal.cpp
+++ b/src/pal/src/exception/signal.cpp
@@ -808,10 +808,8 @@ static bool common_signal_handler(int code, siginfo_t *siginfo, void *sigcontext
     ucontext = (native_context_t *)sigcontext;
     g_common_signal_handler_context_locvar_offset = (int)((char*)&signalContextRecord - (char*)__builtin_frame_address(0));
 
-    //AllocateExceptionRecords(&exceptionRecord, &contextRecord);
-
     exceptionRecord.ExceptionCode = CONTEXTGetExceptionCodeForSignal(siginfo, ucontext);
-    exceptionRecord.ExceptionFlags = EXCEPTION_IS_SIGNAL | EXCEPTION_ON_STACK;
+    exceptionRecord.ExceptionFlags = EXCEPTION_IS_SIGNAL;
     exceptionRecord.ExceptionRecord = NULL;
     exceptionRecord.ExceptionAddress = GetNativeContextPC(ucontext);
     exceptionRecord.NumberParameters = numParams;
@@ -853,7 +851,7 @@ static bool common_signal_handler(int code, siginfo_t *siginfo, void *sigcontext
     memcpy_s(&signalContextRecord, sizeof(CONTEXT), &contextRecord, sizeof(CONTEXT));
 
     // The exception object takes ownership of the exceptionRecord and contextRecord
-    PAL_SEHException exception(&exceptionRecord, &contextRecord);
+    PAL_SEHException exception(&exceptionRecord, &contextRecord, true);
 
     if (SEHProcessException(&exception))
     {
-- 
2.7.4