Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 "unstarted_runtime.h" |
| 18 | |
| 19 | #include <cmath> |
| 20 | #include <unordered_map> |
| 21 | |
Andreas Gampe | aacc25d | 2015-04-01 14:49:06 -0700 | [diff] [blame] | 22 | #include "ScopedLocalRef.h" |
| 23 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 24 | #include "art_method-inl.h" |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 25 | #include "base/logging.h" |
| 26 | #include "base/macros.h" |
| 27 | #include "class_linker.h" |
| 28 | #include "common_throws.h" |
| 29 | #include "entrypoints/entrypoint_utils-inl.h" |
| 30 | #include "handle_scope-inl.h" |
| 31 | #include "interpreter/interpreter_common.h" |
| 32 | #include "mirror/array-inl.h" |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 33 | #include "mirror/class.h" |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 34 | #include "mirror/field-inl.h" |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 35 | #include "mirror/object-inl.h" |
| 36 | #include "mirror/object_array-inl.h" |
| 37 | #include "mirror/string-inl.h" |
| 38 | #include "nth_caller_visitor.h" |
| 39 | #include "thread.h" |
Sebastien Hertz | 2fd7e69 | 2015-04-02 11:11:19 +0200 | [diff] [blame] | 40 | #include "transaction.h" |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 41 | #include "well_known_classes.h" |
Andreas Gampe | f778eb2 | 2015-04-13 14:17:09 -0700 | [diff] [blame] | 42 | #include "zip_archive.h" |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 43 | |
| 44 | namespace art { |
| 45 | namespace interpreter { |
| 46 | |
Andreas Gampe | 068b0c0 | 2015-03-11 12:44:47 -0700 | [diff] [blame] | 47 | static void AbortTransactionOrFail(Thread* self, const char* fmt, ...) |
Sebastien Hertz | 45b1597 | 2015-04-03 16:07:05 +0200 | [diff] [blame] | 48 | __attribute__((__format__(__printf__, 2, 3))) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 49 | SHARED_REQUIRES(Locks::mutator_lock_); |
Sebastien Hertz | 45b1597 | 2015-04-03 16:07:05 +0200 | [diff] [blame] | 50 | |
| 51 | static void AbortTransactionOrFail(Thread* self, const char* fmt, ...) { |
Andreas Gampe | 068b0c0 | 2015-03-11 12:44:47 -0700 | [diff] [blame] | 52 | va_list args; |
Andreas Gampe | 068b0c0 | 2015-03-11 12:44:47 -0700 | [diff] [blame] | 53 | if (Runtime::Current()->IsActiveTransaction()) { |
Sebastien Hertz | 45b1597 | 2015-04-03 16:07:05 +0200 | [diff] [blame] | 54 | va_start(args, fmt); |
| 55 | AbortTransactionV(self, fmt, args); |
Andreas Gampe | 068b0c0 | 2015-03-11 12:44:47 -0700 | [diff] [blame] | 56 | va_end(args); |
| 57 | } else { |
Sebastien Hertz | 45b1597 | 2015-04-03 16:07:05 +0200 | [diff] [blame] | 58 | va_start(args, fmt); |
| 59 | std::string msg; |
| 60 | StringAppendV(&msg, fmt, args); |
| 61 | va_end(args); |
| 62 | LOG(FATAL) << "Trying to abort, but not in transaction mode: " << msg; |
Andreas Gampe | 068b0c0 | 2015-03-11 12:44:47 -0700 | [diff] [blame] | 63 | UNREACHABLE(); |
| 64 | } |
| 65 | } |
| 66 | |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 67 | // Helper function to deal with class loading in an unstarted runtime. |
| 68 | static void UnstartedRuntimeFindClass(Thread* self, Handle<mirror::String> className, |
| 69 | Handle<mirror::ClassLoader> class_loader, JValue* result, |
| 70 | const std::string& method_name, bool initialize_class, |
| 71 | bool abort_if_not_found) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 72 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 73 | CHECK(className.Get() != nullptr); |
| 74 | std::string descriptor(DotToDescriptor(className->ToModifiedUtf8().c_str())); |
| 75 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 76 | |
| 77 | mirror::Class* found = class_linker->FindClass(self, descriptor.c_str(), class_loader); |
| 78 | if (found == nullptr && abort_if_not_found) { |
| 79 | if (!self->IsExceptionPending()) { |
Andreas Gampe | 068b0c0 | 2015-03-11 12:44:47 -0700 | [diff] [blame] | 80 | AbortTransactionOrFail(self, "%s failed in un-started runtime for class: %s", |
| 81 | method_name.c_str(), PrettyDescriptor(descriptor.c_str()).c_str()); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 82 | } |
| 83 | return; |
| 84 | } |
| 85 | if (found != nullptr && initialize_class) { |
| 86 | StackHandleScope<1> hs(self); |
| 87 | Handle<mirror::Class> h_class(hs.NewHandle(found)); |
| 88 | if (!class_linker->EnsureInitialized(self, h_class, true, true)) { |
| 89 | CHECK(self->IsExceptionPending()); |
| 90 | return; |
| 91 | } |
| 92 | } |
| 93 | result->SetL(found); |
| 94 | } |
| 95 | |
| 96 | // Common helper for class-loading cutouts in an unstarted runtime. We call Runtime methods that |
| 97 | // rely on Java code to wrap errors in the correct exception class (i.e., NoClassDefFoundError into |
| 98 | // ClassNotFoundException), so need to do the same. The only exception is if the exception is |
Sebastien Hertz | 2fd7e69 | 2015-04-02 11:11:19 +0200 | [diff] [blame] | 99 | // actually the transaction abort exception. This must not be wrapped, as it signals an |
| 100 | // initialization abort. |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 101 | static void CheckExceptionGenerateClassNotFound(Thread* self) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 102 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 103 | if (self->IsExceptionPending()) { |
Sebastien Hertz | 2fd7e69 | 2015-04-02 11:11:19 +0200 | [diff] [blame] | 104 | // If it is not the transaction abort exception, wrap it. |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 105 | std::string type(PrettyTypeOf(self->GetException())); |
Sebastien Hertz | 2fd7e69 | 2015-04-02 11:11:19 +0200 | [diff] [blame] | 106 | if (type != Transaction::kAbortExceptionDescriptor) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 107 | self->ThrowNewWrappedException("Ljava/lang/ClassNotFoundException;", |
| 108 | "ClassNotFoundException"); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
Andreas Gampe | 5d4bb1d | 2015-04-14 22:16:14 -0700 | [diff] [blame] | 113 | static mirror::String* GetClassName(Thread* self, ShadowFrame* shadow_frame, size_t arg_offset) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 114 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Andreas Gampe | 5d4bb1d | 2015-04-14 22:16:14 -0700 | [diff] [blame] | 115 | mirror::Object* param = shadow_frame->GetVRegReference(arg_offset); |
| 116 | if (param == nullptr) { |
| 117 | AbortTransactionOrFail(self, "Null-pointer in Class.forName."); |
| 118 | return nullptr; |
| 119 | } |
| 120 | return param->AsString(); |
| 121 | } |
| 122 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 123 | void UnstartedRuntime::UnstartedClassForName( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 124 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
Andreas Gampe | 5d4bb1d | 2015-04-14 22:16:14 -0700 | [diff] [blame] | 125 | mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset); |
| 126 | if (class_name == nullptr) { |
| 127 | return; |
| 128 | } |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 129 | StackHandleScope<1> hs(self); |
| 130 | Handle<mirror::String> h_class_name(hs.NewHandle(class_name)); |
| 131 | UnstartedRuntimeFindClass(self, h_class_name, NullHandle<mirror::ClassLoader>(), result, |
| 132 | "Class.forName", true, false); |
| 133 | CheckExceptionGenerateClassNotFound(self); |
| 134 | } |
| 135 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 136 | void UnstartedRuntime::UnstartedClassForNameLong( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 137 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
Andreas Gampe | 5d4bb1d | 2015-04-14 22:16:14 -0700 | [diff] [blame] | 138 | mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset); |
| 139 | if (class_name == nullptr) { |
Andreas Gampe | bf4d3af | 2015-04-14 10:10:33 -0700 | [diff] [blame] | 140 | return; |
| 141 | } |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 142 | bool initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0; |
| 143 | mirror::ClassLoader* class_loader = |
| 144 | down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2)); |
| 145 | StackHandleScope<2> hs(self); |
| 146 | Handle<mirror::String> h_class_name(hs.NewHandle(class_name)); |
| 147 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); |
| 148 | UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, "Class.forName", |
| 149 | initialize_class, false); |
| 150 | CheckExceptionGenerateClassNotFound(self); |
| 151 | } |
| 152 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 153 | void UnstartedRuntime::UnstartedClassClassForName( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 154 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
Andreas Gampe | 5d4bb1d | 2015-04-14 22:16:14 -0700 | [diff] [blame] | 155 | mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset); |
| 156 | if (class_name == nullptr) { |
| 157 | return; |
| 158 | } |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 159 | bool initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0; |
| 160 | mirror::ClassLoader* class_loader = |
| 161 | down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2)); |
| 162 | StackHandleScope<2> hs(self); |
| 163 | Handle<mirror::String> h_class_name(hs.NewHandle(class_name)); |
| 164 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); |
| 165 | UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, "Class.classForName", |
| 166 | initialize_class, false); |
| 167 | CheckExceptionGenerateClassNotFound(self); |
| 168 | } |
| 169 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 170 | void UnstartedRuntime::UnstartedClassNewInstance( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 171 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
| 172 | StackHandleScope<2> hs(self); // Class, constructor, object. |
Andreas Gampe | 5d4bb1d | 2015-04-14 22:16:14 -0700 | [diff] [blame] | 173 | mirror::Object* param = shadow_frame->GetVRegReference(arg_offset); |
| 174 | if (param == nullptr) { |
| 175 | AbortTransactionOrFail(self, "Null-pointer in Class.newInstance."); |
| 176 | return; |
| 177 | } |
| 178 | mirror::Class* klass = param->AsClass(); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 179 | Handle<mirror::Class> h_klass(hs.NewHandle(klass)); |
Andreas Gampe | 0f7e3d6 | 2015-03-11 13:24:35 -0700 | [diff] [blame] | 180 | |
| 181 | // Check that it's not null. |
| 182 | if (h_klass.Get() == nullptr) { |
| 183 | AbortTransactionOrFail(self, "Class reference is null for newInstance"); |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | // If we're in a transaction, class must not be finalizable (it or a superclass has a finalizer). |
| 188 | if (Runtime::Current()->IsActiveTransaction()) { |
| 189 | if (h_klass.Get()->IsFinalizable()) { |
Sebastien Hertz | 45b1597 | 2015-04-03 16:07:05 +0200 | [diff] [blame] | 190 | AbortTransactionF(self, "Class for newInstance is finalizable: '%s'", |
| 191 | PrettyClass(h_klass.Get()).c_str()); |
Andreas Gampe | 0f7e3d6 | 2015-03-11 13:24:35 -0700 | [diff] [blame] | 192 | return; |
| 193 | } |
| 194 | } |
| 195 | |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 196 | // There are two situations in which we'll abort this run. |
| 197 | // 1) If the class isn't yet initialized and initialization fails. |
| 198 | // 2) If we can't find the default constructor. We'll postpone the exception to runtime. |
| 199 | // Note that 2) could likely be handled here, but for safety abort the transaction. |
| 200 | bool ok = false; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 201 | auto* cl = Runtime::Current()->GetClassLinker(); |
| 202 | if (cl->EnsureInitialized(self, h_klass, true, true)) { |
| 203 | auto* cons = h_klass->FindDeclaredDirectMethod("<init>", "()V", cl->GetImagePointerSize()); |
| 204 | if (cons != nullptr) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 205 | Handle<mirror::Object> h_obj(hs.NewHandle(klass->AllocObject(self))); |
| 206 | CHECK(h_obj.Get() != nullptr); // We don't expect OOM at compile-time. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 207 | EnterInterpreterFromInvoke(self, cons, h_obj.Get(), nullptr, nullptr); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 208 | if (!self->IsExceptionPending()) { |
| 209 | result->SetL(h_obj.Get()); |
| 210 | ok = true; |
| 211 | } |
| 212 | } else { |
| 213 | self->ThrowNewExceptionF("Ljava/lang/InternalError;", |
| 214 | "Could not find default constructor for '%s'", |
| 215 | PrettyClass(h_klass.Get()).c_str()); |
| 216 | } |
| 217 | } |
| 218 | if (!ok) { |
Andreas Gampe | 068b0c0 | 2015-03-11 12:44:47 -0700 | [diff] [blame] | 219 | AbortTransactionOrFail(self, "Failed in Class.newInstance for '%s' with %s", |
| 220 | PrettyClass(h_klass.Get()).c_str(), |
| 221 | PrettyTypeOf(self->GetException()).c_str()); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 225 | void UnstartedRuntime::UnstartedClassGetDeclaredField( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 226 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 227 | // Special managed code cut-out to allow field lookup in a un-started runtime that'd fail |
| 228 | // going the reflective Dex way. |
| 229 | mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass(); |
| 230 | mirror::String* name2 = shadow_frame->GetVRegReference(arg_offset + 1)->AsString(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 231 | ArtField* found = nullptr; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame^] | 232 | for (ArtField& field : klass->GetIFields()) { |
| 233 | if (name2->Equals(field.GetName())) { |
| 234 | found = &field; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 235 | break; |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | if (found == nullptr) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame^] | 239 | for (ArtField& field : klass->GetSFields()) { |
| 240 | if (name2->Equals(field.GetName())) { |
| 241 | found = &field; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 242 | break; |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | } |
Andreas Gampe | 068b0c0 | 2015-03-11 12:44:47 -0700 | [diff] [blame] | 246 | if (found == nullptr) { |
| 247 | AbortTransactionOrFail(self, "Failed to find field in Class.getDeclaredField in un-started " |
| 248 | " runtime. name=%s class=%s", name2->ToModifiedUtf8().c_str(), |
| 249 | PrettyDescriptor(klass).c_str()); |
| 250 | return; |
| 251 | } |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 252 | if (Runtime::Current()->IsActiveTransaction()) { |
| 253 | result->SetL(mirror::Field::CreateFromArtField<true>(self, found, true)); |
| 254 | } else { |
| 255 | result->SetL(mirror::Field::CreateFromArtField<false>(self, found, true)); |
| 256 | } |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 259 | void UnstartedRuntime::UnstartedVmClassLoaderFindLoadedClass( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 260 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 261 | mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString(); |
| 262 | mirror::ClassLoader* class_loader = |
| 263 | down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset)); |
| 264 | StackHandleScope<2> hs(self); |
| 265 | Handle<mirror::String> h_class_name(hs.NewHandle(class_name)); |
| 266 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); |
| 267 | UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, |
| 268 | "VMClassLoader.findLoadedClass", false, false); |
| 269 | // This might have an error pending. But semantics are to just return null. |
| 270 | if (self->IsExceptionPending()) { |
| 271 | // If it is an InternalError, keep it. See CheckExceptionGenerateClassNotFound. |
| 272 | std::string type(PrettyTypeOf(self->GetException())); |
| 273 | if (type != "java.lang.InternalError") { |
| 274 | self->ClearException(); |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 279 | void UnstartedRuntime::UnstartedVoidLookupType( |
| 280 | Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED, JValue* result, |
| 281 | size_t arg_offset ATTRIBUTE_UNUSED) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 282 | result->SetL(Runtime::Current()->GetClassLinker()->FindPrimitiveClass('V')); |
| 283 | } |
| 284 | |
Andreas Gampe | 8e6c3fd | 2015-03-11 18:34:44 -0700 | [diff] [blame] | 285 | // Arraycopy emulation. |
| 286 | // Note: we can't use any fast copy functions, as they are not available under transaction. |
| 287 | |
| 288 | template <typename T> |
| 289 | static void PrimitiveArrayCopy(Thread* self, |
| 290 | mirror::Array* src_array, int32_t src_pos, |
| 291 | mirror::Array* dst_array, int32_t dst_pos, |
| 292 | int32_t length) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 293 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Andreas Gampe | 8e6c3fd | 2015-03-11 18:34:44 -0700 | [diff] [blame] | 294 | if (src_array->GetClass()->GetComponentType() != dst_array->GetClass()->GetComponentType()) { |
| 295 | AbortTransactionOrFail(self, "Types mismatched in arraycopy: %s vs %s.", |
| 296 | PrettyDescriptor(src_array->GetClass()->GetComponentType()).c_str(), |
| 297 | PrettyDescriptor(dst_array->GetClass()->GetComponentType()).c_str()); |
| 298 | return; |
| 299 | } |
| 300 | mirror::PrimitiveArray<T>* src = down_cast<mirror::PrimitiveArray<T>*>(src_array); |
| 301 | mirror::PrimitiveArray<T>* dst = down_cast<mirror::PrimitiveArray<T>*>(dst_array); |
| 302 | const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length); |
| 303 | if (copy_forward) { |
| 304 | for (int32_t i = 0; i < length; ++i) { |
| 305 | dst->Set(dst_pos + i, src->Get(src_pos + i)); |
| 306 | } |
| 307 | } else { |
| 308 | for (int32_t i = 1; i <= length; ++i) { |
| 309 | dst->Set(dst_pos + length - i, src->Get(src_pos + length - i)); |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 314 | void UnstartedRuntime::UnstartedSystemArraycopy( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 315 | Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 316 | // Special case array copying without initializing System. |
Andreas Gampe | 8e6c3fd | 2015-03-11 18:34:44 -0700 | [diff] [blame] | 317 | jint src_pos = shadow_frame->GetVReg(arg_offset + 1); |
| 318 | jint dst_pos = shadow_frame->GetVReg(arg_offset + 3); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 319 | jint length = shadow_frame->GetVReg(arg_offset + 4); |
Andreas Gampe | 8e6c3fd | 2015-03-11 18:34:44 -0700 | [diff] [blame] | 320 | mirror::Array* src_array = shadow_frame->GetVRegReference(arg_offset)->AsArray(); |
| 321 | mirror::Array* dst_array = shadow_frame->GetVRegReference(arg_offset + 2)->AsArray(); |
| 322 | |
| 323 | // Null checking. |
| 324 | if (src_array == nullptr) { |
| 325 | AbortTransactionOrFail(self, "src is null in arraycopy."); |
| 326 | return; |
| 327 | } |
| 328 | if (dst_array == nullptr) { |
| 329 | AbortTransactionOrFail(self, "dst is null in arraycopy."); |
| 330 | return; |
| 331 | } |
| 332 | |
| 333 | // Bounds checking. |
| 334 | if (UNLIKELY(src_pos < 0) || UNLIKELY(dst_pos < 0) || UNLIKELY(length < 0) || |
| 335 | UNLIKELY(src_pos > src_array->GetLength() - length) || |
| 336 | UNLIKELY(dst_pos > dst_array->GetLength() - length)) { |
| 337 | self->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;", |
| 338 | "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d", |
| 339 | src_array->GetLength(), src_pos, dst_array->GetLength(), dst_pos, |
| 340 | length); |
| 341 | AbortTransactionOrFail(self, "Index out of bounds."); |
| 342 | return; |
| 343 | } |
| 344 | |
| 345 | // Type checking. |
| 346 | mirror::Class* src_type = shadow_frame->GetVRegReference(arg_offset)->GetClass()-> |
| 347 | GetComponentType(); |
| 348 | |
| 349 | if (!src_type->IsPrimitive()) { |
| 350 | // Check that the second type is not primitive. |
| 351 | mirror::Class* trg_type = shadow_frame->GetVRegReference(arg_offset + 2)->GetClass()-> |
| 352 | GetComponentType(); |
| 353 | if (trg_type->IsPrimitiveInt()) { |
| 354 | AbortTransactionOrFail(self, "Type mismatch in arraycopy: %s vs %s", |
| 355 | PrettyDescriptor(src_array->GetClass()->GetComponentType()).c_str(), |
| 356 | PrettyDescriptor(dst_array->GetClass()->GetComponentType()).c_str()); |
| 357 | return; |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 358 | } |
Andreas Gampe | 8e6c3fd | 2015-03-11 18:34:44 -0700 | [diff] [blame] | 359 | |
| 360 | // For simplicity only do this if the component types are the same. Otherwise we have to copy |
| 361 | // even more code from the object-array functions. |
| 362 | if (src_type != trg_type) { |
| 363 | AbortTransactionOrFail(self, "Types not the same in arraycopy: %s vs %s", |
| 364 | PrettyDescriptor(src_array->GetClass()->GetComponentType()).c_str(), |
| 365 | PrettyDescriptor(dst_array->GetClass()->GetComponentType()).c_str()); |
| 366 | return; |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 367 | } |
Andreas Gampe | 8e6c3fd | 2015-03-11 18:34:44 -0700 | [diff] [blame] | 368 | |
| 369 | mirror::ObjectArray<mirror::Object>* src = src_array->AsObjectArray<mirror::Object>(); |
| 370 | mirror::ObjectArray<mirror::Object>* dst = dst_array->AsObjectArray<mirror::Object>(); |
| 371 | if (src == dst) { |
| 372 | // Can overlap, but not have type mismatches. |
| 373 | const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length); |
| 374 | if (copy_forward) { |
| 375 | for (int32_t i = 0; i < length; ++i) { |
| 376 | dst->Set(dst_pos + i, src->Get(src_pos + i)); |
| 377 | } |
| 378 | } else { |
| 379 | for (int32_t i = 1; i <= length; ++i) { |
| 380 | dst->Set(dst_pos + length - i, src->Get(src_pos + length - i)); |
| 381 | } |
| 382 | } |
| 383 | } else { |
| 384 | // Can't overlap. Would need type checks, but we abort above. |
| 385 | for (int32_t i = 0; i < length; ++i) { |
| 386 | dst->Set(dst_pos + i, src->Get(src_pos + i)); |
| 387 | } |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 388 | } |
Andreas Gampe | 8e6c3fd | 2015-03-11 18:34:44 -0700 | [diff] [blame] | 389 | } else if (src_type->IsPrimitiveChar()) { |
| 390 | PrimitiveArrayCopy<uint16_t>(self, src_array, src_pos, dst_array, dst_pos, length); |
| 391 | } else if (src_type->IsPrimitiveInt()) { |
| 392 | PrimitiveArrayCopy<int32_t>(self, src_array, src_pos, dst_array, dst_pos, length); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 393 | } else { |
Andreas Gampe | 068b0c0 | 2015-03-11 12:44:47 -0700 | [diff] [blame] | 394 | AbortTransactionOrFail(self, "Unimplemented System.arraycopy for type '%s'", |
Andreas Gampe | 8e6c3fd | 2015-03-11 18:34:44 -0700 | [diff] [blame] | 395 | PrettyDescriptor(src_type).c_str()); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 399 | void UnstartedRuntime::UnstartedSystemArraycopyChar( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 400 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 401 | // Just forward. |
| 402 | UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset); |
| 403 | } |
| 404 | |
| 405 | void UnstartedRuntime::UnstartedSystemArraycopyInt( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 406 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 407 | // Just forward. |
| 408 | UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset); |
| 409 | } |
| 410 | |
| 411 | void UnstartedRuntime::UnstartedThreadLocalGet( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 412 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 413 | std::string caller(PrettyMethod(shadow_frame->GetLink()->GetMethod())); |
| 414 | bool ok = false; |
| 415 | if (caller == "java.lang.String java.lang.IntegralToString.convertInt" |
| 416 | "(java.lang.AbstractStringBuilder, int)") { |
| 417 | // Allocate non-threadlocal buffer. |
| 418 | result->SetL(mirror::CharArray::Alloc(self, 11)); |
| 419 | ok = true; |
| 420 | } else if (caller == "java.lang.RealToString java.lang.RealToString.getInstance()") { |
| 421 | // Note: RealToString is implemented and used in a different fashion than IntegralToString. |
| 422 | // Conversion is done over an actual object of RealToString (the conversion method is an |
| 423 | // instance method). This means it is not as clear whether it is correct to return a new |
| 424 | // object each time. The caller needs to be inspected by hand to see whether it (incorrectly) |
| 425 | // stores the object for later use. |
| 426 | // See also b/19548084 for a possible rewrite and bringing it in line with IntegralToString. |
| 427 | if (shadow_frame->GetLink()->GetLink() != nullptr) { |
| 428 | std::string caller2(PrettyMethod(shadow_frame->GetLink()->GetLink()->GetMethod())); |
| 429 | if (caller2 == "java.lang.String java.lang.Double.toString(double)") { |
| 430 | // Allocate new object. |
| 431 | StackHandleScope<2> hs(self); |
| 432 | Handle<mirror::Class> h_real_to_string_class(hs.NewHandle( |
| 433 | shadow_frame->GetLink()->GetMethod()->GetDeclaringClass())); |
| 434 | Handle<mirror::Object> h_real_to_string_obj(hs.NewHandle( |
| 435 | h_real_to_string_class->AllocObject(self))); |
| 436 | if (h_real_to_string_obj.Get() != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 437 | auto* cl = Runtime::Current()->GetClassLinker(); |
| 438 | ArtMethod* init_method = h_real_to_string_class->FindDirectMethod( |
| 439 | "<init>", "()V", cl->GetImagePointerSize()); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 440 | if (init_method == nullptr) { |
| 441 | h_real_to_string_class->DumpClass(LOG(FATAL), mirror::Class::kDumpClassFullDetail); |
| 442 | } else { |
| 443 | JValue invoke_result; |
| 444 | EnterInterpreterFromInvoke(self, init_method, h_real_to_string_obj.Get(), nullptr, |
| 445 | nullptr); |
| 446 | if (!self->IsExceptionPending()) { |
| 447 | result->SetL(h_real_to_string_obj.Get()); |
| 448 | ok = true; |
| 449 | } |
| 450 | } |
| 451 | } |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | if (!ok) { |
Andreas Gampe | 068b0c0 | 2015-03-11 12:44:47 -0700 | [diff] [blame] | 457 | AbortTransactionOrFail(self, "Could not create RealToString object"); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 461 | void UnstartedRuntime::UnstartedMathCeil( |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 462 | Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 463 | double in = shadow_frame->GetVRegDouble(arg_offset); |
| 464 | double out; |
| 465 | // Special cases: |
| 466 | // 1) NaN, infinity, +0, -0 -> out := in. All are guaranteed by cmath. |
| 467 | // -1 < in < 0 -> out := -0. |
| 468 | if (-1.0 < in && in < 0) { |
| 469 | out = -0.0; |
| 470 | } else { |
| 471 | out = ceil(in); |
| 472 | } |
| 473 | result->SetD(out); |
| 474 | } |
| 475 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 476 | void UnstartedRuntime::UnstartedObjectHashCode( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 477 | Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 478 | mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset); |
| 479 | result->SetI(obj->IdentityHashCode()); |
| 480 | } |
| 481 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 482 | void UnstartedRuntime::UnstartedDoubleDoubleToRawLongBits( |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 483 | Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 484 | double in = shadow_frame->GetVRegDouble(arg_offset); |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 485 | result->SetJ(bit_cast<int64_t, double>(in)); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 488 | static mirror::Object* GetDexFromDexCache(Thread* self, mirror::DexCache* dex_cache) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 489 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 490 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 491 | if (dex_file == nullptr) { |
| 492 | return nullptr; |
| 493 | } |
| 494 | |
| 495 | // Create the direct byte buffer. |
| 496 | JNIEnv* env = self->GetJniEnv(); |
| 497 | DCHECK(env != nullptr); |
| 498 | void* address = const_cast<void*>(reinterpret_cast<const void*>(dex_file->Begin())); |
Andreas Gampe | aacc25d | 2015-04-01 14:49:06 -0700 | [diff] [blame] | 499 | ScopedLocalRef<jobject> byte_buffer(env, env->NewDirectByteBuffer(address, dex_file->Size())); |
| 500 | if (byte_buffer.get() == nullptr) { |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 501 | DCHECK(self->IsExceptionPending()); |
| 502 | return nullptr; |
| 503 | } |
| 504 | |
| 505 | jvalue args[1]; |
Andreas Gampe | aacc25d | 2015-04-01 14:49:06 -0700 | [diff] [blame] | 506 | args[0].l = byte_buffer.get(); |
| 507 | |
| 508 | ScopedLocalRef<jobject> dex(env, env->CallStaticObjectMethodA( |
| 509 | WellKnownClasses::com_android_dex_Dex, |
| 510 | WellKnownClasses::com_android_dex_Dex_create, |
| 511 | args)); |
| 512 | |
| 513 | return self->DecodeJObject(dex.get()); |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 516 | void UnstartedRuntime::UnstartedDexCacheGetDexNative( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 517 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 518 | // We will create the Dex object, but the image writer will release it before creating the |
| 519 | // art file. |
| 520 | mirror::Object* src = shadow_frame->GetVRegReference(arg_offset); |
| 521 | bool have_dex = false; |
| 522 | if (src != nullptr) { |
| 523 | mirror::Object* dex = GetDexFromDexCache(self, reinterpret_cast<mirror::DexCache*>(src)); |
| 524 | if (dex != nullptr) { |
| 525 | have_dex = true; |
| 526 | result->SetL(dex); |
| 527 | } |
| 528 | } |
| 529 | if (!have_dex) { |
| 530 | self->ClearException(); |
Sebastien Hertz | 2fd7e69 | 2015-04-02 11:11:19 +0200 | [diff] [blame] | 531 | Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Could not create Dex object"); |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 532 | } |
| 533 | } |
| 534 | |
| 535 | static void UnstartedMemoryPeek( |
| 536 | Primitive::Type type, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
| 537 | int64_t address = shadow_frame->GetVRegLong(arg_offset); |
| 538 | // TODO: Check that this is in the heap somewhere. Otherwise we will segfault instead of |
| 539 | // aborting the transaction. |
| 540 | |
| 541 | switch (type) { |
| 542 | case Primitive::kPrimByte: { |
| 543 | result->SetB(*reinterpret_cast<int8_t*>(static_cast<intptr_t>(address))); |
| 544 | return; |
| 545 | } |
| 546 | |
| 547 | case Primitive::kPrimShort: { |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 548 | typedef int16_t unaligned_short __attribute__ ((aligned (1))); |
| 549 | result->SetS(*reinterpret_cast<unaligned_short*>(static_cast<intptr_t>(address))); |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 550 | return; |
| 551 | } |
| 552 | |
| 553 | case Primitive::kPrimInt: { |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 554 | typedef int32_t unaligned_int __attribute__ ((aligned (1))); |
| 555 | result->SetI(*reinterpret_cast<unaligned_int*>(static_cast<intptr_t>(address))); |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 556 | return; |
| 557 | } |
| 558 | |
| 559 | case Primitive::kPrimLong: { |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 560 | typedef int64_t unaligned_long __attribute__ ((aligned (1))); |
| 561 | result->SetJ(*reinterpret_cast<unaligned_long*>(static_cast<intptr_t>(address))); |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 562 | return; |
| 563 | } |
| 564 | |
| 565 | case Primitive::kPrimBoolean: |
| 566 | case Primitive::kPrimChar: |
| 567 | case Primitive::kPrimFloat: |
| 568 | case Primitive::kPrimDouble: |
| 569 | case Primitive::kPrimVoid: |
| 570 | case Primitive::kPrimNot: |
| 571 | LOG(FATAL) << "Not in the Memory API: " << type; |
| 572 | UNREACHABLE(); |
| 573 | } |
| 574 | LOG(FATAL) << "Should not reach here"; |
| 575 | UNREACHABLE(); |
| 576 | } |
| 577 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 578 | void UnstartedRuntime::UnstartedMemoryPeekByte( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 579 | Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 580 | UnstartedMemoryPeek(Primitive::kPrimByte, shadow_frame, result, arg_offset); |
| 581 | } |
| 582 | |
| 583 | void UnstartedRuntime::UnstartedMemoryPeekShort( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 584 | Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 585 | UnstartedMemoryPeek(Primitive::kPrimShort, shadow_frame, result, arg_offset); |
| 586 | } |
| 587 | |
| 588 | void UnstartedRuntime::UnstartedMemoryPeekInt( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 589 | Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 590 | UnstartedMemoryPeek(Primitive::kPrimInt, shadow_frame, result, arg_offset); |
| 591 | } |
| 592 | |
| 593 | void UnstartedRuntime::UnstartedMemoryPeekLong( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 594 | Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 595 | UnstartedMemoryPeek(Primitive::kPrimLong, shadow_frame, result, arg_offset); |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | static void UnstartedMemoryPeekArray( |
| 599 | Primitive::Type type, Thread* self, ShadowFrame* shadow_frame, size_t arg_offset) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 600 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 601 | int64_t address_long = shadow_frame->GetVRegLong(arg_offset); |
| 602 | mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 2); |
| 603 | if (obj == nullptr) { |
Sebastien Hertz | 2fd7e69 | 2015-04-02 11:11:19 +0200 | [diff] [blame] | 604 | Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Null pointer in peekArray"); |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 605 | return; |
| 606 | } |
| 607 | mirror::Array* array = obj->AsArray(); |
| 608 | |
| 609 | int offset = shadow_frame->GetVReg(arg_offset + 3); |
| 610 | int count = shadow_frame->GetVReg(arg_offset + 4); |
| 611 | if (offset < 0 || offset + count > array->GetLength()) { |
| 612 | std::string error_msg(StringPrintf("Array out of bounds in peekArray: %d/%d vs %d", |
| 613 | offset, count, array->GetLength())); |
Sebastien Hertz | 2fd7e69 | 2015-04-02 11:11:19 +0200 | [diff] [blame] | 614 | Runtime::Current()->AbortTransactionAndThrowAbortError(self, error_msg.c_str()); |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 615 | return; |
| 616 | } |
| 617 | |
| 618 | switch (type) { |
| 619 | case Primitive::kPrimByte: { |
| 620 | int8_t* address = reinterpret_cast<int8_t*>(static_cast<intptr_t>(address_long)); |
| 621 | mirror::ByteArray* byte_array = array->AsByteArray(); |
| 622 | for (int32_t i = 0; i < count; ++i, ++address) { |
| 623 | byte_array->SetWithoutChecks<true>(i + offset, *address); |
| 624 | } |
| 625 | return; |
| 626 | } |
| 627 | |
| 628 | case Primitive::kPrimShort: |
| 629 | case Primitive::kPrimInt: |
| 630 | case Primitive::kPrimLong: |
| 631 | LOG(FATAL) << "Type unimplemented for Memory Array API, should not reach here: " << type; |
| 632 | UNREACHABLE(); |
| 633 | |
| 634 | case Primitive::kPrimBoolean: |
| 635 | case Primitive::kPrimChar: |
| 636 | case Primitive::kPrimFloat: |
| 637 | case Primitive::kPrimDouble: |
| 638 | case Primitive::kPrimVoid: |
| 639 | case Primitive::kPrimNot: |
| 640 | LOG(FATAL) << "Not in the Memory API: " << type; |
| 641 | UNREACHABLE(); |
| 642 | } |
| 643 | LOG(FATAL) << "Should not reach here"; |
| 644 | UNREACHABLE(); |
| 645 | } |
| 646 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 647 | void UnstartedRuntime::UnstartedMemoryPeekByteArray( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 648 | Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) { |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 649 | UnstartedMemoryPeekArray(Primitive::kPrimByte, self, shadow_frame, arg_offset); |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 650 | } |
| 651 | |
Andreas Gampe | f778eb2 | 2015-04-13 14:17:09 -0700 | [diff] [blame] | 652 | // This allows reading security.properties in an unstarted runtime and initialize Security. |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 653 | void UnstartedRuntime::UnstartedSecurityGetSecurityPropertiesReader( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 654 | Thread* self, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED, JValue* result, |
| 655 | size_t arg_offset ATTRIBUTE_UNUSED) { |
Andreas Gampe | f778eb2 | 2015-04-13 14:17:09 -0700 | [diff] [blame] | 656 | Runtime* runtime = Runtime::Current(); |
| 657 | const std::vector<const DexFile*>& path = runtime->GetClassLinker()->GetBootClassPath(); |
| 658 | std::string canonical(DexFile::GetDexCanonicalLocation(path[0]->GetLocation().c_str())); |
| 659 | mirror::String* string_data; |
| 660 | |
| 661 | // Use a block to enclose the I/O and MemMap code so buffers are released early. |
| 662 | { |
| 663 | std::string error_msg; |
| 664 | std::unique_ptr<ZipArchive> zip_archive(ZipArchive::Open(canonical.c_str(), &error_msg)); |
| 665 | if (zip_archive.get() == nullptr) { |
| 666 | AbortTransactionOrFail(self, "Could not open zip file %s: %s", canonical.c_str(), |
| 667 | error_msg.c_str()); |
| 668 | return; |
| 669 | } |
| 670 | std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find("java/security/security.properties", |
| 671 | &error_msg)); |
| 672 | if (zip_entry.get() == nullptr) { |
| 673 | AbortTransactionOrFail(self, "Could not find security.properties file in %s: %s", |
| 674 | canonical.c_str(), error_msg.c_str()); |
| 675 | return; |
| 676 | } |
| 677 | std::unique_ptr<MemMap> map(zip_entry->ExtractToMemMap(canonical.c_str(), |
| 678 | "java/security/security.properties", |
| 679 | &error_msg)); |
| 680 | if (map.get() == nullptr) { |
| 681 | AbortTransactionOrFail(self, "Could not unzip security.properties file in %s: %s", |
| 682 | canonical.c_str(), error_msg.c_str()); |
| 683 | return; |
| 684 | } |
| 685 | |
| 686 | uint32_t length = zip_entry->GetUncompressedLength(); |
| 687 | std::unique_ptr<char[]> tmp(new char[length + 1]); |
| 688 | memcpy(tmp.get(), map->Begin(), length); |
| 689 | tmp.get()[length] = 0; // null terminator |
| 690 | |
| 691 | string_data = mirror::String::AllocFromModifiedUtf8(self, tmp.get()); |
| 692 | } |
| 693 | |
| 694 | if (string_data == nullptr) { |
| 695 | AbortTransactionOrFail(self, "Could not create string from file content of %s", |
| 696 | canonical.c_str()); |
| 697 | return; |
| 698 | } |
| 699 | |
| 700 | // Create a StringReader. |
| 701 | StackHandleScope<3> hs(self); |
| 702 | Handle<mirror::String> h_string(hs.NewHandle(string_data)); |
| 703 | |
| 704 | Handle<mirror::Class> h_class(hs.NewHandle( |
| 705 | runtime->GetClassLinker()->FindClass(self, |
| 706 | "Ljava/io/StringReader;", |
| 707 | NullHandle<mirror::ClassLoader>()))); |
| 708 | if (h_class.Get() == nullptr) { |
| 709 | AbortTransactionOrFail(self, "Could not find StringReader class"); |
| 710 | return; |
| 711 | } |
| 712 | |
| 713 | if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) { |
| 714 | AbortTransactionOrFail(self, "Could not initialize StringReader class"); |
| 715 | return; |
| 716 | } |
| 717 | |
| 718 | Handle<mirror::Object> h_obj(hs.NewHandle(h_class->AllocObject(self))); |
| 719 | if (h_obj.Get() == nullptr) { |
| 720 | AbortTransactionOrFail(self, "Could not allocate StringReader object"); |
| 721 | return; |
| 722 | } |
| 723 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 724 | auto* cl = Runtime::Current()->GetClassLinker(); |
| 725 | ArtMethod* constructor = h_class->FindDeclaredDirectMethod( |
| 726 | "<init>", "(Ljava/lang/String;)V", cl->GetImagePointerSize()); |
Andreas Gampe | f778eb2 | 2015-04-13 14:17:09 -0700 | [diff] [blame] | 727 | if (constructor == nullptr) { |
| 728 | AbortTransactionOrFail(self, "Could not find StringReader constructor"); |
| 729 | return; |
| 730 | } |
| 731 | |
| 732 | uint32_t args[1]; |
| 733 | args[0] = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(h_string.Get())); |
| 734 | EnterInterpreterFromInvoke(self, constructor, h_obj.Get(), args, nullptr); |
| 735 | |
| 736 | if (self->IsExceptionPending()) { |
| 737 | AbortTransactionOrFail(self, "Could not run StringReader constructor"); |
| 738 | return; |
| 739 | } |
| 740 | |
| 741 | result->SetL(h_obj.Get()); |
| 742 | } |
| 743 | |
Kenny Root | 1c9e61c | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 744 | // This allows reading the new style of String objects during compilation. |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 745 | void UnstartedRuntime::UnstartedStringGetCharsNoCheck( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 746 | Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) { |
Kenny Root | 1c9e61c | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 747 | jint start = shadow_frame->GetVReg(arg_offset + 1); |
| 748 | jint end = shadow_frame->GetVReg(arg_offset + 2); |
| 749 | jint index = shadow_frame->GetVReg(arg_offset + 4); |
| 750 | mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString(); |
| 751 | if (string == nullptr) { |
| 752 | AbortTransactionOrFail(self, "String.getCharsNoCheck with null object"); |
| 753 | return; |
| 754 | } |
Kenny Root | 57f91e8 | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 755 | DCHECK_GE(start, 0); |
| 756 | DCHECK_GE(end, string->GetLength()); |
Kenny Root | 1c9e61c | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 757 | StackHandleScope<1> hs(self); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 758 | Handle<mirror::CharArray> h_char_array( |
| 759 | hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 3)->AsCharArray())); |
Kenny Root | 57f91e8 | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 760 | DCHECK_LE(index, h_char_array->GetLength()); |
| 761 | DCHECK_LE(end - start, h_char_array->GetLength() - index); |
Kenny Root | 1c9e61c | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 762 | string->GetChars(start, end, h_char_array, index); |
| 763 | } |
| 764 | |
| 765 | // This allows reading chars from the new style of String objects during compilation. |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 766 | void UnstartedRuntime::UnstartedStringCharAt( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 767 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
Kenny Root | 1c9e61c | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 768 | jint index = shadow_frame->GetVReg(arg_offset + 1); |
| 769 | mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString(); |
| 770 | if (string == nullptr) { |
| 771 | AbortTransactionOrFail(self, "String.charAt with null object"); |
| 772 | return; |
| 773 | } |
| 774 | result->SetC(string->CharAt(index)); |
| 775 | } |
| 776 | |
Kenny Root | 57f91e8 | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 777 | // This allows setting chars from the new style of String objects during compilation. |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 778 | void UnstartedRuntime::UnstartedStringSetCharAt( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 779 | Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) { |
Kenny Root | 57f91e8 | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 780 | jint index = shadow_frame->GetVReg(arg_offset + 1); |
| 781 | jchar c = shadow_frame->GetVReg(arg_offset + 2); |
| 782 | mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString(); |
| 783 | if (string == nullptr) { |
| 784 | AbortTransactionOrFail(self, "String.setCharAt with null object"); |
| 785 | return; |
| 786 | } |
| 787 | string->SetCharAt(index, c); |
| 788 | } |
| 789 | |
Kenny Root | 1c9e61c | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 790 | // This allows creating the new style of String objects during compilation. |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 791 | void UnstartedRuntime::UnstartedStringFactoryNewStringFromChars( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 792 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
Kenny Root | 1c9e61c | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 793 | jint offset = shadow_frame->GetVReg(arg_offset); |
| 794 | jint char_count = shadow_frame->GetVReg(arg_offset + 1); |
| 795 | DCHECK_GE(char_count, 0); |
| 796 | StackHandleScope<1> hs(self); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 797 | Handle<mirror::CharArray> h_char_array( |
| 798 | hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray())); |
Kenny Root | 1c9e61c | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 799 | Runtime* runtime = Runtime::Current(); |
| 800 | gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator(); |
| 801 | result->SetL(mirror::String::AllocFromCharArray<true>(self, char_count, h_char_array, offset, allocator)); |
| 802 | } |
| 803 | |
| 804 | // This allows creating the new style of String objects during compilation. |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 805 | void UnstartedRuntime::UnstartedStringFactoryNewStringFromString( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 806 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
Kenny Root | 57f91e8 | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 807 | mirror::String* to_copy = shadow_frame->GetVRegReference(arg_offset)->AsString(); |
| 808 | if (to_copy == nullptr) { |
| 809 | AbortTransactionOrFail(self, "StringFactory.newStringFromString with null object"); |
| 810 | return; |
| 811 | } |
| 812 | StackHandleScope<1> hs(self); |
| 813 | Handle<mirror::String> h_string(hs.NewHandle(to_copy)); |
| 814 | Runtime* runtime = Runtime::Current(); |
| 815 | gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator(); |
| 816 | result->SetL(mirror::String::AllocFromString<true>(self, h_string->GetLength(), h_string, 0, |
| 817 | allocator)); |
| 818 | } |
| 819 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 820 | void UnstartedRuntime::UnstartedStringFastSubstring( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 821 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
Kenny Root | 1c9e61c | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 822 | jint start = shadow_frame->GetVReg(arg_offset + 1); |
| 823 | jint length = shadow_frame->GetVReg(arg_offset + 2); |
Kenny Root | 57f91e8 | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 824 | DCHECK_GE(start, 0); |
Kenny Root | 1c9e61c | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 825 | DCHECK_GE(length, 0); |
| 826 | StackHandleScope<1> hs(self); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 827 | Handle<mirror::String> h_string( |
| 828 | hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsString())); |
Kenny Root | 57f91e8 | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 829 | DCHECK_LE(start, h_string->GetLength()); |
| 830 | DCHECK_LE(start + length, h_string->GetLength()); |
Kenny Root | 1c9e61c | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 831 | Runtime* runtime = Runtime::Current(); |
| 832 | gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator(); |
| 833 | result->SetL(mirror::String::AllocFromString<true>(self, length, h_string, start, allocator)); |
| 834 | } |
| 835 | |
Kenny Root | 57f91e8 | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 836 | // This allows getting the char array for new style of String objects during compilation. |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 837 | void UnstartedRuntime::UnstartedStringToCharArray( |
Kenny Root | 57f91e8 | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 838 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 839 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Kenny Root | 57f91e8 | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 840 | mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString(); |
| 841 | if (string == nullptr) { |
| 842 | AbortTransactionOrFail(self, "String.charAt with null object"); |
| 843 | return; |
| 844 | } |
| 845 | result->SetL(string->ToCharArray(self)); |
| 846 | } |
| 847 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 848 | void UnstartedRuntime::UnstartedJNIVMRuntimeNewUnpaddedArray( |
| 849 | Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED, |
| 850 | uint32_t* args, JValue* result) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 851 | int32_t length = args[1]; |
| 852 | DCHECK_GE(length, 0); |
| 853 | mirror::Class* element_class = reinterpret_cast<mirror::Object*>(args[0])->AsClass(); |
| 854 | Runtime* runtime = Runtime::Current(); |
| 855 | mirror::Class* array_class = runtime->GetClassLinker()->FindArrayClass(self, &element_class); |
| 856 | DCHECK(array_class != nullptr); |
| 857 | gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator(); |
| 858 | result->SetL(mirror::Array::Alloc<true, true>(self, array_class, length, |
| 859 | array_class->GetComponentSizeShift(), allocator)); |
| 860 | } |
| 861 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 862 | void UnstartedRuntime::UnstartedJNIVMStackGetCallingClassLoader( |
| 863 | Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, |
| 864 | mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 865 | result->SetL(nullptr); |
| 866 | } |
| 867 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 868 | void UnstartedRuntime::UnstartedJNIVMStackGetStackClass2( |
| 869 | Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED, |
| 870 | uint32_t* args ATTRIBUTE_UNUSED, JValue* result) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 871 | NthCallerVisitor visitor(self, 3); |
| 872 | visitor.WalkStack(); |
| 873 | if (visitor.caller != nullptr) { |
| 874 | result->SetL(visitor.caller->GetDeclaringClass()); |
| 875 | } |
| 876 | } |
| 877 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 878 | void UnstartedRuntime::UnstartedJNIMathLog( |
| 879 | Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, |
| 880 | mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 881 | JValue value; |
| 882 | value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]); |
| 883 | result->SetD(log(value.GetD())); |
| 884 | } |
| 885 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 886 | void UnstartedRuntime::UnstartedJNIMathExp( |
| 887 | Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, |
| 888 | mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 889 | JValue value; |
| 890 | value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]); |
| 891 | result->SetD(exp(value.GetD())); |
| 892 | } |
| 893 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 894 | void UnstartedRuntime::UnstartedJNIClassGetNameNative( |
| 895 | Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver, |
| 896 | uint32_t* args ATTRIBUTE_UNUSED, JValue* result) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 897 | StackHandleScope<1> hs(self); |
| 898 | result->SetL(mirror::Class::ComputeName(hs.NewHandle(receiver->AsClass()))); |
| 899 | } |
| 900 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 901 | void UnstartedRuntime::UnstartedJNIFloatFloatToRawIntBits( |
| 902 | Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, |
| 903 | mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 904 | result->SetI(args[0]); |
| 905 | } |
| 906 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 907 | void UnstartedRuntime::UnstartedJNIFloatIntBitsToFloat( |
| 908 | Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, |
| 909 | mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 910 | result->SetI(args[0]); |
| 911 | } |
| 912 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 913 | void UnstartedRuntime::UnstartedJNIObjectInternalClone( |
| 914 | Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver, |
| 915 | uint32_t* args ATTRIBUTE_UNUSED, JValue* result) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 916 | result->SetL(receiver->Clone(self)); |
| 917 | } |
| 918 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 919 | void UnstartedRuntime::UnstartedJNIObjectNotifyAll( |
| 920 | Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver, |
| 921 | uint32_t* args ATTRIBUTE_UNUSED, JValue* result ATTRIBUTE_UNUSED) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 922 | receiver->NotifyAll(self); |
| 923 | } |
| 924 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 925 | void UnstartedRuntime::UnstartedJNIStringCompareTo( |
| 926 | Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver, uint32_t* args, |
| 927 | JValue* result) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 928 | mirror::String* rhs = reinterpret_cast<mirror::Object*>(args[0])->AsString(); |
| 929 | if (rhs == nullptr) { |
Andreas Gampe | 068b0c0 | 2015-03-11 12:44:47 -0700 | [diff] [blame] | 930 | AbortTransactionOrFail(self, "String.compareTo with null object"); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 931 | } |
| 932 | result->SetI(receiver->AsString()->CompareTo(rhs)); |
| 933 | } |
| 934 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 935 | void UnstartedRuntime::UnstartedJNIStringIntern( |
| 936 | Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver, |
| 937 | uint32_t* args ATTRIBUTE_UNUSED, JValue* result) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 938 | result->SetL(receiver->AsString()->Intern()); |
| 939 | } |
| 940 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 941 | void UnstartedRuntime::UnstartedJNIStringFastIndexOf( |
| 942 | Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver, |
| 943 | uint32_t* args, JValue* result) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 944 | result->SetI(receiver->AsString()->FastIndexOf(args[0], args[1])); |
| 945 | } |
| 946 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 947 | void UnstartedRuntime::UnstartedJNIArrayCreateMultiArray( |
| 948 | Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED, |
| 949 | uint32_t* args, JValue* result) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 950 | StackHandleScope<2> hs(self); |
| 951 | auto h_class(hs.NewHandle(reinterpret_cast<mirror::Class*>(args[0])->AsClass())); |
| 952 | auto h_dimensions(hs.NewHandle(reinterpret_cast<mirror::IntArray*>(args[1])->AsIntArray())); |
| 953 | result->SetL(mirror::Array::CreateMultiArray(self, h_class, h_dimensions)); |
| 954 | } |
| 955 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 956 | void UnstartedRuntime::UnstartedJNIArrayCreateObjectArray( |
| 957 | Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED, |
| 958 | uint32_t* args, JValue* result) { |
Andreas Gampe | e598e04 | 2015-04-10 14:57:10 -0700 | [diff] [blame] | 959 | int32_t length = static_cast<int32_t>(args[1]); |
| 960 | if (length < 0) { |
| 961 | ThrowNegativeArraySizeException(length); |
| 962 | return; |
| 963 | } |
| 964 | mirror::Class* element_class = reinterpret_cast<mirror::Class*>(args[0])->AsClass(); |
| 965 | Runtime* runtime = Runtime::Current(); |
| 966 | ClassLinker* class_linker = runtime->GetClassLinker(); |
| 967 | mirror::Class* array_class = class_linker->FindArrayClass(self, &element_class); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 968 | if (UNLIKELY(array_class == nullptr)) { |
Andreas Gampe | e598e04 | 2015-04-10 14:57:10 -0700 | [diff] [blame] | 969 | CHECK(self->IsExceptionPending()); |
| 970 | return; |
| 971 | } |
| 972 | DCHECK(array_class->IsObjectArrayClass()); |
| 973 | mirror::Array* new_array = mirror::ObjectArray<mirror::Object*>::Alloc( |
| 974 | self, array_class, length, runtime->GetHeap()->GetCurrentAllocator()); |
| 975 | result->SetL(new_array); |
| 976 | } |
| 977 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 978 | void UnstartedRuntime::UnstartedJNIThrowableNativeFillInStackTrace( |
| 979 | Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED, |
| 980 | uint32_t* args ATTRIBUTE_UNUSED, JValue* result) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 981 | ScopedObjectAccessUnchecked soa(self); |
| 982 | if (Runtime::Current()->IsActiveTransaction()) { |
| 983 | result->SetL(soa.Decode<mirror::Object*>(self->CreateInternalStackTrace<true>(soa))); |
| 984 | } else { |
| 985 | result->SetL(soa.Decode<mirror::Object*>(self->CreateInternalStackTrace<false>(soa))); |
| 986 | } |
| 987 | } |
| 988 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 989 | void UnstartedRuntime::UnstartedJNISystemIdentityHashCode( |
| 990 | Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, |
| 991 | mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 992 | mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]); |
| 993 | result->SetI((obj != nullptr) ? obj->IdentityHashCode() : 0); |
| 994 | } |
| 995 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 996 | void UnstartedRuntime::UnstartedJNIByteOrderIsLittleEndian( |
| 997 | Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, |
| 998 | mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 999 | result->SetZ(JNI_TRUE); |
| 1000 | } |
| 1001 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1002 | void UnstartedRuntime::UnstartedJNIUnsafeCompareAndSwapInt( |
| 1003 | Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, |
| 1004 | mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1005 | mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]); |
| 1006 | jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1]; |
| 1007 | jint expectedValue = args[3]; |
| 1008 | jint newValue = args[4]; |
| 1009 | bool success; |
| 1010 | if (Runtime::Current()->IsActiveTransaction()) { |
| 1011 | success = obj->CasFieldStrongSequentiallyConsistent32<true>(MemberOffset(offset), |
| 1012 | expectedValue, newValue); |
| 1013 | } else { |
| 1014 | success = obj->CasFieldStrongSequentiallyConsistent32<false>(MemberOffset(offset), |
| 1015 | expectedValue, newValue); |
| 1016 | } |
| 1017 | result->SetZ(success ? JNI_TRUE : JNI_FALSE); |
| 1018 | } |
| 1019 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1020 | void UnstartedRuntime::UnstartedJNIUnsafePutObject( |
| 1021 | Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, |
| 1022 | mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result ATTRIBUTE_UNUSED) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1023 | mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]); |
| 1024 | jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1]; |
| 1025 | mirror::Object* newValue = reinterpret_cast<mirror::Object*>(args[3]); |
| 1026 | if (Runtime::Current()->IsActiveTransaction()) { |
| 1027 | obj->SetFieldObject<true>(MemberOffset(offset), newValue); |
| 1028 | } else { |
| 1029 | obj->SetFieldObject<false>(MemberOffset(offset), newValue); |
| 1030 | } |
| 1031 | } |
| 1032 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 1033 | void UnstartedRuntime::UnstartedJNIUnsafeGetArrayBaseOffsetForComponentType( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1034 | Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, |
| 1035 | mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1036 | mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass(); |
| 1037 | Primitive::Type primitive_type = component->GetPrimitiveType(); |
| 1038 | result->SetI(mirror::Array::DataOffset(Primitive::ComponentSize(primitive_type)).Int32Value()); |
| 1039 | } |
| 1040 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 1041 | void UnstartedRuntime::UnstartedJNIUnsafeGetArrayIndexScaleForComponentType( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1042 | Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, |
| 1043 | mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1044 | mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass(); |
| 1045 | Primitive::Type primitive_type = component->GetPrimitiveType(); |
| 1046 | result->SetI(Primitive::ComponentSize(primitive_type)); |
| 1047 | } |
| 1048 | |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1049 | typedef void (*InvokeHandler)(Thread* self, ShadowFrame* shadow_frame, JValue* result, |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1050 | size_t arg_size); |
| 1051 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1052 | typedef void (*JNIHandler)(Thread* self, ArtMethod* method, mirror::Object* receiver, |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1053 | uint32_t* args, JValue* result); |
| 1054 | |
| 1055 | static bool tables_initialized_ = false; |
| 1056 | static std::unordered_map<std::string, InvokeHandler> invoke_handlers_; |
| 1057 | static std::unordered_map<std::string, JNIHandler> jni_handlers_; |
| 1058 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 1059 | void UnstartedRuntime::InitializeInvokeHandlers() { |
| 1060 | #define UNSTARTED_DIRECT(ShortName, Sig) \ |
| 1061 | invoke_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::Unstarted ## ShortName)); |
| 1062 | #include "unstarted_runtime_list.h" |
| 1063 | UNSTARTED_RUNTIME_DIRECT_LIST(UNSTARTED_DIRECT) |
| 1064 | #undef UNSTARTED_RUNTIME_DIRECT_LIST |
| 1065 | #undef UNSTARTED_RUNTIME_JNI_LIST |
| 1066 | #undef UNSTARTED_DIRECT |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1067 | } |
| 1068 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 1069 | void UnstartedRuntime::InitializeJNIHandlers() { |
| 1070 | #define UNSTARTED_JNI(ShortName, Sig) \ |
| 1071 | jni_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::UnstartedJNI ## ShortName)); |
| 1072 | #include "unstarted_runtime_list.h" |
| 1073 | UNSTARTED_RUNTIME_JNI_LIST(UNSTARTED_JNI) |
| 1074 | #undef UNSTARTED_RUNTIME_DIRECT_LIST |
| 1075 | #undef UNSTARTED_RUNTIME_JNI_LIST |
| 1076 | #undef UNSTARTED_JNI |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1077 | } |
| 1078 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 1079 | void UnstartedRuntime::Initialize() { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1080 | CHECK(!tables_initialized_); |
| 1081 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 1082 | InitializeInvokeHandlers(); |
| 1083 | InitializeJNIHandlers(); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1084 | |
| 1085 | tables_initialized_ = true; |
| 1086 | } |
| 1087 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 1088 | void UnstartedRuntime::Invoke(Thread* self, const DexFile::CodeItem* code_item, |
| 1089 | ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1090 | // In a runtime that's not started we intercept certain methods to avoid complicated dependency |
| 1091 | // problems in core libraries. |
| 1092 | CHECK(tables_initialized_); |
| 1093 | |
| 1094 | std::string name(PrettyMethod(shadow_frame->GetMethod())); |
| 1095 | const auto& iter = invoke_handlers_.find(name); |
| 1096 | if (iter != invoke_handlers_.end()) { |
Kenny Root | 57f91e8 | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 1097 | // Clear out the result in case it's not zeroed out. |
| 1098 | result->SetL(0); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1099 | (*iter->second)(self, shadow_frame, result, arg_offset); |
| 1100 | } else { |
| 1101 | // Not special, continue with regular interpreter execution. |
| 1102 | artInterpreterToInterpreterBridge(self, code_item, shadow_frame, result); |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | // Hand select a number of methods to be run in a not yet started runtime without using JNI. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1107 | void UnstartedRuntime::Jni(Thread* self, ArtMethod* method, mirror::Object* receiver, |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 1108 | uint32_t* args, JValue* result) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1109 | std::string name(PrettyMethod(method)); |
| 1110 | const auto& iter = jni_handlers_.find(name); |
| 1111 | if (iter != jni_handlers_.end()) { |
Kenny Root | 57f91e8 | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 1112 | // Clear out the result in case it's not zeroed out. |
| 1113 | result->SetL(0); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1114 | (*iter->second)(self, method, receiver, args, result); |
| 1115 | } else if (Runtime::Current()->IsActiveTransaction()) { |
Sebastien Hertz | 45b1597 | 2015-04-03 16:07:05 +0200 | [diff] [blame] | 1116 | AbortTransactionF(self, "Attempt to invoke native method in non-started runtime: %s", |
| 1117 | name.c_str()); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1118 | } else { |
| 1119 | LOG(FATAL) << "Calling native method " << PrettyMethod(method) << " in an unstarted " |
| 1120 | "non-transactional runtime"; |
| 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | } // namespace interpreter |
| 1125 | } // namespace art |