1 /*
2 * Copyright (C) 2020 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 #include <gtest/gtest.h>
18
19 #include <stdio.h>
20
21 #if defined(__BIONIC__)
22 #include "bionic/pthread_internal.h"
23
24 // Ensure that the layout of these data structures is architecture independent and only depends on
25 // the bitness of the architecture.
26 template <typename CheckSize, typename CheckOffset>
tests(CheckSize check_size,CheckOffset check_offset)27 void tests(CheckSize check_size, CheckOffset check_offset) {
28 #define CHECK_SIZE(name, size) \
29 check_size(#name, sizeof(name), size);
30 #define CHECK_OFFSET(name, field, offset) \
31 check_offset(#name, #field, offsetof(name, field), offset);
32 #ifdef __LP64__
33 CHECK_SIZE(pthread_internal_t, 784);
34 CHECK_OFFSET(pthread_internal_t, next, 0);
35 CHECK_OFFSET(pthread_internal_t, prev, 8);
36 CHECK_OFFSET(pthread_internal_t, tid, 16);
37 CHECK_OFFSET(pthread_internal_t, attr, 24);
38 CHECK_OFFSET(pthread_internal_t, join_state, 80);
39 CHECK_OFFSET(pthread_internal_t, cleanup_stack, 88);
40 CHECK_OFFSET(pthread_internal_t, start_routine, 96);
41 CHECK_OFFSET(pthread_internal_t, start_routine_arg, 104);
42 CHECK_OFFSET(pthread_internal_t, return_value, 112);
43 CHECK_OFFSET(pthread_internal_t, start_mask, 120);
44 CHECK_OFFSET(pthread_internal_t, alternate_signal_stack, 128);
45 CHECK_OFFSET(pthread_internal_t, shadow_call_stack_guard_region, 136);
46 CHECK_OFFSET(pthread_internal_t, stack_top, 144);
47 CHECK_OFFSET(pthread_internal_t, startup_handshake_lock, 156);
48 CHECK_OFFSET(pthread_internal_t, mmap_base, 168);
49 CHECK_OFFSET(pthread_internal_t, mmap_size, 176);
50 CHECK_OFFSET(pthread_internal_t, mmap_base_unguarded, 184);
51 CHECK_OFFSET(pthread_internal_t, mmap_size_unguarded, 192);
52 CHECK_OFFSET(pthread_internal_t, vma_name_buffer, 200);
53 CHECK_OFFSET(pthread_internal_t, thread_local_dtors, 232);
54 CHECK_OFFSET(pthread_internal_t, current_dlerror, 240);
55 CHECK_OFFSET(pthread_internal_t, dlerror_buffer, 248);
56 CHECK_OFFSET(pthread_internal_t, bionic_tls, 760);
57 CHECK_OFFSET(pthread_internal_t, errno_value, 768);
58 CHECK_OFFSET(pthread_internal_t, vfork_child_stack_bottom, 776);
59 CHECK_SIZE(bionic_tls, 12200);
60 CHECK_OFFSET(bionic_tls, key_data, 0);
61 CHECK_OFFSET(bionic_tls, locale, 2080);
62 CHECK_OFFSET(bionic_tls, basename_buf, 2088);
63 CHECK_OFFSET(bionic_tls, dirname_buf, 6184);
64 CHECK_OFFSET(bionic_tls, mntent_buf, 10280);
65 CHECK_OFFSET(bionic_tls, mntent_strings, 10320);
66 CHECK_OFFSET(bionic_tls, ptsname_buf, 11344);
67 CHECK_OFFSET(bionic_tls, ttyname_buf, 11376);
68 CHECK_OFFSET(bionic_tls, strerror_buf, 11440);
69 CHECK_OFFSET(bionic_tls, strsignal_buf, 11695);
70 CHECK_OFFSET(bionic_tls, group, 11952);
71 CHECK_OFFSET(bionic_tls, passwd, 12040);
72 CHECK_OFFSET(bionic_tls, fdtrack_disabled, 12192);
73 CHECK_OFFSET(bionic_tls, bionic_systrace_disabled, 12193);
74 CHECK_OFFSET(bionic_tls, padding, 12194);
75 #else
76 CHECK_SIZE(pthread_internal_t, 672);
77 CHECK_OFFSET(pthread_internal_t, next, 0);
78 CHECK_OFFSET(pthread_internal_t, prev, 4);
79 CHECK_OFFSET(pthread_internal_t, tid, 8);
80 CHECK_OFFSET(pthread_internal_t, attr, 16);
81 CHECK_OFFSET(pthread_internal_t, join_state, 40);
82 CHECK_OFFSET(pthread_internal_t, cleanup_stack, 44);
83 CHECK_OFFSET(pthread_internal_t, start_routine, 48);
84 CHECK_OFFSET(pthread_internal_t, start_routine_arg, 52);
85 CHECK_OFFSET(pthread_internal_t, return_value, 56);
86 CHECK_OFFSET(pthread_internal_t, start_mask, 60);
87 CHECK_OFFSET(pthread_internal_t, alternate_signal_stack, 68);
88 CHECK_OFFSET(pthread_internal_t, shadow_call_stack_guard_region, 72);
89 CHECK_OFFSET(pthread_internal_t, stack_top, 76);
90 CHECK_OFFSET(pthread_internal_t, startup_handshake_lock, 84);
91 CHECK_OFFSET(pthread_internal_t, mmap_base, 92);
92 CHECK_OFFSET(pthread_internal_t, mmap_size, 96);
93 CHECK_OFFSET(pthread_internal_t, mmap_base_unguarded, 100);
94 CHECK_OFFSET(pthread_internal_t, mmap_size_unguarded, 104);
95 CHECK_OFFSET(pthread_internal_t, vma_name_buffer, 108);
96 CHECK_OFFSET(pthread_internal_t, thread_local_dtors, 140);
97 CHECK_OFFSET(pthread_internal_t, current_dlerror, 144);
98 CHECK_OFFSET(pthread_internal_t, dlerror_buffer, 148);
99 CHECK_OFFSET(pthread_internal_t, bionic_tls, 660);
100 CHECK_OFFSET(pthread_internal_t, errno_value, 664);
101 CHECK_OFFSET(pthread_internal_t, vfork_child_stack_bottom, 668);
102 CHECK_SIZE(bionic_tls, 11080);
103 CHECK_OFFSET(bionic_tls, key_data, 0);
104 CHECK_OFFSET(bionic_tls, locale, 1040);
105 CHECK_OFFSET(bionic_tls, basename_buf, 1044);
106 CHECK_OFFSET(bionic_tls, dirname_buf, 5140);
107 CHECK_OFFSET(bionic_tls, mntent_buf, 9236);
108 CHECK_OFFSET(bionic_tls, mntent_strings, 9260);
109 CHECK_OFFSET(bionic_tls, ptsname_buf, 10284);
110 CHECK_OFFSET(bionic_tls, ttyname_buf, 10316);
111 CHECK_OFFSET(bionic_tls, strerror_buf, 10380);
112 CHECK_OFFSET(bionic_tls, strsignal_buf, 10635);
113 CHECK_OFFSET(bionic_tls, group, 10892);
114 CHECK_OFFSET(bionic_tls, passwd, 10952);
115 CHECK_OFFSET(bionic_tls, fdtrack_disabled, 11076);
116 CHECK_OFFSET(bionic_tls, bionic_systrace_disabled, 11077);
117 CHECK_OFFSET(bionic_tls, padding, 11078);
118 #endif // __LP64__
119 #undef CHECK_SIZE
120 #undef CHECK_OFFSET
121 }
122 #endif // defined(__BIONIC__)
123
TEST(struct_layout,sizes_offsets)124 TEST(struct_layout, sizes_offsets) {
125 #if defined(__BIONIC__)
126 bool failed = false;
127
128 auto check_size = [&](const char* name, size_t size, size_t expected_size) {
129 EXPECT_EQ(expected_size, size) << "sizeof(" << name << ")";
130 if (size != expected_size) {
131 failed = true;
132 }
133 };
134 auto check_offset = [&](const char* name, const char* field, size_t offset,
135 size_t expected_offset) {
136 EXPECT_EQ(expected_offset, offset) << "offsetof(" << name << ", " << field << ")";
137 if (offset != expected_offset) {
138 failed = true;
139 }
140 };
141 tests(check_size, check_offset);
142
143 if (failed) {
144 printf(
145 "Please update the tests function in bionic/tests/struct_layout_test.cpp with the "
146 "following contents:\n");
147
148 auto print_size = [&](const char* name, size_t size, size_t expected_size) {
149 (void)expected_size;
150 printf(" CHECK_SIZE(%s, %zu);\n", name, size);
151 };
152 auto print_offset = [&](const char* name, const char* field, size_t offset,
153 size_t expected_offset) {
154 (void)expected_offset;
155 printf(" CHECK_OFFSET(%s, %s, %zu);\n", name, field, offset);
156 };
157 tests(print_size, print_offset);
158 }
159 #else
160 GTEST_SKIP() << "bionic-only test";
161 #endif
162 }
163