Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #include <sys/mman.h> |
| 4 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 5 | #include "UniquePtr.h" |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 6 | #include "assembler.h" |
| 7 | #include "class_linker.h" |
| 8 | #include "common_test.h" |
| 9 | #include "dex_file.h" |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 10 | #include "gtest/gtest.h" |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 11 | #include "jni_compiler.h" |
| 12 | #include "runtime.h" |
| 13 | #include "thread.h" |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 14 | |
| 15 | namespace art { |
| 16 | |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 17 | class ExceptionTest : public CommonTest { |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 18 | protected: |
| 19 | virtual void SetUp() { |
| 20 | CommonTest::SetUp(); |
| 21 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 22 | SirtRef<ClassLoader> class_loader(LoadDex("ExceptionHandle")); |
| 23 | my_klass_ = class_linker_->FindClass("LExceptionHandle;", class_loader.get()); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 24 | ASSERT_TRUE(my_klass_ != NULL); |
Brian Carlstrom | 33f741e | 2011-10-03 11:24:05 -0700 | [diff] [blame] | 25 | |
| 26 | dex_ = &Runtime::Current()->GetClassLinker()->FindDexFile(my_klass_->GetDexCache()); |
| 27 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 28 | for (size_t i = 0 ; i < 12; i++) { |
| 29 | fake_code_.push_back(0x70000000 | i); |
| 30 | } |
| 31 | |
| 32 | fake_mapping_data_.push_back(2); // first element is count of remaining elements |
| 33 | fake_mapping_data_.push_back(3); // offset 3 |
| 34 | fake_mapping_data_.push_back(3); // maps to dex offset 3 |
Brian Carlstrom | 33f741e | 2011-10-03 11:24:05 -0700 | [diff] [blame] | 35 | |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 36 | method_f_ = my_klass_->FindVirtualMethod("f", "()I"); |
| 37 | ASSERT_TRUE(method_f_ != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 38 | method_f_->SetFrameSizeInBytes(kStackAlignment); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 39 | method_f_->SetCode(CompiledMethod::CodePointer(&fake_code_[0], kThumb2)); |
| 40 | method_f_->SetMappingTable(&fake_mapping_data_[0]); |
Brian Carlstrom | 33f741e | 2011-10-03 11:24:05 -0700 | [diff] [blame] | 41 | |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 42 | method_g_ = my_klass_->FindVirtualMethod("g", "(I)V"); |
| 43 | ASSERT_TRUE(method_g_ != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 44 | method_g_->SetFrameSizeInBytes(kStackAlignment); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 45 | method_g_->SetCode(CompiledMethod::CodePointer(&fake_code_[0], kThumb2)); |
| 46 | method_g_->SetMappingTable(&fake_mapping_data_[0]); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Brian Carlstrom | 33f741e | 2011-10-03 11:24:05 -0700 | [diff] [blame] | 49 | const DexFile* dex_; |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 50 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 51 | std::vector<uint8_t> fake_code_; |
| 52 | std::vector<uint32_t> fake_mapping_data_; |
| 53 | |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 54 | Method* method_f_; |
| 55 | Method* method_g_; |
| 56 | |
| 57 | private: |
| 58 | Class* my_klass_; |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 61 | TEST_F(ExceptionTest, FindCatchHandler) { |
Brian Carlstrom | 33f741e | 2011-10-03 11:24:05 -0700 | [diff] [blame] | 62 | const DexFile::CodeItem* code_item = dex_->GetCodeItem(method_f_->GetCodeItemOffset()); |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 63 | |
| 64 | ASSERT_TRUE(code_item != NULL); |
| 65 | |
| 66 | ASSERT_EQ(2u, code_item->tries_size_); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 67 | ASSERT_NE(0u, code_item->insns_size_in_code_units_); |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 68 | |
| 69 | const struct DexFile::TryItem *t0, *t1; |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 70 | t0 = dex_->dexGetTryItems(*code_item, 0); |
| 71 | t1 = dex_->dexGetTryItems(*code_item, 1); |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 72 | EXPECT_LE(t0->start_addr_, t1->start_addr_); |
| 73 | |
| 74 | DexFile::CatchHandlerIterator iter = |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 75 | dex_->dexFindCatchHandler(*code_item, 4 /* Dex PC in the first try block */); |
| 76 | ASSERT_EQ(false, iter.HasNext()); |
| 77 | EXPECT_STREQ("Ljava/io/IOException;", dex_->dexStringByTypeIdx(iter.Get().type_idx_)); |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 78 | iter.Next(); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 79 | ASSERT_EQ(false, iter.HasNext()); |
| 80 | EXPECT_STREQ("Ljava/lang/Exception;", dex_->dexStringByTypeIdx(iter.Get().type_idx_)); |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 81 | iter.Next(); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 82 | ASSERT_EQ(true, iter.HasNext()); |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 83 | |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 84 | iter = dex_->dexFindCatchHandler(*code_item, 8 /* Dex PC in the second try block */); |
| 85 | ASSERT_EQ(false, iter.HasNext()); |
| 86 | EXPECT_STREQ("Ljava/io/IOException;", dex_->dexStringByTypeIdx(iter.Get().type_idx_)); |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 87 | iter.Next(); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 88 | ASSERT_EQ(true, iter.HasNext()); |
| 89 | |
| 90 | iter = dex_->dexFindCatchHandler(*code_item, 11 /* Dex PC not in any try block */); |
| 91 | ASSERT_EQ(true, iter.HasNext()); |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 94 | TEST_F(ExceptionTest, StackTraceElement) { |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 95 | runtime_->Start(); |
| 96 | |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 97 | std::vector<uintptr_t> fake_stack; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 98 | ASSERT_EQ(kStackAlignment, 16); |
| 99 | ASSERT_EQ(sizeof(uintptr_t), sizeof(uint32_t)); |
| 100 | |
Ian Rogers | f57c47c | 2011-10-06 00:06:17 -0700 | [diff] [blame] | 101 | // Create two fake stack frames with mapping data created in SetUp. We map offset 3 in the code |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 102 | // to dex pc 3, however, we set the return pc to 5 as the stack walker always subtracts two |
Ian Rogers | f57c47c | 2011-10-06 00:06:17 -0700 | [diff] [blame] | 103 | // from a return pc. |
Elliott Hughes | a43cb5e | 2011-10-07 11:37:59 -0700 | [diff] [blame] | 104 | const uintptr_t pc_offset = 3 + 2; |
Ian Rogers | f57c47c | 2011-10-06 00:06:17 -0700 | [diff] [blame] | 105 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 106 | // Create/push fake 16byte stack frame for method g |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 107 | fake_stack.push_back(reinterpret_cast<uintptr_t>(method_g_)); |
| 108 | fake_stack.push_back(0); |
| 109 | fake_stack.push_back(0); |
| 110 | fake_stack.push_back(reinterpret_cast<uintptr_t>(method_f_->GetCode()) + pc_offset); // return pc |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 111 | |
| 112 | // Create/push fake 16byte stack frame for method f |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 113 | fake_stack.push_back(reinterpret_cast<uintptr_t>(method_f_)); |
| 114 | fake_stack.push_back(0); |
| 115 | fake_stack.push_back(0); |
| 116 | fake_stack.push_back(0xEBAD6070); // return pc |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 117 | |
| 118 | // Pull Method* of NULL to terminate the trace |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 119 | fake_stack.push_back(NULL); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 120 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 121 | // Set up thread to appear as if we called out of method_g_ at pc 3 |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 122 | Thread* thread = Thread::Current(); |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 123 | thread->SetTopOfStack(&fake_stack[0], reinterpret_cast<uintptr_t>(method_g_->GetCode()) + pc_offset); // return pc |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 124 | |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 125 | JNIEnv* env = thread->GetJniEnv(); |
| 126 | jobject internal = thread->CreateInternalStackTrace(env); |
| 127 | jobjectArray ste_array = Thread::InternalStackTraceToStackTraceElementArray(env, internal); |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 128 | ObjectArray<StackTraceElement>* trace_array = |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 129 | Decode<ObjectArray<StackTraceElement>*>(env, ste_array); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 130 | |
| 131 | ASSERT_TRUE(trace_array->Get(0) != NULL); |
Brian Carlstrom | 33f741e | 2011-10-03 11:24:05 -0700 | [diff] [blame] | 132 | EXPECT_STREQ("ExceptionHandle", |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 133 | trace_array->Get(0)->GetDeclaringClass()->ToModifiedUtf8().c_str()); |
Brian Carlstrom | 33f741e | 2011-10-03 11:24:05 -0700 | [diff] [blame] | 134 | EXPECT_STREQ("ExceptionHandle.java", trace_array->Get(0)->GetFileName()->ToModifiedUtf8().c_str()); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 135 | EXPECT_STREQ("g", trace_array->Get(0)->GetMethodName()->ToModifiedUtf8().c_str()); |
Shih-wei Liao | ff0f9be | 2011-08-29 15:43:53 -0700 | [diff] [blame] | 136 | EXPECT_EQ(22, trace_array->Get(0)->GetLineNumber()); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 137 | |
| 138 | ASSERT_TRUE(trace_array->Get(1) != NULL); |
Brian Carlstrom | 33f741e | 2011-10-03 11:24:05 -0700 | [diff] [blame] | 139 | EXPECT_STREQ("ExceptionHandle", |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 140 | trace_array->Get(1)->GetDeclaringClass()->ToModifiedUtf8().c_str()); |
Brian Carlstrom | 33f741e | 2011-10-03 11:24:05 -0700 | [diff] [blame] | 141 | EXPECT_STREQ("ExceptionHandle.java", trace_array->Get(1)->GetFileName()->ToModifiedUtf8().c_str()); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 142 | EXPECT_STREQ("f", trace_array->Get(1)->GetMethodName()->ToModifiedUtf8().c_str()); |
Shih-wei Liao | ff0f9be | 2011-08-29 15:43:53 -0700 | [diff] [blame] | 143 | EXPECT_EQ(7, trace_array->Get(1)->GetLineNumber()); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 146 | } // namespace art |