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