1 /*
2 * Copyright (C) 2018 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 "intrinsic_objects.h"
18
19 #include "art_field-inl.h"
20 #include "base/casts.h"
21 #include "base/logging.h"
22 #include "intrinsics.h"
23 #include "oat/image.h"
24 #include "obj_ptr-inl.h"
25 #include "well_known_classes.h"
26
27 namespace art HIDDEN {
28
29 static constexpr size_t kIntrinsicObjectsOffset =
30 enum_cast<size_t>(ImageHeader::kIntrinsicObjectsStart);
31
32 template <typename T>
FillIntrinsicsObjects(ArtField * cache_field,ObjPtr<mirror::ObjectArray<mirror::Object>> live_objects,int32_t expected_low,int32_t expected_high,T && type_check,int32_t index)33 static int32_t FillIntrinsicsObjects(
34 ArtField* cache_field,
35 ObjPtr<mirror::ObjectArray<mirror::Object>> live_objects,
36 int32_t expected_low,
37 int32_t expected_high,
38 T&& type_check,
39 int32_t index)
40 REQUIRES_SHARED(Locks::mutator_lock_) {
41 ObjPtr<mirror::ObjectArray<mirror::Object>> cache =
42 ObjPtr<mirror::ObjectArray<mirror::Object>>::DownCast(
43 cache_field->GetObject(cache_field->GetDeclaringClass()));
44 int32_t length = expected_high - expected_low + 1;
45 DCHECK_EQ(length, cache->GetLength());
46 for (int32_t i = 0; i != length; ++i) {
47 ObjPtr<mirror::Object> value = cache->GetWithoutChecks(i);
48 live_objects->Set(index + i, value);
49 type_check(value, expected_low + i);
50 }
51 return index + length;
52 }
53
FillIntrinsicObjects(ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_live_objects,size_t start_index)54 void IntrinsicObjects::FillIntrinsicObjects(
55 ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_live_objects, size_t start_index) {
56 DCHECK_EQ(start_index, ImageHeader::kIntrinsicObjectsStart);
57 int32_t index = dchecked_integral_cast<int32_t>(start_index);
58 #define FILL_OBJECTS(name, low, high, type, offset) \
59 index = FillIntrinsicsObjects( \
60 WellKnownClasses::java_lang_ ##name ##_ ##name ##Cache_cache, \
61 boot_image_live_objects, \
62 low, \
63 high, \
64 [](ObjPtr<mirror::Object> obj, int32_t expected) REQUIRES_SHARED(Locks::mutator_lock_) { \
65 CHECK_EQ(expected, WellKnownClasses::java_lang_ ##name ##_value->Get ##name(obj)); \
66 }, \
67 index);
68 BOXED_TYPES(FILL_OBJECTS)
69 #undef FILL_OBJECTS
70 DCHECK_EQ(dchecked_integral_cast<size_t>(index), start_index + GetNumberOfIntrinsicObjects());
71 }
72
HasIntrinsicObjects(ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_live_objects)73 static bool HasIntrinsicObjects(
74 ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_live_objects)
75 REQUIRES_SHARED(Locks::mutator_lock_) {
76 DCHECK(boot_image_live_objects != nullptr);
77 uint32_t length = static_cast<uint32_t>(boot_image_live_objects->GetLength());
78 DCHECK_GE(length, kIntrinsicObjectsOffset);
79 return length != kIntrinsicObjectsOffset;
80 }
81
GetValueOfObject(ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_live_objects,size_t start_index,uint32_t index)82 ObjPtr<mirror::Object> IntrinsicObjects::GetValueOfObject(
83 ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_live_objects,
84 size_t start_index,
85 uint32_t index) {
86 DCHECK(HasIntrinsicObjects(boot_image_live_objects));
87 // No need for read barrier for boot image object or for verifying the value that was just stored.
88 ObjPtr<mirror::Object> result =
89 boot_image_live_objects->GetWithoutChecks<kVerifyNone, kWithoutReadBarrier>(
90 kIntrinsicObjectsOffset + start_index + index);
91 DCHECK(result != nullptr);
92 return result;
93 }
94
GetValueOfArrayDataOffset(ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_live_objects,size_t start_index)95 MemberOffset IntrinsicObjects::GetValueOfArrayDataOffset(
96 ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_live_objects,
97 size_t start_index) {
98 DCHECK(HasIntrinsicObjects(boot_image_live_objects));
99 MemberOffset result =
100 mirror::ObjectArray<mirror::Object>::OffsetOfElement(kIntrinsicObjectsOffset + start_index);
101 DCHECK_EQ(GetValueOfObject(boot_image_live_objects, start_index, 0u),
102 (boot_image_live_objects
103 ->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier>(result)));
104 return result;
105 }
106
107 } // namespace art
108