blob: 2e9cf3feb6fc509f10975f731540e514f43601bb [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
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
44static const char kMyClassExceptionHandleDex[] =
45 "ZGV4CjAzNQC/bXXtLZJLN1GzLr+ncrvPSl70n8t0yAjgAwAAcAAAAHhWNBIAAAAAAAAAACgDAAAN"
46 "AAAAcAAAAAcAAACkAAAAAwAAAMAAAAAAAAAAAAAAAAYAAADkAAAAAgAAABQBAACMAgAAVAEAAD4C"
47 "AABGAgAASQIAAGUCAAB8AgAAkwIAAKgCAAC8AgAAygIAAM0CAADRAgAA1AIAANcCAAABAAAAAgAA"
48 "AAMAAAAEAAAABQAAAAYAAAAIAAAAAQAAAAAAAAAAAAAACAAAAAYAAAAAAAAACQAAAAYAAAA4AgAA"
49 "AgABAAAAAAADAAEAAAAAAAQAAQAAAAAABAAAAAoAAAAEAAIACwAAAAUAAQAAAAAABQAAAAAAAAD/"
50 "////AAAAAAcAAAAAAAAACQMAAAAAAAAEAAAAAQAAAAUAAAAAAAAABwAAABgCAAATAwAAAAAAAAEA"
51 "AAABAwAAAQABAAAAAADeAgAAAQAAAA4AAAABAAEAAQAAAOMCAAAEAAAAcBAFAAAADgAEAAEAAgAC"
52 "AOgCAAAVAAAAEiISERIQbiAEAAMAEiBuIAQAAwASAA8ADQABECj9DQABICj6DQASMCj3AAADAAAA"
53 "AwABAAcAAAADAAYAAgICDAMPAQISAAAAAwACAAEAAAD3AgAAEwAAABIQMwIIACIAAwBwEAEAAAAn"
54 "ABIgMwIIACIAAgBwEAAAAAAnAA4AAAAAAAAAAAAAAAIAAAAAAAAAAwAAAFQBAAAEAAAAVAEAAAEA"
55 "AAAAAAY8aW5pdD4AAUkAGkxkYWx2aWsvYW5ub3RhdGlvbi9UaHJvd3M7ABVMamF2YS9pby9JT0V4"
56 "Y2VwdGlvbjsAFUxqYXZhL2xhbmcvRXhjZXB0aW9uOwATTGphdmEvbGFuZy9NeUNsYXNzOwASTGph"
57 "dmEvbGFuZy9PYmplY3Q7AAxNeUNsYXNzLmphdmEAAVYAAlZJAAFmAAFnAAV2YWx1ZQADAAcOAAQA"
58 "Bw4ABwAHLFFOAnYsLR4tIR4AFQEABw48aTxpAAIBAQwcARgDAAABAAWAgATcAgAAAQICgYAE8AID"
59 "AIgDAQDgAwAAAA8AAAAAAAAAAQAAAAAAAAABAAAADQAAAHAAAAACAAAABwAAAKQAAAADAAAAAwAA"
60 "AMAAAAAFAAAABgAAAOQAAAAGAAAAAgAAABQBAAADEAAAAQAAAFQBAAABIAAABAAAAFwBAAAGIAAA"
61 "AQAAABgCAAABEAAAAQAAADgCAAACIAAADQAAAD4CAAADIAAABAAAAN4CAAAEIAAAAQAAAAEDAAAA"
62 "IAAAAgAAAAkDAAAAEAAAAQAAACgDAAA=";
63
Brian Carlstromf734cf52011-08-17 16:28:14 -070064class ExceptionTest : public CommonTest {
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -070065 protected:
66 virtual void SetUp() {
67 CommonTest::SetUp();
68
69 dex_.reset(OpenDexFileBase64(kMyClassExceptionHandleDex, "kMyClassExceptionHandleDex"));
Elliott Hughes90a33692011-08-30 13:27:07 -070070 ASSERT_TRUE(dex_.get() != NULL);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070071 const ClassLoader* class_loader = AllocPathClassLoader(dex_.get());
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -070072 ASSERT_TRUE(class_loader != NULL);
73 my_klass_ = class_linker_->FindClass("Ljava/lang/MyClass;", class_loader);
74 ASSERT_TRUE(my_klass_ != NULL);
Ian Rogersbdb03912011-09-14 00:55:44 -070075 ByteArray* fake_code = ByteArray::Alloc(12);
76 ASSERT_TRUE(fake_code != NULL);
77 IntArray* fake_mapping_data = IntArray::Alloc(2);
Brian Carlstrome24fa612011-09-29 00:53:55 -070078 ASSERT_TRUE(fake_mapping_data != NULL);
Ian Rogersbdb03912011-09-14 00:55:44 -070079 fake_mapping_data->Set(0, 3); // offset 3
80 fake_mapping_data->Set(1, 3); // maps to dex offset 3
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -070081 method_f_ = my_klass_->FindVirtualMethod("f", "()I");
82 ASSERT_TRUE(method_f_ != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070083 method_f_->SetFrameSizeInBytes(kStackAlignment);
Ian Rogersbdb03912011-09-14 00:55:44 -070084 method_f_->SetReturnPcOffsetInBytes(kStackAlignment-kPointerSize);
Brian Carlstrome24fa612011-09-29 00:53:55 -070085 method_f_->SetCodeArray(fake_code, kThumb2);
86 method_f_->SetMappingTable(fake_mapping_data);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -070087 method_g_ = my_klass_->FindVirtualMethod("g", "(I)V");
88 ASSERT_TRUE(method_g_ != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070089 method_g_->SetFrameSizeInBytes(kStackAlignment);
Ian Rogersbdb03912011-09-14 00:55:44 -070090 method_g_->SetReturnPcOffsetInBytes(kStackAlignment-kPointerSize);
Brian Carlstrome24fa612011-09-29 00:53:55 -070091 method_g_->SetCodeArray(fake_code, kThumb2);
92 method_g_->SetMappingTable(fake_mapping_data);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -070093 }
94
Elliott Hughes90a33692011-08-30 13:27:07 -070095 UniquePtr<const DexFile> dex_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -070096
97 Method* method_f_;
98 Method* method_g_;
99
100 private:
101 Class* my_klass_;
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700102};
103
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700104TEST_F(ExceptionTest, FindCatchHandler) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700105 const DexFile::CodeItem *code_item = dex_->GetCodeItem(method_f_->GetCodeItemOffset());
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700106
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 Liao1a18c8c2011-08-14 17:47:36 -0700113 t0 = dex_->dexGetTryItems(*code_item, 0);
114 t1 = dex_->dexGetTryItems(*code_item, 1);
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700115 EXPECT_LE(t0->start_addr_, t1->start_addr_);
116
117 DexFile::CatchHandlerIterator iter =
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700118 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 Liao2fb97532011-08-11 16:17:23 -0700121 iter.Next();
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700122 ASSERT_EQ(false, iter.HasNext());
123 EXPECT_STREQ("Ljava/lang/Exception;", dex_->dexStringByTypeIdx(iter.Get().type_idx_));
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700124 iter.Next();
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700125 ASSERT_EQ(true, iter.HasNext());
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700126
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700127 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 Liao2fb97532011-08-11 16:17:23 -0700130 iter.Next();
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700131 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 Liao2fb97532011-08-11 16:17:23 -0700135}
136
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700137TEST_F(ExceptionTest, StackTraceElement) {
Brian Carlstrom25c33252011-09-18 15:58:35 -0700138 runtime_->Start();
139
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700140 enum {STACK_SIZE = 1000};
141 uint32_t top_of_stack = 0;
142 uintptr_t fake_stack[STACK_SIZE];
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700143 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 Liao55df06b2011-08-26 14:39:27 -0700147 fake_stack[top_of_stack++] = reinterpret_cast<uintptr_t>(method_g_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700148 fake_stack[top_of_stack++] = 0;
149 fake_stack[top_of_stack++] = 0;
Ian Rogersbdb03912011-09-14 00:55:44 -0700150 fake_stack[top_of_stack++] = reinterpret_cast<uintptr_t>(method_f_->GetCode()) + 3; // return pc
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700151
152 // Create/push fake 16byte stack frame for method f
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700153 fake_stack[top_of_stack++] = reinterpret_cast<uintptr_t>(method_f_);
Shih-wei Liao9b576b42011-08-29 01:45:07 -0700154 fake_stack[top_of_stack++] = 0;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700155 fake_stack[top_of_stack++] = 0;
Ian Rogersbdb03912011-09-14 00:55:44 -0700156 fake_stack[top_of_stack++] = 0xEBAD6070; // return pc
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700157
158 // Pull Method* of NULL to terminate the trace
159 fake_stack[top_of_stack++] = NULL;
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700160
Ian Rogersbdb03912011-09-14 00:55:44 -0700161 // Set up thread to appear as if we called out of method_g_ at pc 3
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700162 Thread* thread = Thread::Current();
Ian Rogersbdb03912011-09-14 00:55:44 -0700163 thread->SetTopOfStack(fake_stack, reinterpret_cast<uintptr_t>(method_g_->GetCode()) + 3);
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700164
Elliott Hughes01158d72011-09-19 19:47:10 -0700165 JNIEnv* env = thread->GetJniEnv();
166 jobject internal = thread->CreateInternalStackTrace(env);
167 jobjectArray ste_array = Thread::InternalStackTraceToStackTraceElementArray(env, internal);
Ian Rogersaaa20802011-09-11 21:47:37 -0700168 ObjectArray<StackTraceElement>* trace_array =
Elliott Hughes01158d72011-09-19 19:47:10 -0700169 Decode<ObjectArray<StackTraceElement>*>(env, ste_array);
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700170
171 ASSERT_TRUE(trace_array->Get(0) != NULL);
Shih-wei Liao44175362011-08-28 16:59:17 -0700172 EXPECT_STREQ("java.lang.MyClass",
173 trace_array->Get(0)->GetDeclaringClass()->ToModifiedUtf8().c_str());
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700174 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 Liaoff0f9be2011-08-29 15:43:53 -0700176 EXPECT_EQ(22, trace_array->Get(0)->GetLineNumber());
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700177
178 ASSERT_TRUE(trace_array->Get(1) != NULL);
Shih-wei Liao44175362011-08-28 16:59:17 -0700179 EXPECT_STREQ("java.lang.MyClass",
180 trace_array->Get(1)->GetDeclaringClass()->ToModifiedUtf8().c_str());
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700181 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 Liaoff0f9be2011-08-29 15:43:53 -0700183 EXPECT_EQ(7, trace_array->Get(1)->GetLineNumber());
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700184}
185
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700186} // namespace art