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 uint32_t gc_map_offset = mapping_table_offset + fake_gc_map_.size(); 80 OatQuickMethodHeader method_header(mapping_table_offset, vmap_table_offset, gc_map_offset, 81 4 * kPointerSize, 0u, 0u, code_size); 82 fake_header_code_and_maps_.resize(sizeof(method_header)); 83 memcpy(&fake_header_code_and_maps_[0], &method_header, sizeof(method_header)); 84 fake_header_code_and_maps_.insert(fake_header_code_and_maps_.begin(), 85 fake_vmap_table_data.begin(), fake_vmap_table_data.end()); 86 fake_header_code_and_maps_.insert(fake_header_code_and_maps_.begin(), 87 fake_mapping_data.begin(), fake_mapping_data.end()); 88 fake_header_code_and_maps_.insert(fake_header_code_and_maps_.begin(), 89 fake_gc_map_.begin(), fake_gc_map_.end()); 90 fake_header_code_and_maps_.insert(fake_header_code_and_maps_.end(), 91 fake_code_.begin(), fake_code_.end()); 92 93 // NOTE: Don't align the code (it will not be executed) but check that the Thumb2 94 // adjustment will be a NOP, see ArtMethod::EntryPointToCodePointer(). 95 CHECK_EQ(mapping_table_offset & 1u, 0u); 96 const uint8_t* code_ptr = &fake_header_code_and_maps_[gc_map_offset]; 97 98 method_f_ = my_klass_->FindVirtualMethod("f", "()I"); 99 ASSERT_TRUE(method_f_ != NULL); 100 method_f_->SetEntryPointFromQuickCompiledCode(code_ptr); 101 102 method_g_ = my_klass_->FindVirtualMethod("g", "(I)V"); 103 ASSERT_TRUE(method_g_ != NULL); 104 method_g_->SetEntryPointFromQuickCompiledCode(code_ptr); 105 } 106 107 const DexFile* dex_; 108 109 std::vector<uint8_t> fake_code_; 110 Leb128EncodingVector fake_mapping_data_; 111 Leb128EncodingVector fake_vmap_table_data_; 112 std::vector<uint8_t> fake_gc_map_; 113 std::vector<uint8_t> fake_header_code_and_maps_; 114 115 mirror::ArtMethod* method_f_; 116 mirror::ArtMethod* method_g_; 117 118 private: 119 mirror::Class* my_klass_; 120 }; 121 122 TEST_F(ExceptionTest, FindCatchHandler) { 123 ScopedObjectAccess soa(Thread::Current()); 124 const DexFile::CodeItem* code_item = dex_->GetCodeItem(method_f_->GetCodeItemOffset()); 125 126 ASSERT_TRUE(code_item != NULL); 127 128 ASSERT_EQ(2u, code_item->tries_size_); 129 ASSERT_NE(0u, code_item->insns_size_in_code_units_); 130 131 const DexFile::TryItem *t0, *t1; 132 t0 = dex_->GetTryItems(*code_item, 0); 133 t1 = dex_->GetTryItems(*code_item, 1); 134 EXPECT_LE(t0->start_addr_, t1->start_addr_); 135 { 136 CatchHandlerIterator iter(*code_item, 4 /* Dex PC in the first try block */); 137 EXPECT_STREQ("Ljava/io/IOException;", dex_->StringByTypeIdx(iter.GetHandlerTypeIndex())); 138 ASSERT_TRUE(iter.HasNext()); 139 iter.Next(); 140 EXPECT_STREQ("Ljava/lang/Exception;", dex_->StringByTypeIdx(iter.GetHandlerTypeIndex())); 141 ASSERT_TRUE(iter.HasNext()); 142 iter.Next(); 143 EXPECT_FALSE(iter.HasNext()); 144 } 145 { 146 CatchHandlerIterator iter(*code_item, 8 /* Dex PC in the second try block */); 147 EXPECT_STREQ("Ljava/io/IOException;", dex_->StringByTypeIdx(iter.GetHandlerTypeIndex())); 148 ASSERT_TRUE(iter.HasNext()); 149 iter.Next(); 150 EXPECT_FALSE(iter.HasNext()); 151 } 152 { 153 CatchHandlerIterator iter(*code_item, 11 /* Dex PC not in any try block */); 154 EXPECT_FALSE(iter.HasNext()); 155 } 156 } 157 158 TEST_F(ExceptionTest, StackTraceElement) { 159 Thread* thread = Thread::Current(); 160 thread->TransitionFromSuspendedToRunnable(); 161 bool started = runtime_->Start(); 162 CHECK(started); 163 JNIEnv* env = thread->GetJniEnv(); 164 ScopedObjectAccess soa(env); 165 166 std::vector<uintptr_t> fake_stack; 167 ASSERT_EQ(kStackAlignment, 16U); 168 // ASSERT_EQ(sizeof(uintptr_t), sizeof(uint32_t)); 169 170 if (!kUsePortableCompiler) { 171 // Create two fake stack frames with mapping data created in SetUp. We map offset 3 in the code 172 // to dex pc 3. 173 const uint32_t dex_pc = 3; 174 175 // Create/push fake 16byte stack frame for method g 176 fake_stack.push_back(reinterpret_cast<uintptr_t>(method_g_)); 177 fake_stack.push_back(0); 178 fake_stack.push_back(0); 179 fake_stack.push_back(method_f_->ToNativePc(dex_pc)); // return pc 180 181 // Create/push fake 16byte stack frame for method f 182 fake_stack.push_back(reinterpret_cast<uintptr_t>(method_f_)); 183 fake_stack.push_back(0); 184 fake_stack.push_back(0); 185 fake_stack.push_back(0xEBAD6070); // return pc 186 187 // Pull Method* of NULL to terminate the trace 188 fake_stack.push_back(0); 189 190 // Push null values which will become null incoming arguments. 191 fake_stack.push_back(0); 192 fake_stack.push_back(0); 193 fake_stack.push_back(0); 194 195 // Set up thread to appear as if we called out of method_g_ at pc dex 3 196 thread->SetTopOfStack( 197 reinterpret_cast<StackReference<mirror::ArtMethod>*>(&fake_stack[0]), 198 method_g_->ToNativePc(dex_pc)); // return pc 199 } else { 200 // Create/push fake 20-byte shadow frame for method g 201 fake_stack.push_back(0); 202 fake_stack.push_back(0); 203 fake_stack.push_back(reinterpret_cast<uintptr_t>(method_g_)); 204 fake_stack.push_back(3); 205 fake_stack.push_back(0); 206 207 // Create/push fake 20-byte shadow frame for method f 208 fake_stack.push_back(0); 209 fake_stack.push_back(0); 210 fake_stack.push_back(reinterpret_cast<uintptr_t>(method_f_)); 211 fake_stack.push_back(3); 212 fake_stack.push_back(0); 213 214 thread->PushShadowFrame(reinterpret_cast<ShadowFrame*>(&fake_stack[5])); 215 thread->PushShadowFrame(reinterpret_cast<ShadowFrame*>(&fake_stack[0])); 216 } 217 218 jobject internal = thread->CreateInternalStackTrace<false>(soa); 219 ASSERT_TRUE(internal != NULL); 220 jobjectArray ste_array = Thread::InternalStackTraceToStackTraceElementArray(soa, internal); 221 ASSERT_TRUE(ste_array != NULL); 222 mirror::ObjectArray<mirror::StackTraceElement>* trace_array = 223 soa.Decode<mirror::ObjectArray<mirror::StackTraceElement>*>(ste_array); 224 225 ASSERT_TRUE(trace_array != NULL); 226 ASSERT_TRUE(trace_array->Get(0) != NULL); 227 EXPECT_STREQ("ExceptionHandle", 228 trace_array->Get(0)->GetDeclaringClass()->ToModifiedUtf8().c_str()); 229 EXPECT_STREQ("ExceptionHandle.java", trace_array->Get(0)->GetFileName()->ToModifiedUtf8().c_str()); 230 EXPECT_STREQ("g", trace_array->Get(0)->GetMethodName()->ToModifiedUtf8().c_str()); 231 EXPECT_EQ(37, trace_array->Get(0)->GetLineNumber()); 232 233 ASSERT_TRUE(trace_array->Get(1) != NULL); 234 EXPECT_STREQ("ExceptionHandle", 235 trace_array->Get(1)->GetDeclaringClass()->ToModifiedUtf8().c_str()); 236 EXPECT_STREQ("ExceptionHandle.java", trace_array->Get(1)->GetFileName()->ToModifiedUtf8().c_str()); 237 EXPECT_STREQ("f", trace_array->Get(1)->GetMethodName()->ToModifiedUtf8().c_str()); 238 EXPECT_EQ(22, trace_array->Get(1)->GetLineNumber()); 239 240 #if !defined(ART_USE_PORTABLE_COMPILER) 241 thread->SetTopOfStack(NULL, 0); // Disarm the assertion that no code is running when we detach. 242 #else 243 thread->PopShadowFrame(); 244 thread->PopShadowFrame(); 245 #endif 246 } 247 248 } // namespace art 249