blob: 1f473e404c91163d75fee173cde32b6cd659a785 [file] [log] [blame]
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001/*
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 Gampe8ce9c302016-04-15 21:24:28 -070019#include <ctype.h>
Andreas Gampe13fc1be2016-04-05 20:14:30 -070020#include <errno.h>
21#include <stdlib.h>
22
Andreas Gampe2969bcd2015-03-09 12:57:41 -070023#include <cmath>
Andreas Gampe13fc1be2016-04-05 20:14:30 -070024#include <limits>
Andreas Gampe8ce9c302016-04-15 21:24:28 -070025#include <locale>
Andreas Gampe2969bcd2015-03-09 12:57:41 -070026#include <unordered_map>
27
Andreas Gampeaacc25d2015-04-01 14:49:06 -070028#include "ScopedLocalRef.h"
29
Mathieu Chartiere401d142015-04-22 13:56:20 -070030#include "art_method-inl.h"
Andreas Gampebc4d2182016-02-22 10:03:12 -080031#include "base/casts.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070032#include "base/logging.h"
33#include "base/macros.h"
34#include "class_linker.h"
35#include "common_throws.h"
36#include "entrypoints/entrypoint_utils-inl.h"
Andreas Gampebc4d2182016-02-22 10:03:12 -080037#include "gc/reference_processor.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070038#include "handle_scope-inl.h"
39#include "interpreter/interpreter_common.h"
40#include "mirror/array-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070041#include "mirror/class.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070042#include "mirror/field-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070043#include "mirror/object-inl.h"
44#include "mirror/object_array-inl.h"
45#include "mirror/string-inl.h"
46#include "nth_caller_visitor.h"
Andreas Gampe715fdc22016-04-18 17:07:30 -070047#include "reflection.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070048#include "thread.h"
Sebastien Hertz2fd7e692015-04-02 11:11:19 +020049#include "transaction.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070050#include "well_known_classes.h"
Andreas Gampef778eb22015-04-13 14:17:09 -070051#include "zip_archive.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070052
53namespace art {
54namespace interpreter {
55
Andreas Gampe068b0c02015-03-11 12:44:47 -070056static void AbortTransactionOrFail(Thread* self, const char* fmt, ...)
Sebastien Hertz45b15972015-04-03 16:07:05 +020057 __attribute__((__format__(__printf__, 2, 3)))
Mathieu Chartier90443472015-07-16 20:32:27 -070058 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz45b15972015-04-03 16:07:05 +020059
60static void AbortTransactionOrFail(Thread* self, const char* fmt, ...) {
Andreas Gampe068b0c02015-03-11 12:44:47 -070061 va_list args;
Andreas Gampe068b0c02015-03-11 12:44:47 -070062 if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +020063 va_start(args, fmt);
64 AbortTransactionV(self, fmt, args);
Andreas Gampe068b0c02015-03-11 12:44:47 -070065 va_end(args);
66 } else {
Sebastien Hertz45b15972015-04-03 16:07:05 +020067 va_start(args, fmt);
68 std::string msg;
69 StringAppendV(&msg, fmt, args);
70 va_end(args);
71 LOG(FATAL) << "Trying to abort, but not in transaction mode: " << msg;
Andreas Gampe068b0c02015-03-11 12:44:47 -070072 UNREACHABLE();
73 }
74}
75
Andreas Gampe8ce9c302016-04-15 21:24:28 -070076// Restricted support for character upper case / lower case. Only support ASCII, where
77// it's easy. Abort the transaction otherwise.
78static void CharacterLowerUpper(Thread* self,
79 ShadowFrame* shadow_frame,
80 JValue* result,
81 size_t arg_offset,
82 bool to_lower_case) SHARED_REQUIRES(Locks::mutator_lock_) {
83 uint32_t int_value = static_cast<uint32_t>(shadow_frame->GetVReg(arg_offset));
84
85 // Only ASCII (7-bit).
86 if (!isascii(int_value)) {
87 AbortTransactionOrFail(self,
88 "Only support ASCII characters for toLowerCase/toUpperCase: %u",
89 int_value);
90 return;
91 }
92
93 std::locale c_locale("C");
94 char char_value = static_cast<char>(int_value);
95
96 if (to_lower_case) {
97 result->SetI(std::tolower(char_value, c_locale));
98 } else {
99 result->SetI(std::toupper(char_value, c_locale));
100 }
101}
102
103void UnstartedRuntime::UnstartedCharacterToLowerCase(
104 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
105 CharacterLowerUpper(self, shadow_frame, result, arg_offset, true);
106}
107
108void UnstartedRuntime::UnstartedCharacterToUpperCase(
109 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
110 CharacterLowerUpper(self, shadow_frame, result, arg_offset, false);
111}
112
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700113// Helper function to deal with class loading in an unstarted runtime.
114static void UnstartedRuntimeFindClass(Thread* self, Handle<mirror::String> className,
115 Handle<mirror::ClassLoader> class_loader, JValue* result,
116 const std::string& method_name, bool initialize_class,
117 bool abort_if_not_found)
Mathieu Chartier90443472015-07-16 20:32:27 -0700118 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700119 CHECK(className.Get() != nullptr);
120 std::string descriptor(DotToDescriptor(className->ToModifiedUtf8().c_str()));
121 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
122
123 mirror::Class* found = class_linker->FindClass(self, descriptor.c_str(), class_loader);
124 if (found == nullptr && abort_if_not_found) {
125 if (!self->IsExceptionPending()) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700126 AbortTransactionOrFail(self, "%s failed in un-started runtime for class: %s",
127 method_name.c_str(), PrettyDescriptor(descriptor.c_str()).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700128 }
129 return;
130 }
131 if (found != nullptr && initialize_class) {
132 StackHandleScope<1> hs(self);
133 Handle<mirror::Class> h_class(hs.NewHandle(found));
134 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
135 CHECK(self->IsExceptionPending());
136 return;
137 }
138 }
139 result->SetL(found);
140}
141
142// Common helper for class-loading cutouts in an unstarted runtime. We call Runtime methods that
143// rely on Java code to wrap errors in the correct exception class (i.e., NoClassDefFoundError into
144// ClassNotFoundException), so need to do the same. The only exception is if the exception is
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200145// actually the transaction abort exception. This must not be wrapped, as it signals an
146// initialization abort.
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700147static void CheckExceptionGenerateClassNotFound(Thread* self)
Mathieu Chartier90443472015-07-16 20:32:27 -0700148 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700149 if (self->IsExceptionPending()) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200150 // If it is not the transaction abort exception, wrap it.
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700151 std::string type(PrettyTypeOf(self->GetException()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200152 if (type != Transaction::kAbortExceptionDescriptor) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700153 self->ThrowNewWrappedException("Ljava/lang/ClassNotFoundException;",
154 "ClassNotFoundException");
155 }
156 }
157}
158
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700159static mirror::String* GetClassName(Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700160 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700161 mirror::Object* param = shadow_frame->GetVRegReference(arg_offset);
162 if (param == nullptr) {
163 AbortTransactionOrFail(self, "Null-pointer in Class.forName.");
164 return nullptr;
165 }
166 return param->AsString();
167}
168
Andreas Gampe799681b2015-05-15 19:24:12 -0700169void UnstartedRuntime::UnstartedClassForName(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700170 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700171 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
172 if (class_name == nullptr) {
173 return;
174 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700175 StackHandleScope<1> hs(self);
176 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800177 UnstartedRuntimeFindClass(self,
178 h_class_name,
179 ScopedNullHandle<mirror::ClassLoader>(),
180 result,
181 "Class.forName",
182 true,
183 false);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700184 CheckExceptionGenerateClassNotFound(self);
185}
186
Andreas Gampe799681b2015-05-15 19:24:12 -0700187void UnstartedRuntime::UnstartedClassForNameLong(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700188 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700189 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
190 if (class_name == nullptr) {
Andreas Gampebf4d3af2015-04-14 10:10:33 -0700191 return;
192 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700193 bool initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0;
194 mirror::ClassLoader* class_loader =
195 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2));
196 StackHandleScope<2> hs(self);
197 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
198 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
199 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, "Class.forName",
200 initialize_class, false);
201 CheckExceptionGenerateClassNotFound(self);
202}
203
Andreas Gampe799681b2015-05-15 19:24:12 -0700204void UnstartedRuntime::UnstartedClassClassForName(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700205 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700206 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
207 if (class_name == nullptr) {
208 return;
209 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700210 bool initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0;
211 mirror::ClassLoader* class_loader =
212 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2));
213 StackHandleScope<2> hs(self);
214 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
215 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
216 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, "Class.classForName",
217 initialize_class, false);
218 CheckExceptionGenerateClassNotFound(self);
219}
220
Andreas Gampe799681b2015-05-15 19:24:12 -0700221void UnstartedRuntime::UnstartedClassNewInstance(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700222 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
223 StackHandleScope<2> hs(self); // Class, constructor, object.
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700224 mirror::Object* param = shadow_frame->GetVRegReference(arg_offset);
225 if (param == nullptr) {
226 AbortTransactionOrFail(self, "Null-pointer in Class.newInstance.");
227 return;
228 }
229 mirror::Class* klass = param->AsClass();
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700230 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700231
232 // Check that it's not null.
233 if (h_klass.Get() == nullptr) {
234 AbortTransactionOrFail(self, "Class reference is null for newInstance");
235 return;
236 }
237
238 // If we're in a transaction, class must not be finalizable (it or a superclass has a finalizer).
239 if (Runtime::Current()->IsActiveTransaction()) {
240 if (h_klass.Get()->IsFinalizable()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +0200241 AbortTransactionF(self, "Class for newInstance is finalizable: '%s'",
242 PrettyClass(h_klass.Get()).c_str());
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700243 return;
244 }
245 }
246
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700247 // There are two situations in which we'll abort this run.
248 // 1) If the class isn't yet initialized and initialization fails.
249 // 2) If we can't find the default constructor. We'll postpone the exception to runtime.
250 // Note that 2) could likely be handled here, but for safety abort the transaction.
251 bool ok = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700252 auto* cl = Runtime::Current()->GetClassLinker();
253 if (cl->EnsureInitialized(self, h_klass, true, true)) {
254 auto* cons = h_klass->FindDeclaredDirectMethod("<init>", "()V", cl->GetImagePointerSize());
255 if (cons != nullptr) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700256 Handle<mirror::Object> h_obj(hs.NewHandle(klass->AllocObject(self)));
257 CHECK(h_obj.Get() != nullptr); // We don't expect OOM at compile-time.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700258 EnterInterpreterFromInvoke(self, cons, h_obj.Get(), nullptr, nullptr);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700259 if (!self->IsExceptionPending()) {
260 result->SetL(h_obj.Get());
261 ok = true;
262 }
263 } else {
264 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
265 "Could not find default constructor for '%s'",
266 PrettyClass(h_klass.Get()).c_str());
267 }
268 }
269 if (!ok) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700270 AbortTransactionOrFail(self, "Failed in Class.newInstance for '%s' with %s",
271 PrettyClass(h_klass.Get()).c_str(),
272 PrettyTypeOf(self->GetException()).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700273 }
274}
275
Andreas Gampe799681b2015-05-15 19:24:12 -0700276void UnstartedRuntime::UnstartedClassGetDeclaredField(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700277 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700278 // Special managed code cut-out to allow field lookup in a un-started runtime that'd fail
279 // going the reflective Dex way.
280 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
281 mirror::String* name2 = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700282 ArtField* found = nullptr;
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700283 for (ArtField& field : klass->GetIFields()) {
284 if (name2->Equals(field.GetName())) {
285 found = &field;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700286 break;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700287 }
288 }
289 if (found == nullptr) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700290 for (ArtField& field : klass->GetSFields()) {
291 if (name2->Equals(field.GetName())) {
292 found = &field;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700293 break;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700294 }
295 }
296 }
Andreas Gampe068b0c02015-03-11 12:44:47 -0700297 if (found == nullptr) {
298 AbortTransactionOrFail(self, "Failed to find field in Class.getDeclaredField in un-started "
299 " runtime. name=%s class=%s", name2->ToModifiedUtf8().c_str(),
300 PrettyDescriptor(klass).c_str());
301 return;
302 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700303 if (Runtime::Current()->IsActiveTransaction()) {
304 result->SetL(mirror::Field::CreateFromArtField<true>(self, found, true));
305 } else {
306 result->SetL(mirror::Field::CreateFromArtField<false>(self, found, true));
307 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700308}
309
Andreas Gampebc4d2182016-02-22 10:03:12 -0800310// This is required for Enum(Set) code, as that uses reflection to inspect enum classes.
311void UnstartedRuntime::UnstartedClassGetDeclaredMethod(
312 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
313 // Special managed code cut-out to allow method lookup in a un-started runtime.
314 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
315 if (klass == nullptr) {
316 ThrowNullPointerExceptionForMethodAccess(shadow_frame->GetMethod(), InvokeType::kVirtual);
317 return;
318 }
319 mirror::String* name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
320 mirror::ObjectArray<mirror::Class>* args =
321 shadow_frame->GetVRegReference(arg_offset + 2)->AsObjectArray<mirror::Class>();
322 if (Runtime::Current()->IsActiveTransaction()) {
323 result->SetL(mirror::Class::GetDeclaredMethodInternal<true>(self, klass, name, args));
324 } else {
325 result->SetL(mirror::Class::GetDeclaredMethodInternal<false>(self, klass, name, args));
326 }
327}
328
Andreas Gampe6039e562016-04-05 18:18:43 -0700329// Special managed code cut-out to allow constructor lookup in a un-started runtime.
330void UnstartedRuntime::UnstartedClassGetDeclaredConstructor(
331 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
332 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
333 if (klass == nullptr) {
334 ThrowNullPointerExceptionForMethodAccess(shadow_frame->GetMethod(), InvokeType::kVirtual);
335 return;
336 }
337 mirror::ObjectArray<mirror::Class>* args =
338 shadow_frame->GetVRegReference(arg_offset + 1)->AsObjectArray<mirror::Class>();
339 if (Runtime::Current()->IsActiveTransaction()) {
340 result->SetL(mirror::Class::GetDeclaredConstructorInternal<true>(self, klass, args));
341 } else {
342 result->SetL(mirror::Class::GetDeclaredConstructorInternal<false>(self, klass, args));
343 }
344}
345
Andreas Gampe633750c2016-02-19 10:49:50 -0800346void UnstartedRuntime::UnstartedClassGetEnclosingClass(
347 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
348 StackHandleScope<1> hs(self);
349 Handle<mirror::Class> klass(hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsClass()));
350 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
351 result->SetL(nullptr);
352 }
353 result->SetL(klass->GetDexFile().GetEnclosingClass(klass));
354}
355
Andreas Gampe715fdc22016-04-18 17:07:30 -0700356void UnstartedRuntime::UnstartedClassGetInnerClassFlags(
357 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
358 StackHandleScope<1> hs(self);
359 Handle<mirror::Class> klass(hs.NewHandle(
360 reinterpret_cast<mirror::Class*>(shadow_frame->GetVRegReference(arg_offset))));
361 const int32_t default_value = shadow_frame->GetVReg(arg_offset + 1);
362 result->SetI(mirror::Class::GetInnerClassFlags(klass, default_value));
363}
364
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700365static std::unique_ptr<MemMap> FindAndExtractEntry(const std::string& jar_file,
366 const char* entry_name,
367 size_t* size,
368 std::string* error_msg) {
369 CHECK(size != nullptr);
370
371 std::unique_ptr<ZipArchive> zip_archive(ZipArchive::Open(jar_file.c_str(), error_msg));
372 if (zip_archive == nullptr) {
373 return nullptr;;
374 }
375 std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find(entry_name, error_msg));
376 if (zip_entry == nullptr) {
377 return nullptr;
378 }
379 std::unique_ptr<MemMap> tmp_map(
380 zip_entry->ExtractToMemMap(jar_file.c_str(), entry_name, error_msg));
381 if (tmp_map == nullptr) {
382 return nullptr;
383 }
384
385 // OK, from here everything seems fine.
386 *size = zip_entry->GetUncompressedLength();
387 return tmp_map;
388}
389
390static void GetResourceAsStream(Thread* self,
391 ShadowFrame* shadow_frame,
392 JValue* result,
393 size_t arg_offset) SHARED_REQUIRES(Locks::mutator_lock_) {
394 mirror::Object* resource_obj = shadow_frame->GetVRegReference(arg_offset + 1);
395 if (resource_obj == nullptr) {
396 AbortTransactionOrFail(self, "null name for getResourceAsStream");
397 return;
398 }
399 CHECK(resource_obj->IsString());
400 mirror::String* resource_name = resource_obj->AsString();
401
402 std::string resource_name_str = resource_name->ToModifiedUtf8();
403 if (resource_name_str.empty() || resource_name_str == "/") {
404 AbortTransactionOrFail(self,
405 "Unsupported name %s for getResourceAsStream",
406 resource_name_str.c_str());
407 return;
408 }
409 const char* resource_cstr = resource_name_str.c_str();
410 if (resource_cstr[0] == '/') {
411 resource_cstr++;
412 }
413
414 Runtime* runtime = Runtime::Current();
415
416 std::vector<std::string> split;
417 Split(runtime->GetBootClassPathString(), ':', &split);
418 if (split.empty()) {
419 AbortTransactionOrFail(self,
420 "Boot classpath not set or split error:: %s",
421 runtime->GetBootClassPathString().c_str());
422 return;
423 }
424
425 std::unique_ptr<MemMap> mem_map;
426 size_t map_size;
427 std::string last_error_msg; // Only store the last message (we could concatenate).
428
429 for (const std::string& jar_file : split) {
430 mem_map = FindAndExtractEntry(jar_file, resource_cstr, &map_size, &last_error_msg);
431 if (mem_map != nullptr) {
432 break;
433 }
434 }
435
436 if (mem_map == nullptr) {
437 // Didn't find it. There's a good chance this will be the same at runtime, but still
438 // conservatively abort the transaction here.
439 AbortTransactionOrFail(self,
440 "Could not find resource %s. Last error was %s.",
441 resource_name_str.c_str(),
442 last_error_msg.c_str());
443 return;
444 }
445
446 StackHandleScope<3> hs(self);
447
448 // Create byte array for content.
449 Handle<mirror::ByteArray> h_array(hs.NewHandle(mirror::ByteArray::Alloc(self, map_size)));
450 if (h_array.Get() == nullptr) {
451 AbortTransactionOrFail(self, "Could not find/create byte array class");
452 return;
453 }
454 // Copy in content.
455 memcpy(h_array->GetData(), mem_map->Begin(), map_size);
456 // Be proactive releasing memory.
457 mem_map.release();
458
459 // Create a ByteArrayInputStream.
460 Handle<mirror::Class> h_class(hs.NewHandle(
461 runtime->GetClassLinker()->FindClass(self,
462 "Ljava/io/ByteArrayInputStream;",
463 ScopedNullHandle<mirror::ClassLoader>())));
464 if (h_class.Get() == nullptr) {
465 AbortTransactionOrFail(self, "Could not find ByteArrayInputStream class");
466 return;
467 }
468 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
469 AbortTransactionOrFail(self, "Could not initialize ByteArrayInputStream class");
470 return;
471 }
472
473 Handle<mirror::Object> h_obj(hs.NewHandle(h_class->AllocObject(self)));
474 if (h_obj.Get() == nullptr) {
475 AbortTransactionOrFail(self, "Could not allocate ByteArrayInputStream object");
476 return;
477 }
478
479 auto* cl = Runtime::Current()->GetClassLinker();
480 ArtMethod* constructor = h_class->FindDeclaredDirectMethod(
481 "<init>", "([B)V", cl->GetImagePointerSize());
482 if (constructor == nullptr) {
483 AbortTransactionOrFail(self, "Could not find ByteArrayInputStream constructor");
484 return;
485 }
486
487 uint32_t args[1];
488 args[0] = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(h_array.Get()));
489 EnterInterpreterFromInvoke(self, constructor, h_obj.Get(), args, nullptr);
490
491 if (self->IsExceptionPending()) {
492 AbortTransactionOrFail(self, "Could not run ByteArrayInputStream constructor");
493 return;
494 }
495
496 result->SetL(h_obj.Get());
497}
498
499void UnstartedRuntime::UnstartedClassLoaderGetResourceAsStream(
500 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
501 {
502 mirror::Object* this_obj = shadow_frame->GetVRegReference(arg_offset);
503 CHECK(this_obj != nullptr);
504 CHECK(this_obj->IsClassLoader());
505
506 StackHandleScope<1> hs(self);
507 Handle<mirror::Class> this_classloader_class(hs.NewHandle(this_obj->GetClass()));
508
509 if (self->DecodeJObject(WellKnownClasses::java_lang_BootClassLoader) !=
510 this_classloader_class.Get()) {
511 AbortTransactionOrFail(self,
512 "Unsupported classloader type %s for getResourceAsStream",
513 PrettyClass(this_classloader_class.Get()).c_str());
514 return;
515 }
516 }
517
518 GetResourceAsStream(self, shadow_frame, result, arg_offset);
519}
520
Andreas Gampe799681b2015-05-15 19:24:12 -0700521void UnstartedRuntime::UnstartedVmClassLoaderFindLoadedClass(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700522 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700523 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
524 mirror::ClassLoader* class_loader =
525 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset));
526 StackHandleScope<2> hs(self);
527 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
528 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
529 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result,
530 "VMClassLoader.findLoadedClass", false, false);
531 // This might have an error pending. But semantics are to just return null.
532 if (self->IsExceptionPending()) {
533 // If it is an InternalError, keep it. See CheckExceptionGenerateClassNotFound.
534 std::string type(PrettyTypeOf(self->GetException()));
535 if (type != "java.lang.InternalError") {
536 self->ClearException();
537 }
538 }
539}
540
Mathieu Chartiere401d142015-04-22 13:56:20 -0700541void UnstartedRuntime::UnstartedVoidLookupType(
542 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED, JValue* result,
543 size_t arg_offset ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700544 result->SetL(Runtime::Current()->GetClassLinker()->FindPrimitiveClass('V'));
545}
546
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700547// Arraycopy emulation.
548// Note: we can't use any fast copy functions, as they are not available under transaction.
549
550template <typename T>
551static void PrimitiveArrayCopy(Thread* self,
552 mirror::Array* src_array, int32_t src_pos,
553 mirror::Array* dst_array, int32_t dst_pos,
554 int32_t length)
Mathieu Chartier90443472015-07-16 20:32:27 -0700555 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700556 if (src_array->GetClass()->GetComponentType() != dst_array->GetClass()->GetComponentType()) {
557 AbortTransactionOrFail(self, "Types mismatched in arraycopy: %s vs %s.",
558 PrettyDescriptor(src_array->GetClass()->GetComponentType()).c_str(),
559 PrettyDescriptor(dst_array->GetClass()->GetComponentType()).c_str());
560 return;
561 }
562 mirror::PrimitiveArray<T>* src = down_cast<mirror::PrimitiveArray<T>*>(src_array);
563 mirror::PrimitiveArray<T>* dst = down_cast<mirror::PrimitiveArray<T>*>(dst_array);
564 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
565 if (copy_forward) {
566 for (int32_t i = 0; i < length; ++i) {
567 dst->Set(dst_pos + i, src->Get(src_pos + i));
568 }
569 } else {
570 for (int32_t i = 1; i <= length; ++i) {
571 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
572 }
573 }
574}
575
Andreas Gampe799681b2015-05-15 19:24:12 -0700576void UnstartedRuntime::UnstartedSystemArraycopy(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700577 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700578 // Special case array copying without initializing System.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700579 jint src_pos = shadow_frame->GetVReg(arg_offset + 1);
580 jint dst_pos = shadow_frame->GetVReg(arg_offset + 3);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700581 jint length = shadow_frame->GetVReg(arg_offset + 4);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700582
Andreas Gampe85a098a2016-03-31 13:30:53 -0700583 mirror::Object* src_obj = shadow_frame->GetVRegReference(arg_offset);
584 mirror::Object* dst_obj = shadow_frame->GetVRegReference(arg_offset + 2);
585 // Null checking. For simplicity, abort transaction.
586 if (src_obj == nullptr) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700587 AbortTransactionOrFail(self, "src is null in arraycopy.");
588 return;
589 }
Andreas Gampe85a098a2016-03-31 13:30:53 -0700590 if (dst_obj == nullptr) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700591 AbortTransactionOrFail(self, "dst is null in arraycopy.");
592 return;
593 }
Andreas Gampe85a098a2016-03-31 13:30:53 -0700594 // Test for arrayness. Throw ArrayStoreException.
595 if (!src_obj->IsArrayInstance() || !dst_obj->IsArrayInstance()) {
596 self->ThrowNewException("Ljava/lang/ArrayStoreException;", "src or trg is not an array");
597 return;
598 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700599
Andreas Gampe85a098a2016-03-31 13:30:53 -0700600 mirror::Array* src_array = src_obj->AsArray();
601 mirror::Array* dst_array = dst_obj->AsArray();
602
603 // Bounds checking. Throw IndexOutOfBoundsException.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700604 if (UNLIKELY(src_pos < 0) || UNLIKELY(dst_pos < 0) || UNLIKELY(length < 0) ||
605 UNLIKELY(src_pos > src_array->GetLength() - length) ||
606 UNLIKELY(dst_pos > dst_array->GetLength() - length)) {
Andreas Gampe85a098a2016-03-31 13:30:53 -0700607 self->ThrowNewExceptionF("Ljava/lang/IndexOutOfBoundsException;",
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700608 "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d",
609 src_array->GetLength(), src_pos, dst_array->GetLength(), dst_pos,
610 length);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700611 return;
612 }
613
614 // Type checking.
615 mirror::Class* src_type = shadow_frame->GetVRegReference(arg_offset)->GetClass()->
616 GetComponentType();
617
618 if (!src_type->IsPrimitive()) {
619 // Check that the second type is not primitive.
620 mirror::Class* trg_type = shadow_frame->GetVRegReference(arg_offset + 2)->GetClass()->
621 GetComponentType();
622 if (trg_type->IsPrimitiveInt()) {
623 AbortTransactionOrFail(self, "Type mismatch in arraycopy: %s vs %s",
624 PrettyDescriptor(src_array->GetClass()->GetComponentType()).c_str(),
625 PrettyDescriptor(dst_array->GetClass()->GetComponentType()).c_str());
626 return;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700627 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700628
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700629 mirror::ObjectArray<mirror::Object>* src = src_array->AsObjectArray<mirror::Object>();
630 mirror::ObjectArray<mirror::Object>* dst = dst_array->AsObjectArray<mirror::Object>();
631 if (src == dst) {
632 // Can overlap, but not have type mismatches.
Andreas Gampe85a098a2016-03-31 13:30:53 -0700633 // We cannot use ObjectArray::MemMove here, as it doesn't support transactions.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700634 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
635 if (copy_forward) {
636 for (int32_t i = 0; i < length; ++i) {
637 dst->Set(dst_pos + i, src->Get(src_pos + i));
638 }
639 } else {
640 for (int32_t i = 1; i <= length; ++i) {
641 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
642 }
643 }
644 } else {
Andreas Gampe85a098a2016-03-31 13:30:53 -0700645 // We're being lazy here. Optimally this could be a memcpy (if component types are
646 // assignable), but the ObjectArray implementation doesn't support transactions. The
647 // checking version, however, does.
648 if (Runtime::Current()->IsActiveTransaction()) {
649 dst->AssignableCheckingMemcpy<true>(
650 dst_pos, src, src_pos, length, true /* throw_exception */);
651 } else {
652 dst->AssignableCheckingMemcpy<false>(
653 dst_pos, src, src_pos, length, true /* throw_exception */);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700654 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700655 }
Andreas Gampe5c9af612016-04-05 14:16:10 -0700656 } else if (src_type->IsPrimitiveByte()) {
657 PrimitiveArrayCopy<uint8_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700658 } else if (src_type->IsPrimitiveChar()) {
659 PrimitiveArrayCopy<uint16_t>(self, src_array, src_pos, dst_array, dst_pos, length);
660 } else if (src_type->IsPrimitiveInt()) {
661 PrimitiveArrayCopy<int32_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700662 } else {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700663 AbortTransactionOrFail(self, "Unimplemented System.arraycopy for type '%s'",
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700664 PrettyDescriptor(src_type).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700665 }
666}
667
Andreas Gampe5c9af612016-04-05 14:16:10 -0700668void UnstartedRuntime::UnstartedSystemArraycopyByte(
669 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
670 // Just forward.
671 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
672}
673
Andreas Gampe799681b2015-05-15 19:24:12 -0700674void UnstartedRuntime::UnstartedSystemArraycopyChar(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700675 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700676 // Just forward.
677 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
678}
679
680void UnstartedRuntime::UnstartedSystemArraycopyInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700681 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700682 // Just forward.
683 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
684}
685
Narayan Kamath34a316f2016-03-30 13:11:18 +0100686void UnstartedRuntime::UnstartedSystemGetSecurityManager(
687 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED,
688 JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
689 result->SetL(nullptr);
690}
691
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700692static constexpr const char* kAndroidHardcodedSystemPropertiesFieldName = "STATIC_PROPERTIES";
693
694static void GetSystemProperty(Thread* self,
695 ShadowFrame* shadow_frame,
696 JValue* result,
697 size_t arg_offset,
698 bool is_default_version)
699 SHARED_REQUIRES(Locks::mutator_lock_) {
700 StackHandleScope<4> hs(self);
701 Handle<mirror::String> h_key(
702 hs.NewHandle(reinterpret_cast<mirror::String*>(shadow_frame->GetVRegReference(arg_offset))));
703 if (h_key.Get() == nullptr) {
704 AbortTransactionOrFail(self, "getProperty key was null");
705 return;
706 }
707
708 // This is overall inefficient, but reflecting the values here is not great, either. So
709 // for simplicity, and with the assumption that the number of getProperty calls is not
710 // too great, just iterate each time.
711
712 // Get the storage class.
713 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
714 Handle<mirror::Class> h_props_class(hs.NewHandle(
715 class_linker->FindClass(self,
716 "Ljava/lang/AndroidHardcodedSystemProperties;",
717 ScopedNullHandle<mirror::ClassLoader>())));
718 if (h_props_class.Get() == nullptr) {
719 AbortTransactionOrFail(self, "Could not find AndroidHardcodedSystemProperties");
720 return;
721 }
722 if (!class_linker->EnsureInitialized(self, h_props_class, true, true)) {
723 AbortTransactionOrFail(self, "Could not initialize AndroidHardcodedSystemProperties");
724 return;
725 }
726
727 // Get the storage array.
728 ArtField* static_properties =
729 h_props_class->FindDeclaredStaticField(kAndroidHardcodedSystemPropertiesFieldName,
730 "[[Ljava/lang/String;");
731 if (static_properties == nullptr) {
732 AbortTransactionOrFail(self,
733 "Could not find %s field",
734 kAndroidHardcodedSystemPropertiesFieldName);
735 return;
736 }
737 Handle<mirror::ObjectArray<mirror::ObjectArray<mirror::String>>> h_2string_array(
738 hs.NewHandle(reinterpret_cast<mirror::ObjectArray<mirror::ObjectArray<mirror::String>>*>(
739 static_properties->GetObject(h_props_class.Get()))));
740 if (h_2string_array.Get() == nullptr) {
741 AbortTransactionOrFail(self, "Field %s is null", kAndroidHardcodedSystemPropertiesFieldName);
742 return;
743 }
744
745 // Iterate over it.
746 const int32_t prop_count = h_2string_array->GetLength();
747 // Use the third handle as mutable.
748 MutableHandle<mirror::ObjectArray<mirror::String>> h_string_array(
749 hs.NewHandle<mirror::ObjectArray<mirror::String>>(nullptr));
750 for (int32_t i = 0; i < prop_count; ++i) {
751 h_string_array.Assign(h_2string_array->Get(i));
752 if (h_string_array.Get() == nullptr ||
753 h_string_array->GetLength() != 2 ||
754 h_string_array->Get(0) == nullptr) {
755 AbortTransactionOrFail(self,
756 "Unexpected content of %s",
757 kAndroidHardcodedSystemPropertiesFieldName);
758 return;
759 }
760 if (h_key->Equals(h_string_array->Get(0))) {
761 // Found a value.
762 if (h_string_array->Get(1) == nullptr && is_default_version) {
763 // Null is being delegated to the default map, and then resolved to the given default value.
764 // As there's no default map, return the given value.
765 result->SetL(shadow_frame->GetVRegReference(arg_offset + 1));
766 } else {
767 result->SetL(h_string_array->Get(1));
768 }
769 return;
770 }
771 }
772
773 // Key is not supported.
774 AbortTransactionOrFail(self, "getProperty key %s not supported", h_key->ToModifiedUtf8().c_str());
775}
776
777void UnstartedRuntime::UnstartedSystemGetProperty(
778 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
779 GetSystemProperty(self, shadow_frame, result, arg_offset, false);
780}
781
782void UnstartedRuntime::UnstartedSystemGetPropertyWithDefault(
783 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
784 GetSystemProperty(self, shadow_frame, result, arg_offset, true);
785}
786
Andreas Gampe799681b2015-05-15 19:24:12 -0700787void UnstartedRuntime::UnstartedThreadLocalGet(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700788 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700789 std::string caller(PrettyMethod(shadow_frame->GetLink()->GetMethod()));
790 bool ok = false;
Narayan Kamatha1e93122016-03-30 15:41:54 +0100791 if (caller == "void java.lang.FloatingDecimal.developLongDigits(int, long, long)" ||
792 caller == "java.lang.String java.lang.FloatingDecimal.toJavaFormatString()") {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700793 // Allocate non-threadlocal buffer.
Narayan Kamatha1e93122016-03-30 15:41:54 +0100794 result->SetL(mirror::CharArray::Alloc(self, 26));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700795 ok = true;
Narayan Kamatha1e93122016-03-30 15:41:54 +0100796 } else if (caller ==
797 "java.lang.FloatingDecimal java.lang.FloatingDecimal.getThreadLocalInstance()") {
798 // Allocate new object.
799 StackHandleScope<2> hs(self);
800 Handle<mirror::Class> h_real_to_string_class(hs.NewHandle(
801 shadow_frame->GetLink()->GetMethod()->GetDeclaringClass()));
802 Handle<mirror::Object> h_real_to_string_obj(hs.NewHandle(
803 h_real_to_string_class->AllocObject(self)));
804 if (h_real_to_string_obj.Get() != nullptr) {
805 auto* cl = Runtime::Current()->GetClassLinker();
806 ArtMethod* init_method = h_real_to_string_class->FindDirectMethod(
807 "<init>", "()V", cl->GetImagePointerSize());
808 if (init_method == nullptr) {
809 h_real_to_string_class->DumpClass(LOG(FATAL), mirror::Class::kDumpClassFullDetail);
810 } else {
811 JValue invoke_result;
812 EnterInterpreterFromInvoke(self, init_method, h_real_to_string_obj.Get(), nullptr,
813 nullptr);
814 if (!self->IsExceptionPending()) {
815 result->SetL(h_real_to_string_obj.Get());
816 ok = true;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700817 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700818 }
819 }
820 }
821
822 if (!ok) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700823 AbortTransactionOrFail(self, "Could not create RealToString object");
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700824 }
825}
826
Sergio Giro83261202016-04-11 20:49:20 +0100827void UnstartedRuntime::UnstartedMathCeil(
828 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe89e3b482016-04-12 18:07:36 -0700829 result->SetD(ceil(shadow_frame->GetVRegDouble(arg_offset)));
Sergio Giro83261202016-04-11 20:49:20 +0100830}
831
832void UnstartedRuntime::UnstartedMathFloor(
833 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe89e3b482016-04-12 18:07:36 -0700834 result->SetD(floor(shadow_frame->GetVRegDouble(arg_offset)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700835}
836
Andreas Gampeb8a00f92016-04-18 20:51:13 -0700837void UnstartedRuntime::UnstartedMathSin(
838 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
839 result->SetD(sin(shadow_frame->GetVRegDouble(arg_offset)));
840}
841
842void UnstartedRuntime::UnstartedMathCos(
843 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
844 result->SetD(cos(shadow_frame->GetVRegDouble(arg_offset)));
845}
846
847void UnstartedRuntime::UnstartedMathPow(
848 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
849 result->SetD(pow(shadow_frame->GetVRegDouble(arg_offset),
850 shadow_frame->GetVRegDouble(arg_offset + 2)));
851}
852
Andreas Gampe799681b2015-05-15 19:24:12 -0700853void UnstartedRuntime::UnstartedObjectHashCode(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700854 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700855 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
856 result->SetI(obj->IdentityHashCode());
857}
858
Andreas Gampe799681b2015-05-15 19:24:12 -0700859void UnstartedRuntime::UnstartedDoubleDoubleToRawLongBits(
Andreas Gampedd9d0552015-03-09 12:57:41 -0700860 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700861 double in = shadow_frame->GetVRegDouble(arg_offset);
Roland Levillainda4d79b2015-03-24 14:36:11 +0000862 result->SetJ(bit_cast<int64_t, double>(in));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700863}
864
Andreas Gampedd9d0552015-03-09 12:57:41 -0700865static mirror::Object* GetDexFromDexCache(Thread* self, mirror::DexCache* dex_cache)
Mathieu Chartier90443472015-07-16 20:32:27 -0700866 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700867 const DexFile* dex_file = dex_cache->GetDexFile();
868 if (dex_file == nullptr) {
869 return nullptr;
870 }
871
872 // Create the direct byte buffer.
873 JNIEnv* env = self->GetJniEnv();
874 DCHECK(env != nullptr);
875 void* address = const_cast<void*>(reinterpret_cast<const void*>(dex_file->Begin()));
Andreas Gampeaacc25d2015-04-01 14:49:06 -0700876 ScopedLocalRef<jobject> byte_buffer(env, env->NewDirectByteBuffer(address, dex_file->Size()));
877 if (byte_buffer.get() == nullptr) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700878 DCHECK(self->IsExceptionPending());
879 return nullptr;
880 }
881
882 jvalue args[1];
Andreas Gampeaacc25d2015-04-01 14:49:06 -0700883 args[0].l = byte_buffer.get();
884
885 ScopedLocalRef<jobject> dex(env, env->CallStaticObjectMethodA(
886 WellKnownClasses::com_android_dex_Dex,
887 WellKnownClasses::com_android_dex_Dex_create,
888 args));
889
890 return self->DecodeJObject(dex.get());
Andreas Gampedd9d0552015-03-09 12:57:41 -0700891}
892
Andreas Gampe799681b2015-05-15 19:24:12 -0700893void UnstartedRuntime::UnstartedDexCacheGetDexNative(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700894 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700895 // We will create the Dex object, but the image writer will release it before creating the
896 // art file.
897 mirror::Object* src = shadow_frame->GetVRegReference(arg_offset);
898 bool have_dex = false;
899 if (src != nullptr) {
900 mirror::Object* dex = GetDexFromDexCache(self, reinterpret_cast<mirror::DexCache*>(src));
901 if (dex != nullptr) {
902 have_dex = true;
903 result->SetL(dex);
904 }
905 }
906 if (!have_dex) {
907 self->ClearException();
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200908 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Could not create Dex object");
Andreas Gampedd9d0552015-03-09 12:57:41 -0700909 }
910}
911
912static void UnstartedMemoryPeek(
913 Primitive::Type type, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
914 int64_t address = shadow_frame->GetVRegLong(arg_offset);
915 // TODO: Check that this is in the heap somewhere. Otherwise we will segfault instead of
916 // aborting the transaction.
917
918 switch (type) {
919 case Primitive::kPrimByte: {
920 result->SetB(*reinterpret_cast<int8_t*>(static_cast<intptr_t>(address)));
921 return;
922 }
923
924 case Primitive::kPrimShort: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700925 typedef int16_t unaligned_short __attribute__ ((aligned (1)));
926 result->SetS(*reinterpret_cast<unaligned_short*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700927 return;
928 }
929
930 case Primitive::kPrimInt: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700931 typedef int32_t unaligned_int __attribute__ ((aligned (1)));
932 result->SetI(*reinterpret_cast<unaligned_int*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700933 return;
934 }
935
936 case Primitive::kPrimLong: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700937 typedef int64_t unaligned_long __attribute__ ((aligned (1)));
938 result->SetJ(*reinterpret_cast<unaligned_long*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700939 return;
940 }
941
942 case Primitive::kPrimBoolean:
943 case Primitive::kPrimChar:
944 case Primitive::kPrimFloat:
945 case Primitive::kPrimDouble:
946 case Primitive::kPrimVoid:
947 case Primitive::kPrimNot:
948 LOG(FATAL) << "Not in the Memory API: " << type;
949 UNREACHABLE();
950 }
951 LOG(FATAL) << "Should not reach here";
952 UNREACHABLE();
953}
954
Andreas Gampe799681b2015-05-15 19:24:12 -0700955void UnstartedRuntime::UnstartedMemoryPeekByte(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700956 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700957 UnstartedMemoryPeek(Primitive::kPrimByte, shadow_frame, result, arg_offset);
958}
959
960void UnstartedRuntime::UnstartedMemoryPeekShort(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700961 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700962 UnstartedMemoryPeek(Primitive::kPrimShort, shadow_frame, result, arg_offset);
963}
964
965void UnstartedRuntime::UnstartedMemoryPeekInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700966 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700967 UnstartedMemoryPeek(Primitive::kPrimInt, shadow_frame, result, arg_offset);
968}
969
970void UnstartedRuntime::UnstartedMemoryPeekLong(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700971 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700972 UnstartedMemoryPeek(Primitive::kPrimLong, shadow_frame, result, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -0700973}
974
975static void UnstartedMemoryPeekArray(
976 Primitive::Type type, Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700977 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700978 int64_t address_long = shadow_frame->GetVRegLong(arg_offset);
979 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 2);
980 if (obj == nullptr) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200981 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Null pointer in peekArray");
Andreas Gampedd9d0552015-03-09 12:57:41 -0700982 return;
983 }
984 mirror::Array* array = obj->AsArray();
985
986 int offset = shadow_frame->GetVReg(arg_offset + 3);
987 int count = shadow_frame->GetVReg(arg_offset + 4);
988 if (offset < 0 || offset + count > array->GetLength()) {
989 std::string error_msg(StringPrintf("Array out of bounds in peekArray: %d/%d vs %d",
990 offset, count, array->GetLength()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200991 Runtime::Current()->AbortTransactionAndThrowAbortError(self, error_msg.c_str());
Andreas Gampedd9d0552015-03-09 12:57:41 -0700992 return;
993 }
994
995 switch (type) {
996 case Primitive::kPrimByte: {
997 int8_t* address = reinterpret_cast<int8_t*>(static_cast<intptr_t>(address_long));
998 mirror::ByteArray* byte_array = array->AsByteArray();
999 for (int32_t i = 0; i < count; ++i, ++address) {
1000 byte_array->SetWithoutChecks<true>(i + offset, *address);
1001 }
1002 return;
1003 }
1004
1005 case Primitive::kPrimShort:
1006 case Primitive::kPrimInt:
1007 case Primitive::kPrimLong:
1008 LOG(FATAL) << "Type unimplemented for Memory Array API, should not reach here: " << type;
1009 UNREACHABLE();
1010
1011 case Primitive::kPrimBoolean:
1012 case Primitive::kPrimChar:
1013 case Primitive::kPrimFloat:
1014 case Primitive::kPrimDouble:
1015 case Primitive::kPrimVoid:
1016 case Primitive::kPrimNot:
1017 LOG(FATAL) << "Not in the Memory API: " << type;
1018 UNREACHABLE();
1019 }
1020 LOG(FATAL) << "Should not reach here";
1021 UNREACHABLE();
1022}
1023
Andreas Gampe799681b2015-05-15 19:24:12 -07001024void UnstartedRuntime::UnstartedMemoryPeekByteArray(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001025 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001026 UnstartedMemoryPeekArray(Primitive::kPrimByte, self, shadow_frame, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -07001027}
1028
Kenny Root1c9e61c2015-05-14 15:58:17 -07001029// This allows reading the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001030void UnstartedRuntime::UnstartedStringGetCharsNoCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001031 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001032 jint start = shadow_frame->GetVReg(arg_offset + 1);
1033 jint end = shadow_frame->GetVReg(arg_offset + 2);
1034 jint index = shadow_frame->GetVReg(arg_offset + 4);
1035 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1036 if (string == nullptr) {
1037 AbortTransactionOrFail(self, "String.getCharsNoCheck with null object");
1038 return;
1039 }
Kenny Root57f91e82015-05-14 15:58:17 -07001040 DCHECK_GE(start, 0);
1041 DCHECK_GE(end, string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -07001042 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001043 Handle<mirror::CharArray> h_char_array(
1044 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 3)->AsCharArray()));
Kenny Root57f91e82015-05-14 15:58:17 -07001045 DCHECK_LE(index, h_char_array->GetLength());
1046 DCHECK_LE(end - start, h_char_array->GetLength() - index);
Kenny Root1c9e61c2015-05-14 15:58:17 -07001047 string->GetChars(start, end, h_char_array, index);
1048}
1049
1050// This allows reading chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001051void UnstartedRuntime::UnstartedStringCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001052 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001053 jint index = shadow_frame->GetVReg(arg_offset + 1);
1054 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1055 if (string == nullptr) {
1056 AbortTransactionOrFail(self, "String.charAt with null object");
1057 return;
1058 }
1059 result->SetC(string->CharAt(index));
1060}
1061
Kenny Root57f91e82015-05-14 15:58:17 -07001062// This allows setting chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001063void UnstartedRuntime::UnstartedStringSetCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001064 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -07001065 jint index = shadow_frame->GetVReg(arg_offset + 1);
1066 jchar c = shadow_frame->GetVReg(arg_offset + 2);
1067 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1068 if (string == nullptr) {
1069 AbortTransactionOrFail(self, "String.setCharAt with null object");
1070 return;
1071 }
1072 string->SetCharAt(index, c);
1073}
1074
Kenny Root1c9e61c2015-05-14 15:58:17 -07001075// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001076void UnstartedRuntime::UnstartedStringFactoryNewStringFromChars(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001077 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001078 jint offset = shadow_frame->GetVReg(arg_offset);
1079 jint char_count = shadow_frame->GetVReg(arg_offset + 1);
1080 DCHECK_GE(char_count, 0);
1081 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001082 Handle<mirror::CharArray> h_char_array(
1083 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray()));
Kenny Root1c9e61c2015-05-14 15:58:17 -07001084 Runtime* runtime = Runtime::Current();
1085 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1086 result->SetL(mirror::String::AllocFromCharArray<true>(self, char_count, h_char_array, offset, allocator));
1087}
1088
1089// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001090void UnstartedRuntime::UnstartedStringFactoryNewStringFromString(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001091 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -07001092 mirror::String* to_copy = shadow_frame->GetVRegReference(arg_offset)->AsString();
1093 if (to_copy == nullptr) {
1094 AbortTransactionOrFail(self, "StringFactory.newStringFromString with null object");
1095 return;
1096 }
1097 StackHandleScope<1> hs(self);
1098 Handle<mirror::String> h_string(hs.NewHandle(to_copy));
1099 Runtime* runtime = Runtime::Current();
1100 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1101 result->SetL(mirror::String::AllocFromString<true>(self, h_string->GetLength(), h_string, 0,
1102 allocator));
1103}
1104
Andreas Gampe799681b2015-05-15 19:24:12 -07001105void UnstartedRuntime::UnstartedStringFastSubstring(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001106 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001107 jint start = shadow_frame->GetVReg(arg_offset + 1);
1108 jint length = shadow_frame->GetVReg(arg_offset + 2);
Kenny Root57f91e82015-05-14 15:58:17 -07001109 DCHECK_GE(start, 0);
Kenny Root1c9e61c2015-05-14 15:58:17 -07001110 DCHECK_GE(length, 0);
1111 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001112 Handle<mirror::String> h_string(
1113 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsString()));
Kenny Root57f91e82015-05-14 15:58:17 -07001114 DCHECK_LE(start, h_string->GetLength());
1115 DCHECK_LE(start + length, h_string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -07001116 Runtime* runtime = Runtime::Current();
1117 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1118 result->SetL(mirror::String::AllocFromString<true>(self, length, h_string, start, allocator));
1119}
1120
Kenny Root57f91e82015-05-14 15:58:17 -07001121// This allows getting the char array for new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001122void UnstartedRuntime::UnstartedStringToCharArray(
Kenny Root57f91e82015-05-14 15:58:17 -07001123 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -07001124 SHARED_REQUIRES(Locks::mutator_lock_) {
Kenny Root57f91e82015-05-14 15:58:17 -07001125 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1126 if (string == nullptr) {
1127 AbortTransactionOrFail(self, "String.charAt with null object");
1128 return;
1129 }
1130 result->SetL(string->ToCharArray(self));
1131}
1132
Andreas Gampebc4d2182016-02-22 10:03:12 -08001133// This allows statically initializing ConcurrentHashMap and SynchronousQueue.
1134void UnstartedRuntime::UnstartedReferenceGetReferent(
1135 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1136 mirror::Reference* const ref = down_cast<mirror::Reference*>(
1137 shadow_frame->GetVRegReference(arg_offset));
1138 if (ref == nullptr) {
1139 AbortTransactionOrFail(self, "Reference.getReferent() with null object");
1140 return;
1141 }
1142 mirror::Object* const referent =
1143 Runtime::Current()->GetHeap()->GetReferenceProcessor()->GetReferent(self, ref);
1144 result->SetL(referent);
1145}
1146
1147// This allows statically initializing ConcurrentHashMap and SynchronousQueue. We use a somewhat
1148// conservative upper bound. We restrict the callers to SynchronousQueue and ConcurrentHashMap,
1149// where we can predict the behavior (somewhat).
1150// Note: this is required (instead of lazy initialization) as these classes are used in the static
1151// initialization of other classes, so will *use* the value.
1152void UnstartedRuntime::UnstartedRuntimeAvailableProcessors(
1153 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
1154 std::string caller(PrettyMethod(shadow_frame->GetLink()->GetMethod()));
1155 if (caller == "void java.util.concurrent.SynchronousQueue.<clinit>()") {
1156 // SynchronousQueue really only separates between single- and multiprocessor case. Return
1157 // 8 as a conservative upper approximation.
1158 result->SetI(8);
1159 } else if (caller == "void java.util.concurrent.ConcurrentHashMap.<clinit>()") {
1160 // ConcurrentHashMap uses it for striding. 8 still seems an OK general value, as it's likely
1161 // a good upper bound.
1162 // TODO: Consider resetting in the zygote?
1163 result->SetI(8);
1164 } else {
1165 // Not supported.
1166 AbortTransactionOrFail(self, "Accessing availableProcessors not allowed");
1167 }
1168}
1169
1170// This allows accessing ConcurrentHashMap/SynchronousQueue.
1171
1172void UnstartedRuntime::UnstartedUnsafeCompareAndSwapLong(
1173 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1174 // Argument 0 is the Unsafe instance, skip.
1175 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1176 if (obj == nullptr) {
1177 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1178 return;
1179 }
1180 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1181 int64_t expectedValue = shadow_frame->GetVRegLong(arg_offset + 4);
1182 int64_t newValue = shadow_frame->GetVRegLong(arg_offset + 6);
1183
1184 // Must use non transactional mode.
1185 if (kUseReadBarrier) {
1186 // Need to make sure the reference stored in the field is a to-space one before attempting the
1187 // CAS or the CAS could fail incorrectly.
1188 mirror::HeapReference<mirror::Object>* field_addr =
1189 reinterpret_cast<mirror::HeapReference<mirror::Object>*>(
1190 reinterpret_cast<uint8_t*>(obj) + static_cast<size_t>(offset));
1191 ReadBarrier::Barrier<mirror::Object, kWithReadBarrier, /*kAlwaysUpdateField*/true>(
1192 obj,
1193 MemberOffset(offset),
1194 field_addr);
1195 }
1196 bool success;
1197 // Check whether we're in a transaction, call accordingly.
1198 if (Runtime::Current()->IsActiveTransaction()) {
1199 success = obj->CasFieldStrongSequentiallyConsistent64<true>(MemberOffset(offset),
1200 expectedValue,
1201 newValue);
1202 } else {
1203 success = obj->CasFieldStrongSequentiallyConsistent64<false>(MemberOffset(offset),
1204 expectedValue,
1205 newValue);
1206 }
1207 result->SetZ(success ? 1 : 0);
1208}
1209
1210void UnstartedRuntime::UnstartedUnsafeCompareAndSwapObject(
1211 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1212 // Argument 0 is the Unsafe instance, skip.
1213 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1214 if (obj == nullptr) {
1215 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1216 return;
1217 }
1218 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1219 mirror::Object* expected_value = shadow_frame->GetVRegReference(arg_offset + 4);
1220 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 5);
1221
1222 // Must use non transactional mode.
1223 if (kUseReadBarrier) {
1224 // Need to make sure the reference stored in the field is a to-space one before attempting the
1225 // CAS or the CAS could fail incorrectly.
1226 mirror::HeapReference<mirror::Object>* field_addr =
1227 reinterpret_cast<mirror::HeapReference<mirror::Object>*>(
1228 reinterpret_cast<uint8_t*>(obj) + static_cast<size_t>(offset));
1229 ReadBarrier::Barrier<mirror::Object, kWithReadBarrier, /*kAlwaysUpdateField*/true>(
1230 obj,
1231 MemberOffset(offset),
1232 field_addr);
1233 }
1234 bool success;
1235 // Check whether we're in a transaction, call accordingly.
1236 if (Runtime::Current()->IsActiveTransaction()) {
1237 success = obj->CasFieldStrongSequentiallyConsistentObject<true>(MemberOffset(offset),
1238 expected_value,
1239 newValue);
1240 } else {
1241 success = obj->CasFieldStrongSequentiallyConsistentObject<false>(MemberOffset(offset),
1242 expected_value,
1243 newValue);
1244 }
1245 result->SetZ(success ? 1 : 0);
1246}
1247
1248void UnstartedRuntime::UnstartedUnsafeGetObjectVolatile(
1249 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
1250 SHARED_REQUIRES(Locks::mutator_lock_) {
1251 // Argument 0 is the Unsafe instance, skip.
1252 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1253 if (obj == nullptr) {
1254 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1255 return;
1256 }
1257 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1258 mirror::Object* value = obj->GetFieldObjectVolatile<mirror::Object>(MemberOffset(offset));
1259 result->SetL(value);
1260}
1261
Andreas Gampe8a18fde2016-04-05 21:12:51 -07001262void UnstartedRuntime::UnstartedUnsafePutObjectVolatile(
1263 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
1264 SHARED_REQUIRES(Locks::mutator_lock_) {
1265 // Argument 0 is the Unsafe instance, skip.
1266 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1267 if (obj == nullptr) {
1268 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1269 return;
1270 }
1271 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1272 mirror::Object* value = shadow_frame->GetVRegReference(arg_offset + 4);
1273 if (Runtime::Current()->IsActiveTransaction()) {
1274 obj->SetFieldObjectVolatile<true>(MemberOffset(offset), value);
1275 } else {
1276 obj->SetFieldObjectVolatile<false>(MemberOffset(offset), value);
1277 }
1278}
1279
Andreas Gampebc4d2182016-02-22 10:03:12 -08001280void UnstartedRuntime::UnstartedUnsafePutOrderedObject(
1281 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
1282 SHARED_REQUIRES(Locks::mutator_lock_) {
1283 // Argument 0 is the Unsafe instance, skip.
1284 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1285 if (obj == nullptr) {
1286 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1287 return;
1288 }
1289 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1290 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 4);
1291 QuasiAtomic::ThreadFenceRelease();
1292 if (Runtime::Current()->IsActiveTransaction()) {
1293 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1294 } else {
1295 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1296 }
1297}
1298
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001299// A cutout for Integer.parseInt(String). Note: this code is conservative and will bail instead
1300// of correctly handling the corner cases.
1301void UnstartedRuntime::UnstartedIntegerParseInt(
1302 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
1303 SHARED_REQUIRES(Locks::mutator_lock_) {
1304 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1305 if (obj == nullptr) {
1306 AbortTransactionOrFail(self, "Cannot parse null string, retry at runtime.");
1307 return;
1308 }
1309
1310 std::string string_value = obj->AsString()->ToModifiedUtf8();
1311 if (string_value.empty()) {
1312 AbortTransactionOrFail(self, "Cannot parse empty string, retry at runtime.");
1313 return;
1314 }
1315
1316 const char* c_str = string_value.c_str();
1317 char *end;
1318 // Can we set errno to 0? Is this always a variable, and not a macro?
1319 // Worst case, we'll incorrectly fail a transaction. Seems OK.
1320 int64_t l = strtol(c_str, &end, 10);
1321
1322 if ((errno == ERANGE && l == LONG_MAX) || l > std::numeric_limits<int32_t>::max() ||
1323 (errno == ERANGE && l == LONG_MIN) || l < std::numeric_limits<int32_t>::min()) {
1324 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1325 return;
1326 }
1327 if (l == 0) {
1328 // Check whether the string wasn't exactly zero.
1329 if (string_value != "0") {
1330 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1331 return;
1332 }
1333 } else if (*end != '\0') {
1334 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1335 return;
1336 }
1337
1338 result->SetI(static_cast<int32_t>(l));
1339}
1340
1341// A cutout for Long.parseLong.
1342//
1343// Note: for now use code equivalent to Integer.parseInt, as the full range may not be supported
1344// well.
1345void UnstartedRuntime::UnstartedLongParseLong(
1346 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
1347 SHARED_REQUIRES(Locks::mutator_lock_) {
1348 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1349 if (obj == nullptr) {
1350 AbortTransactionOrFail(self, "Cannot parse null string, retry at runtime.");
1351 return;
1352 }
1353
1354 std::string string_value = obj->AsString()->ToModifiedUtf8();
1355 if (string_value.empty()) {
1356 AbortTransactionOrFail(self, "Cannot parse empty string, retry at runtime.");
1357 return;
1358 }
1359
1360 const char* c_str = string_value.c_str();
1361 char *end;
1362 // Can we set errno to 0? Is this always a variable, and not a macro?
1363 // Worst case, we'll incorrectly fail a transaction. Seems OK.
1364 int64_t l = strtol(c_str, &end, 10);
1365
1366 // Note: comparing against int32_t min/max is intentional here.
1367 if ((errno == ERANGE && l == LONG_MAX) || l > std::numeric_limits<int32_t>::max() ||
1368 (errno == ERANGE && l == LONG_MIN) || l < std::numeric_limits<int32_t>::min()) {
1369 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1370 return;
1371 }
1372 if (l == 0) {
1373 // Check whether the string wasn't exactly zero.
1374 if (string_value != "0") {
1375 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1376 return;
1377 }
1378 } else if (*end != '\0') {
1379 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1380 return;
1381 }
1382
1383 result->SetJ(l);
1384}
1385
Andreas Gampe715fdc22016-04-18 17:07:30 -07001386void UnstartedRuntime::UnstartedMethodInvoke(
1387 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
1388 SHARED_REQUIRES(Locks::mutator_lock_) {
1389 JNIEnvExt* env = self->GetJniEnv();
1390 ScopedObjectAccessUnchecked soa(self);
1391
1392 mirror::Object* java_method_obj = shadow_frame->GetVRegReference(arg_offset);
1393 ScopedLocalRef<jobject> java_method(env,
1394 java_method_obj == nullptr ? nullptr :env->AddLocalReference<jobject>(java_method_obj));
1395
1396 mirror::Object* java_receiver_obj = shadow_frame->GetVRegReference(arg_offset + 1);
1397 ScopedLocalRef<jobject> java_receiver(env,
1398 java_receiver_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_receiver_obj));
1399
1400 mirror::Object* java_args_obj = shadow_frame->GetVRegReference(arg_offset + 2);
1401 ScopedLocalRef<jobject> java_args(env,
1402 java_args_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_args_obj));
1403
1404 ScopedLocalRef<jobject> result_jobj(env,
1405 InvokeMethod(soa, java_method.get(), java_receiver.get(), java_args.get()));
1406
1407 result->SetL(self->DecodeJObject(result_jobj.get()));
1408
1409 // Conservatively flag all exceptions as transaction aborts. This way we don't need to unwrap
1410 // InvocationTargetExceptions.
1411 if (self->IsExceptionPending()) {
1412 AbortTransactionOrFail(self, "Failed Method.invoke");
1413 }
1414}
1415
Andreas Gampebc4d2182016-02-22 10:03:12 -08001416
Mathieu Chartiere401d142015-04-22 13:56:20 -07001417void UnstartedRuntime::UnstartedJNIVMRuntimeNewUnpaddedArray(
1418 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1419 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001420 int32_t length = args[1];
1421 DCHECK_GE(length, 0);
1422 mirror::Class* element_class = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1423 Runtime* runtime = Runtime::Current();
1424 mirror::Class* array_class = runtime->GetClassLinker()->FindArrayClass(self, &element_class);
1425 DCHECK(array_class != nullptr);
1426 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1427 result->SetL(mirror::Array::Alloc<true, true>(self, array_class, length,
1428 array_class->GetComponentSizeShift(), allocator));
1429}
1430
Mathieu Chartiere401d142015-04-22 13:56:20 -07001431void UnstartedRuntime::UnstartedJNIVMStackGetCallingClassLoader(
1432 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1433 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001434 result->SetL(nullptr);
1435}
1436
Mathieu Chartiere401d142015-04-22 13:56:20 -07001437void UnstartedRuntime::UnstartedJNIVMStackGetStackClass2(
1438 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1439 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001440 NthCallerVisitor visitor(self, 3);
1441 visitor.WalkStack();
1442 if (visitor.caller != nullptr) {
1443 result->SetL(visitor.caller->GetDeclaringClass());
1444 }
1445}
1446
Mathieu Chartiere401d142015-04-22 13:56:20 -07001447void UnstartedRuntime::UnstartedJNIMathLog(
1448 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1449 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001450 JValue value;
1451 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1452 result->SetD(log(value.GetD()));
1453}
1454
Mathieu Chartiere401d142015-04-22 13:56:20 -07001455void UnstartedRuntime::UnstartedJNIMathExp(
1456 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1457 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001458 JValue value;
1459 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1460 result->SetD(exp(value.GetD()));
1461}
1462
Andreas Gampebc4d2182016-02-22 10:03:12 -08001463void UnstartedRuntime::UnstartedJNIAtomicLongVMSupportsCS8(
1464 Thread* self ATTRIBUTE_UNUSED,
1465 ArtMethod* method ATTRIBUTE_UNUSED,
1466 mirror::Object* receiver ATTRIBUTE_UNUSED,
1467 uint32_t* args ATTRIBUTE_UNUSED,
1468 JValue* result) {
1469 result->SetZ(QuasiAtomic::LongAtomicsUseMutexes(Runtime::Current()->GetInstructionSet())
1470 ? 0
1471 : 1);
1472}
1473
Mathieu Chartiere401d142015-04-22 13:56:20 -07001474void UnstartedRuntime::UnstartedJNIClassGetNameNative(
1475 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1476 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001477 StackHandleScope<1> hs(self);
1478 result->SetL(mirror::Class::ComputeName(hs.NewHandle(receiver->AsClass())));
1479}
1480
Andreas Gampebc4d2182016-02-22 10:03:12 -08001481void UnstartedRuntime::UnstartedJNIDoubleLongBitsToDouble(
1482 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1483 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
1484 uint64_t long_input = args[0] | (static_cast<uint64_t>(args[1]) << 32);
1485 result->SetD(bit_cast<double>(long_input));
1486}
1487
Mathieu Chartiere401d142015-04-22 13:56:20 -07001488void UnstartedRuntime::UnstartedJNIFloatFloatToRawIntBits(
1489 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1490 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001491 result->SetI(args[0]);
1492}
1493
Mathieu Chartiere401d142015-04-22 13:56:20 -07001494void UnstartedRuntime::UnstartedJNIFloatIntBitsToFloat(
1495 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1496 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001497 result->SetI(args[0]);
1498}
1499
Mathieu Chartiere401d142015-04-22 13:56:20 -07001500void UnstartedRuntime::UnstartedJNIObjectInternalClone(
1501 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1502 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001503 result->SetL(receiver->Clone(self));
1504}
1505
Mathieu Chartiere401d142015-04-22 13:56:20 -07001506void UnstartedRuntime::UnstartedJNIObjectNotifyAll(
1507 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1508 uint32_t* args ATTRIBUTE_UNUSED, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001509 receiver->NotifyAll(self);
1510}
1511
Mathieu Chartiere401d142015-04-22 13:56:20 -07001512void UnstartedRuntime::UnstartedJNIStringCompareTo(
1513 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver, uint32_t* args,
1514 JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001515 mirror::String* rhs = reinterpret_cast<mirror::Object*>(args[0])->AsString();
1516 if (rhs == nullptr) {
Andreas Gampe068b0c02015-03-11 12:44:47 -07001517 AbortTransactionOrFail(self, "String.compareTo with null object");
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001518 }
1519 result->SetI(receiver->AsString()->CompareTo(rhs));
1520}
1521
Mathieu Chartiere401d142015-04-22 13:56:20 -07001522void UnstartedRuntime::UnstartedJNIStringIntern(
1523 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1524 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001525 result->SetL(receiver->AsString()->Intern());
1526}
1527
Mathieu Chartiere401d142015-04-22 13:56:20 -07001528void UnstartedRuntime::UnstartedJNIStringFastIndexOf(
1529 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1530 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001531 result->SetI(receiver->AsString()->FastIndexOf(args[0], args[1]));
1532}
1533
Mathieu Chartiere401d142015-04-22 13:56:20 -07001534void UnstartedRuntime::UnstartedJNIArrayCreateMultiArray(
1535 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1536 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001537 StackHandleScope<2> hs(self);
1538 auto h_class(hs.NewHandle(reinterpret_cast<mirror::Class*>(args[0])->AsClass()));
1539 auto h_dimensions(hs.NewHandle(reinterpret_cast<mirror::IntArray*>(args[1])->AsIntArray()));
1540 result->SetL(mirror::Array::CreateMultiArray(self, h_class, h_dimensions));
1541}
1542
Mathieu Chartiere401d142015-04-22 13:56:20 -07001543void UnstartedRuntime::UnstartedJNIArrayCreateObjectArray(
1544 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1545 uint32_t* args, JValue* result) {
Andreas Gampee598e042015-04-10 14:57:10 -07001546 int32_t length = static_cast<int32_t>(args[1]);
1547 if (length < 0) {
1548 ThrowNegativeArraySizeException(length);
1549 return;
1550 }
1551 mirror::Class* element_class = reinterpret_cast<mirror::Class*>(args[0])->AsClass();
1552 Runtime* runtime = Runtime::Current();
1553 ClassLinker* class_linker = runtime->GetClassLinker();
1554 mirror::Class* array_class = class_linker->FindArrayClass(self, &element_class);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001555 if (UNLIKELY(array_class == nullptr)) {
Andreas Gampee598e042015-04-10 14:57:10 -07001556 CHECK(self->IsExceptionPending());
1557 return;
1558 }
1559 DCHECK(array_class->IsObjectArrayClass());
1560 mirror::Array* new_array = mirror::ObjectArray<mirror::Object*>::Alloc(
1561 self, array_class, length, runtime->GetHeap()->GetCurrentAllocator());
1562 result->SetL(new_array);
1563}
1564
Mathieu Chartiere401d142015-04-22 13:56:20 -07001565void UnstartedRuntime::UnstartedJNIThrowableNativeFillInStackTrace(
1566 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1567 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001568 ScopedObjectAccessUnchecked soa(self);
1569 if (Runtime::Current()->IsActiveTransaction()) {
1570 result->SetL(soa.Decode<mirror::Object*>(self->CreateInternalStackTrace<true>(soa)));
1571 } else {
1572 result->SetL(soa.Decode<mirror::Object*>(self->CreateInternalStackTrace<false>(soa)));
1573 }
1574}
1575
Mathieu Chartiere401d142015-04-22 13:56:20 -07001576void UnstartedRuntime::UnstartedJNISystemIdentityHashCode(
1577 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1578 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001579 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1580 result->SetI((obj != nullptr) ? obj->IdentityHashCode() : 0);
1581}
1582
Mathieu Chartiere401d142015-04-22 13:56:20 -07001583void UnstartedRuntime::UnstartedJNIByteOrderIsLittleEndian(
1584 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1585 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001586 result->SetZ(JNI_TRUE);
1587}
1588
Mathieu Chartiere401d142015-04-22 13:56:20 -07001589void UnstartedRuntime::UnstartedJNIUnsafeCompareAndSwapInt(
1590 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1591 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001592 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1593 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1594 jint expectedValue = args[3];
1595 jint newValue = args[4];
1596 bool success;
1597 if (Runtime::Current()->IsActiveTransaction()) {
1598 success = obj->CasFieldStrongSequentiallyConsistent32<true>(MemberOffset(offset),
1599 expectedValue, newValue);
1600 } else {
1601 success = obj->CasFieldStrongSequentiallyConsistent32<false>(MemberOffset(offset),
1602 expectedValue, newValue);
1603 }
1604 result->SetZ(success ? JNI_TRUE : JNI_FALSE);
1605}
1606
Narayan Kamath34a316f2016-03-30 13:11:18 +01001607void UnstartedRuntime::UnstartedJNIUnsafeGetIntVolatile(
1608 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1609 uint32_t* args, JValue* result) {
1610 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1611 if (obj == nullptr) {
1612 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1613 return;
1614 }
1615
1616 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1617 result->SetI(obj->GetField32Volatile(MemberOffset(offset)));
1618}
1619
Mathieu Chartiere401d142015-04-22 13:56:20 -07001620void UnstartedRuntime::UnstartedJNIUnsafePutObject(
1621 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1622 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001623 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1624 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1625 mirror::Object* newValue = reinterpret_cast<mirror::Object*>(args[3]);
1626 if (Runtime::Current()->IsActiveTransaction()) {
1627 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1628 } else {
1629 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1630 }
1631}
1632
Andreas Gampe799681b2015-05-15 19:24:12 -07001633void UnstartedRuntime::UnstartedJNIUnsafeGetArrayBaseOffsetForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001634 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1635 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001636 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1637 Primitive::Type primitive_type = component->GetPrimitiveType();
1638 result->SetI(mirror::Array::DataOffset(Primitive::ComponentSize(primitive_type)).Int32Value());
1639}
1640
Andreas Gampe799681b2015-05-15 19:24:12 -07001641void UnstartedRuntime::UnstartedJNIUnsafeGetArrayIndexScaleForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001642 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1643 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001644 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1645 Primitive::Type primitive_type = component->GetPrimitiveType();
1646 result->SetI(Primitive::ComponentSize(primitive_type));
1647}
1648
Andreas Gampedd9d0552015-03-09 12:57:41 -07001649typedef void (*InvokeHandler)(Thread* self, ShadowFrame* shadow_frame, JValue* result,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001650 size_t arg_size);
1651
Mathieu Chartiere401d142015-04-22 13:56:20 -07001652typedef void (*JNIHandler)(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001653 uint32_t* args, JValue* result);
1654
1655static bool tables_initialized_ = false;
1656static std::unordered_map<std::string, InvokeHandler> invoke_handlers_;
1657static std::unordered_map<std::string, JNIHandler> jni_handlers_;
1658
Andreas Gampe799681b2015-05-15 19:24:12 -07001659void UnstartedRuntime::InitializeInvokeHandlers() {
1660#define UNSTARTED_DIRECT(ShortName, Sig) \
1661 invoke_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::Unstarted ## ShortName));
1662#include "unstarted_runtime_list.h"
1663 UNSTARTED_RUNTIME_DIRECT_LIST(UNSTARTED_DIRECT)
1664#undef UNSTARTED_RUNTIME_DIRECT_LIST
1665#undef UNSTARTED_RUNTIME_JNI_LIST
1666#undef UNSTARTED_DIRECT
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001667}
1668
Andreas Gampe799681b2015-05-15 19:24:12 -07001669void UnstartedRuntime::InitializeJNIHandlers() {
1670#define UNSTARTED_JNI(ShortName, Sig) \
1671 jni_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::UnstartedJNI ## ShortName));
1672#include "unstarted_runtime_list.h"
1673 UNSTARTED_RUNTIME_JNI_LIST(UNSTARTED_JNI)
1674#undef UNSTARTED_RUNTIME_DIRECT_LIST
1675#undef UNSTARTED_RUNTIME_JNI_LIST
1676#undef UNSTARTED_JNI
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001677}
1678
Andreas Gampe799681b2015-05-15 19:24:12 -07001679void UnstartedRuntime::Initialize() {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001680 CHECK(!tables_initialized_);
1681
Andreas Gampe799681b2015-05-15 19:24:12 -07001682 InitializeInvokeHandlers();
1683 InitializeJNIHandlers();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001684
1685 tables_initialized_ = true;
1686}
1687
Andreas Gampe799681b2015-05-15 19:24:12 -07001688void UnstartedRuntime::Invoke(Thread* self, const DexFile::CodeItem* code_item,
1689 ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001690 // In a runtime that's not started we intercept certain methods to avoid complicated dependency
1691 // problems in core libraries.
1692 CHECK(tables_initialized_);
1693
1694 std::string name(PrettyMethod(shadow_frame->GetMethod()));
1695 const auto& iter = invoke_handlers_.find(name);
1696 if (iter != invoke_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001697 // Clear out the result in case it's not zeroed out.
1698 result->SetL(0);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001699
1700 // Push the shadow frame. This is so the failing method can be seen in abort dumps.
1701 self->PushShadowFrame(shadow_frame);
1702
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001703 (*iter->second)(self, shadow_frame, result, arg_offset);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001704
1705 self->PopShadowFrame();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001706 } else {
1707 // Not special, continue with regular interpreter execution.
Andreas Gampe3cfa4d02015-10-06 17:04:01 -07001708 ArtInterpreterToInterpreterBridge(self, code_item, shadow_frame, result);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001709 }
1710}
1711
1712// Hand select a number of methods to be run in a not yet started runtime without using JNI.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001713void UnstartedRuntime::Jni(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe799681b2015-05-15 19:24:12 -07001714 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001715 std::string name(PrettyMethod(method));
1716 const auto& iter = jni_handlers_.find(name);
1717 if (iter != jni_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001718 // Clear out the result in case it's not zeroed out.
1719 result->SetL(0);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001720 (*iter->second)(self, method, receiver, args, result);
1721 } else if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +02001722 AbortTransactionF(self, "Attempt to invoke native method in non-started runtime: %s",
1723 name.c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001724 } else {
1725 LOG(FATAL) << "Calling native method " << PrettyMethod(method) << " in an unstarted "
1726 "non-transactional runtime";
1727 }
1728}
1729
1730} // namespace interpreter
1731} // namespace art