summaryrefslogtreecommitdiff
path: root/packaging/0026-Prevent-memory-allocation-in-signal-handler.patch
blob: b3617fa8c3c6eb471748af38c9923ff89fbdfd19 (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
From 97b80457b091641dd6fb366038b8e362b1409a58 Mon Sep 17 00:00:00 2001
From: Mikhail Labiuk <m.labiuk@samsung.com>
Date: Tue, 13 Feb 2018 15:02:12 +0300
Subject: [PATCH 26/47] Prevent memory allocation in signal handler

If the signal occurs when heap being inconsistent we should not
use heap. We should call signal-safe functions only from signal handler.

fix https://github.com/dotnet/coreclr/issues/16338
---
 src/pal/src/exception/seh-unwind.cpp | 6 ++++--
 src/pal/src/exception/signal.cpp     | 2 +-
 src/pal/src/include/pal/seh.hpp      | 3 ++-
 3 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/src/pal/src/exception/seh-unwind.cpp b/src/pal/src/exception/seh-unwind.cpp
index 7746bbb..eba2c80 100644
--- a/src/pal/src/exception/seh-unwind.cpp
+++ b/src/pal/src/exception/seh-unwind.cpp
@@ -620,12 +620,14 @@ Function:
 Parameters:
     exceptionRecord - output pointer to the allocated exception record
     contextRecord - output pointer to the allocated context record
+    allocationProhibited - input flag to avoid memory allocation in critical situations
 --*/
 VOID
-AllocateExceptionRecords(EXCEPTION_RECORD** exceptionRecord, CONTEXT** contextRecord)
+AllocateExceptionRecords(EXCEPTION_RECORD** exceptionRecord, CONTEXT** contextRecord, BOOL allocationProhibited)
 {
     ExceptionRecords* records;
-    if (posix_memalign((void**)&records, alignof(ExceptionRecords), sizeof(ExceptionRecords)) != 0)
+    if (allocationProhibited ||
+	(posix_memalign((void**)&records, alignof(ExceptionRecords), sizeof(ExceptionRecords)) != 0) )
     {
         size_t bitmap;
         size_t newBitmap;
diff --git a/src/pal/src/exception/signal.cpp b/src/pal/src/exception/signal.cpp
index bf48619..90da207 100644
--- a/src/pal/src/exception/signal.cpp
+++ b/src/pal/src/exception/signal.cpp
@@ -808,7 +808,7 @@ 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);
+    AllocateExceptionRecords(&exceptionRecord, &contextRecord, true);
 
     exceptionRecord->ExceptionCode = CONTEXTGetExceptionCodeForSignal(siginfo, ucontext);
     exceptionRecord->ExceptionFlags = EXCEPTION_IS_SIGNAL;
diff --git a/src/pal/src/include/pal/seh.hpp b/src/pal/src/include/pal/seh.hpp
index 3ac93d6..5edc214 100644
--- a/src/pal/src/include/pal/seh.hpp
+++ b/src/pal/src/include/pal/seh.hpp
@@ -84,9 +84,10 @@ Function:
 Parameters:
     exceptionRecord - output pointer to the allocated Windows exception record
     contextRecord - output pointer to the allocated Windows context record
+    allocationProhibited - input flag to avoid memory allocation in critical situations
 --*/
 VOID
-AllocateExceptionRecords(EXCEPTION_RECORD** exceptionRecord, CONTEXT** contextRecord);
+AllocateExceptionRecords(EXCEPTION_RECORD** exceptionRecord, CONTEXT** contextRecord,  BOOL allocationProhibited=false);
 
 #if !HAVE_MACH_EXCEPTIONS
 // TODO: Implement for Mach exceptions.  Not in CoreCLR surface area.
-- 
2.7.4