blob: 0dd33cf03c07869a5a4caa94ef030ee4db70a129 [file] [log] [blame]
Andreas Gampe4352b452014-06-04 18:59:01 -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
17#include <memory>
18
19#include "base/macros.h"
20#include "common_runtime_test.h"
21#include "thread.h"
22
23// This test checks the offsets of values in the thread TLS and entrypoint structures. A failure
24// of this test means that offsets have changed from the last update of the test. This indicates
25// that an oat version bump may be in order, and some defines should be carefully checked (or their
26// corresponding tests run).
27
28namespace art {
29
30// OFFSETOF_MEMBER uses reinterpret_cast. This means it is not a constexpr. So we cannot use
31// compile-time assertions. Once we find another way, adjust the define accordingly.
32#define CHECKED(expr, name) \
33 EXPECT_TRUE(expr) << #name
34
35// Macro to check whether two fields have an expected difference in offsets. The error is named
36// name.
37#define EXPECT_OFFSET_DIFF(first_type, first_field, second_type, second_field, diff, name) \
38 CHECKED(OFFSETOF_MEMBER(second_type, second_field) \
39 - OFFSETOF_MEMBER(first_type, first_field) == diff, name)
40
41// Helper macro for when the fields are from the same type.
42#define EXPECT_OFFSET_DIFFNP(type, first_field, second_field, diff) \
43 EXPECT_OFFSET_DIFF(type, first_field, type, second_field, diff, \
44 type ## _ ## first_field ## _ ## second_field)
45
46// Helper macro for when the fields are from the same type and in the same member of said type.
47#define EXPECT_OFFSET_DIFFP(type, prefix, first_field, second_field, diff) \
48 EXPECT_OFFSET_DIFF(type, prefix . first_field, type, prefix . second_field, diff, \
49 type ## _ ## prefix ## _ ## first_field ## _ ## second_field)
50
51// Macro to check whether two fields have at least an expected difference in offsets. The error is
52// named name.
53#define EXPECT_OFFSET_DIFF_GT(first_type, first_field, second_type, second_field, diff, name) \
54 CHECKED(OFFSETOF_MEMBER(second_type, second_field) \
55 - OFFSETOF_MEMBER(first_type, first_field) >= diff, name)
56
57// Helper macro for when the fields are from the same type.
58#define EXPECT_OFFSET_DIFF_GT3(type, first_field, second_field, diff, name) \
59 EXPECT_OFFSET_DIFF_GT(type, first_field, type, second_field, diff, name)
60
61class EntrypointsOrderTest : public CommonRuntimeTest {
62 protected:
63 void CheckThreadOffsets() {
64 CHECKED(OFFSETOF_MEMBER(Thread, tls32_.state_and_flags) == 0, thread_flags_at_zero);
65 EXPECT_OFFSET_DIFFP(Thread, tls32_, state_and_flags, suspend_count, 4);
66 EXPECT_OFFSET_DIFFP(Thread, tls32_, suspend_count, debug_suspend_count, 4);
67 EXPECT_OFFSET_DIFFP(Thread, tls32_, debug_suspend_count, thin_lock_thread_id, 4);
68 EXPECT_OFFSET_DIFFP(Thread, tls32_, thin_lock_thread_id, tid, 4);
69 EXPECT_OFFSET_DIFFP(Thread, tls32_, tid, daemon, 4);
70 EXPECT_OFFSET_DIFFP(Thread, tls32_, daemon, throwing_OutOfMemoryError, 4);
71 EXPECT_OFFSET_DIFFP(Thread, tls32_, throwing_OutOfMemoryError, no_thread_suspension, 4);
72 EXPECT_OFFSET_DIFFP(Thread, tls32_, no_thread_suspension, thread_exit_check_count, 4);
73
74 // TODO: Better connection. Take alignment into account.
75 EXPECT_OFFSET_DIFF_GT3(Thread, tls32_.thread_exit_check_count, tls64_.trace_clock_base, 4,
76 thread_tls32_to_tls64);
77
78 EXPECT_OFFSET_DIFFP(Thread, tls64_, trace_clock_base, deoptimization_return_value, 8);
79 EXPECT_OFFSET_DIFFP(Thread, tls64_, deoptimization_return_value, stats, 8);
80
81 // TODO: Better connection. Take alignment into account.
82 EXPECT_OFFSET_DIFF_GT3(Thread, tls64_.stats, tlsPtr_.card_table, 8, thread_tls64_to_tlsptr);
83
84 EXPECT_OFFSET_DIFFP(Thread, tlsPtr_, card_table, exception, kPointerSize);
85 EXPECT_OFFSET_DIFFP(Thread, tlsPtr_, exception, stack_end, kPointerSize);
86 EXPECT_OFFSET_DIFFP(Thread, tlsPtr_, stack_end, managed_stack, kPointerSize);
87 EXPECT_OFFSET_DIFFP(Thread, tlsPtr_, managed_stack, suspend_trigger, sizeof(ManagedStack));
88 EXPECT_OFFSET_DIFFP(Thread, tlsPtr_, suspend_trigger, jni_env, kPointerSize);
89 EXPECT_OFFSET_DIFFP(Thread, tlsPtr_, jni_env, self, kPointerSize);
90 EXPECT_OFFSET_DIFFP(Thread, tlsPtr_, self, opeer, kPointerSize);
91 EXPECT_OFFSET_DIFFP(Thread, tlsPtr_, opeer, jpeer, kPointerSize);
92 EXPECT_OFFSET_DIFFP(Thread, tlsPtr_, jpeer, stack_begin, kPointerSize);
93 EXPECT_OFFSET_DIFFP(Thread, tlsPtr_, stack_begin, stack_size, kPointerSize);
94 EXPECT_OFFSET_DIFFP(Thread, tlsPtr_, stack_size, throw_location, kPointerSize);
95 EXPECT_OFFSET_DIFFP(Thread, tlsPtr_, throw_location, stack_trace_sample, sizeof(ThrowLocation));
96 EXPECT_OFFSET_DIFFP(Thread, tlsPtr_, stack_trace_sample, wait_next, kPointerSize);
97 EXPECT_OFFSET_DIFFP(Thread, tlsPtr_, wait_next, monitor_enter_object, kPointerSize);
98 EXPECT_OFFSET_DIFFP(Thread, tlsPtr_, monitor_enter_object, top_handle_scope, kPointerSize);
99 EXPECT_OFFSET_DIFFP(Thread, tlsPtr_, top_handle_scope, class_loader_override, kPointerSize);
100 EXPECT_OFFSET_DIFFP(Thread, tlsPtr_, class_loader_override, long_jump_context, kPointerSize);
101 EXPECT_OFFSET_DIFFP(Thread, tlsPtr_, long_jump_context, instrumentation_stack, kPointerSize);
102 EXPECT_OFFSET_DIFFP(Thread, tlsPtr_, instrumentation_stack, debug_invoke_req, kPointerSize);
103 EXPECT_OFFSET_DIFFP(Thread, tlsPtr_, debug_invoke_req, single_step_control, kPointerSize);
104 EXPECT_OFFSET_DIFFP(Thread, tlsPtr_, single_step_control, deoptimization_shadow_frame,
105 kPointerSize);
106 EXPECT_OFFSET_DIFFP(Thread, tlsPtr_, deoptimization_shadow_frame,
107 shadow_frame_under_construction, kPointerSize);
108 EXPECT_OFFSET_DIFFP(Thread, tlsPtr_, shadow_frame_under_construction, name, kPointerSize);
109 EXPECT_OFFSET_DIFFP(Thread, tlsPtr_, name, pthread_self, kPointerSize);
110 EXPECT_OFFSET_DIFFP(Thread, tlsPtr_, pthread_self, last_no_thread_suspension_cause,
111 kPointerSize);
112 EXPECT_OFFSET_DIFFP(Thread, tlsPtr_, last_no_thread_suspension_cause, checkpoint_functions,
113 kPointerSize);
114 EXPECT_OFFSET_DIFFP(Thread, tlsPtr_, checkpoint_functions, interpreter_entrypoints,
115 kPointerSize * 3);
116
117 // Skip across the entrypoints structures.
118
119 EXPECT_OFFSET_DIFFP(Thread, tlsPtr_, thread_local_start, thread_local_pos, kPointerSize);
120 EXPECT_OFFSET_DIFFP(Thread, tlsPtr_, thread_local_pos, thread_local_end, kPointerSize);
121 EXPECT_OFFSET_DIFFP(Thread, tlsPtr_, thread_local_end, thread_local_objects, kPointerSize);
122 EXPECT_OFFSET_DIFFP(Thread, tlsPtr_, thread_local_objects, rosalloc_runs, kPointerSize);
123 EXPECT_OFFSET_DIFFP(Thread, tlsPtr_, rosalloc_runs, thread_local_alloc_stack_top,
124 kPointerSize * gc::allocator::RosAlloc::kNumThreadLocalSizeBrackets);
125 EXPECT_OFFSET_DIFFP(Thread, tlsPtr_, thread_local_alloc_stack_top, thread_local_alloc_stack_end,
126 kPointerSize);
127 EXPECT_OFFSET_DIFFP(Thread, tlsPtr_, thread_local_alloc_stack_end, held_mutexes, kPointerSize);
128 EXPECT_OFFSET_DIFF(Thread, tlsPtr_.held_mutexes, Thread, wait_mutex_,
129 kPointerSize * kLockLevelCount, thread_tlsptr_end);
130 }
131
132 void CheckInterpreterEntryPoints() {
133 CHECKED(OFFSETOF_MEMBER(InterpreterEntryPoints, pInterpreterToInterpreterBridge) == 0,
134 InterpreterEntryPoints_start_with_i2i);
135 EXPECT_OFFSET_DIFFNP(InterpreterEntryPoints, pInterpreterToInterpreterBridge,
136 pInterpreterToCompiledCodeBridge, kPointerSize);
137 CHECKED(OFFSETOF_MEMBER(InterpreterEntryPoints, pInterpreterToCompiledCodeBridge)
138 + kPointerSize == sizeof(InterpreterEntryPoints), InterpreterEntryPoints_all);
139 }
140
141 void CheckJniEntryPoints() {
142 CHECKED(OFFSETOF_MEMBER(JniEntryPoints, pDlsymLookup) == 0,
143 JniEntryPoints_start_with_dlsymlookup);
144 CHECKED(OFFSETOF_MEMBER(JniEntryPoints, pDlsymLookup)
145 + kPointerSize == sizeof(JniEntryPoints), JniEntryPoints_all);
146 }
147
148 void CheckPortableEntryPoints() {
149 CHECKED(OFFSETOF_MEMBER(PortableEntryPoints, pPortableImtConflictTrampoline) == 0,
150 PortableEntryPoints_start_with_imt);
151 EXPECT_OFFSET_DIFFNP(PortableEntryPoints, pPortableImtConflictTrampoline,
152 pPortableResolutionTrampoline, kPointerSize);
153 EXPECT_OFFSET_DIFFNP(PortableEntryPoints, pPortableResolutionTrampoline,
154 pPortableToInterpreterBridge, kPointerSize);
155 CHECKED(OFFSETOF_MEMBER(PortableEntryPoints, pPortableToInterpreterBridge)
156 + kPointerSize == sizeof(PortableEntryPoints), PortableEntryPoints_all);
157 }
158
159 void CheckQuickEntryPoints() {
160 CHECKED(OFFSETOF_MEMBER(QuickEntryPoints, pAllocArray) == 0,
161 QuickEntryPoints_start_with_allocarray);
162 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pAllocArray, pAllocArrayResolved, kPointerSize);
163 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pAllocArrayResolved, pAllocArrayWithAccessCheck,
164 kPointerSize);
165 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pAllocArrayWithAccessCheck, pAllocObject, kPointerSize);
166 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pAllocObject, pAllocObjectResolved, kPointerSize);
167 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pAllocObjectResolved, pAllocObjectInitialized,
168 kPointerSize);
169 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pAllocObjectInitialized, pAllocObjectWithAccessCheck,
170 kPointerSize);
171 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pAllocObjectWithAccessCheck, pCheckAndAllocArray,
172 kPointerSize);
173 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pCheckAndAllocArray, pCheckAndAllocArrayWithAccessCheck,
174 kPointerSize);
175 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pCheckAndAllocArrayWithAccessCheck,
176 pInstanceofNonTrivial, kPointerSize);
177 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pInstanceofNonTrivial, pCheckCast, kPointerSize);
178 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pCheckCast, pInitializeStaticStorage, kPointerSize);
179 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pInitializeStaticStorage, pInitializeTypeAndVerifyAccess,
180 kPointerSize);
181 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pInitializeTypeAndVerifyAccess, pInitializeType,
182 kPointerSize);
183 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pInitializeType, pResolveString, kPointerSize);
184 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pResolveString, pSet32Instance, kPointerSize);
185 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pSet32Instance, pSet32Static, kPointerSize);
186 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pSet32Static, pSet64Instance, kPointerSize);
187 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pSet64Instance, pSet64Static, kPointerSize);
188 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pSet64Static, pSetObjInstance, kPointerSize);
189 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pSetObjInstance, pSetObjStatic, kPointerSize);
190 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pSetObjStatic, pGet32Instance, kPointerSize);
191 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pGet32Instance, pGet32Static, kPointerSize);
192 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pGet32Static, pGet64Instance, kPointerSize);
193 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pGet64Instance, pGet64Static, kPointerSize);
194 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pGet64Static, pGetObjInstance, kPointerSize);
195 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pGetObjInstance, pGetObjStatic, kPointerSize);
196 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pGetObjStatic, pAputObjectWithNullAndBoundCheck,
197 kPointerSize);
198 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pAputObjectWithNullAndBoundCheck,
199 pAputObjectWithBoundCheck, kPointerSize);
200 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pAputObjectWithBoundCheck, pAputObject, kPointerSize);
201 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pAputObject, pHandleFillArrayData, kPointerSize);
202 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pHandleFillArrayData, pJniMethodStart, kPointerSize);
203 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pJniMethodStart, pJniMethodStartSynchronized,
204 kPointerSize);
205 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pJniMethodStartSynchronized, pJniMethodEnd,
206 kPointerSize);
207 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pJniMethodEnd, pJniMethodEndSynchronized, kPointerSize);
208 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pJniMethodEndSynchronized, pJniMethodEndWithReference,
209 kPointerSize);
210 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pJniMethodEndWithReference,
211 pJniMethodEndWithReferenceSynchronized, kPointerSize);
212 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pJniMethodEndWithReferenceSynchronized,
213 pQuickGenericJniTrampoline, kPointerSize);
214 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pQuickGenericJniTrampoline, pLockObject, kPointerSize);
215 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pLockObject, pUnlockObject, kPointerSize);
216 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pUnlockObject, pCmpgDouble, kPointerSize);
217 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pCmpgDouble, pCmpgFloat, kPointerSize);
218 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pCmpgFloat, pCmplDouble, kPointerSize);
219 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pCmplDouble, pCmplFloat, kPointerSize);
220 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pCmplFloat, pFmod, kPointerSize);
221 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pFmod, pL2d, kPointerSize);
222 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pL2d, pFmodf, kPointerSize);
223 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pFmodf, pL2f, kPointerSize);
224 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pL2f, pD2iz, kPointerSize);
225 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pD2iz, pF2iz, kPointerSize);
226 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pF2iz, pIdivmod, kPointerSize);
227 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pIdivmod, pD2l, kPointerSize);
228 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pD2l, pF2l, kPointerSize);
229 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pF2l, pLdiv, kPointerSize);
230 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pLdiv, pLmod, kPointerSize);
231 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pLmod, pLmul, kPointerSize);
232 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pLmul, pShlLong, kPointerSize);
233 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pShlLong, pShrLong, kPointerSize);
234 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pShrLong, pUshrLong, kPointerSize);
235 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pUshrLong, pIndexOf, kPointerSize);
Andreas Gampe4d0589c2014-06-10 16:10:56 -0700236 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pIndexOf, pStringCompareTo, kPointerSize);
Andreas Gampe4352b452014-06-04 18:59:01 -0700237 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pStringCompareTo, pMemcpy, kPointerSize);
238 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pMemcpy, pQuickImtConflictTrampoline, kPointerSize);
239 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pQuickImtConflictTrampoline, pQuickResolutionTrampoline,
240 kPointerSize);
241 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pQuickResolutionTrampoline, pQuickToInterpreterBridge,
242 kPointerSize);
243 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pQuickToInterpreterBridge,
244 pInvokeDirectTrampolineWithAccessCheck, kPointerSize);
245 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pInvokeDirectTrampolineWithAccessCheck,
246 pInvokeInterfaceTrampolineWithAccessCheck, kPointerSize);
247 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pInvokeInterfaceTrampolineWithAccessCheck,
248 pInvokeStaticTrampolineWithAccessCheck, kPointerSize);
249 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pInvokeStaticTrampolineWithAccessCheck,
250 pInvokeSuperTrampolineWithAccessCheck, kPointerSize);
251 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pInvokeSuperTrampolineWithAccessCheck,
252 pInvokeVirtualTrampolineWithAccessCheck, kPointerSize);
253 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pInvokeVirtualTrampolineWithAccessCheck,
254 pCheckSuspend, kPointerSize);
255 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pCheckSuspend, pTestSuspend, kPointerSize);
256 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pTestSuspend, pDeliverException, kPointerSize);
257
258 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pDeliverException, pThrowArrayBounds, kPointerSize);
259 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pThrowArrayBounds, pThrowDivZero, kPointerSize);
260 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pThrowDivZero, pThrowNoSuchMethod, kPointerSize);
261 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pThrowNoSuchMethod, pThrowNullPointer, kPointerSize);
262 EXPECT_OFFSET_DIFFNP(QuickEntryPoints, pThrowNullPointer, pThrowStackOverflow, kPointerSize);
263
264 CHECKED(OFFSETOF_MEMBER(QuickEntryPoints, pThrowStackOverflow)
265 + kPointerSize == sizeof(QuickEntryPoints), QuickEntryPoints_all);
266 }
267};
268
269TEST_F(EntrypointsOrderTest, ThreadOffsets) {
270 CheckThreadOffsets();
271}
272
273TEST_F(EntrypointsOrderTest, InterpreterEntryPoints) {
274 CheckInterpreterEntryPoints();
275}
276
277TEST_F(EntrypointsOrderTest, JniEntryPoints) {
278 CheckJniEntryPoints();
279}
280
281TEST_F(EntrypointsOrderTest, PortableEntryPoints) {
282 CheckPortableEntryPoints();
283}
284
285TEST_F(EntrypointsOrderTest, QuickEntryPoints) {
286 CheckQuickEntryPoints();
287}
288
289} // namespace art