blob: ab462a9c03f64cb14012d72774245766f851aa97 [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_->SetReturnPcOffsetInBytes(kStackAlignment - kPointerSize);
40 method_f_->SetCode(CompiledMethod::CodePointer(&fake_code_[0], kThumb2));
41 method_f_->SetMappingTable(&fake_mapping_data_[0]);
Brian Carlstrom33f741e2011-10-03 11:24:05 -070042
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -070043 method_g_ = my_klass_->FindVirtualMethod("g", "(I)V");
44 ASSERT_TRUE(method_g_ != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070045 method_g_->SetFrameSizeInBytes(kStackAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -070046 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 Liao1a18c8c2011-08-14 17:47:36 -070049 }
50
Brian Carlstrom33f741e2011-10-03 11:24:05 -070051 const DexFile* dex_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -070052
Brian Carlstrom3320cf42011-10-04 14:58:28 -070053 std::vector<uint8_t> fake_code_;
54 std::vector<uint32_t> fake_mapping_data_;
55
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -070056 Method* method_f_;
57 Method* method_g_;
58
59 private:
60 Class* my_klass_;
Shih-wei Liao2fb97532011-08-11 16:17:23 -070061};
62
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -070063TEST_F(ExceptionTest, FindCatchHandler) {
Brian Carlstrom33f741e2011-10-03 11:24:05 -070064 const DexFile::CodeItem* code_item = dex_->GetCodeItem(method_f_->GetCodeItemOffset());
Shih-wei Liao2fb97532011-08-11 16:17:23 -070065
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 Liao1a18c8c2011-08-14 17:47:36 -070072 t0 = dex_->dexGetTryItems(*code_item, 0);
73 t1 = dex_->dexGetTryItems(*code_item, 1);
Shih-wei Liao2fb97532011-08-11 16:17:23 -070074 EXPECT_LE(t0->start_addr_, t1->start_addr_);
75
76 DexFile::CatchHandlerIterator iter =
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -070077 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 Liao2fb97532011-08-11 16:17:23 -070080 iter.Next();
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -070081 ASSERT_EQ(false, iter.HasNext());
82 EXPECT_STREQ("Ljava/lang/Exception;", dex_->dexStringByTypeIdx(iter.Get().type_idx_));
Shih-wei Liao2fb97532011-08-11 16:17:23 -070083 iter.Next();
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -070084 ASSERT_EQ(true, iter.HasNext());
Shih-wei Liao2fb97532011-08-11 16:17:23 -070085
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -070086 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 Liao2fb97532011-08-11 16:17:23 -070089 iter.Next();
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -070090 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 Liao2fb97532011-08-11 16:17:23 -070094}
95
Shih-wei Liao55df06b2011-08-26 14:39:27 -070096TEST_F(ExceptionTest, StackTraceElement) {
Brian Carlstrom25c33252011-09-18 15:58:35 -070097 runtime_->Start();
98
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070099 std::vector<uintptr_t> fake_stack;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700100 ASSERT_EQ(kStackAlignment, 16);
101 ASSERT_EQ(sizeof(uintptr_t), sizeof(uint32_t));
102
Ian Rogersf57c47c2011-10-06 00:06:17 -0700103 // 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 -0700104 // 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 -0700105 // from a return pc.
Elliott Hughesa43cb5e2011-10-07 11:37:59 -0700106 const uintptr_t pc_offset = 3 + 2;
Ian Rogersf57c47c2011-10-06 00:06:17 -0700107
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700108 // Create/push fake 16byte stack frame for method g
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700109 fake_stack.push_back(reinterpret_cast<uintptr_t>(method_g_));
110 fake_stack.push_back(0);
111 fake_stack.push_back(0);
112 fake_stack.push_back(reinterpret_cast<uintptr_t>(method_f_->GetCode()) + pc_offset); // return pc
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700113
114 // Create/push fake 16byte stack frame for method f
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700115 fake_stack.push_back(reinterpret_cast<uintptr_t>(method_f_));
116 fake_stack.push_back(0);
117 fake_stack.push_back(0);
118 fake_stack.push_back(0xEBAD6070); // return pc
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700119
120 // Pull Method* of NULL to terminate the trace
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700121 fake_stack.push_back(NULL);
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700122
Ian Rogersbdb03912011-09-14 00:55:44 -0700123 // Set up thread to appear as if we called out of method_g_ at pc 3
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700124 Thread* thread = Thread::Current();
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700125 thread->SetTopOfStack(&fake_stack[0], reinterpret_cast<uintptr_t>(method_g_->GetCode()) + pc_offset); // return pc
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700126
Elliott Hughes01158d72011-09-19 19:47:10 -0700127 JNIEnv* env = thread->GetJniEnv();
128 jobject internal = thread->CreateInternalStackTrace(env);
129 jobjectArray ste_array = Thread::InternalStackTraceToStackTraceElementArray(env, internal);
Ian Rogersaaa20802011-09-11 21:47:37 -0700130 ObjectArray<StackTraceElement>* trace_array =
Elliott Hughes01158d72011-09-19 19:47:10 -0700131 Decode<ObjectArray<StackTraceElement>*>(env, ste_array);
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700132
133 ASSERT_TRUE(trace_array->Get(0) != NULL);
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700134 EXPECT_STREQ("ExceptionHandle",
Shih-wei Liao44175362011-08-28 16:59:17 -0700135 trace_array->Get(0)->GetDeclaringClass()->ToModifiedUtf8().c_str());
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700136 EXPECT_STREQ("ExceptionHandle.java", trace_array->Get(0)->GetFileName()->ToModifiedUtf8().c_str());
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700137 EXPECT_STREQ("g", trace_array->Get(0)->GetMethodName()->ToModifiedUtf8().c_str());
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -0700138 EXPECT_EQ(22, trace_array->Get(0)->GetLineNumber());
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700139
140 ASSERT_TRUE(trace_array->Get(1) != NULL);
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700141 EXPECT_STREQ("ExceptionHandle",
Shih-wei Liao44175362011-08-28 16:59:17 -0700142 trace_array->Get(1)->GetDeclaringClass()->ToModifiedUtf8().c_str());
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700143 EXPECT_STREQ("ExceptionHandle.java", trace_array->Get(1)->GetFileName()->ToModifiedUtf8().c_str());
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700144 EXPECT_STREQ("f", trace_array->Get(1)->GetMethodName()->ToModifiedUtf8().c_str());
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -0700145 EXPECT_EQ(7, trace_array->Get(1)->GetLineNumber());
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700146}
147
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700148} // namespace art