blob: 3611b6722dff92f0338de4b4a4796d33a3a20dfd [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 Carlstrom8a487412011-08-29 20:08:52 -070071 const PathClassLoader* 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);
75 method_f_ = my_klass_->FindVirtualMethod("f", "()I");
76 ASSERT_TRUE(method_f_ != NULL);
Shih-wei Liao55df06b2011-08-26 14:39:27 -070077 method_f_->SetFrameSizeInBytes(8);
78 method_f_->SetReturnPcOffsetInBytes(4);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -070079 method_g_ = my_klass_->FindVirtualMethod("g", "(I)V");
80 ASSERT_TRUE(method_g_ != NULL);
Shih-wei Liao55df06b2011-08-26 14:39:27 -070081 method_g_->SetFrameSizeInBytes(8);
82 method_g_->SetReturnPcOffsetInBytes(4);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -070083 }
84
85 DexFile::CatchHandlerItem FindCatchHandlerItem(Method* method,
86 const char exception_type[],
87 uint32_t addr) {
88 const DexFile::CodeItem* code_item = dex_->GetCodeItem(method->code_off_);
89 for (DexFile::CatchHandlerIterator iter = dex_->dexFindCatchHandler(*code_item, addr);
90 !iter.HasNext(); iter.Next()) {
91 if (strcmp(exception_type, dex_->dexStringByTypeIdx(iter.Get().type_idx_)) == 0) {
92 return iter.Get();
93 }
94 }
95 return DexFile::CatchHandlerItem();
96 }
97
Elliott Hughes90a33692011-08-30 13:27:07 -070098 UniquePtr<const DexFile> dex_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -070099
100 Method* method_f_;
101 Method* method_g_;
102
103 private:
104 Class* my_klass_;
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700105};
106
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700107TEST_F(ExceptionTest, FindCatchHandler) {
108 const DexFile::CodeItem *code_item = dex_->GetCodeItem(method_f_->code_off_);
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700109
110 ASSERT_TRUE(code_item != NULL);
111
112 ASSERT_EQ(2u, code_item->tries_size_);
113 ASSERT_NE(0u, code_item->insns_size_);
114
115 const struct DexFile::TryItem *t0, *t1;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700116 t0 = dex_->dexGetTryItems(*code_item, 0);
117 t1 = dex_->dexGetTryItems(*code_item, 1);
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700118 EXPECT_LE(t0->start_addr_, t1->start_addr_);
119
120 DexFile::CatchHandlerIterator iter =
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700121 dex_->dexFindCatchHandler(*code_item, 4 /* Dex PC in the first try block */);
122 ASSERT_EQ(false, iter.HasNext());
123 EXPECT_STREQ("Ljava/io/IOException;", 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(false, iter.HasNext());
126 EXPECT_STREQ("Ljava/lang/Exception;", dex_->dexStringByTypeIdx(iter.Get().type_idx_));
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700127 iter.Next();
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700128 ASSERT_EQ(true, iter.HasNext());
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700129
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700130 iter = dex_->dexFindCatchHandler(*code_item, 8 /* Dex PC in the second try block */);
131 ASSERT_EQ(false, iter.HasNext());
132 EXPECT_STREQ("Ljava/io/IOException;", dex_->dexStringByTypeIdx(iter.Get().type_idx_));
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700133 iter.Next();
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700134 ASSERT_EQ(true, iter.HasNext());
135
136 iter = dex_->dexFindCatchHandler(*code_item, 11 /* Dex PC not in any try block */);
137 ASSERT_EQ(true, iter.HasNext());
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700138}
139
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700140TEST_F(ExceptionTest, StackTraceElement) {
141 enum {STACK_SIZE = 1000};
142 uint32_t top_of_stack = 0;
143 uintptr_t fake_stack[STACK_SIZE];
144 fake_stack[top_of_stack++] = reinterpret_cast<uintptr_t>(method_g_);
145 fake_stack[top_of_stack++] = 3;
146 fake_stack[top_of_stack++] = reinterpret_cast<uintptr_t>(method_f_);
147 fake_stack[top_of_stack++] = 3;
Shih-wei Liao9b576b42011-08-29 01:45:07 -0700148 fake_stack[top_of_stack++] = NULL;
149 fake_stack[top_of_stack++] = 0;
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700150
151 Thread* thread = Thread::Current();
152 thread->SetTopOfStack(fake_stack);
153
Shih-wei Liao9b576b42011-08-29 01:45:07 -0700154 ObjectArray<StackTraceElement>* trace_array = thread->AllocStackTrace();
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700155
156 ASSERT_TRUE(trace_array->Get(0) != NULL);
Shih-wei Liao44175362011-08-28 16:59:17 -0700157 EXPECT_STREQ("java.lang.MyClass",
158 trace_array->Get(0)->GetDeclaringClass()->ToModifiedUtf8().c_str());
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700159 EXPECT_STREQ("MyClass.java", trace_array->Get(0)->GetFileName()->ToModifiedUtf8().c_str());
160 EXPECT_STREQ("g", trace_array->Get(0)->GetMethodName()->ToModifiedUtf8().c_str());
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -0700161 EXPECT_EQ(22, trace_array->Get(0)->GetLineNumber());
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700162
163 ASSERT_TRUE(trace_array->Get(1) != NULL);
Shih-wei Liao44175362011-08-28 16:59:17 -0700164 EXPECT_STREQ("java.lang.MyClass",
165 trace_array->Get(1)->GetDeclaringClass()->ToModifiedUtf8().c_str());
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700166 EXPECT_STREQ("MyClass.java", trace_array->Get(1)->GetFileName()->ToModifiedUtf8().c_str());
167 EXPECT_STREQ("f", trace_array->Get(1)->GetMethodName()->ToModifiedUtf8().c_str());
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -0700168 EXPECT_EQ(7, trace_array->Get(1)->GetLineNumber());
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700169}
170
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700171} // namespace art