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