1*8d67ca89SAndroid Build Coastguard Worker /*
2*8d67ca89SAndroid Build Coastguard Worker * Copyright (C) 2014 The Android Open Source Project
3*8d67ca89SAndroid Build Coastguard Worker *
4*8d67ca89SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*8d67ca89SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*8d67ca89SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*8d67ca89SAndroid Build Coastguard Worker *
8*8d67ca89SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*8d67ca89SAndroid Build Coastguard Worker *
10*8d67ca89SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*8d67ca89SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*8d67ca89SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8d67ca89SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*8d67ca89SAndroid Build Coastguard Worker * limitations under the License.
15*8d67ca89SAndroid Build Coastguard Worker */
16*8d67ca89SAndroid Build Coastguard Worker
17*8d67ca89SAndroid Build Coastguard Worker #include <gtest/gtest.h>
18*8d67ca89SAndroid Build Coastguard Worker
19*8d67ca89SAndroid Build Coastguard Worker #include <dlfcn.h>
20*8d67ca89SAndroid Build Coastguard Worker #include <libgen.h>
21*8d67ca89SAndroid Build Coastguard Worker #include <limits.h>
22*8d67ca89SAndroid Build Coastguard Worker #include <stdio.h>
23*8d67ca89SAndroid Build Coastguard Worker #include <stdint.h>
24*8d67ca89SAndroid Build Coastguard Worker
25*8d67ca89SAndroid Build Coastguard Worker #include <string>
26*8d67ca89SAndroid Build Coastguard Worker
TEST(atexit,sofile)27*8d67ca89SAndroid Build Coastguard Worker TEST(atexit, sofile) {
28*8d67ca89SAndroid Build Coastguard Worker std::string atexit_call_sequence;
29*8d67ca89SAndroid Build Coastguard Worker bool valid_this_in_static_dtor = false;
30*8d67ca89SAndroid Build Coastguard Worker bool attr_dtor_called = false;
31*8d67ca89SAndroid Build Coastguard Worker
32*8d67ca89SAndroid Build Coastguard Worker void* handle = dlopen("libtest_atexit.so", RTLD_NOW);
33*8d67ca89SAndroid Build Coastguard Worker ASSERT_TRUE(handle != nullptr);
34*8d67ca89SAndroid Build Coastguard Worker
35*8d67ca89SAndroid Build Coastguard Worker typedef int (*int_fn)(void);
36*8d67ca89SAndroid Build Coastguard Worker int_fn get_cxx_ctor_called, get_attr_ctor_called;
37*8d67ca89SAndroid Build Coastguard Worker get_cxx_ctor_called = reinterpret_cast<int_fn>(dlsym(handle, "get_cxx_ctor_called"));
38*8d67ca89SAndroid Build Coastguard Worker get_attr_ctor_called = reinterpret_cast<int_fn>(dlsym(handle, "get_attr_ctor_called"));
39*8d67ca89SAndroid Build Coastguard Worker ASSERT_TRUE(get_cxx_ctor_called != nullptr);
40*8d67ca89SAndroid Build Coastguard Worker ASSERT_TRUE(get_attr_ctor_called != nullptr);
41*8d67ca89SAndroid Build Coastguard Worker
42*8d67ca89SAndroid Build Coastguard Worker ASSERT_EQ(1, get_cxx_ctor_called());
43*8d67ca89SAndroid Build Coastguard Worker ASSERT_EQ(1, get_attr_ctor_called());
44*8d67ca89SAndroid Build Coastguard Worker
45*8d67ca89SAndroid Build Coastguard Worker void* sym = dlsym(handle, "register_atexit");
46*8d67ca89SAndroid Build Coastguard Worker ASSERT_TRUE(sym != nullptr);
47*8d67ca89SAndroid Build Coastguard Worker reinterpret_cast<void (*)(std::string*, bool*, bool*)>(sym)(&atexit_call_sequence, &valid_this_in_static_dtor, &attr_dtor_called);
48*8d67ca89SAndroid Build Coastguard Worker
49*8d67ca89SAndroid Build Coastguard Worker ASSERT_EQ(0, dlclose(handle));
50*8d67ca89SAndroid Build Coastguard Worker // this test verifies atexit call from atexit handler. as well as the order of calls
51*8d67ca89SAndroid Build Coastguard Worker ASSERT_EQ("Humpty Dumpty sat on a wall", atexit_call_sequence);
52*8d67ca89SAndroid Build Coastguard Worker ASSERT_TRUE(valid_this_in_static_dtor);
53*8d67ca89SAndroid Build Coastguard Worker ASSERT_TRUE(attr_dtor_called);
54*8d67ca89SAndroid Build Coastguard Worker }
55*8d67ca89SAndroid Build Coastguard Worker
56*8d67ca89SAndroid Build Coastguard Worker class TestMainStaticDtorClass {
57*8d67ca89SAndroid Build Coastguard Worker public:
TestMainStaticDtorClass()58*8d67ca89SAndroid Build Coastguard Worker TestMainStaticDtorClass() {
59*8d67ca89SAndroid Build Coastguard Worker expected_this = this;
60*8d67ca89SAndroid Build Coastguard Worker }
61*8d67ca89SAndroid Build Coastguard Worker
~TestMainStaticDtorClass()62*8d67ca89SAndroid Build Coastguard Worker ~TestMainStaticDtorClass() {
63*8d67ca89SAndroid Build Coastguard Worker if (this != expected_this) {
64*8d67ca89SAndroid Build Coastguard Worker fprintf(stderr, "\nerror: static d-tor called with incorrect this pointer: %p, expected: %p\n", this, expected_this);
65*8d67ca89SAndroid Build Coastguard Worker } else {
66*8d67ca89SAndroid Build Coastguard Worker fprintf(stderr, "6");
67*8d67ca89SAndroid Build Coastguard Worker }
68*8d67ca89SAndroid Build Coastguard Worker }
69*8d67ca89SAndroid Build Coastguard Worker private:
70*8d67ca89SAndroid Build Coastguard Worker static const TestMainStaticDtorClass* expected_this;
71*8d67ca89SAndroid Build Coastguard Worker };
72*8d67ca89SAndroid Build Coastguard Worker
73*8d67ca89SAndroid Build Coastguard Worker const TestMainStaticDtorClass* TestMainStaticDtorClass::expected_this = nullptr;
74*8d67ca89SAndroid Build Coastguard Worker
atexit_func5()75*8d67ca89SAndroid Build Coastguard Worker static void atexit_func5() {
76*8d67ca89SAndroid Build Coastguard Worker fprintf(stderr, "5");
77*8d67ca89SAndroid Build Coastguard Worker }
78*8d67ca89SAndroid Build Coastguard Worker
atexit_func4()79*8d67ca89SAndroid Build Coastguard Worker static void atexit_func4() {
80*8d67ca89SAndroid Build Coastguard Worker fprintf(stderr, "4");
81*8d67ca89SAndroid Build Coastguard Worker }
82*8d67ca89SAndroid Build Coastguard Worker
atexit_func3()83*8d67ca89SAndroid Build Coastguard Worker static void atexit_func3() {
84*8d67ca89SAndroid Build Coastguard Worker fprintf(stderr, "3");
85*8d67ca89SAndroid Build Coastguard Worker atexit(atexit_func4);
86*8d67ca89SAndroid Build Coastguard Worker }
87*8d67ca89SAndroid Build Coastguard Worker
atexit_func2()88*8d67ca89SAndroid Build Coastguard Worker static void atexit_func2() {
89*8d67ca89SAndroid Build Coastguard Worker fprintf(stderr, "2");
90*8d67ca89SAndroid Build Coastguard Worker }
91*8d67ca89SAndroid Build Coastguard Worker
atexit_func1()92*8d67ca89SAndroid Build Coastguard Worker static void atexit_func1() {
93*8d67ca89SAndroid Build Coastguard Worker fprintf(stderr, "1");
94*8d67ca89SAndroid Build Coastguard Worker }
95*8d67ca89SAndroid Build Coastguard Worker
atexit_main()96*8d67ca89SAndroid Build Coastguard Worker static void atexit_main() {
97*8d67ca89SAndroid Build Coastguard Worker // This should result in "123456" output to stderr
98*8d67ca89SAndroid Build Coastguard Worker static TestMainStaticDtorClass static_obj;
99*8d67ca89SAndroid Build Coastguard Worker atexit(atexit_func5);
100*8d67ca89SAndroid Build Coastguard Worker atexit(atexit_func3);
101*8d67ca89SAndroid Build Coastguard Worker atexit(atexit_func2);
102*8d67ca89SAndroid Build Coastguard Worker atexit(atexit_func1);
103*8d67ca89SAndroid Build Coastguard Worker exit(0);
104*8d67ca89SAndroid Build Coastguard Worker }
105*8d67ca89SAndroid Build Coastguard Worker
TEST(atexit,exit)106*8d67ca89SAndroid Build Coastguard Worker TEST(atexit, exit) {
107*8d67ca89SAndroid Build Coastguard Worker ASSERT_EXIT(atexit_main(), testing::ExitedWithCode(0), "123456");
108*8d67ca89SAndroid Build Coastguard Worker }
109*8d67ca89SAndroid Build Coastguard Worker
110