Dmitriy Ivanov | 6b56691 | 2014-04-29 08:41:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | |
| 19 | #include <string> |
| 20 | |
| 21 | // use external control number from main test |
Dmitriy Ivanov | 4571f31 | 2014-08-15 14:22:07 -0700 | [diff] [blame] | 22 | static std::string* atexit_sequence = nullptr; |
| 23 | static bool* atexit_valid_this_in_static_dtor = nullptr; |
| 24 | static bool* atexit_attr_dtor_called = nullptr; |
| 25 | |
| 26 | static int cxx_ctor_called = 0; |
| 27 | static int attr_ctor_called = 0; |
Dmitriy Ivanov | 6b56691 | 2014-04-29 08:41:29 -0700 | [diff] [blame] | 28 | |
Dmitriy Ivanov | 6b56691 | 2014-04-29 08:41:29 -0700 | [diff] [blame] | 29 | static class AtExitStaticClass { |
Dmitriy Ivanov | 6396da9 | 2014-05-05 19:52:13 -0700 | [diff] [blame] | 30 | public: |
Dmitriy Ivanov | 4571f31 | 2014-08-15 14:22:07 -0700 | [diff] [blame] | 31 | AtExitStaticClass() { |
| 32 | expected_this = this; |
| 33 | cxx_ctor_called = 1; |
| 34 | } |
Dmitriy Ivanov | 6b56691 | 2014-04-29 08:41:29 -0700 | [diff] [blame] | 35 | ~AtExitStaticClass() { |
| 36 | if (atexit_valid_this_in_static_dtor) { |
Dmitriy Ivanov | 6396da9 | 2014-05-05 19:52:13 -0700 | [diff] [blame] | 37 | *atexit_valid_this_in_static_dtor = (expected_this == this); |
Dmitriy Ivanov | 6b56691 | 2014-04-29 08:41:29 -0700 | [diff] [blame] | 38 | } |
| 39 | } |
Dmitriy Ivanov | 6396da9 | 2014-05-05 19:52:13 -0700 | [diff] [blame] | 40 | private: |
| 41 | static const AtExitStaticClass* expected_this; |
| 42 | |
| 43 | } static_obj; |
| 44 | |
Dmitriy Ivanov | 4571f31 | 2014-08-15 14:22:07 -0700 | [diff] [blame] | 45 | const AtExitStaticClass* AtExitStaticClass::expected_this = nullptr; |
Dmitriy Ivanov | 6b56691 | 2014-04-29 08:41:29 -0700 | [diff] [blame] | 46 | |
| 47 | // 4 |
| 48 | static void atexit_handler_from_atexit_from_atexit2() { |
| 49 | *atexit_sequence += " on"; |
| 50 | } |
| 51 | |
| 52 | // 3 |
| 53 | static void atexit_handler_from_atexit_from_atexit1() { |
| 54 | *atexit_sequence += " sat"; |
| 55 | } |
| 56 | |
| 57 | // 2 |
| 58 | static void atexit_handler_from_atexit() { |
| 59 | *atexit_sequence += " Dumpty"; |
| 60 | // register 2 others |
| 61 | atexit(atexit_handler_from_atexit_from_atexit2); |
| 62 | atexit(atexit_handler_from_atexit_from_atexit1); |
| 63 | } |
| 64 | |
| 65 | // 1 |
| 66 | static void atexit_handler_with_atexit() { |
| 67 | *atexit_sequence += "Humpty"; |
| 68 | atexit(atexit_handler_from_atexit); |
| 69 | } |
| 70 | |
| 71 | // last |
| 72 | static void atexit_handler_regular() { |
| 73 | *atexit_sequence += " a wall"; |
| 74 | } |
| 75 | |
Dmitriy Ivanov | 4571f31 | 2014-08-15 14:22:07 -0700 | [diff] [blame] | 76 | // attribute c-tor and d-tor |
| 77 | static void __attribute__((constructor)) atexit_attr_ctor() { |
| 78 | attr_ctor_called = 1; |
| 79 | } |
| 80 | |
| 81 | static void __attribute__((destructor)) atexit_attr_dtor() { |
| 82 | if (atexit_attr_dtor_called) { |
| 83 | *atexit_attr_dtor_called = true; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | extern "C" void register_atexit(std::string* sequence, bool* valid_this_in_static_dtor, bool* attr_dtor_called) { |
Dmitriy Ivanov | 6b56691 | 2014-04-29 08:41:29 -0700 | [diff] [blame] | 88 | atexit_sequence = sequence; |
| 89 | atexit_valid_this_in_static_dtor = valid_this_in_static_dtor; |
Dmitriy Ivanov | 4571f31 | 2014-08-15 14:22:07 -0700 | [diff] [blame] | 90 | atexit_attr_dtor_called = attr_dtor_called; |
Dmitriy Ivanov | 6b56691 | 2014-04-29 08:41:29 -0700 | [diff] [blame] | 91 | atexit(atexit_handler_regular); |
| 92 | atexit(atexit_handler_with_atexit); |
Dmitriy Ivanov | 6b56691 | 2014-04-29 08:41:29 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Dmitriy Ivanov | 4571f31 | 2014-08-15 14:22:07 -0700 | [diff] [blame] | 95 | extern "C" int get_cxx_ctor_called() { |
| 96 | return cxx_ctor_called; |
| 97 | } |
| 98 | |
| 99 | extern "C" int get_attr_ctor_called() { |
| 100 | return attr_ctor_called; |
| 101 | } |
| 102 | |