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