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 "class_linker.h" 20 #include "common_runtime_test.h" 21 #include "dex_file.h" 22 #include "gtest/gtest.h" 23 #include "leb128.h" 24 #include "mirror/class-inl.h" 25 #include "mirror/object_array-inl.h" 26 #include "mirror/object-inl.h" 27 #include "mirror/stack_trace_element.h" 28 #include "runtime.h" 29 #include "scoped_thread_state_change.h" 30 #include "handle_scope-inl.h" 31 #include "thread.h" 32 #include "vmap_table.h" 33 34 namespace art { 35 36 class ExceptionTest : public CommonRuntimeTest { 37 protected: 38 virtual void SetUp() { 39 CommonRuntimeTest::SetUp(); 40 41 ScopedObjectAccess soa(Thread::Current()); 42 StackHandleScope<2> hs(soa.Self()); 43 Handle<mirror::ClassLoader> class_loader( 44 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("ExceptionHandle")))); 45 my_klass_ = class_linker_->FindClass(soa.Self(), "LExceptionHandle;", class_loader); 46 ASSERT_TRUE(my_klass_ != NULL); 47 Handle<mirror::Class> klass(hs.NewHandle(my_klass_)); 48 class_linker_->EnsureInitialized(klass, true, true); 49 my_klass_ = klass.Get(); 50 51 dex_ = my_klass_->GetDexCache()->GetDexFile(); 52 53 uint32_t code_size = 12; 54 for (size_t i = 0 ; i < code_size; i++) { 55 fake_code_.push_back(0x70 | i); 56 } 57 58 fake_mapping_data_.PushBackUnsigned(4); // first element is count 59 fake_mapping_data_.PushBackUnsigned(4); // total (non-length) elements 60 fake_mapping_data_.PushBackUnsigned(2); // count of pc to dex elements 61 // --- pc to dex table 62 fake_mapping_data_.PushBackUnsigned(3 - 0); // offset 3 63 fake_mapping_data_.PushBackSigned(3 - 0); // maps to dex offset 3 64 // --- dex to pc table 65 fake_mapping_data_.PushBackUnsigned(3 - 0); // offset 3 66 fake_mapping_data_.PushBackSigned(3 - 0); // maps to dex offset 3 67 68 fake_vmap_table_data_.PushBackUnsigned(0 + VmapTable::kEntryAdjustment); 69 70 fake_gc_map_.push_back(0); // 0 bytes to encode references and native pc offsets. 71 fake_gc_map_.push_back(0); 72 fake_gc_map_.push_back(0); // 0 entries. 73 fake_gc_map_.push_back(0); 74 75 const std::vector<uint8_t>& fake_vmap_table_data = fake_vmap_table_data_.GetData(); 76 const std::vector<uint8_t>& fake_mapping_data = fake_mapping_data_.GetData(); 77 uint32_t vmap_table_offset = sizeof(OatQuickMethodHeader) + fake_vmap_table_data.size(); 78 uint32_t mapping_table_offset = vmap_table_offset + fake_mapping_data.size(); 79 OatQuickMethodHeader method_header(mapping_table_offset, vmap_table_offset, 80 4 * kPointerSize, 0u, 0u, code_size); 81 fake_header_code_and_maps_.resize(sizeof(method_header)); 82 memcpy(&fake_header_code_and_maps_[0], &method_header, sizeof(method_header)); 83 fake_header_code_and_maps_.insert(fake_header_code_and_maps_.begin(), 84 fake_vmap_table_data.begin(), fake_vmap_table_data.end()); 85 fake_header_code_and_maps_.insert(fake_header_code_and_maps_.begin(), 86 fake_mapping_data.begin(), fake_mapping_data.end()); 87 fake_header_code_and_maps_.insert(fake_header_code_and_maps_.end(), 88 fake_code_.begin(), fake_code_.end()); 89 90 // NOTE: Don't align the code (it will not be executed) but check that the Thumb2 91 // adjustment will be a NOP, see ArtMethod::EntryPointToCodePointer(). 92 CHECK_EQ(mapping_table_offset & 1u, 0u); 93 const uint8_t* code_ptr = &fake_header_code_and_maps_[mapping_table_offset]; 94 95 method_f_ = my_klass_->FindVirtualMethod("f", "()I"); 96 ASSERT_TRUE(method_f_ != NULL); 97 method_f_->SetEntryPointFromQuickCompiledCode(code_ptr); 98 method_f_->SetNativeGcMap(&fake_gc_map_[0]); 99 100 method_g_ = my_klass_->FindVirtualMethod("g", "(I)V"); 101 ASSERT_TRUE(method_g_ != NULL); 102 method_g_->SetEntryPointFromQuickCompiledCode(code_ptr); 103 method_g_->SetNativeGcMap(&fake_gc_map_[0]); 104 } 105 106 const DexFile* dex_; 107 108 std::vector<uint8_t> fake_code_; 109 Leb128EncodingVector fake_mapping_data_; 110 Leb128EncodingVector fake_vmap_table_data_; 111 std::vector<uint8_t> fake_gc_map_; 112 std::vector<uint8_t> fake_header_code_and_maps_; 113 114 mirror::ArtMethod* method_f_; 115 mirror::ArtMethod* method_g_; 116 117 private: 118 mirror::Class* my_klass_; 119 }; 120 121 TEST_F(ExceptionTest, FindCatchHandler) { 122 ScopedObjectAccess soa(Thread::Current()); 123 const DexFile::CodeItem* code_item = dex_->GetCodeItem(method_f_->GetCodeItemOffset()); 124 125 ASSERT_TRUE(code_item != NULL); 126 127 ASSERT_EQ(2u, code_item->tries_size_); 128 ASSERT_NE(0u, code_item->insns_size_in_code_units_); 129 130 const DexFile::TryItem *t0, *t1; 131 t0 = dex_->GetTryItems(*code_item, 0); 132 t1 = dex_->GetTryItems(*code_item, 1); 133 EXPECT_LE(t0->start_addr_, t1->start_addr_); 134 { 135 CatchHandlerIterator iter(*code_item, 4 /* Dex PC in the first try block */); 136 EXPECT_STREQ("Ljava/io/IOException;", dex_->StringByTypeIdx(iter.GetHandlerTypeIndex())); 137 ASSERT_TRUE(iter.HasNext()); 138 iter.Next(); 139 EXPECT_STREQ("Ljava/lang/Exception;", dex_->StringByTypeIdx(iter.GetHandlerTypeIndex())); 140 ASSERT_TRUE(iter.HasNext()); 141 iter.Next(); 142 EXPECT_FALSE(iter.HasNext()); 143 } 144 { 145 CatchHandlerIterator iter(*code_item, 8 /* Dex PC in the second try block */); 146 EXPECT_STREQ("Ljava/io/IOException;", dex_->StringByTypeIdx(iter.GetHandlerTypeIndex())); 147 ASSERT_TRUE(iter.HasNext()); 148 iter.Next(); 149 EXPECT_FALSE(iter.HasNext()); 150 } 151 { 152 CatchHandlerIterator iter(*code_item, 11 /* Dex PC not in any try block */); 153 EXPECT_FALSE(iter.HasNext()); 154 } 155 } 156 157 TEST_F(ExceptionTest, StackTraceElement) { 158 Thread* thread = Thread::Current(); 159 thread->TransitionFromSuspendedToRunnable(); 160 bool started = runtime_->Start(); 161 CHECK(started); 162 JNIEnv* env = thread->GetJniEnv(); 163 ScopedObjectAccess soa(env); 164 165 std::vector<uintptr_t> fake_stack; 166 ASSERT_EQ(kStackAlignment, 16U); 167 // ASSERT_EQ(sizeof(uintptr_t), sizeof(uint32_t)); 168 169 if (!kUsePortableCompiler) { 170 // Create two fake stack frames with mapping data created in SetUp. We map offset 3 in the code 171 // to dex pc 3. 172 const uint32_t dex_pc = 3; 173 174 // Create/push fake 16byte stack frame for method g 175 fake_stack.push_back(reinterpret_cast<uintptr_t>(method_g_)); 176 fake_stack.push_back(0); 177 fake_stack.push_back(0); 178 fake_stack.push_back(method_f_->ToNativePc(dex_pc)); // return pc 179 180 // Create/push fake 16byte stack frame for method f 181 fake_stack.push_back(reinterpret_cast<uintptr_t>(method_f_)); 182 fake_stack.push_back(0); 183 fake_stack.push_back(0); 184 fake_stack.push_back(0xEBAD6070); // return pc 185 186 // Pull Method* of NULL to terminate the trace 187 fake_stack.push_back(0); 188 189 // Push null values which will become null incoming arguments. 190 fake_stack.push_back(0); 191 fake_stack.push_back(0); 192 fake_stack.push_back(0); 193 194 // Set up thread to appear as if we called out of method_g_ at pc dex 3 195 thread->SetTopOfStack( 196 reinterpret_cast<StackReference<mirror::ArtMethod>*>(&fake_stack[0]), 197 method_g_->ToNativePc(dex_pc)); // return pc 198 } else { 199 // Create/push fake 20-byte shadow frame for method g 200 fake_stack.push_back(0); 201 fake_stack.push_back(0); 202 fake_stack.push_back(reinterpret_cast<uintptr_t>(method_g_)); 203 fake_stack.push_back(3); 204 fake_stack.push_back(0); 205 206 // Create/push fake 20-byte shadow frame for method f 207 fake_stack.push_back(0); 208 fake_stack.push_back(0); 209 fake_stack.push_back(reinterpret_cast<uintptr_t>(method_f_)); 210 fake_stack.push_back(3); 211 fake_stack.push_back(0); 212 213 thread->PushShadowFrame(reinterpret_cast<ShadowFrame*>(&fake_stack[5])); 214 thread->PushShadowFrame(reinterpret_cast<ShadowFrame*>(&fake_stack[0])); 215 } 216 217 jobject internal = thread->CreateInternalStackTrace<false>(soa); 218 ASSERT_TRUE(internal != NULL); 219 jobjectArray ste_array = Thread::InternalStackTraceToStackTraceElementArray(soa, internal); 220 ASSERT_TRUE(ste_array != NULL); 221 mirror::ObjectArray<mirror::StackTraceElement>* trace_array = 222 soa.Decode<mirror::ObjectArray<mirror::StackTraceElement>*>(ste_array); 223 224 ASSERT_TRUE(trace_array != NULL); 225 ASSERT_TRUE(trace_array->Get(0) != NULL); 226 EXPECT_STREQ("ExceptionHandle", 227 trace_array->Get(0)->GetDeclaringClass()->ToModifiedUtf8().c_str()); 228 EXPECT_STREQ("ExceptionHandle.java", trace_array->Get(0)->GetFileName()->ToModifiedUtf8().c_str()); 229 EXPECT_STREQ("g", trace_array->Get(0)->GetMethodName()->ToModifiedUtf8().c_str()); 230 EXPECT_EQ(37, trace_array->Get(0)->GetLineNumber()); 231 232 ASSERT_TRUE(trace_array->Get(1) != NULL); 233 EXPECT_STREQ("ExceptionHandle", 234 trace_array->Get(1)->GetDeclaringClass()->ToModifiedUtf8().c_str()); 235 EXPECT_STREQ("ExceptionHandle.java", trace_array->Get(1)->GetFileName()->ToModifiedUtf8().c_str()); 236 EXPECT_STREQ("f", trace_array->Get(1)->GetMethodName()->ToModifiedUtf8().c_str()); 237 EXPECT_EQ(22, trace_array->Get(1)->GetLineNumber()); 238 239 #if !defined(ART_USE_PORTABLE_COMPILER) 240 thread->SetTopOfStack(NULL, 0); // Disarm the assertion that no code is running when we detach. 241 #else 242 thread->PopShadowFrame(); 243 thread->PopShadowFrame(); 244 #endif 245 } 246 247 } // namespace art 248