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, 776);
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_SIZE(bionic_tls, 12200);
59 CHECK_OFFSET(bionic_tls, key_data, 0);
60 CHECK_OFFSET(bionic_tls, locale, 2080);
61 CHECK_OFFSET(bionic_tls, basename_buf, 2088);
62 CHECK_OFFSET(bionic_tls, dirname_buf, 6184);
63 CHECK_OFFSET(bionic_tls, mntent_buf, 10280);
64 CHECK_OFFSET(bionic_tls, mntent_strings, 10320);
65 CHECK_OFFSET(bionic_tls, ptsname_buf, 11344);
66 CHECK_OFFSET(bionic_tls, ttyname_buf, 11376);
67 CHECK_OFFSET(bionic_tls, strerror_buf, 11440);
68 CHECK_OFFSET(bionic_tls, strsignal_buf, 11695);
69 CHECK_OFFSET(bionic_tls, group, 11952);
70 CHECK_OFFSET(bionic_tls, passwd, 12040);
71 CHECK_OFFSET(bionic_tls, fdtrack_disabled, 12192);
72 CHECK_OFFSET(bionic_tls, bionic_systrace_disabled, 12193);
73 CHECK_OFFSET(bionic_tls, padding, 12194);
74 #else
75 CHECK_SIZE(pthread_internal_t, 668);
76 CHECK_OFFSET(pthread_internal_t, next, 0);
77 CHECK_OFFSET(pthread_internal_t, prev, 4);
78 CHECK_OFFSET(pthread_internal_t, tid, 8);
79 CHECK_OFFSET(pthread_internal_t, attr, 16);
80 CHECK_OFFSET(pthread_internal_t, join_state, 40);
81 CHECK_OFFSET(pthread_internal_t, cleanup_stack, 44);
82 CHECK_OFFSET(pthread_internal_t, start_routine, 48);
83 CHECK_OFFSET(pthread_internal_t, start_routine_arg, 52);
84 CHECK_OFFSET(pthread_internal_t, return_value, 56);
85 CHECK_OFFSET(pthread_internal_t, start_mask, 60);
86 CHECK_OFFSET(pthread_internal_t, alternate_signal_stack, 68);
87 CHECK_OFFSET(pthread_internal_t, shadow_call_stack_guard_region, 72);
88 CHECK_OFFSET(pthread_internal_t, stack_top, 76);
89 CHECK_OFFSET(pthread_internal_t, startup_handshake_lock, 84);
90 CHECK_OFFSET(pthread_internal_t, mmap_base, 92);
91 CHECK_OFFSET(pthread_internal_t, mmap_size, 96);
92 CHECK_OFFSET(pthread_internal_t, mmap_base_unguarded, 100);
93 CHECK_OFFSET(pthread_internal_t, mmap_size_unguarded, 104);
94 CHECK_OFFSET(pthread_internal_t, vma_name_buffer, 108);
95 CHECK_OFFSET(pthread_internal_t, thread_local_dtors, 140);
96 CHECK_OFFSET(pthread_internal_t, current_dlerror, 144);
97 CHECK_OFFSET(pthread_internal_t, dlerror_buffer, 148);
98 CHECK_OFFSET(pthread_internal_t, bionic_tls, 660);
99 CHECK_OFFSET(pthread_internal_t, errno_value, 664);
100 CHECK_SIZE(bionic_tls, 11080);
101 CHECK_OFFSET(bionic_tls, key_data, 0);
102 CHECK_OFFSET(bionic_tls, locale, 1040);
103 CHECK_OFFSET(bionic_tls, basename_buf, 1044);
104 CHECK_OFFSET(bionic_tls, dirname_buf, 5140);
105 CHECK_OFFSET(bionic_tls, mntent_buf, 9236);
106 CHECK_OFFSET(bionic_tls, mntent_strings, 9260);
107 CHECK_OFFSET(bionic_tls, ptsname_buf, 10284);
108 CHECK_OFFSET(bionic_tls, ttyname_buf, 10316);
109 CHECK_OFFSET(bionic_tls, strerror_buf, 10380);
110 CHECK_OFFSET(bionic_tls, strsignal_buf, 10635);
111 CHECK_OFFSET(bionic_tls, group, 10892);
112 CHECK_OFFSET(bionic_tls, passwd, 10952);
113 CHECK_OFFSET(bionic_tls, fdtrack_disabled, 11076);
114 CHECK_OFFSET(bionic_tls, bionic_systrace_disabled, 11077);
115 CHECK_OFFSET(bionic_tls, padding, 11078);
116 #endif // __LP64__
117 #undef CHECK_SIZE
118 #undef CHECK_OFFSET
119 }
120 #endif // defined(__BIONIC__)
121
TEST(struct_layout,sizes_offsets)122 TEST(struct_layout, sizes_offsets) {
123 #if defined(__BIONIC__)
124 bool failed = false;
125
126 auto check_size = [&](const char* name, size_t size, size_t expected_size) {
127 EXPECT_EQ(expected_size, size) << "sizeof(" << name << ")";
128 if (size != expected_size) {
129 failed = true;
130 }
131 };
132 auto check_offset = [&](const char* name, const char* field, size_t offset,
133 size_t expected_offset) {
134 EXPECT_EQ(expected_offset, offset) << "offsetof(" << name << ", " << field << ")";
135 if (offset != expected_offset) {
136 failed = true;
137 }
138 };
139 tests(check_size, check_offset);
140
141 if (failed) {
142 printf(
143 "Please update the tests function in bionic/tests/struct_layout_test.cpp with the "
144 "following contents:\n");
145
146 auto print_size = [&](const char* name, size_t size, size_t expected_size) {
147 (void)expected_size;
148 printf(" CHECK_SIZE(%s, %zu);\n", name, size);
149 };
150 auto print_offset = [&](const char* name, const char* field, size_t offset,
151 size_t expected_offset) {
152 (void)expected_offset;
153 printf(" CHECK_OFFSET(%s, %s, %zu);\n", name, field, offset);
154 };
155 tests(print_size, print_offset);
156 }
157 #else
158 GTEST_SKIP() << "bionic-only test";
159 #endif
160 }
161