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