/src/yara/libyara/exception.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | Copyright (c) 2015. The YARA Authors. All Rights Reserved. |
3 | | |
4 | | Redistribution and use in source and binary forms, with or without modification, |
5 | | are permitted provided that the following conditions are met: |
6 | | |
7 | | 1. Redistributions of source code must retain the above copyright notice, this |
8 | | list of conditions and the following disclaimer. |
9 | | |
10 | | 2. Redistributions in binary form must reproduce the above copyright notice, |
11 | | this list of conditions and the following disclaimer in the documentation and/or |
12 | | other materials provided with the distribution. |
13 | | |
14 | | 3. Neither the name of the copyright holder nor the names of its contributors |
15 | | may be used to endorse or promote products derived from this software without |
16 | | specific prior written permission. |
17 | | |
18 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
19 | | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
20 | | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
21 | | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
22 | | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
23 | | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
24 | | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
25 | | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
27 | | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | */ |
29 | | |
30 | | #ifndef YR_EXCEPTION_H |
31 | | #define YR_EXCEPTION_H |
32 | | |
33 | | #include <assert.h> |
34 | | #include <yara/globals.h> |
35 | | |
36 | | #if _WIN32 || __CYGWIN__ |
37 | | |
38 | | #include <windows.h> |
39 | | |
40 | | // If compiling with Microsoft's compiler use structured exception handling. |
41 | | |
42 | | #ifdef _MSC_VER |
43 | | |
44 | | #include <excpt.h> |
45 | | |
46 | | static LONG CALLBACK exception_handler(PEXCEPTION_POINTERS ExceptionInfo) |
47 | | { |
48 | | switch (ExceptionInfo->ExceptionRecord->ExceptionCode) |
49 | | { |
50 | | case EXCEPTION_IN_PAGE_ERROR: |
51 | | case EXCEPTION_ACCESS_VIOLATION: |
52 | | return EXCEPTION_EXECUTE_HANDLER; |
53 | | } |
54 | | |
55 | | return EXCEPTION_CONTINUE_SEARCH; |
56 | | } |
57 | | |
58 | | #define YR_TRYCATCH(_do_, _try_clause_, _catch_clause_) \ |
59 | | do \ |
60 | | { \ |
61 | | if (_do_) \ |
62 | | { \ |
63 | | __try \ |
64 | | { \ |
65 | | _try_clause_ \ |
66 | | } \ |
67 | | __except (exception_handler(GetExceptionInformation())) \ |
68 | | { \ |
69 | | _catch_clause_ \ |
70 | | } \ |
71 | | } \ |
72 | | else \ |
73 | | { \ |
74 | | _try_clause_ \ |
75 | | } \ |
76 | | } while (0) |
77 | | |
78 | | #else |
79 | | |
80 | | // If not compiling with Microsoft's compiler use vectored exception handling. |
81 | | |
82 | | #include <setjmp.h> |
83 | | |
84 | | static LONG CALLBACK exception_handler(PEXCEPTION_POINTERS ExceptionInfo) |
85 | | { |
86 | | jmp_buf* jb_ptr; |
87 | | |
88 | | switch (ExceptionInfo->ExceptionRecord->ExceptionCode) |
89 | | { |
90 | | case EXCEPTION_IN_PAGE_ERROR: |
91 | | case EXCEPTION_ACCESS_VIOLATION: |
92 | | jb_ptr = |
93 | | (jmp_buf*) yr_thread_storage_get_value(&yr_trycatch_trampoline_tls); |
94 | | |
95 | | if (jb_ptr != NULL) |
96 | | longjmp(*jb_ptr, 1); |
97 | | } |
98 | | |
99 | | return EXCEPTION_CONTINUE_SEARCH; |
100 | | } |
101 | | |
102 | | #define YR_TRYCATCH(_do_, _try_clause_, _catch_clause_) \ |
103 | | do \ |
104 | | { \ |
105 | | if (_do_) \ |
106 | | { \ |
107 | | jmp_buf jb; \ |
108 | | /* Store pointer to sigjmp_buf in TLS */ \ |
109 | | yr_thread_storage_set_value(&yr_trycatch_trampoline_tls, &jb); \ |
110 | | HANDLE exh = AddVectoredExceptionHandler(1, exception_handler); \ |
111 | | if (setjmp(jb) == 0) \ |
112 | | { \ |
113 | | _try_clause_ \ |
114 | | } \ |
115 | | else \ |
116 | | { \ |
117 | | _catch_clause_ \ |
118 | | } \ |
119 | | RemoveVectoredExceptionHandler(exh); \ |
120 | | yr_thread_storage_set_value(&yr_trycatch_trampoline_tls, NULL); \ |
121 | | } \ |
122 | | else \ |
123 | | { \ |
124 | | _try_clause_ \ |
125 | | } \ |
126 | | } while (0) |
127 | | |
128 | | #endif |
129 | | |
130 | | #else |
131 | | |
132 | | #include <setjmp.h> |
133 | | #include <signal.h> |
134 | | #include <yara/globals.h> |
135 | | |
136 | | static void exception_handler(int sig) |
137 | 0 | { |
138 | 0 | if (sig == SIGBUS || sig == SIGSEGV) |
139 | 0 | { |
140 | 0 | jmp_buf* jb_ptr = |
141 | 0 | (jmp_buf*) yr_thread_storage_get_value(&yr_trycatch_trampoline_tls); |
142 | |
|
143 | 0 | if (jb_ptr != NULL) |
144 | 0 | siglongjmp(*jb_ptr, 1); |
145 | 0 | } |
146 | 0 | } |
147 | | |
148 | | typedef struct sigaction sa; |
149 | | |
150 | | #define YR_TRYCATCH(_do_, _try_clause_, _catch_clause_) \ |
151 | 12.6k | do \ |
152 | 12.6k | { \ |
153 | 12.6k | if (_do_) \ |
154 | 12.6k | { \ |
155 | 0 | struct sigaction old_sigbus_act; \ |
156 | 0 | struct sigaction old_sigsegv_act; \ |
157 | 0 | struct sigaction act; \ |
158 | 0 | sigjmp_buf jb; \ |
159 | 0 | /* Store pointer to sigjmp_buf in TLS */ \ |
160 | 0 | yr_thread_storage_set_value(&yr_trycatch_trampoline_tls, &jb); \ |
161 | 0 | /* Set exception handler for SIGBUS and SIGSEGV*/ \ |
162 | 0 | act.sa_handler = exception_handler; \ |
163 | 0 | act.sa_flags = 0; /* SA_ONSTACK? */ \ |
164 | 0 | sigfillset(&act.sa_mask); \ |
165 | 0 | sigaction(SIGBUS, &act, &old_sigbus_act); \ |
166 | 0 | sigaction(SIGSEGV, &act, &old_sigsegv_act); \ |
167 | 0 | if (sigsetjmp(jb, 1) == 0) \ |
168 | 0 | { \ |
169 | 0 | _try_clause_ \ |
170 | 0 | } \ |
171 | 0 | else \ |
172 | 0 | { \ |
173 | 0 | _catch_clause_ \ |
174 | 0 | } \ |
175 | 0 | /* Stop capturing SIGBUS and SIGSEGV */ \ |
176 | 0 | sigaction(SIGBUS, &old_sigbus_act, NULL); \ |
177 | 0 | sigaction(SIGSEGV, &old_sigsegv_act, NULL); \ |
178 | 0 | yr_thread_storage_set_value(&yr_trycatch_trampoline_tls, NULL); \ |
179 | 0 | } \ |
180 | 12.6k | else \ |
181 | 12.6k | { \ |
182 | 12.6k | _try_clause_ \ |
183 | 12.6k | } \ |
184 | 12.6k | } while (0) |
185 | | |
186 | | #endif |
187 | | |
188 | | #endif |