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