1 /* 2 * Copyright (C) 2011 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/arena_allocator.h" 20 #include "base/callee_save_type.h" 21 #include "base/enums.h" 22 #include "base/leb128.h" 23 #include "class_linker.h" 24 #include "common_runtime_test.h" 25 #include "dex/code_item_accessors-inl.h" 26 #include "dex/dex_file-inl.h" 27 #include "dex/dex_file.h" 28 #include "dex/dex_file_exception_helpers.h" 29 #include "gtest/gtest.h" 30 #include "handle_scope-inl.h" 31 #include "mirror/class-inl.h" 32 #include "mirror/object-inl.h" 33 #include "mirror/object_array-inl.h" 34 #include "mirror/stack_trace_element.h" 35 #include "oat_quick_method_header.h" 36 #include "optimizing/stack_map_stream.h" 37 #include "runtime-inl.h" 38 #include "scoped_thread_state_change-inl.h" 39 #include "thread.h" 40 41 namespace art { 42 43 class ExceptionTest : public CommonRuntimeTest { 44 protected: 45 // Since various dexers may differ in bytecode layout, we play 46 // it safe and simply set the dex pc to the start of the method, 47 // which always points to the first source statement. 48 static constexpr const uint32_t kDexPc = 0; 49 50 virtual void SetUp() { 51 CommonRuntimeTest::SetUp(); 52 53 ScopedObjectAccess soa(Thread::Current()); 54 StackHandleScope<2> hs(soa.Self()); 55 Handle<mirror::ClassLoader> class_loader( 56 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("ExceptionHandle")))); 57 my_klass_ = class_linker_->FindClass(soa.Self(), "LExceptionHandle;", class_loader); 58 ASSERT_TRUE(my_klass_ != nullptr); 59 Handle<mirror::Class> klass(hs.NewHandle(my_klass_)); 60 class_linker_->EnsureInitialized(soa.Self(), klass, true, true); 61 my_klass_ = klass.Get(); 62 63 dex_ = my_klass_->GetDexCache()->GetDexFile(); 64 65 uint32_t code_size = 12; 66 for (size_t i = 0 ; i < code_size; i++) { 67 fake_code_.push_back(0x70 | i); 68 } 69 70 ArenaPool pool; 71 ArenaStack arena_stack(&pool); 72 ScopedArenaAllocator allocator(&arena_stack); 73 StackMapStream stack_maps(&allocator, kRuntimeISA); 74 stack_maps.BeginStackMapEntry(kDexPc, 75 /* native_pc_offset */ 3u, 76 /* register_mask */ 0u, 77 /* sp_mask */ nullptr, 78 /* num_dex_registers */ 0u, 79 /* inlining_depth */ 0u); 80 stack_maps.EndStackMapEntry(); 81 size_t stack_maps_size = stack_maps.PrepareForFillIn(); 82 size_t stack_maps_offset = stack_maps_size + sizeof(OatQuickMethodHeader); 83 84 fake_header_code_and_maps_.resize(stack_maps_offset + fake_code_.size()); 85 MemoryRegion stack_maps_region(&fake_header_code_and_maps_[0], stack_maps_size); 86 stack_maps.FillInCodeInfo(stack_maps_region); 87 OatQuickMethodHeader method_header(stack_maps_offset, 0u, 4 * sizeof(void*), 0u, 0u, code_size); 88 memcpy(&fake_header_code_and_maps_[stack_maps_size], &method_header, sizeof(method_header)); 89 std::copy(fake_code_.begin(), 90 fake_code_.end(), 91 fake_header_code_and_maps_.begin() + stack_maps_offset); 92 93 // Align the code. 94 const size_t alignment = GetInstructionSetAlignment(kRuntimeISA); 95 fake_header_code_and_maps_.reserve(fake_header_code_and_maps_.size() + alignment); 96 const void* unaligned_code_ptr = 97 fake_header_code_and_maps_.data() + (fake_header_code_and_maps_.size() - code_size); 98 size_t offset = dchecked_integral_cast<size_t>(reinterpret_cast<uintptr_t>(unaligned_code_ptr)); 99 size_t padding = RoundUp(offset, alignment) - offset; 100 // Make sure no resizing takes place. 101 CHECK_GE(fake_header_code_and_maps_.capacity(), fake_header_code_and_maps_.size() + padding); 102 fake_header_code_and_maps_.insert(fake_header_code_and_maps_.begin(), padding, 0); 103 const void* code_ptr = reinterpret_cast<const uint8_t*>(unaligned_code_ptr) + padding; 104 CHECK_EQ(code_ptr, 105 static_cast<const void*>(fake_header_code_and_maps_.data() + 106 (fake_header_code_and_maps_.size() - code_size))); 107 108 if (kRuntimeISA == InstructionSet::kArm) { 109 // Check that the Thumb2 adjustment will be a NOP, see EntryPointToCodePointer(). 110 CHECK_ALIGNED(stack_maps_offset, 2); 111 } 112 113 method_f_ = my_klass_->FindClassMethod("f", "()I", kRuntimePointerSize); 114 ASSERT_TRUE(method_f_ != nullptr); 115 ASSERT_FALSE(method_f_->IsDirect()); 116 method_f_->SetEntryPointFromQuickCompiledCode(code_ptr); 117 118 method_g_ = my_klass_->FindClassMethod("g", "(I)V", kRuntimePointerSize); 119 ASSERT_TRUE(method_g_ != nullptr); 120 ASSERT_FALSE(method_g_->IsDirect()); 121 method_g_->SetEntryPointFromQuickCompiledCode(code_ptr); 122 } 123 124 const DexFile* dex_; 125 126 std::vector<uint8_t> fake_code_; 127 std::vector<uint8_t> fake_header_code_and_maps_; 128 129 ArtMethod* method_f_; 130 ArtMethod* method_g_; 131 132 private: 133 mirror::Class* my_klass_; 134 }; 135 136 TEST_F(ExceptionTest, FindCatchHandler) { 137 ScopedObjectAccess soa(Thread::Current()); 138 CodeItemDataAccessor accessor(*dex_, dex_->GetCodeItem(method_f_->GetCodeItemOffset())); 139 140 ASSERT_TRUE(accessor.HasCodeItem()); 141 142 ASSERT_EQ(2u, accessor.TriesSize()); 143 ASSERT_NE(0u, accessor.InsnsSizeInCodeUnits()); 144 145 const DexFile::TryItem& t0 = accessor.TryItems().begin()[0]; 146 const DexFile::TryItem& t1 = accessor.TryItems().begin()[1]; 147 EXPECT_LE(t0.start_addr_, t1.start_addr_); 148 { 149 CatchHandlerIterator iter(accessor, 4 /* Dex PC in the first try block */); 150 EXPECT_STREQ("Ljava/io/IOException;", dex_->StringByTypeIdx(iter.GetHandlerTypeIndex())); 151 ASSERT_TRUE(iter.HasNext()); 152 iter.Next(); 153 EXPECT_STREQ("Ljava/lang/Exception;", dex_->StringByTypeIdx(iter.GetHandlerTypeIndex())); 154 ASSERT_TRUE(iter.HasNext()); 155 iter.Next(); 156 EXPECT_FALSE(iter.HasNext()); 157 } 158 { 159 CatchHandlerIterator iter(accessor, 8 /* Dex PC in the second try block */); 160 EXPECT_STREQ("Ljava/io/IOException;", dex_->StringByTypeIdx(iter.GetHandlerTypeIndex())); 161 ASSERT_TRUE(iter.HasNext()); 162 iter.Next(); 163 EXPECT_FALSE(iter.HasNext()); 164 } 165 { 166 CatchHandlerIterator iter(accessor, 11 /* Dex PC not in any try block */); 167 EXPECT_FALSE(iter.HasNext()); 168 } 169 } 170 171 TEST_F(ExceptionTest, StackTraceElement) { 172 Thread* thread = Thread::Current(); 173 thread->TransitionFromSuspendedToRunnable(); 174 bool started = runtime_->Start(); 175 CHECK(started); 176 JNIEnv* env = thread->GetJniEnv(); 177 ScopedObjectAccess soa(env); 178 179 std::vector<uintptr_t> fake_stack; 180 Runtime* r = Runtime::Current(); 181 r->SetInstructionSet(kRuntimeISA); 182 ArtMethod* save_method = r->CreateCalleeSaveMethod(); 183 r->SetCalleeSaveMethod(save_method, CalleeSaveType::kSaveAllCalleeSaves); 184 QuickMethodFrameInfo frame_info = r->GetRuntimeMethodFrameInfo(save_method); 185 186 ASSERT_EQ(kStackAlignment, 16U); 187 // ASSERT_EQ(sizeof(uintptr_t), sizeof(uint32_t)); 188 189 // Create the stack frame for the callee save method, expected by the runtime. 190 fake_stack.push_back(reinterpret_cast<uintptr_t>(save_method)); 191 for (size_t i = 0; i < frame_info.FrameSizeInBytes() - 2 * sizeof(uintptr_t); 192 i += sizeof(uintptr_t)) { 193 fake_stack.push_back(0); 194 } 195 196 fake_stack.push_back(method_g_->GetOatQuickMethodHeader(0)->ToNativeQuickPc( 197 method_g_, kDexPc, /* is_catch_handler */ false)); // return pc 198 199 // Create/push fake 16byte stack frame for method g 200 fake_stack.push_back(reinterpret_cast<uintptr_t>(method_g_)); 201 fake_stack.push_back(0); 202 fake_stack.push_back(0); 203 fake_stack.push_back(method_g_->GetOatQuickMethodHeader(0)->ToNativeQuickPc( 204 method_g_, kDexPc, /* is_catch_handler */ false)); // return pc 205 206 // Create/push fake 16byte stack frame for method f 207 fake_stack.push_back(reinterpret_cast<uintptr_t>(method_f_)); 208 fake_stack.push_back(0); 209 fake_stack.push_back(0); 210 fake_stack.push_back(0xEBAD6070); // return pc 211 212 // Push Method* of null to terminate the trace 213 fake_stack.push_back(0); 214 215 // Push null values which will become null incoming arguments. 216 fake_stack.push_back(0); 217 fake_stack.push_back(0); 218 fake_stack.push_back(0); 219 220 // Set up thread to appear as if we called out of method_g_ at given pc dex. 221 thread->SetTopOfStack(reinterpret_cast<ArtMethod**>(&fake_stack[0])); 222 223 jobject internal = thread->CreateInternalStackTrace<false>(soa); 224 ASSERT_TRUE(internal != nullptr); 225 jobjectArray ste_array = Thread::InternalStackTraceToStackTraceElementArray(soa, internal); 226 ASSERT_TRUE(ste_array != nullptr); 227 auto trace_array = soa.Decode<mirror::ObjectArray<mirror::StackTraceElement>>(ste_array); 228 229 ASSERT_TRUE(trace_array != nullptr); 230 ASSERT_TRUE(trace_array->Get(0) != nullptr); 231 EXPECT_STREQ("ExceptionHandle", 232 trace_array->Get(0)->GetDeclaringClass()->ToModifiedUtf8().c_str()); 233 EXPECT_STREQ("ExceptionHandle.java", 234 trace_array->Get(0)->GetFileName()->ToModifiedUtf8().c_str()); 235 EXPECT_STREQ("g", trace_array->Get(0)->GetMethodName()->ToModifiedUtf8().c_str()); 236 EXPECT_EQ(36, trace_array->Get(0)->GetLineNumber()); 237 238 ASSERT_TRUE(trace_array->Get(1) != nullptr); 239 EXPECT_STREQ("ExceptionHandle", 240 trace_array->Get(1)->GetDeclaringClass()->ToModifiedUtf8().c_str()); 241 EXPECT_STREQ("ExceptionHandle.java", 242 trace_array->Get(1)->GetFileName()->ToModifiedUtf8().c_str()); 243 EXPECT_STREQ("f", trace_array->Get(1)->GetMethodName()->ToModifiedUtf8().c_str()); 244 EXPECT_EQ(22, trace_array->Get(1)->GetLineNumber()); 245 246 thread->SetTopOfStack(nullptr); // Disarm the assertion that no code is running when we detach. 247 } 248 249 } // namespace art 250