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