blob: 2e039384715ce1e9d701ddc3e8554568f76b416b [file] [log] [blame]
Sergey Melnikovc45087b2013-01-25 16:40:13 +04001/*
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 Hughesbee19932014-09-17 17:21:20 -070021#include <dlfcn.h>
Sergey Melnikovc45087b2013-01-25 16:40:13 +040022#include <signal.h>
Elliott Hughesbee19932014-09-17 17:21:20 -070023#include <stdio.h>
Sergey Melnikovc45087b2013-01-25 16:40:13 +040024#include <stdlib.h>
25#include <string.h>
Elliott Hughesbee19932014-09-17 17:21:20 -070026#include <unistd.h>
Sergey Melnikovc45087b2013-01-25 16:40:13 +040027#include <unwind.h>
28
29#define noinline __attribute__((__noinline__))
Sergey Melnikovc45087b2013-01-25 16:40:13 +040030
Elliott Hughesbee19932014-09-17 17:21:20 -070031#ifndef __unused
32#define __unused __attribute__((__unused__))
33#endif
34
35static noinline _Unwind_Reason_Code cleanup_unwind_fn(int a __unused,
Sergey Melnikovc45087b2013-01-25 16:40:13 +040036 _Unwind_Action action,
Elliott Hughesbee19932014-09-17 17:21:20 -070037 _Unwind_Exception_Class b __unused,
38 struct _Unwind_Exception* c __unused,
39 struct _Unwind_Context* ctx __unused,
40 void* e __unused) {
Sergey Melnikovc45087b2013-01-25 16:40:13 +040041 if ((action & _UA_END_OF_STACK) != 0) {
Elliott Hughesbee19932014-09-17 17:21:20 -070042 abort(); // We reached the end of the stack without executing foo_cleanup (which would have exited). Test failed.
Sergey Melnikovc45087b2013-01-25 16:40:13 +040043 }
44 return _URC_NO_REASON;
45}
46
Elliott Hughesbee19932014-09-17 17:21:20 -070047static void noinline foo_cleanup(char* param __unused) {
Sergey Melnikovc45087b2013-01-25 16:40:13 +040048 exit(42);
49}
50
Elliott Hughesbee19932014-09-17 17:21:20 -070051static void noinline function_with_cleanup_function() {
52 char c __attribute__((cleanup(foo_cleanup))) __unused;
53 *((int*) 1) = 0;
Sergey Melnikovc45087b2013-01-25 16:40:13 +040054}
55
Elliott Hughesbee19932014-09-17 17:21:20 -070056static 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 Melnikovc45087b2013-01-25 16:40:13 +040059}
60
Elliott Hughesbee19932014-09-17 17:21:20 -070061void unwind_through_frame_with_cleanup_function() {
62 signal(SIGSEGV, &cleanup_sigsegv_handler);
63 function_with_cleanup_function();
Sergey Melnikovc45087b2013-01-25 16:40:13 +040064}