1 /*
2 * Copyright (C) 2015 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 <gtest/gtest.h>
17
18 #include <dlfcn.h>
19
20 #include "utils.h"
21
22 static int g_atfork_prepare_calls = 0;
AtForkPrepare1()23 static void AtForkPrepare1() { g_atfork_prepare_calls = (g_atfork_prepare_calls * 10) + 1; }
AtForkPrepare2()24 static void AtForkPrepare2() { g_atfork_prepare_calls = (g_atfork_prepare_calls * 10) + 2; }
AtForkPrepare3()25 static void AtForkPrepare3() { g_atfork_prepare_calls = (g_atfork_prepare_calls * 10) + 3; }
AtForkPrepare4()26 static void AtForkPrepare4() { g_atfork_prepare_calls = (g_atfork_prepare_calls * 10) + 4; }
27
28 static int g_atfork_parent_calls = 0;
AtForkParent1()29 static void AtForkParent1() { g_atfork_parent_calls = (g_atfork_parent_calls * 10) + 1; }
AtForkParent2()30 static void AtForkParent2() { g_atfork_parent_calls = (g_atfork_parent_calls * 10) + 2; }
AtForkParent3()31 static void AtForkParent3() { g_atfork_parent_calls = (g_atfork_parent_calls * 10) + 3; }
AtForkParent4()32 static void AtForkParent4() { g_atfork_parent_calls = (g_atfork_parent_calls * 10) + 4; }
33
34 static int g_atfork_child_calls = 0;
AtForkChild1()35 static void AtForkChild1() { g_atfork_child_calls = (g_atfork_child_calls * 10) + 1; }
AtForkChild2()36 static void AtForkChild2() { g_atfork_child_calls = (g_atfork_child_calls * 10) + 2; }
AtForkChild3()37 static void AtForkChild3() { g_atfork_child_calls = (g_atfork_child_calls * 10) + 3; }
AtForkChild4()38 static void AtForkChild4() { g_atfork_child_calls = (g_atfork_child_calls * 10) + 4; }
39
40 static void* g_atfork_test_handle = nullptr;
AtForkPrepare()41 static void AtForkPrepare() {}
AtForkParent()42 static void AtForkParent() {}
AtForkChild()43 static void AtForkChild() { dlclose(g_atfork_test_handle); g_atfork_test_handle = dlopen("libtest_pthread_atfork.so", RTLD_NOW | RTLD_LOCAL); }
44
TEST(pthread,pthread_atfork_with_dlclose)45 TEST(pthread, pthread_atfork_with_dlclose) {
46 ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1));
47
48 void* handle = dlopen("libtest_pthread_atfork.so", RTLD_NOW | RTLD_LOCAL);
49 ASSERT_TRUE(handle != nullptr) << dlerror();
50 typedef int (*fn_t)(void (*)(void), void (*)(void), void (*)(void));
51 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "proxy_pthread_atfork"));
52 ASSERT_TRUE(fn != nullptr) << dlerror();
53 // the library registers 2 additional atfork handlers in a constructor
54 ASSERT_EQ(0, fn(AtForkPrepare2, AtForkParent2, AtForkChild2));
55 ASSERT_EQ(0, fn(AtForkPrepare3, AtForkParent3, AtForkChild3));
56
57 ASSERT_EQ(0, pthread_atfork(AtForkPrepare4, AtForkParent4, AtForkChild4));
58
59 pid_t pid = fork();
60
61 ASSERT_NE(-1, pid) << strerror(errno);
62
63 if (pid == 0) {
64 ASSERT_EQ(1234, g_atfork_child_calls);
65 _exit(0);
66 }
67
68 ASSERT_EQ(1234, g_atfork_parent_calls);
69 ASSERT_EQ(4321, g_atfork_prepare_calls);
70
71 EXPECT_EQ(0, dlclose(handle));
72 g_atfork_prepare_calls = g_atfork_parent_calls = g_atfork_child_calls = 0;
73
74 AssertChildExited(pid, 0);
75
76 pid = fork();
77
78 ASSERT_NE(-1, pid) << strerror(errno);
79
80 if (pid == 0) {
81 ASSERT_EQ(14, g_atfork_child_calls);
82 _exit(0);
83 }
84
85 ASSERT_EQ(14, g_atfork_parent_calls);
86 ASSERT_EQ(41, g_atfork_prepare_calls);
87
88 AssertChildExited(pid, 0);
89 }
90
TEST(pthread,pthread_atfork_child_with_dlclose)91 TEST(pthread, pthread_atfork_child_with_dlclose) {
92
93 g_atfork_test_handle = dlopen("libtest_pthread_atfork.so", RTLD_NOW | RTLD_LOCAL);
94 ASSERT_TRUE(g_atfork_test_handle != nullptr) << dlerror();
95 typedef int (*fn_t)(void (*)(void), void (*)(void), void (*)(void));
96 fn_t fn = reinterpret_cast<fn_t>(dlsym(g_atfork_test_handle, "proxy_pthread_atfork"));
97 ASSERT_TRUE(fn != nullptr) << dlerror();
98 // the library registers 2 additional atfork handlers in a constructor
99
100 ASSERT_EQ(0, pthread_atfork(AtForkPrepare, AtForkParent, AtForkChild));
101
102 pid_t pid = fork();
103
104 ASSERT_NE(-1, pid) << strerror(errno);
105
106 if (pid == 0) {
107 _exit(0);
108 }
109
110 AssertChildExited(pid, 0);
111
112 EXPECT_EQ(0, dlclose(g_atfork_test_handle));
113 g_atfork_test_handle = nullptr;
114 }
115