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 | |
| 17 | // package java.lang; |
| 18 | // import java.io.IOException; |
| 19 | // class Object {}; |
| 20 | // public class MyClass { |
| 21 | // int f() throws Exception { |
| 22 | // try { |
| 23 | // g(1); |
| 24 | // } catch (IOException e) { |
| 25 | // return 1; |
| 26 | // } catch (Exception e) { |
| 27 | // return 2; |
| 28 | // } |
| 29 | // try { |
| 30 | // g(2); |
| 31 | // } catch (IOException e) { |
| 32 | // return 3; |
| 33 | // } |
| 34 | // return 0; |
| 35 | // } |
| 36 | // void g(int doThrow) throws Exception { |
| 37 | // if (doThrow == 1) |
| 38 | // throw new Exception(); |
| 39 | // else if (doThrow == 2) |
| 40 | // throw new IOException(); |
| 41 | // } |
| 42 | // } |
| 43 | |
| 44 | static const char kMyClassExceptionHandleDex[] = |
| 45 | "ZGV4CjAzNQC/bXXtLZJLN1GzLr+ncrvPSl70n8t0yAjgAwAAcAAAAHhWNBIAAAAAAAAAACgDAAAN" |
| 46 | "AAAAcAAAAAcAAACkAAAAAwAAAMAAAAA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AwAAAA8AAAAAAAAAAQAAAAAAAAABAAAADQAAAHAAAAACAAAABwAAAKQAAAADAAAAAwAA" |
| 60 | "AMAAAAAFAAAABgAAAOQAAAAGAAAAAgAAABQBAAADEAAAAQAAAFQBAAABIAAABAAAAFwBAAAGIAAA" |
| 61 | "AQAAABgCAAABEAAAAQAAADgCAAACIAAADQAAAD4CAAADIAAABAAAAN4CAAAEIAAAAQAAAAEDAAAA" |
| 62 | "IAAAAgAAAAkDAAAAEAAAAQAAACgDAAA="; |
| 63 | |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 64 | class ExceptionTest : public CommonTest { |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 65 | protected: |
| 66 | virtual void SetUp() { |
| 67 | CommonTest::SetUp(); |
| 68 | |
| 69 | dex_.reset(OpenDexFileBase64(kMyClassExceptionHandleDex, "kMyClassExceptionHandleDex")); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 70 | ASSERT_TRUE(dex_.get() != NULL); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 71 | const ClassLoader* class_loader = AllocPathClassLoader(dex_.get()); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 72 | ASSERT_TRUE(class_loader != NULL); |
| 73 | my_klass_ = class_linker_->FindClass("Ljava/lang/MyClass;", class_loader); |
| 74 | ASSERT_TRUE(my_klass_ != NULL); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 75 | ByteArray* fake_code = ByteArray::Alloc(12); |
| 76 | ASSERT_TRUE(fake_code != NULL); |
| 77 | IntArray* fake_mapping_data = IntArray::Alloc(2); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 78 | ASSERT_TRUE(fake_mapping_data != NULL); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 79 | fake_mapping_data->Set(0, 3); // offset 3 |
| 80 | fake_mapping_data->Set(1, 3); // maps to dex offset 3 |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 81 | method_f_ = my_klass_->FindVirtualMethod("f", "()I"); |
| 82 | ASSERT_TRUE(method_f_ != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 83 | method_f_->SetFrameSizeInBytes(kStackAlignment); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 84 | method_f_->SetReturnPcOffsetInBytes(kStackAlignment-kPointerSize); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 85 | method_f_->SetCodeArray(fake_code, kThumb2); |
| 86 | method_f_->SetMappingTable(fake_mapping_data); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 87 | method_g_ = my_klass_->FindVirtualMethod("g", "(I)V"); |
| 88 | ASSERT_TRUE(method_g_ != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 89 | method_g_->SetFrameSizeInBytes(kStackAlignment); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 90 | method_g_->SetReturnPcOffsetInBytes(kStackAlignment-kPointerSize); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 91 | method_g_->SetCodeArray(fake_code, kThumb2); |
| 92 | method_g_->SetMappingTable(fake_mapping_data); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 95 | UniquePtr<const DexFile> dex_; |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 96 | |
| 97 | Method* method_f_; |
| 98 | Method* method_g_; |
| 99 | |
| 100 | private: |
| 101 | Class* my_klass_; |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 102 | }; |
| 103 | |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 104 | TEST_F(ExceptionTest, FindCatchHandler) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 105 | const DexFile::CodeItem *code_item = dex_->GetCodeItem(method_f_->GetCodeItemOffset()); |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 106 | |
| 107 | ASSERT_TRUE(code_item != NULL); |
| 108 | |
| 109 | ASSERT_EQ(2u, code_item->tries_size_); |
| 110 | ASSERT_NE(0u, code_item->insns_size_); |
| 111 | |
| 112 | const struct DexFile::TryItem *t0, *t1; |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 113 | t0 = dex_->dexGetTryItems(*code_item, 0); |
| 114 | t1 = dex_->dexGetTryItems(*code_item, 1); |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 115 | EXPECT_LE(t0->start_addr_, t1->start_addr_); |
| 116 | |
| 117 | DexFile::CatchHandlerIterator iter = |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 118 | dex_->dexFindCatchHandler(*code_item, 4 /* Dex PC in the first try block */); |
| 119 | ASSERT_EQ(false, iter.HasNext()); |
| 120 | EXPECT_STREQ("Ljava/io/IOException;", dex_->dexStringByTypeIdx(iter.Get().type_idx_)); |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 121 | iter.Next(); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 122 | ASSERT_EQ(false, iter.HasNext()); |
| 123 | EXPECT_STREQ("Ljava/lang/Exception;", dex_->dexStringByTypeIdx(iter.Get().type_idx_)); |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 124 | iter.Next(); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 125 | ASSERT_EQ(true, iter.HasNext()); |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 126 | |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 127 | iter = dex_->dexFindCatchHandler(*code_item, 8 /* Dex PC in the second try block */); |
| 128 | ASSERT_EQ(false, iter.HasNext()); |
| 129 | EXPECT_STREQ("Ljava/io/IOException;", dex_->dexStringByTypeIdx(iter.Get().type_idx_)); |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 130 | iter.Next(); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 131 | ASSERT_EQ(true, iter.HasNext()); |
| 132 | |
| 133 | iter = dex_->dexFindCatchHandler(*code_item, 11 /* Dex PC not in any try block */); |
| 134 | ASSERT_EQ(true, iter.HasNext()); |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 137 | TEST_F(ExceptionTest, StackTraceElement) { |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 138 | runtime_->Start(); |
| 139 | |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 140 | enum {STACK_SIZE = 1000}; |
| 141 | uint32_t top_of_stack = 0; |
| 142 | uintptr_t fake_stack[STACK_SIZE]; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 143 | ASSERT_EQ(kStackAlignment, 16); |
| 144 | ASSERT_EQ(sizeof(uintptr_t), sizeof(uint32_t)); |
| 145 | |
| 146 | // Create/push fake 16byte stack frame for method g |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 147 | fake_stack[top_of_stack++] = reinterpret_cast<uintptr_t>(method_g_); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 148 | fake_stack[top_of_stack++] = 0; |
| 149 | fake_stack[top_of_stack++] = 0; |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 150 | fake_stack[top_of_stack++] = reinterpret_cast<uintptr_t>(method_f_->GetCode()) + 3; // return pc |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 151 | |
| 152 | // Create/push fake 16byte stack frame for method f |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 153 | fake_stack[top_of_stack++] = reinterpret_cast<uintptr_t>(method_f_); |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 154 | fake_stack[top_of_stack++] = 0; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 155 | fake_stack[top_of_stack++] = 0; |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 156 | fake_stack[top_of_stack++] = 0xEBAD6070; // return pc |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 157 | |
| 158 | // Pull Method* of NULL to terminate the trace |
| 159 | fake_stack[top_of_stack++] = NULL; |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 160 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 161 | // 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] | 162 | Thread* thread = Thread::Current(); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 163 | thread->SetTopOfStack(fake_stack, reinterpret_cast<uintptr_t>(method_g_->GetCode()) + 3); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 164 | |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 165 | JNIEnv* env = thread->GetJniEnv(); |
| 166 | jobject internal = thread->CreateInternalStackTrace(env); |
| 167 | jobjectArray ste_array = Thread::InternalStackTraceToStackTraceElementArray(env, internal); |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 168 | ObjectArray<StackTraceElement>* trace_array = |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 169 | Decode<ObjectArray<StackTraceElement>*>(env, ste_array); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 170 | |
| 171 | ASSERT_TRUE(trace_array->Get(0) != NULL); |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 172 | EXPECT_STREQ("java.lang.MyClass", |
| 173 | trace_array->Get(0)->GetDeclaringClass()->ToModifiedUtf8().c_str()); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 174 | EXPECT_STREQ("MyClass.java", trace_array->Get(0)->GetFileName()->ToModifiedUtf8().c_str()); |
| 175 | EXPECT_STREQ("g", trace_array->Get(0)->GetMethodName()->ToModifiedUtf8().c_str()); |
Shih-wei Liao | ff0f9be | 2011-08-29 15:43:53 -0700 | [diff] [blame] | 176 | EXPECT_EQ(22, trace_array->Get(0)->GetLineNumber()); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 177 | |
| 178 | ASSERT_TRUE(trace_array->Get(1) != NULL); |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 179 | EXPECT_STREQ("java.lang.MyClass", |
| 180 | trace_array->Get(1)->GetDeclaringClass()->ToModifiedUtf8().c_str()); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 181 | EXPECT_STREQ("MyClass.java", trace_array->Get(1)->GetFileName()->ToModifiedUtf8().c_str()); |
| 182 | EXPECT_STREQ("f", trace_array->Get(1)->GetMethodName()->ToModifiedUtf8().c_str()); |
Shih-wei Liao | ff0f9be | 2011-08-29 15:43:53 -0700 | [diff] [blame] | 183 | EXPECT_EQ(7, trace_array->Get(1)->GetLineNumber()); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 186 | } // namespace art |