blob: 4bc910adf34a4746b07ddaf6643a6a3f8a72ef40 [file] [log] [blame]
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "greenland/runtime_entry_points.h"
18
19#include "nth_caller_visitor.h"
20#include "runtime_utils.h"
21#include "runtime_support.h"
22
23using namespace art;
24using namespace art::greenland;
25
26namespace {
27
Mathieu Chartier66f19252012-09-18 08:57:04 -070028int32_t art_find_catch_block(AbstractMethod* current_method, uint32_t ti_offset) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -070029 Thread* thread = art_get_current_thread();
30 Class* exception_type = thread->GetException()->GetClass();
31 MethodHelper mh(current_method);
32 const DexFile::CodeItem* code_item = mh.GetCodeItem();
33 DCHECK_LT(ti_offset, code_item->tries_size_);
34 const DexFile::TryItem* try_item = DexFile::GetTryItems(*code_item, ti_offset);
35
36 int iter_index = 0;
37 // Iterate over the catch handlers associated with dex_pc
38 for (CatchHandlerIterator it(*code_item, *try_item); it.HasNext(); it.Next()) {
39 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
40 // Catch all case
41 if (iter_type_idx == DexFile::kDexNoIndex16) {
42 return iter_index;
43 }
44 // Does this catch exception type apply?
45 Class* iter_exception_type = mh.GetDexCacheResolvedType(iter_type_idx);
46 if (iter_exception_type == NULL) {
47 // The verifier should take care of resolving all exception classes early
48 LOG(WARNING) << "Unresolved exception class when finding catch block: "
49 << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx);
50 } else if (iter_exception_type->IsAssignableFrom(exception_type)) {
51 return iter_index;
52 }
53 ++iter_index;
54 }
55 // Handler not found
56 return -1;
57}
58
59void art_throw_array_bounds(int32_t length, int32_t index) {
60 Thread* thread = art_get_current_thread();
61 thread->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
62 "length=%d; index=%d", length, index);
63}
64
65void art_throw_null_pointer_exception(uint32_t dex_pc) {
66 Thread* thread = art_get_current_thread();
67 NthCallerVisitor visitor(0);
68 thread->WalkStack(&visitor);
Mathieu Chartier66f19252012-09-18 08:57:04 -070069 AbstractMethod* throw_method = visitor.caller;
Shih-wei Liao21d28f52012-06-12 05:55:00 -070070 ThrowNullPointerExceptionFromDexPC(thread, throw_method, dex_pc);
71}
72
73} // anonymous namespace
74
75namespace art {
76namespace greenland {
77
78void InitExceptionRuntimes(RuntimeEntryPoints* entry_points) {
79 entry_points->FindCatchBlock = art_find_catch_block;
80 entry_points->ThrowIndexOutOfBounds = art_throw_array_bounds;
81 entry_points->ThrowNullPointerException = art_throw_null_pointer_exception;
82}
83
84} // namespace greenland
85} // namespace art