Sergey Melnikov | c45087b | 2013-01-25 16:40:13 +0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * Contributed by: Intel Corporation |
| 19 | */ |
| 20 | |
Elliott Hughes | bee1993 | 2014-09-17 17:21:20 -0700 | [diff] [blame] | 21 | #include <dlfcn.h> |
Sergey Melnikov | c45087b | 2013-01-25 16:40:13 +0400 | [diff] [blame] | 22 | #include <signal.h> |
Elliott Hughes | bee1993 | 2014-09-17 17:21:20 -0700 | [diff] [blame] | 23 | #include <stdio.h> |
Sergey Melnikov | c45087b | 2013-01-25 16:40:13 +0400 | [diff] [blame] | 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
Elliott Hughes | bee1993 | 2014-09-17 17:21:20 -0700 | [diff] [blame] | 26 | #include <unistd.h> |
Sergey Melnikov | c45087b | 2013-01-25 16:40:13 +0400 | [diff] [blame] | 27 | #include <unwind.h> |
| 28 | |
| 29 | #define noinline __attribute__((__noinline__)) |
Sergey Melnikov | c45087b | 2013-01-25 16:40:13 +0400 | [diff] [blame] | 30 | |
Elliott Hughes | bee1993 | 2014-09-17 17:21:20 -0700 | [diff] [blame] | 31 | #ifndef __unused |
| 32 | #define __unused __attribute__((__unused__)) |
| 33 | #endif |
| 34 | |
| 35 | static noinline _Unwind_Reason_Code cleanup_unwind_fn(int a __unused, |
Sergey Melnikov | c45087b | 2013-01-25 16:40:13 +0400 | [diff] [blame] | 36 | _Unwind_Action action, |
Elliott Hughes | bee1993 | 2014-09-17 17:21:20 -0700 | [diff] [blame] | 37 | _Unwind_Exception_Class b __unused, |
| 38 | struct _Unwind_Exception* c __unused, |
| 39 | struct _Unwind_Context* ctx __unused, |
| 40 | void* e __unused) { |
Sergey Melnikov | c45087b | 2013-01-25 16:40:13 +0400 | [diff] [blame] | 41 | if ((action & _UA_END_OF_STACK) != 0) { |
Elliott Hughes | bee1993 | 2014-09-17 17:21:20 -0700 | [diff] [blame] | 42 | abort(); // We reached the end of the stack without executing foo_cleanup (which would have exited). Test failed. |
Sergey Melnikov | c45087b | 2013-01-25 16:40:13 +0400 | [diff] [blame] | 43 | } |
| 44 | return _URC_NO_REASON; |
| 45 | } |
| 46 | |
Elliott Hughes | bee1993 | 2014-09-17 17:21:20 -0700 | [diff] [blame] | 47 | static void noinline foo_cleanup(char* param __unused) { |
Sergey Melnikov | c45087b | 2013-01-25 16:40:13 +0400 | [diff] [blame] | 48 | exit(42); |
| 49 | } |
| 50 | |
Elliott Hughes | bee1993 | 2014-09-17 17:21:20 -0700 | [diff] [blame] | 51 | static void noinline function_with_cleanup_function() { |
| 52 | char c __attribute__((cleanup(foo_cleanup))) __unused; |
| 53 | *((int*) 1) = 0; |
Sergey Melnikov | c45087b | 2013-01-25 16:40:13 +0400 | [diff] [blame] | 54 | } |
| 55 | |
Elliott Hughes | bee1993 | 2014-09-17 17:21:20 -0700 | [diff] [blame] | 56 | static void noinline cleanup_sigsegv_handler(int param __unused) { |
| 57 | struct _Unwind_Exception* exception = (struct _Unwind_Exception*) calloc(1, sizeof(*exception)); |
| 58 | _Unwind_ForcedUnwind(exception, cleanup_unwind_fn, 0); |
Sergey Melnikov | c45087b | 2013-01-25 16:40:13 +0400 | [diff] [blame] | 59 | } |
| 60 | |
Elliott Hughes | bee1993 | 2014-09-17 17:21:20 -0700 | [diff] [blame] | 61 | void unwind_through_frame_with_cleanup_function() { |
| 62 | signal(SIGSEGV, &cleanup_sigsegv_handler); |
| 63 | function_with_cleanup_function(); |
Sergey Melnikov | c45087b | 2013-01-25 16:40:13 +0400 | [diff] [blame] | 64 | } |