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