blob: a1a101da78e88df70c0f37cf1d5787b49b66bca1 [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
5#include "assembler.h"
6#include "class_linker.h"
7#include "common_test.h"
8#include "dex_file.h"
9#include "jni_compiler.h"
10#include "runtime.h"
11#include "thread.h"
12#include "gtest/gtest.h"
13
14namespace art {
15
16// package java.lang;
17// import java.io.IOException;
18// class Object {};
19// public class MyClass {
20// int f() throws Exception {
21// try {
22// g(1);
23// } catch (IOException e) {
24// return 1;
25// } catch (Exception e) {
26// return 2;
27// }
28// try {
29// g(2);
30// } catch (IOException e) {
31// return 3;
32// }
33// return 0;
34// }
35// void g(int doThrow) throws Exception {
36// if (doThrow == 1)
37// throw new Exception();
38// else if (doThrow == 2)
39// throw new IOException();
40// }
41// }
42
43static const char kMyClassExceptionHandleDex[] =
44 "ZGV4CjAzNQC/bXXtLZJLN1GzLr+ncrvPSl70n8t0yAjgAwAAcAAAAHhWNBIAAAAAAAAAACgDAAAN"
45 "AAAAcAAAAAcAAACkAAAAAwAAAMAAAAAAAAAAAAAAAAYAAADkAAAAAgAAABQBAACMAgAAVAEAAD4C"
46 "AABGAgAASQIAAGUCAAB8AgAAkwIAAKgCAAC8AgAAygIAAM0CAADRAgAA1AIAANcCAAABAAAAAgAA"
47 "AAMAAAAEAAAABQAAAAYAAAAIAAAAAQAAAAAAAAAAAAAACAAAAAYAAAAAAAAACQAAAAYAAAA4AgAA"
48 "AgABAAAAAAADAAEAAAAAAAQAAQAAAAAABAAAAAoAAAAEAAIACwAAAAUAAQAAAAAABQAAAAAAAAD/"
49 "////AAAAAAcAAAAAAAAACQMAAAAAAAAEAAAAAQAAAAUAAAAAAAAABwAAABgCAAATAwAAAAAAAAEA"
50 "AAABAwAAAQABAAAAAADeAgAAAQAAAA4AAAABAAEAAQAAAOMCAAAEAAAAcBAFAAAADgAEAAEAAgAC"
51 "AOgCAAAVAAAAEiISERIQbiAEAAMAEiBuIAQAAwASAA8ADQABECj9DQABICj6DQASMCj3AAADAAAA"
52 "AwABAAcAAAADAAYAAgICDAMPAQISAAAAAwACAAEAAAD3AgAAEwAAABIQMwIIACIAAwBwEAEAAAAn"
53 "ABIgMwIIACIAAgBwEAAAAAAnAA4AAAAAAAAAAAAAAAIAAAAAAAAAAwAAAFQBAAAEAAAAVAEAAAEA"
54 "AAAAAAY8aW5pdD4AAUkAGkxkYWx2aWsvYW5ub3RhdGlvbi9UaHJvd3M7ABVMamF2YS9pby9JT0V4"
55 "Y2VwdGlvbjsAFUxqYXZhL2xhbmcvRXhjZXB0aW9uOwATTGphdmEvbGFuZy9NeUNsYXNzOwASTGph"
56 "dmEvbGFuZy9PYmplY3Q7AAxNeUNsYXNzLmphdmEAAVYAAlZJAAFmAAFnAAV2YWx1ZQADAAcOAAQA"
57 "Bw4ABwAHLFFOAnYsLR4tIR4AFQEABw48aTxpAAIBAQwcARgDAAABAAWAgATcAgAAAQICgYAE8AID"
58 "AIgDAQDgAwAAAA8AAAAAAAAAAQAAAAAAAAABAAAADQAAAHAAAAACAAAABwAAAKQAAAADAAAAAwAA"
59 "AMAAAAAFAAAABgAAAOQAAAAGAAAAAgAAABQBAAADEAAAAQAAAFQBAAABIAAABAAAAFwBAAAGIAAA"
60 "AQAAABgCAAABEAAAAQAAADgCAAACIAAADQAAAD4CAAADIAAABAAAAN4CAAAEIAAAAQAAAAEDAAAA"
61 "IAAAAgAAAAkDAAAAEAAAAQAAACgDAAA=";
62
Brian Carlstromf734cf52011-08-17 16:28:14 -070063class ExceptionTest : public CommonTest {
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -070064 protected:
65 virtual void SetUp() {
66 CommonTest::SetUp();
67
68 dex_.reset(OpenDexFileBase64(kMyClassExceptionHandleDex, "kMyClassExceptionHandleDex"));
69 ASSERT_TRUE(dex_ != NULL);
70 PathClassLoader* class_loader = AllocPathClassLoader(dex_.get());
71 ASSERT_TRUE(class_loader != NULL);
72 my_klass_ = class_linker_->FindClass("Ljava/lang/MyClass;", class_loader);
73 ASSERT_TRUE(my_klass_ != NULL);
74 method_f_ = my_klass_->FindVirtualMethod("f", "()I");
75 ASSERT_TRUE(method_f_ != NULL);
Shih-wei Liao55df06b2011-08-26 14:39:27 -070076 method_f_->SetFrameSizeInBytes(8);
77 method_f_->SetReturnPcOffsetInBytes(4);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -070078 method_g_ = my_klass_->FindVirtualMethod("g", "(I)V");
79 ASSERT_TRUE(method_g_ != NULL);
Shih-wei Liao55df06b2011-08-26 14:39:27 -070080 method_g_->SetFrameSizeInBytes(8);
81 method_g_->SetReturnPcOffsetInBytes(4);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -070082 }
83
84 DexFile::CatchHandlerItem FindCatchHandlerItem(Method* method,
85 const char exception_type[],
86 uint32_t addr) {
87 const DexFile::CodeItem* code_item = dex_->GetCodeItem(method->code_off_);
88 for (DexFile::CatchHandlerIterator iter = dex_->dexFindCatchHandler(*code_item, addr);
89 !iter.HasNext(); iter.Next()) {
90 if (strcmp(exception_type, dex_->dexStringByTypeIdx(iter.Get().type_idx_)) == 0) {
91 return iter.Get();
92 }
93 }
94 return DexFile::CatchHandlerItem();
95 }
96
97 scoped_ptr<DexFile> dex_;
98
99 Method* method_f_;
100 Method* method_g_;
101
102 private:
103 Class* my_klass_;
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700104};
105
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700106TEST_F(ExceptionTest, FindCatchHandler) {
107 const DexFile::CodeItem *code_item = dex_->GetCodeItem(method_f_->code_off_);
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700108
109 ASSERT_TRUE(code_item != NULL);
110
111 ASSERT_EQ(2u, code_item->tries_size_);
112 ASSERT_NE(0u, code_item->insns_size_);
113
114 const struct DexFile::TryItem *t0, *t1;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700115 t0 = dex_->dexGetTryItems(*code_item, 0);
116 t1 = dex_->dexGetTryItems(*code_item, 1);
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700117 EXPECT_LE(t0->start_addr_, t1->start_addr_);
118
119 DexFile::CatchHandlerIterator iter =
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700120 dex_->dexFindCatchHandler(*code_item, 4 /* Dex PC in the first try block */);
121 ASSERT_EQ(false, iter.HasNext());
122 EXPECT_STREQ("Ljava/io/IOException;", dex_->dexStringByTypeIdx(iter.Get().type_idx_));
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700123 iter.Next();
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700124 ASSERT_EQ(false, iter.HasNext());
125 EXPECT_STREQ("Ljava/lang/Exception;", dex_->dexStringByTypeIdx(iter.Get().type_idx_));
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700126 iter.Next();
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700127 ASSERT_EQ(true, iter.HasNext());
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700128
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700129 iter = dex_->dexFindCatchHandler(*code_item, 8 /* Dex PC in the second try block */);
130 ASSERT_EQ(false, iter.HasNext());
131 EXPECT_STREQ("Ljava/io/IOException;", dex_->dexStringByTypeIdx(iter.Get().type_idx_));
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700132 iter.Next();
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700133 ASSERT_EQ(true, iter.HasNext());
134
135 iter = dex_->dexFindCatchHandler(*code_item, 11 /* Dex PC not in any try block */);
136 ASSERT_EQ(true, iter.HasNext());
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700137}
138
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700139TEST_F(ExceptionTest, StackTraceElement) {
140 enum {STACK_SIZE = 1000};
141 uint32_t top_of_stack = 0;
142 uintptr_t fake_stack[STACK_SIZE];
143 fake_stack[top_of_stack++] = reinterpret_cast<uintptr_t>(method_g_);
144 fake_stack[top_of_stack++] = 3;
145 fake_stack[top_of_stack++] = reinterpret_cast<uintptr_t>(method_f_);
146 fake_stack[top_of_stack++] = 3;
147
148 Thread* thread = Thread::Current();
149 thread->SetTopOfStack(fake_stack);
150
Shih-wei Liao44175362011-08-28 16:59:17 -0700151 ObjectArray<StackTraceElement>* trace_array = thread->AllocStackTrace(2);
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700152
153 ASSERT_TRUE(trace_array->Get(0) != NULL);
Shih-wei Liao44175362011-08-28 16:59:17 -0700154 EXPECT_STREQ("java.lang.MyClass",
155 trace_array->Get(0)->GetDeclaringClass()->ToModifiedUtf8().c_str());
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700156 EXPECT_STREQ("MyClass.java", trace_array->Get(0)->GetFileName()->ToModifiedUtf8().c_str());
157 EXPECT_STREQ("g", trace_array->Get(0)->GetMethodName()->ToModifiedUtf8().c_str());
158 EXPECT_EQ(22u, trace_array->Get(0)->GetLineNumber());
159
160 ASSERT_TRUE(trace_array->Get(1) != NULL);
Shih-wei Liao44175362011-08-28 16:59:17 -0700161 EXPECT_STREQ("java.lang.MyClass",
162 trace_array->Get(1)->GetDeclaringClass()->ToModifiedUtf8().c_str());
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700163 EXPECT_STREQ("MyClass.java", trace_array->Get(1)->GetFileName()->ToModifiedUtf8().c_str());
164 EXPECT_STREQ("f", trace_array->Get(1)->GetMethodName()->ToModifiedUtf8().c_str());
165 EXPECT_EQ(7u, trace_array->Get(1)->GetLineNumber());
166}
167
168// TODO: Test with native frame: For native frame, lineno should be -2 to
169// indicate it is native. That is how libcore tells from the StackTraceElement.
170
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700171} // namespace art