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 | 33f741e | 2011-10-03 11:24:05 -0700 | [diff] [blame] | 22 | const ClassLoader* class_loader = LoadDex("ExceptionHandle"); |
| 23 | my_klass_ = class_linker_->FindClass("LExceptionHandle;", class_loader); |
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_->SetReturnPcOffsetInBytes(kStackAlignment - kPointerSize); |
| 40 | method_f_->SetCode(CompiledMethod::CodePointer(&fake_code_[0], kThumb2)); |
| 41 | method_f_->SetMappingTable(&fake_mapping_data_[0]); |
Brian Carlstrom | 33f741e | 2011-10-03 11:24:05 -0700 | [diff] [blame] | 42 | |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 43 | method_g_ = my_klass_->FindVirtualMethod("g", "(I)V"); |
| 44 | ASSERT_TRUE(method_g_ != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 45 | method_g_->SetFrameSizeInBytes(kStackAlignment); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 46 | method_g_->SetReturnPcOffsetInBytes(kStackAlignment - kPointerSize); |
| 47 | method_g_->SetCode(CompiledMethod::CodePointer(&fake_code_[0], kThumb2)); |
| 48 | method_g_->SetMappingTable(&fake_mapping_data_[0]); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Brian Carlstrom | 33f741e | 2011-10-03 11:24:05 -0700 | [diff] [blame] | 51 | const DexFile* dex_; |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 52 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 53 | std::vector<uint8_t> fake_code_; |
| 54 | std::vector<uint32_t> fake_mapping_data_; |
| 55 | |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 56 | Method* method_f_; |
| 57 | Method* method_g_; |
| 58 | |
| 59 | private: |
| 60 | Class* my_klass_; |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 61 | }; |
| 62 | |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 63 | TEST_F(ExceptionTest, FindCatchHandler) { |
Brian Carlstrom | 33f741e | 2011-10-03 11:24:05 -0700 | [diff] [blame] | 64 | const DexFile::CodeItem* code_item = dex_->GetCodeItem(method_f_->GetCodeItemOffset()); |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 65 | |
| 66 | ASSERT_TRUE(code_item != NULL); |
| 67 | |
| 68 | ASSERT_EQ(2u, code_item->tries_size_); |
| 69 | ASSERT_NE(0u, code_item->insns_size_); |
| 70 | |
| 71 | const struct DexFile::TryItem *t0, *t1; |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 72 | t0 = dex_->dexGetTryItems(*code_item, 0); |
| 73 | t1 = dex_->dexGetTryItems(*code_item, 1); |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 74 | EXPECT_LE(t0->start_addr_, t1->start_addr_); |
| 75 | |
| 76 | DexFile::CatchHandlerIterator iter = |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 77 | dex_->dexFindCatchHandler(*code_item, 4 /* Dex PC in the first try block */); |
| 78 | ASSERT_EQ(false, iter.HasNext()); |
| 79 | EXPECT_STREQ("Ljava/io/IOException;", dex_->dexStringByTypeIdx(iter.Get().type_idx_)); |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 80 | iter.Next(); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 81 | ASSERT_EQ(false, iter.HasNext()); |
| 82 | EXPECT_STREQ("Ljava/lang/Exception;", dex_->dexStringByTypeIdx(iter.Get().type_idx_)); |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 83 | iter.Next(); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 84 | ASSERT_EQ(true, iter.HasNext()); |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 85 | |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 86 | iter = dex_->dexFindCatchHandler(*code_item, 8 /* Dex PC in the second try block */); |
| 87 | ASSERT_EQ(false, iter.HasNext()); |
| 88 | EXPECT_STREQ("Ljava/io/IOException;", dex_->dexStringByTypeIdx(iter.Get().type_idx_)); |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 89 | iter.Next(); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 90 | ASSERT_EQ(true, iter.HasNext()); |
| 91 | |
| 92 | iter = dex_->dexFindCatchHandler(*code_item, 11 /* Dex PC not in any try block */); |
| 93 | ASSERT_EQ(true, iter.HasNext()); |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 96 | TEST_F(ExceptionTest, StackTraceElement) { |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 97 | runtime_->Start(); |
| 98 | |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 99 | enum {STACK_SIZE = 1000}; |
| 100 | uint32_t top_of_stack = 0; |
| 101 | uintptr_t fake_stack[STACK_SIZE]; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 102 | ASSERT_EQ(kStackAlignment, 16); |
| 103 | ASSERT_EQ(sizeof(uintptr_t), sizeof(uint32_t)); |
| 104 | |
Ian Rogers | f57c47c | 2011-10-06 00:06:17 -0700 | [diff] [blame] | 105 | // 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] | 106 | // 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] | 107 | // from a return pc. |
Elliott Hughes | a43cb5e | 2011-10-07 11:37:59 -0700 | [diff] [blame] | 108 | const uintptr_t pc_offset = 3 + 2; |
Ian Rogers | f57c47c | 2011-10-06 00:06:17 -0700 | [diff] [blame] | 109 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 110 | // Create/push fake 16byte stack frame for method g |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 111 | fake_stack[top_of_stack++] = reinterpret_cast<uintptr_t>(method_g_); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 112 | fake_stack[top_of_stack++] = 0; |
| 113 | fake_stack[top_of_stack++] = 0; |
Elliott Hughes | a43cb5e | 2011-10-07 11:37:59 -0700 | [diff] [blame] | 114 | fake_stack[top_of_stack++] = reinterpret_cast<uintptr_t>(method_f_->GetCode()) + pc_offset; // return pc |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 115 | |
| 116 | // Create/push fake 16byte stack frame for method f |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 117 | fake_stack[top_of_stack++] = reinterpret_cast<uintptr_t>(method_f_); |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 118 | fake_stack[top_of_stack++] = 0; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 119 | fake_stack[top_of_stack++] = 0; |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 120 | fake_stack[top_of_stack++] = 0xEBAD6070; // return pc |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 121 | |
| 122 | // Pull Method* of NULL to terminate the trace |
| 123 | fake_stack[top_of_stack++] = NULL; |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 124 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 125 | // 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] | 126 | Thread* thread = Thread::Current(); |
Elliott Hughes | a43cb5e | 2011-10-07 11:37:59 -0700 | [diff] [blame] | 127 | thread->SetTopOfStack(fake_stack, reinterpret_cast<uintptr_t>(method_g_->GetCode()) + pc_offset); // return pc |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 128 | |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 129 | JNIEnv* env = thread->GetJniEnv(); |
| 130 | jobject internal = thread->CreateInternalStackTrace(env); |
| 131 | jobjectArray ste_array = Thread::InternalStackTraceToStackTraceElementArray(env, internal); |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 132 | ObjectArray<StackTraceElement>* trace_array = |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 133 | Decode<ObjectArray<StackTraceElement>*>(env, ste_array); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 134 | |
| 135 | ASSERT_TRUE(trace_array->Get(0) != NULL); |
Brian Carlstrom | 33f741e | 2011-10-03 11:24:05 -0700 | [diff] [blame] | 136 | EXPECT_STREQ("ExceptionHandle", |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 137 | trace_array->Get(0)->GetDeclaringClass()->ToModifiedUtf8().c_str()); |
Brian Carlstrom | 33f741e | 2011-10-03 11:24:05 -0700 | [diff] [blame] | 138 | 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] | 139 | EXPECT_STREQ("g", trace_array->Get(0)->GetMethodName()->ToModifiedUtf8().c_str()); |
Shih-wei Liao | ff0f9be | 2011-08-29 15:43:53 -0700 | [diff] [blame] | 140 | EXPECT_EQ(22, trace_array->Get(0)->GetLineNumber()); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 141 | |
| 142 | ASSERT_TRUE(trace_array->Get(1) != NULL); |
Brian Carlstrom | 33f741e | 2011-10-03 11:24:05 -0700 | [diff] [blame] | 143 | EXPECT_STREQ("ExceptionHandle", |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 144 | trace_array->Get(1)->GetDeclaringClass()->ToModifiedUtf8().c_str()); |
Brian Carlstrom | 33f741e | 2011-10-03 11:24:05 -0700 | [diff] [blame] | 145 | 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] | 146 | EXPECT_STREQ("f", trace_array->Get(1)->GetMethodName()->ToModifiedUtf8().c_str()); |
Shih-wei Liao | ff0f9be | 2011-08-29 15:43:53 -0700 | [diff] [blame] | 147 | EXPECT_EQ(7, trace_array->Get(1)->GetLineNumber()); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 150 | } // namespace art |