blob: 314e8de26ed17251cca8e7a967b297492e330f81 [file] [log] [blame]
Dmitriy Ivanov6b566912014-04-29 08:41:29 -07001/*
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 Ivanov4571f312014-08-15 14:22:07 -070022static std::string* atexit_sequence = nullptr;
23static bool* atexit_valid_this_in_static_dtor = nullptr;
24static bool* atexit_attr_dtor_called = nullptr;
25
26static int cxx_ctor_called = 0;
27static int attr_ctor_called = 0;
Dmitriy Ivanov6b566912014-04-29 08:41:29 -070028
Dmitriy Ivanov6b566912014-04-29 08:41:29 -070029static class AtExitStaticClass {
Dmitriy Ivanov6396da92014-05-05 19:52:13 -070030 public:
Dmitriy Ivanov4571f312014-08-15 14:22:07 -070031 AtExitStaticClass() {
32 expected_this = this;
33 cxx_ctor_called = 1;
34 }
Dmitriy Ivanov6b566912014-04-29 08:41:29 -070035 ~AtExitStaticClass() {
36 if (atexit_valid_this_in_static_dtor) {
Dmitriy Ivanov6396da92014-05-05 19:52:13 -070037 *atexit_valid_this_in_static_dtor = (expected_this == this);
Dmitriy Ivanov6b566912014-04-29 08:41:29 -070038 }
39 }
Dmitriy Ivanov6396da92014-05-05 19:52:13 -070040 private:
41 static const AtExitStaticClass* expected_this;
42
43} static_obj;
44
Dmitriy Ivanov4571f312014-08-15 14:22:07 -070045const AtExitStaticClass* AtExitStaticClass::expected_this = nullptr;
Dmitriy Ivanov6b566912014-04-29 08:41:29 -070046
47// 4
48static void atexit_handler_from_atexit_from_atexit2() {
49 *atexit_sequence += " on";
50}
51
52// 3
53static void atexit_handler_from_atexit_from_atexit1() {
54 *atexit_sequence += " sat";
55}
56
57// 2
58static 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
66static void atexit_handler_with_atexit() {
67 *atexit_sequence += "Humpty";
68 atexit(atexit_handler_from_atexit);
69}
70
71// last
72static void atexit_handler_regular() {
73 *atexit_sequence += " a wall";
74}
75
Dmitriy Ivanov4571f312014-08-15 14:22:07 -070076// attribute c-tor and d-tor
77static void __attribute__((constructor)) atexit_attr_ctor() {
78 attr_ctor_called = 1;
79}
80
81static void __attribute__((destructor)) atexit_attr_dtor() {
82 if (atexit_attr_dtor_called) {
83 *atexit_attr_dtor_called = true;
84 }
85}
86
87extern "C" void register_atexit(std::string* sequence, bool* valid_this_in_static_dtor, bool* attr_dtor_called) {
Dmitriy Ivanov6b566912014-04-29 08:41:29 -070088 atexit_sequence = sequence;
89 atexit_valid_this_in_static_dtor = valid_this_in_static_dtor;
Dmitriy Ivanov4571f312014-08-15 14:22:07 -070090 atexit_attr_dtor_called = attr_dtor_called;
Dmitriy Ivanov6b566912014-04-29 08:41:29 -070091 atexit(atexit_handler_regular);
92 atexit(atexit_handler_with_atexit);
Dmitriy Ivanov6b566912014-04-29 08:41:29 -070093}
94
Dmitriy Ivanov4571f312014-08-15 14:22:07 -070095extern "C" int get_cxx_ctor_called() {
96 return cxx_ctor_called;
97}
98
99extern "C" int get_attr_ctor_called() {
100 return attr_ctor_called;
101}
102