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 | bc4d218 | 2016-02-22 10:03:12 -0800 | [diff] [blame] | 25 | #include "base/casts.h" |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 26 | #include "base/logging.h" |
| 27 | #include "base/macros.h" |
| 28 | #include "class_linker.h" |
| 29 | #include "common_throws.h" |
| 30 | #include "entrypoints/entrypoint_utils-inl.h" |
Andreas Gampe | bc4d218 | 2016-02-22 10:03:12 -0800 | [diff] [blame] | 31 | #include "gc/reference_processor.h" |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 32 | #include "handle_scope-inl.h" |
| 33 | #include "interpreter/interpreter_common.h" |
| 34 | #include "mirror/array-inl.h" |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 35 | #include "mirror/class.h" |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 36 | #include "mirror/field-inl.h" |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 37 | #include "mirror/object-inl.h" |
| 38 | #include "mirror/object_array-inl.h" |
| 39 | #include "mirror/string-inl.h" |
| 40 | #include "nth_caller_visitor.h" |
| 41 | #include "thread.h" |
Sebastien Hertz | 2fd7e69 | 2015-04-02 11:11:19 +0200 | [diff] [blame] | 42 | #include "transaction.h" |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 43 | #include "well_known_classes.h" |
Andreas Gampe | f778eb2 | 2015-04-13 14:17:09 -0700 | [diff] [blame] | 44 | #include "zip_archive.h" |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 45 | |
| 46 | namespace art { |
| 47 | namespace interpreter { |
| 48 | |
Andreas Gampe | 068b0c0 | 2015-03-11 12:44:47 -0700 | [diff] [blame] | 49 | static void AbortTransactionOrFail(Thread* self, const char* fmt, ...) |
Sebastien Hertz | 45b1597 | 2015-04-03 16:07:05 +0200 | [diff] [blame] | 50 | __attribute__((__format__(__printf__, 2, 3))) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 51 | SHARED_REQUIRES(Locks::mutator_lock_); |
Sebastien Hertz | 45b1597 | 2015-04-03 16:07:05 +0200 | [diff] [blame] | 52 | |
| 53 | static void AbortTransactionOrFail(Thread* self, const char* fmt, ...) { |
Andreas Gampe | 068b0c0 | 2015-03-11 12:44:47 -0700 | [diff] [blame] | 54 | va_list args; |
Andreas Gampe | 068b0c0 | 2015-03-11 12:44:47 -0700 | [diff] [blame] | 55 | if (Runtime::Current()->IsActiveTransaction()) { |
Sebastien Hertz | 45b1597 | 2015-04-03 16:07:05 +0200 | [diff] [blame] | 56 | va_start(args, fmt); |
| 57 | AbortTransactionV(self, fmt, args); |
Andreas Gampe | 068b0c0 | 2015-03-11 12:44:47 -0700 | [diff] [blame] | 58 | va_end(args); |
| 59 | } else { |
Sebastien Hertz | 45b1597 | 2015-04-03 16:07:05 +0200 | [diff] [blame] | 60 | va_start(args, fmt); |
| 61 | std::string msg; |
| 62 | StringAppendV(&msg, fmt, args); |
| 63 | va_end(args); |
| 64 | LOG(FATAL) << "Trying to abort, but not in transaction mode: " << msg; |
Andreas Gampe | 068b0c0 | 2015-03-11 12:44:47 -0700 | [diff] [blame] | 65 | UNREACHABLE(); |
| 66 | } |
| 67 | } |
| 68 | |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 69 | // Helper function to deal with class loading in an unstarted runtime. |
| 70 | static void UnstartedRuntimeFindClass(Thread* self, Handle<mirror::String> className, |
| 71 | Handle<mirror::ClassLoader> class_loader, JValue* result, |
| 72 | const std::string& method_name, bool initialize_class, |
| 73 | bool abort_if_not_found) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 74 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 75 | CHECK(className.Get() != nullptr); |
| 76 | std::string descriptor(DotToDescriptor(className->ToModifiedUtf8().c_str())); |
| 77 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 78 | |
| 79 | mirror::Class* found = class_linker->FindClass(self, descriptor.c_str(), class_loader); |
| 80 | if (found == nullptr && abort_if_not_found) { |
| 81 | if (!self->IsExceptionPending()) { |
Andreas Gampe | 068b0c0 | 2015-03-11 12:44:47 -0700 | [diff] [blame] | 82 | AbortTransactionOrFail(self, "%s failed in un-started runtime for class: %s", |
| 83 | method_name.c_str(), PrettyDescriptor(descriptor.c_str()).c_str()); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 84 | } |
| 85 | return; |
| 86 | } |
| 87 | if (found != nullptr && initialize_class) { |
| 88 | StackHandleScope<1> hs(self); |
| 89 | Handle<mirror::Class> h_class(hs.NewHandle(found)); |
| 90 | if (!class_linker->EnsureInitialized(self, h_class, true, true)) { |
| 91 | CHECK(self->IsExceptionPending()); |
| 92 | return; |
| 93 | } |
| 94 | } |
| 95 | result->SetL(found); |
| 96 | } |
| 97 | |
| 98 | // Common helper for class-loading cutouts in an unstarted runtime. We call Runtime methods that |
| 99 | // rely on Java code to wrap errors in the correct exception class (i.e., NoClassDefFoundError into |
| 100 | // 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] | 101 | // actually the transaction abort exception. This must not be wrapped, as it signals an |
| 102 | // initialization abort. |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 103 | static void CheckExceptionGenerateClassNotFound(Thread* self) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 104 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 105 | if (self->IsExceptionPending()) { |
Sebastien Hertz | 2fd7e69 | 2015-04-02 11:11:19 +0200 | [diff] [blame] | 106 | // If it is not the transaction abort exception, wrap it. |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 107 | std::string type(PrettyTypeOf(self->GetException())); |
Sebastien Hertz | 2fd7e69 | 2015-04-02 11:11:19 +0200 | [diff] [blame] | 108 | if (type != Transaction::kAbortExceptionDescriptor) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 109 | self->ThrowNewWrappedException("Ljava/lang/ClassNotFoundException;", |
| 110 | "ClassNotFoundException"); |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
Andreas Gampe | 5d4bb1d | 2015-04-14 22:16:14 -0700 | [diff] [blame] | 115 | 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] | 116 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Andreas Gampe | 5d4bb1d | 2015-04-14 22:16:14 -0700 | [diff] [blame] | 117 | mirror::Object* param = shadow_frame->GetVRegReference(arg_offset); |
| 118 | if (param == nullptr) { |
| 119 | AbortTransactionOrFail(self, "Null-pointer in Class.forName."); |
| 120 | return nullptr; |
| 121 | } |
| 122 | return param->AsString(); |
| 123 | } |
| 124 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 125 | void UnstartedRuntime::UnstartedClassForName( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 126 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
Andreas Gampe | 5d4bb1d | 2015-04-14 22:16:14 -0700 | [diff] [blame] | 127 | mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset); |
| 128 | if (class_name == nullptr) { |
| 129 | return; |
| 130 | } |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 131 | StackHandleScope<1> hs(self); |
| 132 | Handle<mirror::String> h_class_name(hs.NewHandle(class_name)); |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 133 | UnstartedRuntimeFindClass(self, |
| 134 | h_class_name, |
| 135 | ScopedNullHandle<mirror::ClassLoader>(), |
| 136 | result, |
| 137 | "Class.forName", |
| 138 | true, |
| 139 | false); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 140 | CheckExceptionGenerateClassNotFound(self); |
| 141 | } |
| 142 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 143 | void UnstartedRuntime::UnstartedClassForNameLong( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 144 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
Andreas Gampe | 5d4bb1d | 2015-04-14 22:16:14 -0700 | [diff] [blame] | 145 | mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset); |
| 146 | if (class_name == nullptr) { |
Andreas Gampe | bf4d3af | 2015-04-14 10:10:33 -0700 | [diff] [blame] | 147 | return; |
| 148 | } |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 149 | bool initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0; |
| 150 | mirror::ClassLoader* class_loader = |
| 151 | down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2)); |
| 152 | StackHandleScope<2> hs(self); |
| 153 | Handle<mirror::String> h_class_name(hs.NewHandle(class_name)); |
| 154 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); |
| 155 | UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, "Class.forName", |
| 156 | initialize_class, false); |
| 157 | CheckExceptionGenerateClassNotFound(self); |
| 158 | } |
| 159 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 160 | void UnstartedRuntime::UnstartedClassClassForName( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 161 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
Andreas Gampe | 5d4bb1d | 2015-04-14 22:16:14 -0700 | [diff] [blame] | 162 | mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset); |
| 163 | if (class_name == nullptr) { |
| 164 | return; |
| 165 | } |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 166 | bool initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0; |
| 167 | mirror::ClassLoader* class_loader = |
| 168 | down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2)); |
| 169 | StackHandleScope<2> hs(self); |
| 170 | Handle<mirror::String> h_class_name(hs.NewHandle(class_name)); |
| 171 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); |
| 172 | UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, "Class.classForName", |
| 173 | initialize_class, false); |
| 174 | CheckExceptionGenerateClassNotFound(self); |
| 175 | } |
| 176 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 177 | void UnstartedRuntime::UnstartedClassNewInstance( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 178 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
| 179 | StackHandleScope<2> hs(self); // Class, constructor, object. |
Andreas Gampe | 5d4bb1d | 2015-04-14 22:16:14 -0700 | [diff] [blame] | 180 | mirror::Object* param = shadow_frame->GetVRegReference(arg_offset); |
| 181 | if (param == nullptr) { |
| 182 | AbortTransactionOrFail(self, "Null-pointer in Class.newInstance."); |
| 183 | return; |
| 184 | } |
| 185 | mirror::Class* klass = param->AsClass(); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 186 | Handle<mirror::Class> h_klass(hs.NewHandle(klass)); |
Andreas Gampe | 0f7e3d6 | 2015-03-11 13:24:35 -0700 | [diff] [blame] | 187 | |
| 188 | // Check that it's not null. |
| 189 | if (h_klass.Get() == nullptr) { |
| 190 | AbortTransactionOrFail(self, "Class reference is null for newInstance"); |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | // If we're in a transaction, class must not be finalizable (it or a superclass has a finalizer). |
| 195 | if (Runtime::Current()->IsActiveTransaction()) { |
| 196 | if (h_klass.Get()->IsFinalizable()) { |
Sebastien Hertz | 45b1597 | 2015-04-03 16:07:05 +0200 | [diff] [blame] | 197 | AbortTransactionF(self, "Class for newInstance is finalizable: '%s'", |
| 198 | PrettyClass(h_klass.Get()).c_str()); |
Andreas Gampe | 0f7e3d6 | 2015-03-11 13:24:35 -0700 | [diff] [blame] | 199 | return; |
| 200 | } |
| 201 | } |
| 202 | |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 203 | // There are two situations in which we'll abort this run. |
| 204 | // 1) If the class isn't yet initialized and initialization fails. |
| 205 | // 2) If we can't find the default constructor. We'll postpone the exception to runtime. |
| 206 | // Note that 2) could likely be handled here, but for safety abort the transaction. |
| 207 | bool ok = false; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 208 | auto* cl = Runtime::Current()->GetClassLinker(); |
| 209 | if (cl->EnsureInitialized(self, h_klass, true, true)) { |
| 210 | auto* cons = h_klass->FindDeclaredDirectMethod("<init>", "()V", cl->GetImagePointerSize()); |
| 211 | if (cons != nullptr) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 212 | Handle<mirror::Object> h_obj(hs.NewHandle(klass->AllocObject(self))); |
| 213 | 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] | 214 | EnterInterpreterFromInvoke(self, cons, h_obj.Get(), nullptr, nullptr); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 215 | if (!self->IsExceptionPending()) { |
| 216 | result->SetL(h_obj.Get()); |
| 217 | ok = true; |
| 218 | } |
| 219 | } else { |
| 220 | self->ThrowNewExceptionF("Ljava/lang/InternalError;", |
| 221 | "Could not find default constructor for '%s'", |
| 222 | PrettyClass(h_klass.Get()).c_str()); |
| 223 | } |
| 224 | } |
| 225 | if (!ok) { |
Andreas Gampe | 068b0c0 | 2015-03-11 12:44:47 -0700 | [diff] [blame] | 226 | AbortTransactionOrFail(self, "Failed in Class.newInstance for '%s' with %s", |
| 227 | PrettyClass(h_klass.Get()).c_str(), |
| 228 | PrettyTypeOf(self->GetException()).c_str()); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 232 | void UnstartedRuntime::UnstartedClassGetDeclaredField( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 233 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 234 | // Special managed code cut-out to allow field lookup in a un-started runtime that'd fail |
| 235 | // going the reflective Dex way. |
| 236 | mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass(); |
| 237 | mirror::String* name2 = shadow_frame->GetVRegReference(arg_offset + 1)->AsString(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 238 | ArtField* found = nullptr; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 239 | for (ArtField& field : klass->GetIFields()) { |
| 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 | if (found == nullptr) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 246 | for (ArtField& field : klass->GetSFields()) { |
| 247 | if (name2->Equals(field.GetName())) { |
| 248 | found = &field; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 249 | break; |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | } |
Andreas Gampe | 068b0c0 | 2015-03-11 12:44:47 -0700 | [diff] [blame] | 253 | if (found == nullptr) { |
| 254 | AbortTransactionOrFail(self, "Failed to find field in Class.getDeclaredField in un-started " |
| 255 | " runtime. name=%s class=%s", name2->ToModifiedUtf8().c_str(), |
| 256 | PrettyDescriptor(klass).c_str()); |
| 257 | return; |
| 258 | } |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 259 | if (Runtime::Current()->IsActiveTransaction()) { |
| 260 | result->SetL(mirror::Field::CreateFromArtField<true>(self, found, true)); |
| 261 | } else { |
| 262 | result->SetL(mirror::Field::CreateFromArtField<false>(self, found, true)); |
| 263 | } |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Andreas Gampe | bc4d218 | 2016-02-22 10:03:12 -0800 | [diff] [blame] | 266 | // This is required for Enum(Set) code, as that uses reflection to inspect enum classes. |
| 267 | void UnstartedRuntime::UnstartedClassGetDeclaredMethod( |
| 268 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
| 269 | // Special managed code cut-out to allow method lookup in a un-started runtime. |
| 270 | mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass(); |
| 271 | if (klass == nullptr) { |
| 272 | ThrowNullPointerExceptionForMethodAccess(shadow_frame->GetMethod(), InvokeType::kVirtual); |
| 273 | return; |
| 274 | } |
| 275 | mirror::String* name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString(); |
| 276 | mirror::ObjectArray<mirror::Class>* args = |
| 277 | shadow_frame->GetVRegReference(arg_offset + 2)->AsObjectArray<mirror::Class>(); |
| 278 | if (Runtime::Current()->IsActiveTransaction()) { |
| 279 | result->SetL(mirror::Class::GetDeclaredMethodInternal<true>(self, klass, name, args)); |
| 280 | } else { |
| 281 | result->SetL(mirror::Class::GetDeclaredMethodInternal<false>(self, klass, name, args)); |
| 282 | } |
| 283 | } |
| 284 | |
Andreas Gampe | 633750c | 2016-02-19 10:49:50 -0800 | [diff] [blame] | 285 | void UnstartedRuntime::UnstartedClassGetEnclosingClass( |
| 286 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
| 287 | StackHandleScope<1> hs(self); |
| 288 | Handle<mirror::Class> klass(hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsClass())); |
| 289 | if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) { |
| 290 | result->SetL(nullptr); |
| 291 | } |
| 292 | result->SetL(klass->GetDexFile().GetEnclosingClass(klass)); |
| 293 | } |
| 294 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 295 | void UnstartedRuntime::UnstartedVmClassLoaderFindLoadedClass( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 296 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 297 | mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString(); |
| 298 | mirror::ClassLoader* class_loader = |
| 299 | down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset)); |
| 300 | StackHandleScope<2> hs(self); |
| 301 | Handle<mirror::String> h_class_name(hs.NewHandle(class_name)); |
| 302 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); |
| 303 | UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, |
| 304 | "VMClassLoader.findLoadedClass", false, false); |
| 305 | // This might have an error pending. But semantics are to just return null. |
| 306 | if (self->IsExceptionPending()) { |
| 307 | // If it is an InternalError, keep it. See CheckExceptionGenerateClassNotFound. |
| 308 | std::string type(PrettyTypeOf(self->GetException())); |
| 309 | if (type != "java.lang.InternalError") { |
| 310 | self->ClearException(); |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 315 | void UnstartedRuntime::UnstartedVoidLookupType( |
| 316 | Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED, JValue* result, |
| 317 | size_t arg_offset ATTRIBUTE_UNUSED) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 318 | result->SetL(Runtime::Current()->GetClassLinker()->FindPrimitiveClass('V')); |
| 319 | } |
| 320 | |
Andreas Gampe | 8e6c3fd | 2015-03-11 18:34:44 -0700 | [diff] [blame] | 321 | // Arraycopy emulation. |
| 322 | // Note: we can't use any fast copy functions, as they are not available under transaction. |
| 323 | |
| 324 | template <typename T> |
| 325 | static void PrimitiveArrayCopy(Thread* self, |
| 326 | mirror::Array* src_array, int32_t src_pos, |
| 327 | mirror::Array* dst_array, int32_t dst_pos, |
| 328 | int32_t length) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 329 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Andreas Gampe | 8e6c3fd | 2015-03-11 18:34:44 -0700 | [diff] [blame] | 330 | if (src_array->GetClass()->GetComponentType() != dst_array->GetClass()->GetComponentType()) { |
| 331 | AbortTransactionOrFail(self, "Types mismatched in arraycopy: %s vs %s.", |
| 332 | PrettyDescriptor(src_array->GetClass()->GetComponentType()).c_str(), |
| 333 | PrettyDescriptor(dst_array->GetClass()->GetComponentType()).c_str()); |
| 334 | return; |
| 335 | } |
| 336 | mirror::PrimitiveArray<T>* src = down_cast<mirror::PrimitiveArray<T>*>(src_array); |
| 337 | mirror::PrimitiveArray<T>* dst = down_cast<mirror::PrimitiveArray<T>*>(dst_array); |
| 338 | const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length); |
| 339 | if (copy_forward) { |
| 340 | for (int32_t i = 0; i < length; ++i) { |
| 341 | dst->Set(dst_pos + i, src->Get(src_pos + i)); |
| 342 | } |
| 343 | } else { |
| 344 | for (int32_t i = 1; i <= length; ++i) { |
| 345 | dst->Set(dst_pos + length - i, src->Get(src_pos + length - i)); |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 350 | void UnstartedRuntime::UnstartedSystemArraycopy( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 351 | 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] | 352 | // Special case array copying without initializing System. |
Andreas Gampe | 8e6c3fd | 2015-03-11 18:34:44 -0700 | [diff] [blame] | 353 | jint src_pos = shadow_frame->GetVReg(arg_offset + 1); |
| 354 | jint dst_pos = shadow_frame->GetVReg(arg_offset + 3); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 355 | jint length = shadow_frame->GetVReg(arg_offset + 4); |
Andreas Gampe | 8e6c3fd | 2015-03-11 18:34:44 -0700 | [diff] [blame] | 356 | |
Andreas Gampe | 85a098a | 2016-03-31 13:30:53 -0700 | [diff] [blame] | 357 | mirror::Object* src_obj = shadow_frame->GetVRegReference(arg_offset); |
| 358 | mirror::Object* dst_obj = shadow_frame->GetVRegReference(arg_offset + 2); |
| 359 | // Null checking. For simplicity, abort transaction. |
| 360 | if (src_obj == nullptr) { |
Andreas Gampe | 8e6c3fd | 2015-03-11 18:34:44 -0700 | [diff] [blame] | 361 | AbortTransactionOrFail(self, "src is null in arraycopy."); |
| 362 | return; |
| 363 | } |
Andreas Gampe | 85a098a | 2016-03-31 13:30:53 -0700 | [diff] [blame] | 364 | if (dst_obj == nullptr) { |
Andreas Gampe | 8e6c3fd | 2015-03-11 18:34:44 -0700 | [diff] [blame] | 365 | AbortTransactionOrFail(self, "dst is null in arraycopy."); |
| 366 | return; |
| 367 | } |
Andreas Gampe | 85a098a | 2016-03-31 13:30:53 -0700 | [diff] [blame] | 368 | // Test for arrayness. Throw ArrayStoreException. |
| 369 | if (!src_obj->IsArrayInstance() || !dst_obj->IsArrayInstance()) { |
| 370 | self->ThrowNewException("Ljava/lang/ArrayStoreException;", "src or trg is not an array"); |
| 371 | return; |
| 372 | } |
Andreas Gampe | 8e6c3fd | 2015-03-11 18:34:44 -0700 | [diff] [blame] | 373 | |
Andreas Gampe | 85a098a | 2016-03-31 13:30:53 -0700 | [diff] [blame] | 374 | mirror::Array* src_array = src_obj->AsArray(); |
| 375 | mirror::Array* dst_array = dst_obj->AsArray(); |
| 376 | |
| 377 | // Bounds checking. Throw IndexOutOfBoundsException. |
Andreas Gampe | 8e6c3fd | 2015-03-11 18:34:44 -0700 | [diff] [blame] | 378 | if (UNLIKELY(src_pos < 0) || UNLIKELY(dst_pos < 0) || UNLIKELY(length < 0) || |
| 379 | UNLIKELY(src_pos > src_array->GetLength() - length) || |
| 380 | UNLIKELY(dst_pos > dst_array->GetLength() - length)) { |
Andreas Gampe | 85a098a | 2016-03-31 13:30:53 -0700 | [diff] [blame] | 381 | self->ThrowNewExceptionF("Ljava/lang/IndexOutOfBoundsException;", |
Andreas Gampe | 8e6c3fd | 2015-03-11 18:34:44 -0700 | [diff] [blame] | 382 | "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d", |
| 383 | src_array->GetLength(), src_pos, dst_array->GetLength(), dst_pos, |
| 384 | length); |
Andreas Gampe | 8e6c3fd | 2015-03-11 18:34:44 -0700 | [diff] [blame] | 385 | return; |
| 386 | } |
| 387 | |
| 388 | // Type checking. |
| 389 | mirror::Class* src_type = shadow_frame->GetVRegReference(arg_offset)->GetClass()-> |
| 390 | GetComponentType(); |
| 391 | |
| 392 | if (!src_type->IsPrimitive()) { |
| 393 | // Check that the second type is not primitive. |
| 394 | mirror::Class* trg_type = shadow_frame->GetVRegReference(arg_offset + 2)->GetClass()-> |
| 395 | GetComponentType(); |
| 396 | if (trg_type->IsPrimitiveInt()) { |
| 397 | AbortTransactionOrFail(self, "Type mismatch in arraycopy: %s vs %s", |
| 398 | PrettyDescriptor(src_array->GetClass()->GetComponentType()).c_str(), |
| 399 | PrettyDescriptor(dst_array->GetClass()->GetComponentType()).c_str()); |
| 400 | return; |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 401 | } |
Andreas Gampe | 8e6c3fd | 2015-03-11 18:34:44 -0700 | [diff] [blame] | 402 | |
Andreas Gampe | 8e6c3fd | 2015-03-11 18:34:44 -0700 | [diff] [blame] | 403 | mirror::ObjectArray<mirror::Object>* src = src_array->AsObjectArray<mirror::Object>(); |
| 404 | mirror::ObjectArray<mirror::Object>* dst = dst_array->AsObjectArray<mirror::Object>(); |
| 405 | if (src == dst) { |
| 406 | // Can overlap, but not have type mismatches. |
Andreas Gampe | 85a098a | 2016-03-31 13:30:53 -0700 | [diff] [blame] | 407 | // We cannot use ObjectArray::MemMove here, as it doesn't support transactions. |
Andreas Gampe | 8e6c3fd | 2015-03-11 18:34:44 -0700 | [diff] [blame] | 408 | const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length); |
| 409 | if (copy_forward) { |
| 410 | for (int32_t i = 0; i < length; ++i) { |
| 411 | dst->Set(dst_pos + i, src->Get(src_pos + i)); |
| 412 | } |
| 413 | } else { |
| 414 | for (int32_t i = 1; i <= length; ++i) { |
| 415 | dst->Set(dst_pos + length - i, src->Get(src_pos + length - i)); |
| 416 | } |
| 417 | } |
| 418 | } else { |
Andreas Gampe | 85a098a | 2016-03-31 13:30:53 -0700 | [diff] [blame] | 419 | // We're being lazy here. Optimally this could be a memcpy (if component types are |
| 420 | // assignable), but the ObjectArray implementation doesn't support transactions. The |
| 421 | // checking version, however, does. |
| 422 | if (Runtime::Current()->IsActiveTransaction()) { |
| 423 | dst->AssignableCheckingMemcpy<true>( |
| 424 | dst_pos, src, src_pos, length, true /* throw_exception */); |
| 425 | } else { |
| 426 | dst->AssignableCheckingMemcpy<false>( |
| 427 | dst_pos, src, src_pos, length, true /* throw_exception */); |
Andreas Gampe | 8e6c3fd | 2015-03-11 18:34:44 -0700 | [diff] [blame] | 428 | } |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 429 | } |
Andreas Gampe | 5c9af61 | 2016-04-05 14:16:10 -0700 | [diff] [blame^] | 430 | } else if (src_type->IsPrimitiveByte()) { |
| 431 | PrimitiveArrayCopy<uint8_t>(self, src_array, src_pos, dst_array, dst_pos, length); |
Andreas Gampe | 8e6c3fd | 2015-03-11 18:34:44 -0700 | [diff] [blame] | 432 | } else if (src_type->IsPrimitiveChar()) { |
| 433 | PrimitiveArrayCopy<uint16_t>(self, src_array, src_pos, dst_array, dst_pos, length); |
| 434 | } else if (src_type->IsPrimitiveInt()) { |
| 435 | 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] | 436 | } else { |
Andreas Gampe | 068b0c0 | 2015-03-11 12:44:47 -0700 | [diff] [blame] | 437 | AbortTransactionOrFail(self, "Unimplemented System.arraycopy for type '%s'", |
Andreas Gampe | 8e6c3fd | 2015-03-11 18:34:44 -0700 | [diff] [blame] | 438 | PrettyDescriptor(src_type).c_str()); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | |
Andreas Gampe | 5c9af61 | 2016-04-05 14:16:10 -0700 | [diff] [blame^] | 442 | void UnstartedRuntime::UnstartedSystemArraycopyByte( |
| 443 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
| 444 | // Just forward. |
| 445 | UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset); |
| 446 | } |
| 447 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 448 | void UnstartedRuntime::UnstartedSystemArraycopyChar( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 449 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 450 | // Just forward. |
| 451 | UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset); |
| 452 | } |
| 453 | |
| 454 | void UnstartedRuntime::UnstartedSystemArraycopyInt( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 455 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 456 | // Just forward. |
| 457 | UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset); |
| 458 | } |
| 459 | |
Narayan Kamath | 34a316f | 2016-03-30 13:11:18 +0100 | [diff] [blame] | 460 | void UnstartedRuntime::UnstartedSystemGetSecurityManager( |
| 461 | Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED, |
| 462 | JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) { |
| 463 | result->SetL(nullptr); |
| 464 | } |
| 465 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 466 | void UnstartedRuntime::UnstartedThreadLocalGet( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 467 | 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] | 468 | std::string caller(PrettyMethod(shadow_frame->GetLink()->GetMethod())); |
| 469 | bool ok = false; |
Narayan Kamath | a1e9312 | 2016-03-30 15:41:54 +0100 | [diff] [blame] | 470 | if (caller == "void java.lang.FloatingDecimal.developLongDigits(int, long, long)" || |
| 471 | caller == "java.lang.String java.lang.FloatingDecimal.toJavaFormatString()") { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 472 | // Allocate non-threadlocal buffer. |
Narayan Kamath | a1e9312 | 2016-03-30 15:41:54 +0100 | [diff] [blame] | 473 | result->SetL(mirror::CharArray::Alloc(self, 26)); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 474 | ok = true; |
Narayan Kamath | a1e9312 | 2016-03-30 15:41:54 +0100 | [diff] [blame] | 475 | } else if (caller == |
| 476 | "java.lang.FloatingDecimal java.lang.FloatingDecimal.getThreadLocalInstance()") { |
| 477 | // Allocate new object. |
| 478 | StackHandleScope<2> hs(self); |
| 479 | Handle<mirror::Class> h_real_to_string_class(hs.NewHandle( |
| 480 | shadow_frame->GetLink()->GetMethod()->GetDeclaringClass())); |
| 481 | Handle<mirror::Object> h_real_to_string_obj(hs.NewHandle( |
| 482 | h_real_to_string_class->AllocObject(self))); |
| 483 | if (h_real_to_string_obj.Get() != nullptr) { |
| 484 | auto* cl = Runtime::Current()->GetClassLinker(); |
| 485 | ArtMethod* init_method = h_real_to_string_class->FindDirectMethod( |
| 486 | "<init>", "()V", cl->GetImagePointerSize()); |
| 487 | if (init_method == nullptr) { |
| 488 | h_real_to_string_class->DumpClass(LOG(FATAL), mirror::Class::kDumpClassFullDetail); |
| 489 | } else { |
| 490 | JValue invoke_result; |
| 491 | EnterInterpreterFromInvoke(self, init_method, h_real_to_string_obj.Get(), nullptr, |
| 492 | nullptr); |
| 493 | if (!self->IsExceptionPending()) { |
| 494 | result->SetL(h_real_to_string_obj.Get()); |
| 495 | ok = true; |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 496 | } |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | if (!ok) { |
Andreas Gampe | 068b0c0 | 2015-03-11 12:44:47 -0700 | [diff] [blame] | 502 | AbortTransactionOrFail(self, "Could not create RealToString object"); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 506 | void UnstartedRuntime::UnstartedMathCeil( |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 507 | 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] | 508 | double in = shadow_frame->GetVRegDouble(arg_offset); |
| 509 | double out; |
| 510 | // Special cases: |
| 511 | // 1) NaN, infinity, +0, -0 -> out := in. All are guaranteed by cmath. |
| 512 | // -1 < in < 0 -> out := -0. |
| 513 | if (-1.0 < in && in < 0) { |
| 514 | out = -0.0; |
| 515 | } else { |
| 516 | out = ceil(in); |
| 517 | } |
| 518 | result->SetD(out); |
| 519 | } |
| 520 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 521 | void UnstartedRuntime::UnstartedObjectHashCode( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 522 | 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] | 523 | mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset); |
| 524 | result->SetI(obj->IdentityHashCode()); |
| 525 | } |
| 526 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 527 | void UnstartedRuntime::UnstartedDoubleDoubleToRawLongBits( |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 528 | 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] | 529 | double in = shadow_frame->GetVRegDouble(arg_offset); |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 530 | result->SetJ(bit_cast<int64_t, double>(in)); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 531 | } |
| 532 | |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 533 | static mirror::Object* GetDexFromDexCache(Thread* self, mirror::DexCache* dex_cache) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 534 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 535 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 536 | if (dex_file == nullptr) { |
| 537 | return nullptr; |
| 538 | } |
| 539 | |
| 540 | // Create the direct byte buffer. |
| 541 | JNIEnv* env = self->GetJniEnv(); |
| 542 | DCHECK(env != nullptr); |
| 543 | void* address = const_cast<void*>(reinterpret_cast<const void*>(dex_file->Begin())); |
Andreas Gampe | aacc25d | 2015-04-01 14:49:06 -0700 | [diff] [blame] | 544 | ScopedLocalRef<jobject> byte_buffer(env, env->NewDirectByteBuffer(address, dex_file->Size())); |
| 545 | if (byte_buffer.get() == nullptr) { |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 546 | DCHECK(self->IsExceptionPending()); |
| 547 | return nullptr; |
| 548 | } |
| 549 | |
| 550 | jvalue args[1]; |
Andreas Gampe | aacc25d | 2015-04-01 14:49:06 -0700 | [diff] [blame] | 551 | args[0].l = byte_buffer.get(); |
| 552 | |
| 553 | ScopedLocalRef<jobject> dex(env, env->CallStaticObjectMethodA( |
| 554 | WellKnownClasses::com_android_dex_Dex, |
| 555 | WellKnownClasses::com_android_dex_Dex_create, |
| 556 | args)); |
| 557 | |
| 558 | return self->DecodeJObject(dex.get()); |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 559 | } |
| 560 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 561 | void UnstartedRuntime::UnstartedDexCacheGetDexNative( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 562 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 563 | // We will create the Dex object, but the image writer will release it before creating the |
| 564 | // art file. |
| 565 | mirror::Object* src = shadow_frame->GetVRegReference(arg_offset); |
| 566 | bool have_dex = false; |
| 567 | if (src != nullptr) { |
| 568 | mirror::Object* dex = GetDexFromDexCache(self, reinterpret_cast<mirror::DexCache*>(src)); |
| 569 | if (dex != nullptr) { |
| 570 | have_dex = true; |
| 571 | result->SetL(dex); |
| 572 | } |
| 573 | } |
| 574 | if (!have_dex) { |
| 575 | self->ClearException(); |
Sebastien Hertz | 2fd7e69 | 2015-04-02 11:11:19 +0200 | [diff] [blame] | 576 | Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Could not create Dex object"); |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | |
| 580 | static void UnstartedMemoryPeek( |
| 581 | Primitive::Type type, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
| 582 | int64_t address = shadow_frame->GetVRegLong(arg_offset); |
| 583 | // TODO: Check that this is in the heap somewhere. Otherwise we will segfault instead of |
| 584 | // aborting the transaction. |
| 585 | |
| 586 | switch (type) { |
| 587 | case Primitive::kPrimByte: { |
| 588 | result->SetB(*reinterpret_cast<int8_t*>(static_cast<intptr_t>(address))); |
| 589 | return; |
| 590 | } |
| 591 | |
| 592 | case Primitive::kPrimShort: { |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 593 | typedef int16_t unaligned_short __attribute__ ((aligned (1))); |
| 594 | result->SetS(*reinterpret_cast<unaligned_short*>(static_cast<intptr_t>(address))); |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 595 | return; |
| 596 | } |
| 597 | |
| 598 | case Primitive::kPrimInt: { |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 599 | typedef int32_t unaligned_int __attribute__ ((aligned (1))); |
| 600 | result->SetI(*reinterpret_cast<unaligned_int*>(static_cast<intptr_t>(address))); |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 601 | return; |
| 602 | } |
| 603 | |
| 604 | case Primitive::kPrimLong: { |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 605 | typedef int64_t unaligned_long __attribute__ ((aligned (1))); |
| 606 | result->SetJ(*reinterpret_cast<unaligned_long*>(static_cast<intptr_t>(address))); |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 607 | return; |
| 608 | } |
| 609 | |
| 610 | case Primitive::kPrimBoolean: |
| 611 | case Primitive::kPrimChar: |
| 612 | case Primitive::kPrimFloat: |
| 613 | case Primitive::kPrimDouble: |
| 614 | case Primitive::kPrimVoid: |
| 615 | case Primitive::kPrimNot: |
| 616 | LOG(FATAL) << "Not in the Memory API: " << type; |
| 617 | UNREACHABLE(); |
| 618 | } |
| 619 | LOG(FATAL) << "Should not reach here"; |
| 620 | UNREACHABLE(); |
| 621 | } |
| 622 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 623 | void UnstartedRuntime::UnstartedMemoryPeekByte( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 624 | 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] | 625 | UnstartedMemoryPeek(Primitive::kPrimByte, shadow_frame, result, arg_offset); |
| 626 | } |
| 627 | |
| 628 | void UnstartedRuntime::UnstartedMemoryPeekShort( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 629 | 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] | 630 | UnstartedMemoryPeek(Primitive::kPrimShort, shadow_frame, result, arg_offset); |
| 631 | } |
| 632 | |
| 633 | void UnstartedRuntime::UnstartedMemoryPeekInt( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 634 | 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] | 635 | UnstartedMemoryPeek(Primitive::kPrimInt, shadow_frame, result, arg_offset); |
| 636 | } |
| 637 | |
| 638 | void UnstartedRuntime::UnstartedMemoryPeekLong( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 639 | 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] | 640 | UnstartedMemoryPeek(Primitive::kPrimLong, shadow_frame, result, arg_offset); |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | static void UnstartedMemoryPeekArray( |
| 644 | Primitive::Type type, Thread* self, ShadowFrame* shadow_frame, size_t arg_offset) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 645 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 646 | int64_t address_long = shadow_frame->GetVRegLong(arg_offset); |
| 647 | mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 2); |
| 648 | if (obj == nullptr) { |
Sebastien Hertz | 2fd7e69 | 2015-04-02 11:11:19 +0200 | [diff] [blame] | 649 | Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Null pointer in peekArray"); |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 650 | return; |
| 651 | } |
| 652 | mirror::Array* array = obj->AsArray(); |
| 653 | |
| 654 | int offset = shadow_frame->GetVReg(arg_offset + 3); |
| 655 | int count = shadow_frame->GetVReg(arg_offset + 4); |
| 656 | if (offset < 0 || offset + count > array->GetLength()) { |
| 657 | std::string error_msg(StringPrintf("Array out of bounds in peekArray: %d/%d vs %d", |
| 658 | offset, count, array->GetLength())); |
Sebastien Hertz | 2fd7e69 | 2015-04-02 11:11:19 +0200 | [diff] [blame] | 659 | Runtime::Current()->AbortTransactionAndThrowAbortError(self, error_msg.c_str()); |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 660 | return; |
| 661 | } |
| 662 | |
| 663 | switch (type) { |
| 664 | case Primitive::kPrimByte: { |
| 665 | int8_t* address = reinterpret_cast<int8_t*>(static_cast<intptr_t>(address_long)); |
| 666 | mirror::ByteArray* byte_array = array->AsByteArray(); |
| 667 | for (int32_t i = 0; i < count; ++i, ++address) { |
| 668 | byte_array->SetWithoutChecks<true>(i + offset, *address); |
| 669 | } |
| 670 | return; |
| 671 | } |
| 672 | |
| 673 | case Primitive::kPrimShort: |
| 674 | case Primitive::kPrimInt: |
| 675 | case Primitive::kPrimLong: |
| 676 | LOG(FATAL) << "Type unimplemented for Memory Array API, should not reach here: " << type; |
| 677 | UNREACHABLE(); |
| 678 | |
| 679 | case Primitive::kPrimBoolean: |
| 680 | case Primitive::kPrimChar: |
| 681 | case Primitive::kPrimFloat: |
| 682 | case Primitive::kPrimDouble: |
| 683 | case Primitive::kPrimVoid: |
| 684 | case Primitive::kPrimNot: |
| 685 | LOG(FATAL) << "Not in the Memory API: " << type; |
| 686 | UNREACHABLE(); |
| 687 | } |
| 688 | LOG(FATAL) << "Should not reach here"; |
| 689 | UNREACHABLE(); |
| 690 | } |
| 691 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 692 | void UnstartedRuntime::UnstartedMemoryPeekByteArray( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 693 | 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] | 694 | UnstartedMemoryPeekArray(Primitive::kPrimByte, self, shadow_frame, arg_offset); |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 695 | } |
| 696 | |
Andreas Gampe | f778eb2 | 2015-04-13 14:17:09 -0700 | [diff] [blame] | 697 | // This allows reading security.properties in an unstarted runtime and initialize Security. |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 698 | void UnstartedRuntime::UnstartedSecurityGetSecurityPropertiesReader( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 699 | Thread* self, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED, JValue* result, |
| 700 | size_t arg_offset ATTRIBUTE_UNUSED) { |
Andreas Gampe | f778eb2 | 2015-04-13 14:17:09 -0700 | [diff] [blame] | 701 | Runtime* runtime = Runtime::Current(); |
Andreas Gampe | e0f633e | 2016-03-29 19:33:56 -0700 | [diff] [blame] | 702 | |
| 703 | std::vector<std::string> split; |
| 704 | Split(runtime->GetBootClassPathString(), ':', &split); |
| 705 | if (split.empty()) { |
| 706 | AbortTransactionOrFail(self, |
| 707 | "Boot classpath not set or split error:: %s", |
| 708 | runtime->GetBootClassPathString().c_str()); |
| 709 | return; |
| 710 | } |
| 711 | const std::string& source = split[0]; |
| 712 | |
Andreas Gampe | f778eb2 | 2015-04-13 14:17:09 -0700 | [diff] [blame] | 713 | mirror::String* string_data; |
| 714 | |
| 715 | // Use a block to enclose the I/O and MemMap code so buffers are released early. |
| 716 | { |
| 717 | std::string error_msg; |
Andreas Gampe | e0f633e | 2016-03-29 19:33:56 -0700 | [diff] [blame] | 718 | std::unique_ptr<ZipArchive> zip_archive(ZipArchive::Open(source.c_str(), &error_msg)); |
Andreas Gampe | f778eb2 | 2015-04-13 14:17:09 -0700 | [diff] [blame] | 719 | if (zip_archive.get() == nullptr) { |
Andreas Gampe | e0f633e | 2016-03-29 19:33:56 -0700 | [diff] [blame] | 720 | AbortTransactionOrFail(self, |
| 721 | "Could not open zip file %s: %s", |
| 722 | source.c_str(), |
Andreas Gampe | f778eb2 | 2015-04-13 14:17:09 -0700 | [diff] [blame] | 723 | error_msg.c_str()); |
| 724 | return; |
| 725 | } |
| 726 | std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find("java/security/security.properties", |
| 727 | &error_msg)); |
| 728 | if (zip_entry.get() == nullptr) { |
Andreas Gampe | e0f633e | 2016-03-29 19:33:56 -0700 | [diff] [blame] | 729 | AbortTransactionOrFail(self, |
| 730 | "Could not find security.properties file in %s: %s", |
| 731 | source.c_str(), |
| 732 | error_msg.c_str()); |
Andreas Gampe | f778eb2 | 2015-04-13 14:17:09 -0700 | [diff] [blame] | 733 | return; |
| 734 | } |
Andreas Gampe | e0f633e | 2016-03-29 19:33:56 -0700 | [diff] [blame] | 735 | std::unique_ptr<MemMap> map(zip_entry->ExtractToMemMap(source.c_str(), |
Andreas Gampe | f778eb2 | 2015-04-13 14:17:09 -0700 | [diff] [blame] | 736 | "java/security/security.properties", |
| 737 | &error_msg)); |
| 738 | if (map.get() == nullptr) { |
Andreas Gampe | e0f633e | 2016-03-29 19:33:56 -0700 | [diff] [blame] | 739 | AbortTransactionOrFail(self, |
| 740 | "Could not unzip security.properties file in %s: %s", |
| 741 | source.c_str(), |
| 742 | error_msg.c_str()); |
Andreas Gampe | f778eb2 | 2015-04-13 14:17:09 -0700 | [diff] [blame] | 743 | return; |
| 744 | } |
| 745 | |
| 746 | uint32_t length = zip_entry->GetUncompressedLength(); |
| 747 | std::unique_ptr<char[]> tmp(new char[length + 1]); |
| 748 | memcpy(tmp.get(), map->Begin(), length); |
| 749 | tmp.get()[length] = 0; // null terminator |
| 750 | |
| 751 | string_data = mirror::String::AllocFromModifiedUtf8(self, tmp.get()); |
| 752 | } |
| 753 | |
| 754 | if (string_data == nullptr) { |
Andreas Gampe | e0f633e | 2016-03-29 19:33:56 -0700 | [diff] [blame] | 755 | AbortTransactionOrFail(self, "Could not create string from file content of %s", source.c_str()); |
Andreas Gampe | f778eb2 | 2015-04-13 14:17:09 -0700 | [diff] [blame] | 756 | return; |
| 757 | } |
| 758 | |
| 759 | // Create a StringReader. |
| 760 | StackHandleScope<3> hs(self); |
| 761 | Handle<mirror::String> h_string(hs.NewHandle(string_data)); |
| 762 | |
| 763 | Handle<mirror::Class> h_class(hs.NewHandle( |
| 764 | runtime->GetClassLinker()->FindClass(self, |
| 765 | "Ljava/io/StringReader;", |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 766 | ScopedNullHandle<mirror::ClassLoader>()))); |
Andreas Gampe | f778eb2 | 2015-04-13 14:17:09 -0700 | [diff] [blame] | 767 | if (h_class.Get() == nullptr) { |
| 768 | AbortTransactionOrFail(self, "Could not find StringReader class"); |
| 769 | return; |
| 770 | } |
| 771 | |
| 772 | if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) { |
| 773 | AbortTransactionOrFail(self, "Could not initialize StringReader class"); |
| 774 | return; |
| 775 | } |
| 776 | |
| 777 | Handle<mirror::Object> h_obj(hs.NewHandle(h_class->AllocObject(self))); |
| 778 | if (h_obj.Get() == nullptr) { |
| 779 | AbortTransactionOrFail(self, "Could not allocate StringReader object"); |
| 780 | return; |
| 781 | } |
| 782 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 783 | auto* cl = Runtime::Current()->GetClassLinker(); |
| 784 | ArtMethod* constructor = h_class->FindDeclaredDirectMethod( |
| 785 | "<init>", "(Ljava/lang/String;)V", cl->GetImagePointerSize()); |
Andreas Gampe | f778eb2 | 2015-04-13 14:17:09 -0700 | [diff] [blame] | 786 | if (constructor == nullptr) { |
| 787 | AbortTransactionOrFail(self, "Could not find StringReader constructor"); |
| 788 | return; |
| 789 | } |
| 790 | |
| 791 | uint32_t args[1]; |
| 792 | args[0] = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(h_string.Get())); |
| 793 | EnterInterpreterFromInvoke(self, constructor, h_obj.Get(), args, nullptr); |
| 794 | |
| 795 | if (self->IsExceptionPending()) { |
| 796 | AbortTransactionOrFail(self, "Could not run StringReader constructor"); |
| 797 | return; |
| 798 | } |
| 799 | |
| 800 | result->SetL(h_obj.Get()); |
| 801 | } |
| 802 | |
Kenny Root | 1c9e61c | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 803 | // This allows reading the new style of String objects during compilation. |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 804 | void UnstartedRuntime::UnstartedStringGetCharsNoCheck( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 805 | 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] | 806 | jint start = shadow_frame->GetVReg(arg_offset + 1); |
| 807 | jint end = shadow_frame->GetVReg(arg_offset + 2); |
| 808 | jint index = shadow_frame->GetVReg(arg_offset + 4); |
| 809 | mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString(); |
| 810 | if (string == nullptr) { |
| 811 | AbortTransactionOrFail(self, "String.getCharsNoCheck with null object"); |
| 812 | return; |
| 813 | } |
Kenny Root | 57f91e8 | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 814 | DCHECK_GE(start, 0); |
| 815 | DCHECK_GE(end, string->GetLength()); |
Kenny Root | 1c9e61c | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 816 | StackHandleScope<1> hs(self); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 817 | Handle<mirror::CharArray> h_char_array( |
| 818 | hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 3)->AsCharArray())); |
Kenny Root | 57f91e8 | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 819 | DCHECK_LE(index, h_char_array->GetLength()); |
| 820 | DCHECK_LE(end - start, h_char_array->GetLength() - index); |
Kenny Root | 1c9e61c | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 821 | string->GetChars(start, end, h_char_array, index); |
| 822 | } |
| 823 | |
| 824 | // 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] | 825 | void UnstartedRuntime::UnstartedStringCharAt( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 826 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
Kenny Root | 1c9e61c | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 827 | jint index = shadow_frame->GetVReg(arg_offset + 1); |
| 828 | mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString(); |
| 829 | if (string == nullptr) { |
| 830 | AbortTransactionOrFail(self, "String.charAt with null object"); |
| 831 | return; |
| 832 | } |
| 833 | result->SetC(string->CharAt(index)); |
| 834 | } |
| 835 | |
Kenny Root | 57f91e8 | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 836 | // 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] | 837 | void UnstartedRuntime::UnstartedStringSetCharAt( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 838 | 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] | 839 | jint index = shadow_frame->GetVReg(arg_offset + 1); |
| 840 | jchar c = shadow_frame->GetVReg(arg_offset + 2); |
| 841 | mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString(); |
| 842 | if (string == nullptr) { |
| 843 | AbortTransactionOrFail(self, "String.setCharAt with null object"); |
| 844 | return; |
| 845 | } |
| 846 | string->SetCharAt(index, c); |
| 847 | } |
| 848 | |
Kenny Root | 1c9e61c | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 849 | // This allows creating the new style of String objects during compilation. |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 850 | void UnstartedRuntime::UnstartedStringFactoryNewStringFromChars( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 851 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
Kenny Root | 1c9e61c | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 852 | jint offset = shadow_frame->GetVReg(arg_offset); |
| 853 | jint char_count = shadow_frame->GetVReg(arg_offset + 1); |
| 854 | DCHECK_GE(char_count, 0); |
| 855 | StackHandleScope<1> hs(self); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 856 | Handle<mirror::CharArray> h_char_array( |
| 857 | hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray())); |
Kenny Root | 1c9e61c | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 858 | Runtime* runtime = Runtime::Current(); |
| 859 | gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator(); |
| 860 | result->SetL(mirror::String::AllocFromCharArray<true>(self, char_count, h_char_array, offset, allocator)); |
| 861 | } |
| 862 | |
| 863 | // This allows creating the new style of String objects during compilation. |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 864 | void UnstartedRuntime::UnstartedStringFactoryNewStringFromString( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 865 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
Kenny Root | 57f91e8 | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 866 | mirror::String* to_copy = shadow_frame->GetVRegReference(arg_offset)->AsString(); |
| 867 | if (to_copy == nullptr) { |
| 868 | AbortTransactionOrFail(self, "StringFactory.newStringFromString with null object"); |
| 869 | return; |
| 870 | } |
| 871 | StackHandleScope<1> hs(self); |
| 872 | Handle<mirror::String> h_string(hs.NewHandle(to_copy)); |
| 873 | Runtime* runtime = Runtime::Current(); |
| 874 | gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator(); |
| 875 | result->SetL(mirror::String::AllocFromString<true>(self, h_string->GetLength(), h_string, 0, |
| 876 | allocator)); |
| 877 | } |
| 878 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 879 | void UnstartedRuntime::UnstartedStringFastSubstring( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 880 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
Kenny Root | 1c9e61c | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 881 | jint start = shadow_frame->GetVReg(arg_offset + 1); |
| 882 | jint length = shadow_frame->GetVReg(arg_offset + 2); |
Kenny Root | 57f91e8 | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 883 | DCHECK_GE(start, 0); |
Kenny Root | 1c9e61c | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 884 | DCHECK_GE(length, 0); |
| 885 | StackHandleScope<1> hs(self); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 886 | Handle<mirror::String> h_string( |
| 887 | hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsString())); |
Kenny Root | 57f91e8 | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 888 | DCHECK_LE(start, h_string->GetLength()); |
| 889 | DCHECK_LE(start + length, h_string->GetLength()); |
Kenny Root | 1c9e61c | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 890 | Runtime* runtime = Runtime::Current(); |
| 891 | gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator(); |
| 892 | result->SetL(mirror::String::AllocFromString<true>(self, length, h_string, start, allocator)); |
| 893 | } |
| 894 | |
Kenny Root | 57f91e8 | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 895 | // 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] | 896 | void UnstartedRuntime::UnstartedStringToCharArray( |
Kenny Root | 57f91e8 | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 897 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 898 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Kenny Root | 57f91e8 | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 899 | mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString(); |
| 900 | if (string == nullptr) { |
| 901 | AbortTransactionOrFail(self, "String.charAt with null object"); |
| 902 | return; |
| 903 | } |
| 904 | result->SetL(string->ToCharArray(self)); |
| 905 | } |
| 906 | |
Andreas Gampe | bc4d218 | 2016-02-22 10:03:12 -0800 | [diff] [blame] | 907 | // This allows statically initializing ConcurrentHashMap and SynchronousQueue. |
| 908 | void UnstartedRuntime::UnstartedReferenceGetReferent( |
| 909 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
| 910 | mirror::Reference* const ref = down_cast<mirror::Reference*>( |
| 911 | shadow_frame->GetVRegReference(arg_offset)); |
| 912 | if (ref == nullptr) { |
| 913 | AbortTransactionOrFail(self, "Reference.getReferent() with null object"); |
| 914 | return; |
| 915 | } |
| 916 | mirror::Object* const referent = |
| 917 | Runtime::Current()->GetHeap()->GetReferenceProcessor()->GetReferent(self, ref); |
| 918 | result->SetL(referent); |
| 919 | } |
| 920 | |
| 921 | // This allows statically initializing ConcurrentHashMap and SynchronousQueue. We use a somewhat |
| 922 | // conservative upper bound. We restrict the callers to SynchronousQueue and ConcurrentHashMap, |
| 923 | // where we can predict the behavior (somewhat). |
| 924 | // Note: this is required (instead of lazy initialization) as these classes are used in the static |
| 925 | // initialization of other classes, so will *use* the value. |
| 926 | void UnstartedRuntime::UnstartedRuntimeAvailableProcessors( |
| 927 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) { |
| 928 | std::string caller(PrettyMethod(shadow_frame->GetLink()->GetMethod())); |
| 929 | if (caller == "void java.util.concurrent.SynchronousQueue.<clinit>()") { |
| 930 | // SynchronousQueue really only separates between single- and multiprocessor case. Return |
| 931 | // 8 as a conservative upper approximation. |
| 932 | result->SetI(8); |
| 933 | } else if (caller == "void java.util.concurrent.ConcurrentHashMap.<clinit>()") { |
| 934 | // ConcurrentHashMap uses it for striding. 8 still seems an OK general value, as it's likely |
| 935 | // a good upper bound. |
| 936 | // TODO: Consider resetting in the zygote? |
| 937 | result->SetI(8); |
| 938 | } else { |
| 939 | // Not supported. |
| 940 | AbortTransactionOrFail(self, "Accessing availableProcessors not allowed"); |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | // This allows accessing ConcurrentHashMap/SynchronousQueue. |
| 945 | |
| 946 | void UnstartedRuntime::UnstartedUnsafeCompareAndSwapLong( |
| 947 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
| 948 | // Argument 0 is the Unsafe instance, skip. |
| 949 | mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1); |
| 950 | if (obj == nullptr) { |
| 951 | AbortTransactionOrFail(self, "Cannot access null object, retry at runtime."); |
| 952 | return; |
| 953 | } |
| 954 | int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2); |
| 955 | int64_t expectedValue = shadow_frame->GetVRegLong(arg_offset + 4); |
| 956 | int64_t newValue = shadow_frame->GetVRegLong(arg_offset + 6); |
| 957 | |
| 958 | // Must use non transactional mode. |
| 959 | if (kUseReadBarrier) { |
| 960 | // Need to make sure the reference stored in the field is a to-space one before attempting the |
| 961 | // CAS or the CAS could fail incorrectly. |
| 962 | mirror::HeapReference<mirror::Object>* field_addr = |
| 963 | reinterpret_cast<mirror::HeapReference<mirror::Object>*>( |
| 964 | reinterpret_cast<uint8_t*>(obj) + static_cast<size_t>(offset)); |
| 965 | ReadBarrier::Barrier<mirror::Object, kWithReadBarrier, /*kAlwaysUpdateField*/true>( |
| 966 | obj, |
| 967 | MemberOffset(offset), |
| 968 | field_addr); |
| 969 | } |
| 970 | bool success; |
| 971 | // Check whether we're in a transaction, call accordingly. |
| 972 | if (Runtime::Current()->IsActiveTransaction()) { |
| 973 | success = obj->CasFieldStrongSequentiallyConsistent64<true>(MemberOffset(offset), |
| 974 | expectedValue, |
| 975 | newValue); |
| 976 | } else { |
| 977 | success = obj->CasFieldStrongSequentiallyConsistent64<false>(MemberOffset(offset), |
| 978 | expectedValue, |
| 979 | newValue); |
| 980 | } |
| 981 | result->SetZ(success ? 1 : 0); |
| 982 | } |
| 983 | |
| 984 | void UnstartedRuntime::UnstartedUnsafeCompareAndSwapObject( |
| 985 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
| 986 | // Argument 0 is the Unsafe instance, skip. |
| 987 | mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1); |
| 988 | if (obj == nullptr) { |
| 989 | AbortTransactionOrFail(self, "Cannot access null object, retry at runtime."); |
| 990 | return; |
| 991 | } |
| 992 | int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2); |
| 993 | mirror::Object* expected_value = shadow_frame->GetVRegReference(arg_offset + 4); |
| 994 | mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 5); |
| 995 | |
| 996 | // Must use non transactional mode. |
| 997 | if (kUseReadBarrier) { |
| 998 | // Need to make sure the reference stored in the field is a to-space one before attempting the |
| 999 | // CAS or the CAS could fail incorrectly. |
| 1000 | mirror::HeapReference<mirror::Object>* field_addr = |
| 1001 | reinterpret_cast<mirror::HeapReference<mirror::Object>*>( |
| 1002 | reinterpret_cast<uint8_t*>(obj) + static_cast<size_t>(offset)); |
| 1003 | ReadBarrier::Barrier<mirror::Object, kWithReadBarrier, /*kAlwaysUpdateField*/true>( |
| 1004 | obj, |
| 1005 | MemberOffset(offset), |
| 1006 | field_addr); |
| 1007 | } |
| 1008 | bool success; |
| 1009 | // Check whether we're in a transaction, call accordingly. |
| 1010 | if (Runtime::Current()->IsActiveTransaction()) { |
| 1011 | success = obj->CasFieldStrongSequentiallyConsistentObject<true>(MemberOffset(offset), |
| 1012 | expected_value, |
| 1013 | newValue); |
| 1014 | } else { |
| 1015 | success = obj->CasFieldStrongSequentiallyConsistentObject<false>(MemberOffset(offset), |
| 1016 | expected_value, |
| 1017 | newValue); |
| 1018 | } |
| 1019 | result->SetZ(success ? 1 : 0); |
| 1020 | } |
| 1021 | |
| 1022 | void UnstartedRuntime::UnstartedUnsafeGetObjectVolatile( |
| 1023 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) |
| 1024 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 1025 | // Argument 0 is the Unsafe instance, skip. |
| 1026 | mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1); |
| 1027 | if (obj == nullptr) { |
| 1028 | AbortTransactionOrFail(self, "Cannot access null object, retry at runtime."); |
| 1029 | return; |
| 1030 | } |
| 1031 | int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2); |
| 1032 | mirror::Object* value = obj->GetFieldObjectVolatile<mirror::Object>(MemberOffset(offset)); |
| 1033 | result->SetL(value); |
| 1034 | } |
| 1035 | |
| 1036 | void UnstartedRuntime::UnstartedUnsafePutOrderedObject( |
| 1037 | Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) |
| 1038 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 1039 | // Argument 0 is the Unsafe instance, skip. |
| 1040 | mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1); |
| 1041 | if (obj == nullptr) { |
| 1042 | AbortTransactionOrFail(self, "Cannot access null object, retry at runtime."); |
| 1043 | return; |
| 1044 | } |
| 1045 | int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2); |
| 1046 | mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 4); |
| 1047 | QuasiAtomic::ThreadFenceRelease(); |
| 1048 | if (Runtime::Current()->IsActiveTransaction()) { |
| 1049 | obj->SetFieldObject<true>(MemberOffset(offset), newValue); |
| 1050 | } else { |
| 1051 | obj->SetFieldObject<false>(MemberOffset(offset), newValue); |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1056 | void UnstartedRuntime::UnstartedJNIVMRuntimeNewUnpaddedArray( |
| 1057 | Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED, |
| 1058 | uint32_t* args, JValue* result) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1059 | int32_t length = args[1]; |
| 1060 | DCHECK_GE(length, 0); |
| 1061 | mirror::Class* element_class = reinterpret_cast<mirror::Object*>(args[0])->AsClass(); |
| 1062 | Runtime* runtime = Runtime::Current(); |
| 1063 | mirror::Class* array_class = runtime->GetClassLinker()->FindArrayClass(self, &element_class); |
| 1064 | DCHECK(array_class != nullptr); |
| 1065 | gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator(); |
| 1066 | result->SetL(mirror::Array::Alloc<true, true>(self, array_class, length, |
| 1067 | array_class->GetComponentSizeShift(), allocator)); |
| 1068 | } |
| 1069 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1070 | void UnstartedRuntime::UnstartedJNIVMStackGetCallingClassLoader( |
| 1071 | Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, |
| 1072 | mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1073 | result->SetL(nullptr); |
| 1074 | } |
| 1075 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1076 | void UnstartedRuntime::UnstartedJNIVMStackGetStackClass2( |
| 1077 | Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED, |
| 1078 | uint32_t* args ATTRIBUTE_UNUSED, JValue* result) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1079 | NthCallerVisitor visitor(self, 3); |
| 1080 | visitor.WalkStack(); |
| 1081 | if (visitor.caller != nullptr) { |
| 1082 | result->SetL(visitor.caller->GetDeclaringClass()); |
| 1083 | } |
| 1084 | } |
| 1085 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1086 | void UnstartedRuntime::UnstartedJNIMathLog( |
| 1087 | Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, |
| 1088 | mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1089 | JValue value; |
| 1090 | value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]); |
| 1091 | result->SetD(log(value.GetD())); |
| 1092 | } |
| 1093 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1094 | void UnstartedRuntime::UnstartedJNIMathExp( |
| 1095 | Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, |
| 1096 | mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1097 | JValue value; |
| 1098 | value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]); |
| 1099 | result->SetD(exp(value.GetD())); |
| 1100 | } |
| 1101 | |
Andreas Gampe | bc4d218 | 2016-02-22 10:03:12 -0800 | [diff] [blame] | 1102 | void UnstartedRuntime::UnstartedJNIAtomicLongVMSupportsCS8( |
| 1103 | Thread* self ATTRIBUTE_UNUSED, |
| 1104 | ArtMethod* method ATTRIBUTE_UNUSED, |
| 1105 | mirror::Object* receiver ATTRIBUTE_UNUSED, |
| 1106 | uint32_t* args ATTRIBUTE_UNUSED, |
| 1107 | JValue* result) { |
| 1108 | result->SetZ(QuasiAtomic::LongAtomicsUseMutexes(Runtime::Current()->GetInstructionSet()) |
| 1109 | ? 0 |
| 1110 | : 1); |
| 1111 | } |
| 1112 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1113 | void UnstartedRuntime::UnstartedJNIClassGetNameNative( |
| 1114 | Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver, |
| 1115 | uint32_t* args ATTRIBUTE_UNUSED, JValue* result) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1116 | StackHandleScope<1> hs(self); |
| 1117 | result->SetL(mirror::Class::ComputeName(hs.NewHandle(receiver->AsClass()))); |
| 1118 | } |
| 1119 | |
Andreas Gampe | bc4d218 | 2016-02-22 10:03:12 -0800 | [diff] [blame] | 1120 | void UnstartedRuntime::UnstartedJNIDoubleLongBitsToDouble( |
| 1121 | Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, |
| 1122 | mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) { |
| 1123 | uint64_t long_input = args[0] | (static_cast<uint64_t>(args[1]) << 32); |
| 1124 | result->SetD(bit_cast<double>(long_input)); |
| 1125 | } |
| 1126 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1127 | void UnstartedRuntime::UnstartedJNIFloatFloatToRawIntBits( |
| 1128 | Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, |
| 1129 | mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1130 | result->SetI(args[0]); |
| 1131 | } |
| 1132 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1133 | void UnstartedRuntime::UnstartedJNIFloatIntBitsToFloat( |
| 1134 | Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, |
| 1135 | mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1136 | result->SetI(args[0]); |
| 1137 | } |
| 1138 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1139 | void UnstartedRuntime::UnstartedJNIObjectInternalClone( |
| 1140 | Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver, |
| 1141 | uint32_t* args ATTRIBUTE_UNUSED, JValue* result) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1142 | result->SetL(receiver->Clone(self)); |
| 1143 | } |
| 1144 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1145 | void UnstartedRuntime::UnstartedJNIObjectNotifyAll( |
| 1146 | Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver, |
| 1147 | uint32_t* args ATTRIBUTE_UNUSED, JValue* result ATTRIBUTE_UNUSED) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1148 | receiver->NotifyAll(self); |
| 1149 | } |
| 1150 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1151 | void UnstartedRuntime::UnstartedJNIStringCompareTo( |
| 1152 | Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver, uint32_t* args, |
| 1153 | JValue* result) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1154 | mirror::String* rhs = reinterpret_cast<mirror::Object*>(args[0])->AsString(); |
| 1155 | if (rhs == nullptr) { |
Andreas Gampe | 068b0c0 | 2015-03-11 12:44:47 -0700 | [diff] [blame] | 1156 | AbortTransactionOrFail(self, "String.compareTo with null object"); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1157 | } |
| 1158 | result->SetI(receiver->AsString()->CompareTo(rhs)); |
| 1159 | } |
| 1160 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1161 | void UnstartedRuntime::UnstartedJNIStringIntern( |
| 1162 | Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver, |
| 1163 | uint32_t* args ATTRIBUTE_UNUSED, JValue* result) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1164 | result->SetL(receiver->AsString()->Intern()); |
| 1165 | } |
| 1166 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1167 | void UnstartedRuntime::UnstartedJNIStringFastIndexOf( |
| 1168 | Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver, |
| 1169 | uint32_t* args, JValue* result) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1170 | result->SetI(receiver->AsString()->FastIndexOf(args[0], args[1])); |
| 1171 | } |
| 1172 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1173 | void UnstartedRuntime::UnstartedJNIArrayCreateMultiArray( |
| 1174 | Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED, |
| 1175 | uint32_t* args, JValue* result) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1176 | StackHandleScope<2> hs(self); |
| 1177 | auto h_class(hs.NewHandle(reinterpret_cast<mirror::Class*>(args[0])->AsClass())); |
| 1178 | auto h_dimensions(hs.NewHandle(reinterpret_cast<mirror::IntArray*>(args[1])->AsIntArray())); |
| 1179 | result->SetL(mirror::Array::CreateMultiArray(self, h_class, h_dimensions)); |
| 1180 | } |
| 1181 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1182 | void UnstartedRuntime::UnstartedJNIArrayCreateObjectArray( |
| 1183 | Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED, |
| 1184 | uint32_t* args, JValue* result) { |
Andreas Gampe | e598e04 | 2015-04-10 14:57:10 -0700 | [diff] [blame] | 1185 | int32_t length = static_cast<int32_t>(args[1]); |
| 1186 | if (length < 0) { |
| 1187 | ThrowNegativeArraySizeException(length); |
| 1188 | return; |
| 1189 | } |
| 1190 | mirror::Class* element_class = reinterpret_cast<mirror::Class*>(args[0])->AsClass(); |
| 1191 | Runtime* runtime = Runtime::Current(); |
| 1192 | ClassLinker* class_linker = runtime->GetClassLinker(); |
| 1193 | mirror::Class* array_class = class_linker->FindArrayClass(self, &element_class); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1194 | if (UNLIKELY(array_class == nullptr)) { |
Andreas Gampe | e598e04 | 2015-04-10 14:57:10 -0700 | [diff] [blame] | 1195 | CHECK(self->IsExceptionPending()); |
| 1196 | return; |
| 1197 | } |
| 1198 | DCHECK(array_class->IsObjectArrayClass()); |
| 1199 | mirror::Array* new_array = mirror::ObjectArray<mirror::Object*>::Alloc( |
| 1200 | self, array_class, length, runtime->GetHeap()->GetCurrentAllocator()); |
| 1201 | result->SetL(new_array); |
| 1202 | } |
| 1203 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1204 | void UnstartedRuntime::UnstartedJNIThrowableNativeFillInStackTrace( |
| 1205 | Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED, |
| 1206 | uint32_t* args ATTRIBUTE_UNUSED, JValue* result) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1207 | ScopedObjectAccessUnchecked soa(self); |
| 1208 | if (Runtime::Current()->IsActiveTransaction()) { |
| 1209 | result->SetL(soa.Decode<mirror::Object*>(self->CreateInternalStackTrace<true>(soa))); |
| 1210 | } else { |
| 1211 | result->SetL(soa.Decode<mirror::Object*>(self->CreateInternalStackTrace<false>(soa))); |
| 1212 | } |
| 1213 | } |
| 1214 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1215 | void UnstartedRuntime::UnstartedJNISystemIdentityHashCode( |
| 1216 | Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, |
| 1217 | mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1218 | mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]); |
| 1219 | result->SetI((obj != nullptr) ? obj->IdentityHashCode() : 0); |
| 1220 | } |
| 1221 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1222 | void UnstartedRuntime::UnstartedJNIByteOrderIsLittleEndian( |
| 1223 | Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, |
| 1224 | mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1225 | result->SetZ(JNI_TRUE); |
| 1226 | } |
| 1227 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1228 | void UnstartedRuntime::UnstartedJNIUnsafeCompareAndSwapInt( |
| 1229 | Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, |
| 1230 | mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1231 | mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]); |
| 1232 | jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1]; |
| 1233 | jint expectedValue = args[3]; |
| 1234 | jint newValue = args[4]; |
| 1235 | bool success; |
| 1236 | if (Runtime::Current()->IsActiveTransaction()) { |
| 1237 | success = obj->CasFieldStrongSequentiallyConsistent32<true>(MemberOffset(offset), |
| 1238 | expectedValue, newValue); |
| 1239 | } else { |
| 1240 | success = obj->CasFieldStrongSequentiallyConsistent32<false>(MemberOffset(offset), |
| 1241 | expectedValue, newValue); |
| 1242 | } |
| 1243 | result->SetZ(success ? JNI_TRUE : JNI_FALSE); |
| 1244 | } |
| 1245 | |
Narayan Kamath | 34a316f | 2016-03-30 13:11:18 +0100 | [diff] [blame] | 1246 | void UnstartedRuntime::UnstartedJNIUnsafeGetIntVolatile( |
| 1247 | Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED, |
| 1248 | uint32_t* args, JValue* result) { |
| 1249 | mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]); |
| 1250 | if (obj == nullptr) { |
| 1251 | AbortTransactionOrFail(self, "Cannot access null object, retry at runtime."); |
| 1252 | return; |
| 1253 | } |
| 1254 | |
| 1255 | jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1]; |
| 1256 | result->SetI(obj->GetField32Volatile(MemberOffset(offset))); |
| 1257 | } |
| 1258 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1259 | void UnstartedRuntime::UnstartedJNIUnsafePutObject( |
| 1260 | Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, |
| 1261 | mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result ATTRIBUTE_UNUSED) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1262 | mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]); |
| 1263 | jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1]; |
| 1264 | mirror::Object* newValue = reinterpret_cast<mirror::Object*>(args[3]); |
| 1265 | if (Runtime::Current()->IsActiveTransaction()) { |
| 1266 | obj->SetFieldObject<true>(MemberOffset(offset), newValue); |
| 1267 | } else { |
| 1268 | obj->SetFieldObject<false>(MemberOffset(offset), newValue); |
| 1269 | } |
| 1270 | } |
| 1271 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 1272 | void UnstartedRuntime::UnstartedJNIUnsafeGetArrayBaseOffsetForComponentType( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1273 | Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, |
| 1274 | mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1275 | mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass(); |
| 1276 | Primitive::Type primitive_type = component->GetPrimitiveType(); |
| 1277 | result->SetI(mirror::Array::DataOffset(Primitive::ComponentSize(primitive_type)).Int32Value()); |
| 1278 | } |
| 1279 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 1280 | void UnstartedRuntime::UnstartedJNIUnsafeGetArrayIndexScaleForComponentType( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1281 | Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, |
| 1282 | mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1283 | mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass(); |
| 1284 | Primitive::Type primitive_type = component->GetPrimitiveType(); |
| 1285 | result->SetI(Primitive::ComponentSize(primitive_type)); |
| 1286 | } |
| 1287 | |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1288 | typedef void (*InvokeHandler)(Thread* self, ShadowFrame* shadow_frame, JValue* result, |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1289 | size_t arg_size); |
| 1290 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1291 | typedef void (*JNIHandler)(Thread* self, ArtMethod* method, mirror::Object* receiver, |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1292 | uint32_t* args, JValue* result); |
| 1293 | |
| 1294 | static bool tables_initialized_ = false; |
| 1295 | static std::unordered_map<std::string, InvokeHandler> invoke_handlers_; |
| 1296 | static std::unordered_map<std::string, JNIHandler> jni_handlers_; |
| 1297 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 1298 | void UnstartedRuntime::InitializeInvokeHandlers() { |
| 1299 | #define UNSTARTED_DIRECT(ShortName, Sig) \ |
| 1300 | invoke_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::Unstarted ## ShortName)); |
| 1301 | #include "unstarted_runtime_list.h" |
| 1302 | UNSTARTED_RUNTIME_DIRECT_LIST(UNSTARTED_DIRECT) |
| 1303 | #undef UNSTARTED_RUNTIME_DIRECT_LIST |
| 1304 | #undef UNSTARTED_RUNTIME_JNI_LIST |
| 1305 | #undef UNSTARTED_DIRECT |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1306 | } |
| 1307 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 1308 | void UnstartedRuntime::InitializeJNIHandlers() { |
| 1309 | #define UNSTARTED_JNI(ShortName, Sig) \ |
| 1310 | jni_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::UnstartedJNI ## ShortName)); |
| 1311 | #include "unstarted_runtime_list.h" |
| 1312 | UNSTARTED_RUNTIME_JNI_LIST(UNSTARTED_JNI) |
| 1313 | #undef UNSTARTED_RUNTIME_DIRECT_LIST |
| 1314 | #undef UNSTARTED_RUNTIME_JNI_LIST |
| 1315 | #undef UNSTARTED_JNI |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1316 | } |
| 1317 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 1318 | void UnstartedRuntime::Initialize() { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1319 | CHECK(!tables_initialized_); |
| 1320 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 1321 | InitializeInvokeHandlers(); |
| 1322 | InitializeJNIHandlers(); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1323 | |
| 1324 | tables_initialized_ = true; |
| 1325 | } |
| 1326 | |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 1327 | void UnstartedRuntime::Invoke(Thread* self, const DexFile::CodeItem* code_item, |
| 1328 | ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1329 | // In a runtime that's not started we intercept certain methods to avoid complicated dependency |
| 1330 | // problems in core libraries. |
| 1331 | CHECK(tables_initialized_); |
| 1332 | |
| 1333 | std::string name(PrettyMethod(shadow_frame->GetMethod())); |
| 1334 | const auto& iter = invoke_handlers_.find(name); |
| 1335 | if (iter != invoke_handlers_.end()) { |
Kenny Root | 57f91e8 | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 1336 | // Clear out the result in case it's not zeroed out. |
| 1337 | result->SetL(0); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1338 | (*iter->second)(self, shadow_frame, result, arg_offset); |
| 1339 | } else { |
| 1340 | // Not special, continue with regular interpreter execution. |
Andreas Gampe | 3cfa4d0 | 2015-10-06 17:04:01 -0700 | [diff] [blame] | 1341 | ArtInterpreterToInterpreterBridge(self, code_item, shadow_frame, result); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | // 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] | 1346 | void UnstartedRuntime::Jni(Thread* self, ArtMethod* method, mirror::Object* receiver, |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 1347 | uint32_t* args, JValue* result) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1348 | std::string name(PrettyMethod(method)); |
| 1349 | const auto& iter = jni_handlers_.find(name); |
| 1350 | if (iter != jni_handlers_.end()) { |
Kenny Root | 57f91e8 | 2015-05-14 15:58:17 -0700 | [diff] [blame] | 1351 | // Clear out the result in case it's not zeroed out. |
| 1352 | result->SetL(0); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1353 | (*iter->second)(self, method, receiver, args, result); |
| 1354 | } else if (Runtime::Current()->IsActiveTransaction()) { |
Sebastien Hertz | 45b1597 | 2015-04-03 16:07:05 +0200 | [diff] [blame] | 1355 | AbortTransactionF(self, "Attempt to invoke native method in non-started runtime: %s", |
| 1356 | name.c_str()); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1357 | } else { |
| 1358 | LOG(FATAL) << "Calling native method " << PrettyMethod(method) << " in an unstarted " |
| 1359 | "non-transactional runtime"; |
| 1360 | } |
| 1361 | } |
| 1362 | |
| 1363 | } // namespace interpreter |
| 1364 | } // namespace art |