blob: df6432923a630c52e74fe526fd3c4e7d2deac5ef [file] [log] [blame]
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#include <sys/mman.h>
4
Elliott Hughes90a33692011-08-30 13:27:07 -07005#include "UniquePtr.h"
Shih-wei Liao2fb97532011-08-11 16:17:23 -07006#include "assembler.h"
7#include "class_linker.h"
8#include "common_test.h"
9#include "dex_file.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070010#include "gtest/gtest.h"
Shih-wei Liao2fb97532011-08-11 16:17:23 -070011#include "jni_compiler.h"
12#include "runtime.h"
13#include "thread.h"
Shih-wei Liao2fb97532011-08-11 16:17:23 -070014
15namespace art {
16
Brian Carlstromf734cf52011-08-17 16:28:14 -070017class ExceptionTest : public CommonTest {
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -070018 protected:
19 virtual void SetUp() {
20 CommonTest::SetUp();
21
Brian Carlstrom40381fb2011-10-19 14:13:40 -070022 SirtRef<ClassLoader> class_loader(LoadDex("ExceptionHandle"));
23 my_klass_ = class_linker_->FindClass("LExceptionHandle;", class_loader.get());
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -070024 ASSERT_TRUE(my_klass_ != NULL);
Brian Carlstrom33f741e2011-10-03 11:24:05 -070025
26 dex_ = &Runtime::Current()->GetClassLinker()->FindDexFile(my_klass_->GetDexCache());
27
Brian Carlstrom3320cf42011-10-04 14:58:28 -070028 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 Carlstrom33f741e2011-10-03 11:24:05 -070035
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -070036 method_f_ = my_klass_->FindVirtualMethod("f", "()I");
37 ASSERT_TRUE(method_f_ != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070038 method_f_->SetFrameSizeInBytes(kStackAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -070039 method_f_->SetCode(CompiledMethod::CodePointer(&fake_code_[0], kThumb2));
40 method_f_->SetMappingTable(&fake_mapping_data_[0]);
Brian Carlstrom33f741e2011-10-03 11:24:05 -070041
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -070042 method_g_ = my_klass_->FindVirtualMethod("g", "(I)V");
43 ASSERT_TRUE(method_g_ != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070044 method_g_->SetFrameSizeInBytes(kStackAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -070045 method_g_->SetCode(CompiledMethod::CodePointer(&fake_code_[0], kThumb2));
46 method_g_->SetMappingTable(&fake_mapping_data_[0]);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -070047 }
48
Brian Carlstrom33f741e2011-10-03 11:24:05 -070049 const DexFile* dex_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -070050
Brian Carlstrom3320cf42011-10-04 14:58:28 -070051 std::vector<uint8_t> fake_code_;
52 std::vector<uint32_t> fake_mapping_data_;
53
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -070054 Method* method_f_;
55 Method* method_g_;
56
57 private:
58 Class* my_klass_;
Shih-wei Liao2fb97532011-08-11 16:17:23 -070059};
60
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -070061TEST_F(ExceptionTest, FindCatchHandler) {
Brian Carlstrom33f741e2011-10-03 11:24:05 -070062 const DexFile::CodeItem* code_item = dex_->GetCodeItem(method_f_->GetCodeItemOffset());
Shih-wei Liao2fb97532011-08-11 16:17:23 -070063
64 ASSERT_TRUE(code_item != NULL);
65
66 ASSERT_EQ(2u, code_item->tries_size_);
Ian Rogersd81871c2011-10-03 13:57:23 -070067 ASSERT_NE(0u, code_item->insns_size_in_code_units_);
Shih-wei Liao2fb97532011-08-11 16:17:23 -070068
69 const struct DexFile::TryItem *t0, *t1;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -070070 t0 = dex_->dexGetTryItems(*code_item, 0);
71 t1 = dex_->dexGetTryItems(*code_item, 1);
Shih-wei Liao2fb97532011-08-11 16:17:23 -070072 EXPECT_LE(t0->start_addr_, t1->start_addr_);
73
74 DexFile::CatchHandlerIterator iter =
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -070075 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 Liao2fb97532011-08-11 16:17:23 -070078 iter.Next();
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -070079 ASSERT_EQ(false, iter.HasNext());
80 EXPECT_STREQ("Ljava/lang/Exception;", dex_->dexStringByTypeIdx(iter.Get().type_idx_));
Shih-wei Liao2fb97532011-08-11 16:17:23 -070081 iter.Next();
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -070082 ASSERT_EQ(true, iter.HasNext());
Shih-wei Liao2fb97532011-08-11 16:17:23 -070083
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -070084 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 Liao2fb97532011-08-11 16:17:23 -070087 iter.Next();
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -070088 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 Liao2fb97532011-08-11 16:17:23 -070092}
93
Shih-wei Liao55df06b2011-08-26 14:39:27 -070094TEST_F(ExceptionTest, StackTraceElement) {
Brian Carlstrom25c33252011-09-18 15:58:35 -070095 runtime_->Start();
96
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070097 std::vector<uintptr_t> fake_stack;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070098 ASSERT_EQ(kStackAlignment, 16);
99 ASSERT_EQ(sizeof(uintptr_t), sizeof(uint32_t));
100
Ian Rogersf57c47c2011-10-06 00:06:17 -0700101 // Create two fake stack frames with mapping data created in SetUp. We map offset 3 in the code
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700102 // to dex pc 3, however, we set the return pc to 5 as the stack walker always subtracts two
Ian Rogersf57c47c2011-10-06 00:06:17 -0700103 // from a return pc.
Elliott Hughesa43cb5e2011-10-07 11:37:59 -0700104 const uintptr_t pc_offset = 3 + 2;
Ian Rogersf57c47c2011-10-06 00:06:17 -0700105
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700106 // Create/push fake 16byte stack frame for method g
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700107 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 Rogers0cfe1fb2011-08-26 03:29:44 -0700111
112 // Create/push fake 16byte stack frame for method f
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700113 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 Rogers0cfe1fb2011-08-26 03:29:44 -0700117
118 // Pull Method* of NULL to terminate the trace
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700119 fake_stack.push_back(NULL);
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700120
Ian Rogersbdb03912011-09-14 00:55:44 -0700121 // Set up thread to appear as if we called out of method_g_ at pc 3
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700122 Thread* thread = Thread::Current();
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700123 thread->SetTopOfStack(&fake_stack[0], reinterpret_cast<uintptr_t>(method_g_->GetCode()) + pc_offset); // return pc
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700124
Elliott Hughes01158d72011-09-19 19:47:10 -0700125 JNIEnv* env = thread->GetJniEnv();
126 jobject internal = thread->CreateInternalStackTrace(env);
127 jobjectArray ste_array = Thread::InternalStackTraceToStackTraceElementArray(env, internal);
Ian Rogersaaa20802011-09-11 21:47:37 -0700128 ObjectArray<StackTraceElement>* trace_array =
Elliott Hughes01158d72011-09-19 19:47:10 -0700129 Decode<ObjectArray<StackTraceElement>*>(env, ste_array);
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700130
131 ASSERT_TRUE(trace_array->Get(0) != NULL);
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700132 EXPECT_STREQ("ExceptionHandle",
Shih-wei Liao44175362011-08-28 16:59:17 -0700133 trace_array->Get(0)->GetDeclaringClass()->ToModifiedUtf8().c_str());
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700134 EXPECT_STREQ("ExceptionHandle.java", trace_array->Get(0)->GetFileName()->ToModifiedUtf8().c_str());
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700135 EXPECT_STREQ("g", trace_array->Get(0)->GetMethodName()->ToModifiedUtf8().c_str());
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -0700136 EXPECT_EQ(22, trace_array->Get(0)->GetLineNumber());
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700137
138 ASSERT_TRUE(trace_array->Get(1) != NULL);
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700139 EXPECT_STREQ("ExceptionHandle",
Shih-wei Liao44175362011-08-28 16:59:17 -0700140 trace_array->Get(1)->GetDeclaringClass()->ToModifiedUtf8().c_str());
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700141 EXPECT_STREQ("ExceptionHandle.java", trace_array->Get(1)->GetFileName()->ToModifiedUtf8().c_str());
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700142 EXPECT_STREQ("f", trace_array->Get(1)->GetMethodName()->ToModifiedUtf8().c_str());
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -0700143 EXPECT_EQ(7, trace_array->Get(1)->GetLineNumber());
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700144}
145
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700146} // namespace art