/
RuntimeObjectSystem_PlatformPosix.cpp
168 lines (144 loc) · 5.51 KB
/
RuntimeObjectSystem_PlatformPosix.cpp
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
//
// Copyright (c) 2010-2011 Matthew Jack and Doug Binks
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
#ifndef _WIN32
#include "RuntimeObjectSystem.h"
#include "RuntimeProtector.h"
#ifdef __APPLE__
// Mach ports requirements
#include <mach/mach.h>
#endif
#include <pthread.h>
#include <assert.h>
#include <signal.h>
#include <string.h>
static __thread RuntimeProtector* m_pCurrProtector = 0; // for nested threaded handling, one per thread.
#ifdef __APPLE__
static bool ms_bMachPortSet = false;
const size_t NUM_OLD_EXCEPTION_HANDLERS = 16;
static mach_msg_type_number_t old_count;
static exception_mask_t old_masks[NUM_OLD_EXCEPTION_HANDLERS];
static mach_port_t old_ports[NUM_OLD_EXCEPTION_HANDLERS];
static exception_behavior_t old_behaviors[NUM_OLD_EXCEPTION_HANDLERS];
static thread_state_flavor_t old_flavors[NUM_OLD_EXCEPTION_HANDLERS];
#endif
void RuntimeObjectSystem::CreatePlatformImpl()
{
#ifdef __APPLE__
if( !ms_bMachPortSet )
{
// prevent OS X debugger from catching signals in a none re-catchable way
task_get_exception_ports(mach_task_self(), EXC_MASK_BAD_ACCESS | EXC_MASK_BAD_INSTRUCTION, old_masks, &old_count, old_ports, old_behaviors, old_flavors);
task_set_exception_ports(mach_task_self(), EXC_MASK_BAD_ACCESS | EXC_MASK_BAD_INSTRUCTION, MACH_PORT_NULL, EXCEPTION_DEFAULT, MACHINE_THREAD_STATE);
ms_bMachPortSet = true;
}
#endif
}
void RuntimeObjectSystem::DeletePlatformImpl()
{
}
void RuntimeObjectSystem::SetProtectionEnabled( bool bProtectionEnabled_ )
{
m_bProtectionEnabled = bProtectionEnabled_;
#ifdef __APPLE__
if( !m_bProtectionEnabled )
{
if( ms_bMachPortSet )
{
for( int i = 0; i < static_cast<int>(old_count); ++i )
{
task_set_exception_ports( mach_task_self(), old_masks[i], old_ports[i], old_behaviors[i], old_flavors[i]);
}
}
}
else
{
CreatePlatformImpl();
}
#endif
}
void signalHandler(int sig, siginfo_t *info, void *context)
{
(void) context;
// we only handle synchronous signals with this handler, so they come to the correct thread.
assert( m_pCurrProtector );
// store exception information
switch( sig )
{
case SIGILL:
m_pCurrProtector->ExceptionInfo.Type = RuntimeProtector::ESE_InvalidInstruction;
break;
case SIGBUS:
m_pCurrProtector->ExceptionInfo.Type = RuntimeProtector::ESE_AccessViolation;
break;
case SIGSEGV:
m_pCurrProtector->ExceptionInfo.Type = RuntimeProtector::ESE_AccessViolation;
break;
default: assert(false); //should not get here
}
m_pCurrProtector->ExceptionInfo.Addr = info->si_addr;
longjmp(m_pCurrProtector->m_env, sig );
}
bool RuntimeObjectSystem::TryProtectedFunction( RuntimeProtector* pProtectedObject_ )
{
if( !m_bProtectionEnabled )
{
pProtectedObject_->ProtectedFunc();
return true;
}
// allow cascading by storing prev and current impl
pProtectedObject_->m_pPrevious = m_pCurrProtector;
m_pCurrProtector = pProtectedObject_;
struct sigaction oldAction[3]; // we need to store old actions, could remove for optimization
bool bHasJustHadException = false;
if( m_TotalLoadedModulesEver != pProtectedObject_->m_ModulesLoadedCount )
{
// clear exceptions if we've just loaded a new module
pProtectedObject_->m_ModulesLoadedCount = m_TotalLoadedModulesEver;
pProtectedObject_->m_bHashadException = false;
}
if( !pProtectedObject_->m_bHashadException )
{
if( setjmp(m_pCurrProtector->m_env) )
{
pProtectedObject_->m_bHashadException = true;
bHasJustHadException = true;
}
else
{
struct sigaction newAction;
memset( &newAction, 0, sizeof( newAction ));
newAction.sa_sigaction = signalHandler;
newAction.sa_flags = SA_SIGINFO; //use complex signal hander function sa_sigaction not sa_handler
sigaction(SIGILL, &newAction, &oldAction[0] );
sigaction(SIGBUS, &newAction, &oldAction[1] );
sigaction(SIGSEGV, &newAction, &oldAction[2] );
pProtectedObject_->ProtectedFunc();
}
//reset
sigaction(SIGILL, &oldAction[0], NULL );
sigaction(SIGBUS, &oldAction[1], NULL );
sigaction(SIGSEGV, &oldAction[2], NULL );
}
m_pCurrProtector = pProtectedObject_->m_pPrevious;
return !bHasJustHadException;
}
bool RuntimeObjectSystem::TestBuildWaitAndUpdate()
{
usleep( 100 * 1000 );
return true;
}
#endif // #ifndef _WIN32