blob: 6d00ce1c56d003297ace00127e52f54c566c02a0 [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 Gampe799681b2015-05-15 19:24:12 -0700837void UnstartedRuntime::UnstartedObjectHashCode(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700838 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700839 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
840 result->SetI(obj->IdentityHashCode());
841}
842
Andreas Gampe799681b2015-05-15 19:24:12 -0700843void UnstartedRuntime::UnstartedDoubleDoubleToRawLongBits(
Andreas Gampedd9d0552015-03-09 12:57:41 -0700844 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700845 double in = shadow_frame->GetVRegDouble(arg_offset);
Roland Levillainda4d79b2015-03-24 14:36:11 +0000846 result->SetJ(bit_cast<int64_t, double>(in));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700847}
848
Andreas Gampedd9d0552015-03-09 12:57:41 -0700849static mirror::Object* GetDexFromDexCache(Thread* self, mirror::DexCache* dex_cache)
Mathieu Chartier90443472015-07-16 20:32:27 -0700850 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700851 const DexFile* dex_file = dex_cache->GetDexFile();
852 if (dex_file == nullptr) {
853 return nullptr;
854 }
855
856 // Create the direct byte buffer.
857 JNIEnv* env = self->GetJniEnv();
858 DCHECK(env != nullptr);
859 void* address = const_cast<void*>(reinterpret_cast<const void*>(dex_file->Begin()));
Andreas Gampeaacc25d2015-04-01 14:49:06 -0700860 ScopedLocalRef<jobject> byte_buffer(env, env->NewDirectByteBuffer(address, dex_file->Size()));
861 if (byte_buffer.get() == nullptr) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700862 DCHECK(self->IsExceptionPending());
863 return nullptr;
864 }
865
866 jvalue args[1];
Andreas Gampeaacc25d2015-04-01 14:49:06 -0700867 args[0].l = byte_buffer.get();
868
869 ScopedLocalRef<jobject> dex(env, env->CallStaticObjectMethodA(
870 WellKnownClasses::com_android_dex_Dex,
871 WellKnownClasses::com_android_dex_Dex_create,
872 args));
873
874 return self->DecodeJObject(dex.get());
Andreas Gampedd9d0552015-03-09 12:57:41 -0700875}
876
Andreas Gampe799681b2015-05-15 19:24:12 -0700877void UnstartedRuntime::UnstartedDexCacheGetDexNative(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700878 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700879 // We will create the Dex object, but the image writer will release it before creating the
880 // art file.
881 mirror::Object* src = shadow_frame->GetVRegReference(arg_offset);
882 bool have_dex = false;
883 if (src != nullptr) {
884 mirror::Object* dex = GetDexFromDexCache(self, reinterpret_cast<mirror::DexCache*>(src));
885 if (dex != nullptr) {
886 have_dex = true;
887 result->SetL(dex);
888 }
889 }
890 if (!have_dex) {
891 self->ClearException();
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200892 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Could not create Dex object");
Andreas Gampedd9d0552015-03-09 12:57:41 -0700893 }
894}
895
896static void UnstartedMemoryPeek(
897 Primitive::Type type, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
898 int64_t address = shadow_frame->GetVRegLong(arg_offset);
899 // TODO: Check that this is in the heap somewhere. Otherwise we will segfault instead of
900 // aborting the transaction.
901
902 switch (type) {
903 case Primitive::kPrimByte: {
904 result->SetB(*reinterpret_cast<int8_t*>(static_cast<intptr_t>(address)));
905 return;
906 }
907
908 case Primitive::kPrimShort: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700909 typedef int16_t unaligned_short __attribute__ ((aligned (1)));
910 result->SetS(*reinterpret_cast<unaligned_short*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700911 return;
912 }
913
914 case Primitive::kPrimInt: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700915 typedef int32_t unaligned_int __attribute__ ((aligned (1)));
916 result->SetI(*reinterpret_cast<unaligned_int*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700917 return;
918 }
919
920 case Primitive::kPrimLong: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700921 typedef int64_t unaligned_long __attribute__ ((aligned (1)));
922 result->SetJ(*reinterpret_cast<unaligned_long*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700923 return;
924 }
925
926 case Primitive::kPrimBoolean:
927 case Primitive::kPrimChar:
928 case Primitive::kPrimFloat:
929 case Primitive::kPrimDouble:
930 case Primitive::kPrimVoid:
931 case Primitive::kPrimNot:
932 LOG(FATAL) << "Not in the Memory API: " << type;
933 UNREACHABLE();
934 }
935 LOG(FATAL) << "Should not reach here";
936 UNREACHABLE();
937}
938
Andreas Gampe799681b2015-05-15 19:24:12 -0700939void UnstartedRuntime::UnstartedMemoryPeekByte(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700940 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700941 UnstartedMemoryPeek(Primitive::kPrimByte, shadow_frame, result, arg_offset);
942}
943
944void UnstartedRuntime::UnstartedMemoryPeekShort(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700945 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700946 UnstartedMemoryPeek(Primitive::kPrimShort, shadow_frame, result, arg_offset);
947}
948
949void UnstartedRuntime::UnstartedMemoryPeekInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700950 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700951 UnstartedMemoryPeek(Primitive::kPrimInt, shadow_frame, result, arg_offset);
952}
953
954void UnstartedRuntime::UnstartedMemoryPeekLong(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700955 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700956 UnstartedMemoryPeek(Primitive::kPrimLong, shadow_frame, result, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -0700957}
958
959static void UnstartedMemoryPeekArray(
960 Primitive::Type type, Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700961 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700962 int64_t address_long = shadow_frame->GetVRegLong(arg_offset);
963 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 2);
964 if (obj == nullptr) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200965 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Null pointer in peekArray");
Andreas Gampedd9d0552015-03-09 12:57:41 -0700966 return;
967 }
968 mirror::Array* array = obj->AsArray();
969
970 int offset = shadow_frame->GetVReg(arg_offset + 3);
971 int count = shadow_frame->GetVReg(arg_offset + 4);
972 if (offset < 0 || offset + count > array->GetLength()) {
973 std::string error_msg(StringPrintf("Array out of bounds in peekArray: %d/%d vs %d",
974 offset, count, array->GetLength()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200975 Runtime::Current()->AbortTransactionAndThrowAbortError(self, error_msg.c_str());
Andreas Gampedd9d0552015-03-09 12:57:41 -0700976 return;
977 }
978
979 switch (type) {
980 case Primitive::kPrimByte: {
981 int8_t* address = reinterpret_cast<int8_t*>(static_cast<intptr_t>(address_long));
982 mirror::ByteArray* byte_array = array->AsByteArray();
983 for (int32_t i = 0; i < count; ++i, ++address) {
984 byte_array->SetWithoutChecks<true>(i + offset, *address);
985 }
986 return;
987 }
988
989 case Primitive::kPrimShort:
990 case Primitive::kPrimInt:
991 case Primitive::kPrimLong:
992 LOG(FATAL) << "Type unimplemented for Memory Array API, should not reach here: " << type;
993 UNREACHABLE();
994
995 case Primitive::kPrimBoolean:
996 case Primitive::kPrimChar:
997 case Primitive::kPrimFloat:
998 case Primitive::kPrimDouble:
999 case Primitive::kPrimVoid:
1000 case Primitive::kPrimNot:
1001 LOG(FATAL) << "Not in the Memory API: " << type;
1002 UNREACHABLE();
1003 }
1004 LOG(FATAL) << "Should not reach here";
1005 UNREACHABLE();
1006}
1007
Andreas Gampe799681b2015-05-15 19:24:12 -07001008void UnstartedRuntime::UnstartedMemoryPeekByteArray(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001009 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001010 UnstartedMemoryPeekArray(Primitive::kPrimByte, self, shadow_frame, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -07001011}
1012
Kenny Root1c9e61c2015-05-14 15:58:17 -07001013// This allows reading the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001014void UnstartedRuntime::UnstartedStringGetCharsNoCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001015 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001016 jint start = shadow_frame->GetVReg(arg_offset + 1);
1017 jint end = shadow_frame->GetVReg(arg_offset + 2);
1018 jint index = shadow_frame->GetVReg(arg_offset + 4);
1019 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1020 if (string == nullptr) {
1021 AbortTransactionOrFail(self, "String.getCharsNoCheck with null object");
1022 return;
1023 }
Kenny Root57f91e82015-05-14 15:58:17 -07001024 DCHECK_GE(start, 0);
1025 DCHECK_GE(end, string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -07001026 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001027 Handle<mirror::CharArray> h_char_array(
1028 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 3)->AsCharArray()));
Kenny Root57f91e82015-05-14 15:58:17 -07001029 DCHECK_LE(index, h_char_array->GetLength());
1030 DCHECK_LE(end - start, h_char_array->GetLength() - index);
Kenny Root1c9e61c2015-05-14 15:58:17 -07001031 string->GetChars(start, end, h_char_array, index);
1032}
1033
1034// This allows reading chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001035void UnstartedRuntime::UnstartedStringCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001036 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001037 jint index = shadow_frame->GetVReg(arg_offset + 1);
1038 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1039 if (string == nullptr) {
1040 AbortTransactionOrFail(self, "String.charAt with null object");
1041 return;
1042 }
1043 result->SetC(string->CharAt(index));
1044}
1045
Kenny Root57f91e82015-05-14 15:58:17 -07001046// This allows setting chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001047void UnstartedRuntime::UnstartedStringSetCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001048 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -07001049 jint index = shadow_frame->GetVReg(arg_offset + 1);
1050 jchar c = shadow_frame->GetVReg(arg_offset + 2);
1051 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1052 if (string == nullptr) {
1053 AbortTransactionOrFail(self, "String.setCharAt with null object");
1054 return;
1055 }
1056 string->SetCharAt(index, c);
1057}
1058
Kenny Root1c9e61c2015-05-14 15:58:17 -07001059// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001060void UnstartedRuntime::UnstartedStringFactoryNewStringFromChars(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001061 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001062 jint offset = shadow_frame->GetVReg(arg_offset);
1063 jint char_count = shadow_frame->GetVReg(arg_offset + 1);
1064 DCHECK_GE(char_count, 0);
1065 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001066 Handle<mirror::CharArray> h_char_array(
1067 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray()));
Kenny Root1c9e61c2015-05-14 15:58:17 -07001068 Runtime* runtime = Runtime::Current();
1069 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1070 result->SetL(mirror::String::AllocFromCharArray<true>(self, char_count, h_char_array, offset, allocator));
1071}
1072
1073// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001074void UnstartedRuntime::UnstartedStringFactoryNewStringFromString(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001075 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -07001076 mirror::String* to_copy = shadow_frame->GetVRegReference(arg_offset)->AsString();
1077 if (to_copy == nullptr) {
1078 AbortTransactionOrFail(self, "StringFactory.newStringFromString with null object");
1079 return;
1080 }
1081 StackHandleScope<1> hs(self);
1082 Handle<mirror::String> h_string(hs.NewHandle(to_copy));
1083 Runtime* runtime = Runtime::Current();
1084 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1085 result->SetL(mirror::String::AllocFromString<true>(self, h_string->GetLength(), h_string, 0,
1086 allocator));
1087}
1088
Andreas Gampe799681b2015-05-15 19:24:12 -07001089void UnstartedRuntime::UnstartedStringFastSubstring(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001090 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001091 jint start = shadow_frame->GetVReg(arg_offset + 1);
1092 jint length = shadow_frame->GetVReg(arg_offset + 2);
Kenny Root57f91e82015-05-14 15:58:17 -07001093 DCHECK_GE(start, 0);
Kenny Root1c9e61c2015-05-14 15:58:17 -07001094 DCHECK_GE(length, 0);
1095 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001096 Handle<mirror::String> h_string(
1097 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsString()));
Kenny Root57f91e82015-05-14 15:58:17 -07001098 DCHECK_LE(start, h_string->GetLength());
1099 DCHECK_LE(start + length, h_string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -07001100 Runtime* runtime = Runtime::Current();
1101 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1102 result->SetL(mirror::String::AllocFromString<true>(self, length, h_string, start, allocator));
1103}
1104
Kenny Root57f91e82015-05-14 15:58:17 -07001105// This allows getting the char array for new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001106void UnstartedRuntime::UnstartedStringToCharArray(
Kenny Root57f91e82015-05-14 15:58:17 -07001107 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -07001108 SHARED_REQUIRES(Locks::mutator_lock_) {
Kenny Root57f91e82015-05-14 15:58:17 -07001109 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1110 if (string == nullptr) {
1111 AbortTransactionOrFail(self, "String.charAt with null object");
1112 return;
1113 }
1114 result->SetL(string->ToCharArray(self));
1115}
1116
Andreas Gampebc4d2182016-02-22 10:03:12 -08001117// This allows statically initializing ConcurrentHashMap and SynchronousQueue.
1118void UnstartedRuntime::UnstartedReferenceGetReferent(
1119 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1120 mirror::Reference* const ref = down_cast<mirror::Reference*>(
1121 shadow_frame->GetVRegReference(arg_offset));
1122 if (ref == nullptr) {
1123 AbortTransactionOrFail(self, "Reference.getReferent() with null object");
1124 return;
1125 }
1126 mirror::Object* const referent =
1127 Runtime::Current()->GetHeap()->GetReferenceProcessor()->GetReferent(self, ref);
1128 result->SetL(referent);
1129}
1130
1131// This allows statically initializing ConcurrentHashMap and SynchronousQueue. We use a somewhat
1132// conservative upper bound. We restrict the callers to SynchronousQueue and ConcurrentHashMap,
1133// where we can predict the behavior (somewhat).
1134// Note: this is required (instead of lazy initialization) as these classes are used in the static
1135// initialization of other classes, so will *use* the value.
1136void UnstartedRuntime::UnstartedRuntimeAvailableProcessors(
1137 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
1138 std::string caller(PrettyMethod(shadow_frame->GetLink()->GetMethod()));
1139 if (caller == "void java.util.concurrent.SynchronousQueue.<clinit>()") {
1140 // SynchronousQueue really only separates between single- and multiprocessor case. Return
1141 // 8 as a conservative upper approximation.
1142 result->SetI(8);
1143 } else if (caller == "void java.util.concurrent.ConcurrentHashMap.<clinit>()") {
1144 // ConcurrentHashMap uses it for striding. 8 still seems an OK general value, as it's likely
1145 // a good upper bound.
1146 // TODO: Consider resetting in the zygote?
1147 result->SetI(8);
1148 } else {
1149 // Not supported.
1150 AbortTransactionOrFail(self, "Accessing availableProcessors not allowed");
1151 }
1152}
1153
1154// This allows accessing ConcurrentHashMap/SynchronousQueue.
1155
1156void UnstartedRuntime::UnstartedUnsafeCompareAndSwapLong(
1157 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1158 // Argument 0 is the Unsafe instance, skip.
1159 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1160 if (obj == nullptr) {
1161 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1162 return;
1163 }
1164 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1165 int64_t expectedValue = shadow_frame->GetVRegLong(arg_offset + 4);
1166 int64_t newValue = shadow_frame->GetVRegLong(arg_offset + 6);
1167
1168 // Must use non transactional mode.
1169 if (kUseReadBarrier) {
1170 // Need to make sure the reference stored in the field is a to-space one before attempting the
1171 // CAS or the CAS could fail incorrectly.
1172 mirror::HeapReference<mirror::Object>* field_addr =
1173 reinterpret_cast<mirror::HeapReference<mirror::Object>*>(
1174 reinterpret_cast<uint8_t*>(obj) + static_cast<size_t>(offset));
1175 ReadBarrier::Barrier<mirror::Object, kWithReadBarrier, /*kAlwaysUpdateField*/true>(
1176 obj,
1177 MemberOffset(offset),
1178 field_addr);
1179 }
1180 bool success;
1181 // Check whether we're in a transaction, call accordingly.
1182 if (Runtime::Current()->IsActiveTransaction()) {
1183 success = obj->CasFieldStrongSequentiallyConsistent64<true>(MemberOffset(offset),
1184 expectedValue,
1185 newValue);
1186 } else {
1187 success = obj->CasFieldStrongSequentiallyConsistent64<false>(MemberOffset(offset),
1188 expectedValue,
1189 newValue);
1190 }
1191 result->SetZ(success ? 1 : 0);
1192}
1193
1194void UnstartedRuntime::UnstartedUnsafeCompareAndSwapObject(
1195 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1196 // Argument 0 is the Unsafe instance, skip.
1197 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1198 if (obj == nullptr) {
1199 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1200 return;
1201 }
1202 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1203 mirror::Object* expected_value = shadow_frame->GetVRegReference(arg_offset + 4);
1204 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 5);
1205
1206 // Must use non transactional mode.
1207 if (kUseReadBarrier) {
1208 // Need to make sure the reference stored in the field is a to-space one before attempting the
1209 // CAS or the CAS could fail incorrectly.
1210 mirror::HeapReference<mirror::Object>* field_addr =
1211 reinterpret_cast<mirror::HeapReference<mirror::Object>*>(
1212 reinterpret_cast<uint8_t*>(obj) + static_cast<size_t>(offset));
1213 ReadBarrier::Barrier<mirror::Object, kWithReadBarrier, /*kAlwaysUpdateField*/true>(
1214 obj,
1215 MemberOffset(offset),
1216 field_addr);
1217 }
1218 bool success;
1219 // Check whether we're in a transaction, call accordingly.
1220 if (Runtime::Current()->IsActiveTransaction()) {
1221 success = obj->CasFieldStrongSequentiallyConsistentObject<true>(MemberOffset(offset),
1222 expected_value,
1223 newValue);
1224 } else {
1225 success = obj->CasFieldStrongSequentiallyConsistentObject<false>(MemberOffset(offset),
1226 expected_value,
1227 newValue);
1228 }
1229 result->SetZ(success ? 1 : 0);
1230}
1231
1232void UnstartedRuntime::UnstartedUnsafeGetObjectVolatile(
1233 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
1234 SHARED_REQUIRES(Locks::mutator_lock_) {
1235 // Argument 0 is the Unsafe instance, skip.
1236 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1237 if (obj == nullptr) {
1238 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1239 return;
1240 }
1241 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1242 mirror::Object* value = obj->GetFieldObjectVolatile<mirror::Object>(MemberOffset(offset));
1243 result->SetL(value);
1244}
1245
Andreas Gampe8a18fde2016-04-05 21:12:51 -07001246void UnstartedRuntime::UnstartedUnsafePutObjectVolatile(
1247 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
1248 SHARED_REQUIRES(Locks::mutator_lock_) {
1249 // Argument 0 is the Unsafe instance, skip.
1250 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1251 if (obj == nullptr) {
1252 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1253 return;
1254 }
1255 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1256 mirror::Object* value = shadow_frame->GetVRegReference(arg_offset + 4);
1257 if (Runtime::Current()->IsActiveTransaction()) {
1258 obj->SetFieldObjectVolatile<true>(MemberOffset(offset), value);
1259 } else {
1260 obj->SetFieldObjectVolatile<false>(MemberOffset(offset), value);
1261 }
1262}
1263
Andreas Gampebc4d2182016-02-22 10:03:12 -08001264void UnstartedRuntime::UnstartedUnsafePutOrderedObject(
1265 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
1266 SHARED_REQUIRES(Locks::mutator_lock_) {
1267 // Argument 0 is the Unsafe instance, skip.
1268 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1269 if (obj == nullptr) {
1270 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1271 return;
1272 }
1273 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1274 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 4);
1275 QuasiAtomic::ThreadFenceRelease();
1276 if (Runtime::Current()->IsActiveTransaction()) {
1277 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1278 } else {
1279 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1280 }
1281}
1282
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001283// A cutout for Integer.parseInt(String). Note: this code is conservative and will bail instead
1284// of correctly handling the corner cases.
1285void UnstartedRuntime::UnstartedIntegerParseInt(
1286 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
1287 SHARED_REQUIRES(Locks::mutator_lock_) {
1288 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1289 if (obj == nullptr) {
1290 AbortTransactionOrFail(self, "Cannot parse null string, retry at runtime.");
1291 return;
1292 }
1293
1294 std::string string_value = obj->AsString()->ToModifiedUtf8();
1295 if (string_value.empty()) {
1296 AbortTransactionOrFail(self, "Cannot parse empty string, retry at runtime.");
1297 return;
1298 }
1299
1300 const char* c_str = string_value.c_str();
1301 char *end;
1302 // Can we set errno to 0? Is this always a variable, and not a macro?
1303 // Worst case, we'll incorrectly fail a transaction. Seems OK.
1304 int64_t l = strtol(c_str, &end, 10);
1305
1306 if ((errno == ERANGE && l == LONG_MAX) || l > std::numeric_limits<int32_t>::max() ||
1307 (errno == ERANGE && l == LONG_MIN) || l < std::numeric_limits<int32_t>::min()) {
1308 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1309 return;
1310 }
1311 if (l == 0) {
1312 // Check whether the string wasn't exactly zero.
1313 if (string_value != "0") {
1314 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1315 return;
1316 }
1317 } else if (*end != '\0') {
1318 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1319 return;
1320 }
1321
1322 result->SetI(static_cast<int32_t>(l));
1323}
1324
1325// A cutout for Long.parseLong.
1326//
1327// Note: for now use code equivalent to Integer.parseInt, as the full range may not be supported
1328// well.
1329void UnstartedRuntime::UnstartedLongParseLong(
1330 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
1331 SHARED_REQUIRES(Locks::mutator_lock_) {
1332 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1333 if (obj == nullptr) {
1334 AbortTransactionOrFail(self, "Cannot parse null string, retry at runtime.");
1335 return;
1336 }
1337
1338 std::string string_value = obj->AsString()->ToModifiedUtf8();
1339 if (string_value.empty()) {
1340 AbortTransactionOrFail(self, "Cannot parse empty string, retry at runtime.");
1341 return;
1342 }
1343
1344 const char* c_str = string_value.c_str();
1345 char *end;
1346 // Can we set errno to 0? Is this always a variable, and not a macro?
1347 // Worst case, we'll incorrectly fail a transaction. Seems OK.
1348 int64_t l = strtol(c_str, &end, 10);
1349
1350 // Note: comparing against int32_t min/max is intentional here.
1351 if ((errno == ERANGE && l == LONG_MAX) || l > std::numeric_limits<int32_t>::max() ||
1352 (errno == ERANGE && l == LONG_MIN) || l < std::numeric_limits<int32_t>::min()) {
1353 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1354 return;
1355 }
1356 if (l == 0) {
1357 // Check whether the string wasn't exactly zero.
1358 if (string_value != "0") {
1359 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1360 return;
1361 }
1362 } else if (*end != '\0') {
1363 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1364 return;
1365 }
1366
1367 result->SetJ(l);
1368}
1369
Andreas Gampe715fdc22016-04-18 17:07:30 -07001370void UnstartedRuntime::UnstartedMethodInvoke(
1371 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
1372 SHARED_REQUIRES(Locks::mutator_lock_) {
1373 JNIEnvExt* env = self->GetJniEnv();
1374 ScopedObjectAccessUnchecked soa(self);
1375
1376 mirror::Object* java_method_obj = shadow_frame->GetVRegReference(arg_offset);
1377 ScopedLocalRef<jobject> java_method(env,
1378 java_method_obj == nullptr ? nullptr :env->AddLocalReference<jobject>(java_method_obj));
1379
1380 mirror::Object* java_receiver_obj = shadow_frame->GetVRegReference(arg_offset + 1);
1381 ScopedLocalRef<jobject> java_receiver(env,
1382 java_receiver_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_receiver_obj));
1383
1384 mirror::Object* java_args_obj = shadow_frame->GetVRegReference(arg_offset + 2);
1385 ScopedLocalRef<jobject> java_args(env,
1386 java_args_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_args_obj));
1387
1388 ScopedLocalRef<jobject> result_jobj(env,
1389 InvokeMethod(soa, java_method.get(), java_receiver.get(), java_args.get()));
1390
1391 result->SetL(self->DecodeJObject(result_jobj.get()));
1392
1393 // Conservatively flag all exceptions as transaction aborts. This way we don't need to unwrap
1394 // InvocationTargetExceptions.
1395 if (self->IsExceptionPending()) {
1396 AbortTransactionOrFail(self, "Failed Method.invoke");
1397 }
1398}
1399
Andreas Gampebc4d2182016-02-22 10:03:12 -08001400
Mathieu Chartiere401d142015-04-22 13:56:20 -07001401void UnstartedRuntime::UnstartedJNIVMRuntimeNewUnpaddedArray(
1402 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1403 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001404 int32_t length = args[1];
1405 DCHECK_GE(length, 0);
1406 mirror::Class* element_class = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1407 Runtime* runtime = Runtime::Current();
1408 mirror::Class* array_class = runtime->GetClassLinker()->FindArrayClass(self, &element_class);
1409 DCHECK(array_class != nullptr);
1410 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1411 result->SetL(mirror::Array::Alloc<true, true>(self, array_class, length,
1412 array_class->GetComponentSizeShift(), allocator));
1413}
1414
Mathieu Chartiere401d142015-04-22 13:56:20 -07001415void UnstartedRuntime::UnstartedJNIVMStackGetCallingClassLoader(
1416 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1417 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001418 result->SetL(nullptr);
1419}
1420
Mathieu Chartiere401d142015-04-22 13:56:20 -07001421void UnstartedRuntime::UnstartedJNIVMStackGetStackClass2(
1422 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1423 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001424 NthCallerVisitor visitor(self, 3);
1425 visitor.WalkStack();
1426 if (visitor.caller != nullptr) {
1427 result->SetL(visitor.caller->GetDeclaringClass());
1428 }
1429}
1430
Mathieu Chartiere401d142015-04-22 13:56:20 -07001431void UnstartedRuntime::UnstartedJNIMathLog(
1432 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1433 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001434 JValue value;
1435 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1436 result->SetD(log(value.GetD()));
1437}
1438
Mathieu Chartiere401d142015-04-22 13:56:20 -07001439void UnstartedRuntime::UnstartedJNIMathExp(
1440 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1441 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001442 JValue value;
1443 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1444 result->SetD(exp(value.GetD()));
1445}
1446
Andreas Gampebc4d2182016-02-22 10:03:12 -08001447void UnstartedRuntime::UnstartedJNIAtomicLongVMSupportsCS8(
1448 Thread* self ATTRIBUTE_UNUSED,
1449 ArtMethod* method ATTRIBUTE_UNUSED,
1450 mirror::Object* receiver ATTRIBUTE_UNUSED,
1451 uint32_t* args ATTRIBUTE_UNUSED,
1452 JValue* result) {
1453 result->SetZ(QuasiAtomic::LongAtomicsUseMutexes(Runtime::Current()->GetInstructionSet())
1454 ? 0
1455 : 1);
1456}
1457
Mathieu Chartiere401d142015-04-22 13:56:20 -07001458void UnstartedRuntime::UnstartedJNIClassGetNameNative(
1459 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1460 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001461 StackHandleScope<1> hs(self);
1462 result->SetL(mirror::Class::ComputeName(hs.NewHandle(receiver->AsClass())));
1463}
1464
Andreas Gampebc4d2182016-02-22 10:03:12 -08001465void UnstartedRuntime::UnstartedJNIDoubleLongBitsToDouble(
1466 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1467 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
1468 uint64_t long_input = args[0] | (static_cast<uint64_t>(args[1]) << 32);
1469 result->SetD(bit_cast<double>(long_input));
1470}
1471
Mathieu Chartiere401d142015-04-22 13:56:20 -07001472void UnstartedRuntime::UnstartedJNIFloatFloatToRawIntBits(
1473 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1474 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001475 result->SetI(args[0]);
1476}
1477
Mathieu Chartiere401d142015-04-22 13:56:20 -07001478void UnstartedRuntime::UnstartedJNIFloatIntBitsToFloat(
1479 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1480 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001481 result->SetI(args[0]);
1482}
1483
Mathieu Chartiere401d142015-04-22 13:56:20 -07001484void UnstartedRuntime::UnstartedJNIObjectInternalClone(
1485 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1486 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001487 result->SetL(receiver->Clone(self));
1488}
1489
Mathieu Chartiere401d142015-04-22 13:56:20 -07001490void UnstartedRuntime::UnstartedJNIObjectNotifyAll(
1491 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1492 uint32_t* args ATTRIBUTE_UNUSED, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001493 receiver->NotifyAll(self);
1494}
1495
Mathieu Chartiere401d142015-04-22 13:56:20 -07001496void UnstartedRuntime::UnstartedJNIStringCompareTo(
1497 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver, uint32_t* args,
1498 JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001499 mirror::String* rhs = reinterpret_cast<mirror::Object*>(args[0])->AsString();
1500 if (rhs == nullptr) {
Andreas Gampe068b0c02015-03-11 12:44:47 -07001501 AbortTransactionOrFail(self, "String.compareTo with null object");
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001502 }
1503 result->SetI(receiver->AsString()->CompareTo(rhs));
1504}
1505
Mathieu Chartiere401d142015-04-22 13:56:20 -07001506void UnstartedRuntime::UnstartedJNIStringIntern(
1507 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1508 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001509 result->SetL(receiver->AsString()->Intern());
1510}
1511
Mathieu Chartiere401d142015-04-22 13:56:20 -07001512void UnstartedRuntime::UnstartedJNIStringFastIndexOf(
1513 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1514 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001515 result->SetI(receiver->AsString()->FastIndexOf(args[0], args[1]));
1516}
1517
Mathieu Chartiere401d142015-04-22 13:56:20 -07001518void UnstartedRuntime::UnstartedJNIArrayCreateMultiArray(
1519 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1520 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001521 StackHandleScope<2> hs(self);
1522 auto h_class(hs.NewHandle(reinterpret_cast<mirror::Class*>(args[0])->AsClass()));
1523 auto h_dimensions(hs.NewHandle(reinterpret_cast<mirror::IntArray*>(args[1])->AsIntArray()));
1524 result->SetL(mirror::Array::CreateMultiArray(self, h_class, h_dimensions));
1525}
1526
Mathieu Chartiere401d142015-04-22 13:56:20 -07001527void UnstartedRuntime::UnstartedJNIArrayCreateObjectArray(
1528 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1529 uint32_t* args, JValue* result) {
Andreas Gampee598e042015-04-10 14:57:10 -07001530 int32_t length = static_cast<int32_t>(args[1]);
1531 if (length < 0) {
1532 ThrowNegativeArraySizeException(length);
1533 return;
1534 }
1535 mirror::Class* element_class = reinterpret_cast<mirror::Class*>(args[0])->AsClass();
1536 Runtime* runtime = Runtime::Current();
1537 ClassLinker* class_linker = runtime->GetClassLinker();
1538 mirror::Class* array_class = class_linker->FindArrayClass(self, &element_class);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001539 if (UNLIKELY(array_class == nullptr)) {
Andreas Gampee598e042015-04-10 14:57:10 -07001540 CHECK(self->IsExceptionPending());
1541 return;
1542 }
1543 DCHECK(array_class->IsObjectArrayClass());
1544 mirror::Array* new_array = mirror::ObjectArray<mirror::Object*>::Alloc(
1545 self, array_class, length, runtime->GetHeap()->GetCurrentAllocator());
1546 result->SetL(new_array);
1547}
1548
Mathieu Chartiere401d142015-04-22 13:56:20 -07001549void UnstartedRuntime::UnstartedJNIThrowableNativeFillInStackTrace(
1550 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1551 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001552 ScopedObjectAccessUnchecked soa(self);
1553 if (Runtime::Current()->IsActiveTransaction()) {
1554 result->SetL(soa.Decode<mirror::Object*>(self->CreateInternalStackTrace<true>(soa)));
1555 } else {
1556 result->SetL(soa.Decode<mirror::Object*>(self->CreateInternalStackTrace<false>(soa)));
1557 }
1558}
1559
Mathieu Chartiere401d142015-04-22 13:56:20 -07001560void UnstartedRuntime::UnstartedJNISystemIdentityHashCode(
1561 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1562 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001563 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1564 result->SetI((obj != nullptr) ? obj->IdentityHashCode() : 0);
1565}
1566
Mathieu Chartiere401d142015-04-22 13:56:20 -07001567void UnstartedRuntime::UnstartedJNIByteOrderIsLittleEndian(
1568 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1569 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001570 result->SetZ(JNI_TRUE);
1571}
1572
Mathieu Chartiere401d142015-04-22 13:56:20 -07001573void UnstartedRuntime::UnstartedJNIUnsafeCompareAndSwapInt(
1574 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1575 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001576 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1577 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1578 jint expectedValue = args[3];
1579 jint newValue = args[4];
1580 bool success;
1581 if (Runtime::Current()->IsActiveTransaction()) {
1582 success = obj->CasFieldStrongSequentiallyConsistent32<true>(MemberOffset(offset),
1583 expectedValue, newValue);
1584 } else {
1585 success = obj->CasFieldStrongSequentiallyConsistent32<false>(MemberOffset(offset),
1586 expectedValue, newValue);
1587 }
1588 result->SetZ(success ? JNI_TRUE : JNI_FALSE);
1589}
1590
Narayan Kamath34a316f2016-03-30 13:11:18 +01001591void UnstartedRuntime::UnstartedJNIUnsafeGetIntVolatile(
1592 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1593 uint32_t* args, JValue* result) {
1594 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1595 if (obj == nullptr) {
1596 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1597 return;
1598 }
1599
1600 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1601 result->SetI(obj->GetField32Volatile(MemberOffset(offset)));
1602}
1603
Mathieu Chartiere401d142015-04-22 13:56:20 -07001604void UnstartedRuntime::UnstartedJNIUnsafePutObject(
1605 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1606 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001607 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1608 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1609 mirror::Object* newValue = reinterpret_cast<mirror::Object*>(args[3]);
1610 if (Runtime::Current()->IsActiveTransaction()) {
1611 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1612 } else {
1613 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1614 }
1615}
1616
Andreas Gampe799681b2015-05-15 19:24:12 -07001617void UnstartedRuntime::UnstartedJNIUnsafeGetArrayBaseOffsetForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001618 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1619 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001620 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1621 Primitive::Type primitive_type = component->GetPrimitiveType();
1622 result->SetI(mirror::Array::DataOffset(Primitive::ComponentSize(primitive_type)).Int32Value());
1623}
1624
Andreas Gampe799681b2015-05-15 19:24:12 -07001625void UnstartedRuntime::UnstartedJNIUnsafeGetArrayIndexScaleForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001626 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1627 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001628 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1629 Primitive::Type primitive_type = component->GetPrimitiveType();
1630 result->SetI(Primitive::ComponentSize(primitive_type));
1631}
1632
Andreas Gampedd9d0552015-03-09 12:57:41 -07001633typedef void (*InvokeHandler)(Thread* self, ShadowFrame* shadow_frame, JValue* result,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001634 size_t arg_size);
1635
Mathieu Chartiere401d142015-04-22 13:56:20 -07001636typedef void (*JNIHandler)(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001637 uint32_t* args, JValue* result);
1638
1639static bool tables_initialized_ = false;
1640static std::unordered_map<std::string, InvokeHandler> invoke_handlers_;
1641static std::unordered_map<std::string, JNIHandler> jni_handlers_;
1642
Andreas Gampe799681b2015-05-15 19:24:12 -07001643void UnstartedRuntime::InitializeInvokeHandlers() {
1644#define UNSTARTED_DIRECT(ShortName, Sig) \
1645 invoke_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::Unstarted ## ShortName));
1646#include "unstarted_runtime_list.h"
1647 UNSTARTED_RUNTIME_DIRECT_LIST(UNSTARTED_DIRECT)
1648#undef UNSTARTED_RUNTIME_DIRECT_LIST
1649#undef UNSTARTED_RUNTIME_JNI_LIST
1650#undef UNSTARTED_DIRECT
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001651}
1652
Andreas Gampe799681b2015-05-15 19:24:12 -07001653void UnstartedRuntime::InitializeJNIHandlers() {
1654#define UNSTARTED_JNI(ShortName, Sig) \
1655 jni_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::UnstartedJNI ## ShortName));
1656#include "unstarted_runtime_list.h"
1657 UNSTARTED_RUNTIME_JNI_LIST(UNSTARTED_JNI)
1658#undef UNSTARTED_RUNTIME_DIRECT_LIST
1659#undef UNSTARTED_RUNTIME_JNI_LIST
1660#undef UNSTARTED_JNI
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001661}
1662
Andreas Gampe799681b2015-05-15 19:24:12 -07001663void UnstartedRuntime::Initialize() {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001664 CHECK(!tables_initialized_);
1665
Andreas Gampe799681b2015-05-15 19:24:12 -07001666 InitializeInvokeHandlers();
1667 InitializeJNIHandlers();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001668
1669 tables_initialized_ = true;
1670}
1671
Andreas Gampe799681b2015-05-15 19:24:12 -07001672void UnstartedRuntime::Invoke(Thread* self, const DexFile::CodeItem* code_item,
1673 ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001674 // In a runtime that's not started we intercept certain methods to avoid complicated dependency
1675 // problems in core libraries.
1676 CHECK(tables_initialized_);
1677
1678 std::string name(PrettyMethod(shadow_frame->GetMethod()));
1679 const auto& iter = invoke_handlers_.find(name);
1680 if (iter != invoke_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001681 // Clear out the result in case it's not zeroed out.
1682 result->SetL(0);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001683
1684 // Push the shadow frame. This is so the failing method can be seen in abort dumps.
1685 self->PushShadowFrame(shadow_frame);
1686
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001687 (*iter->second)(self, shadow_frame, result, arg_offset);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001688
1689 self->PopShadowFrame();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001690 } else {
1691 // Not special, continue with regular interpreter execution.
Andreas Gampe3cfa4d02015-10-06 17:04:01 -07001692 ArtInterpreterToInterpreterBridge(self, code_item, shadow_frame, result);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001693 }
1694}
1695
1696// 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 -07001697void UnstartedRuntime::Jni(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe799681b2015-05-15 19:24:12 -07001698 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001699 std::string name(PrettyMethod(method));
1700 const auto& iter = jni_handlers_.find(name);
1701 if (iter != jni_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001702 // Clear out the result in case it's not zeroed out.
1703 result->SetL(0);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001704 (*iter->second)(self, method, receiver, args, result);
1705 } else if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +02001706 AbortTransactionF(self, "Attempt to invoke native method in non-started runtime: %s",
1707 name.c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001708 } else {
1709 LOG(FATAL) << "Calling native method " << PrettyMethod(method) << " in an unstarted "
1710 "non-transactional runtime";
1711 }
1712}
1713
1714} // namespace interpreter
1715} // namespace art