1 /**
2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "runtime/include/coretypes/array.h"
17
18 #include "runtime/arch/memory_helpers.h"
19 #include "runtime/include/class-inl.h"
20 #include "runtime/include/class_linker.h"
21 #include "runtime/include/coretypes/dyn_objects.h"
22 #include "runtime/include/runtime.h"
23 #include "runtime/include/panda_vm.h"
24
25 namespace ark::coretypes {
26
AllocateArray(ark::BaseClass * arrayClass,size_t elemSize,ArraySizeT length,ark::SpaceType spaceType,bool pinned=false,const PandaVM * vm=Thread::GetCurrent ()->GetVM ())27 static Array *AllocateArray(ark::BaseClass *arrayClass, size_t elemSize, ArraySizeT length, ark::SpaceType spaceType,
28 bool pinned = false, const PandaVM *vm = Thread::GetCurrent()->GetVM())
29 {
30 size_t size = Array::ComputeSize(elemSize, length);
31 if (UNLIKELY(size == 0)) {
32 LOG(ERROR, RUNTIME) << "Illegal array size: element size: " << elemSize << " array length: " << length;
33 ThrowOutOfMemoryError("OOM when allocating array");
34 return nullptr;
35 }
36 if (LIKELY(spaceType == ark::SpaceType::SPACE_TYPE_OBJECT)) {
37 return static_cast<coretypes::Array *>(
38 vm->GetHeapManager()->AllocateObject(arrayClass, size, DEFAULT_ALIGNMENT, ManagedThread::GetCurrent(),
39 mem::ObjectAllocatorBase::ObjMemInitPolicy::REQUIRE_INIT, pinned));
40 }
41 if (spaceType == ark::SpaceType::SPACE_TYPE_NON_MOVABLE_OBJECT) {
42 return static_cast<coretypes::Array *>(vm->GetHeapManager()->AllocateNonMovableObject(
43 arrayClass, size, DEFAULT_ALIGNMENT, ManagedThread::GetCurrent()));
44 }
45 UNREACHABLE();
46 }
47
48 /* static */
Create(ark::Class * arrayClass,const uint8_t * data,ArraySizeT length,ark::SpaceType spaceType,bool pinned)49 Array *Array::Create(ark::Class *arrayClass, const uint8_t *data, ArraySizeT length, ark::SpaceType spaceType,
50 bool pinned)
51 {
52 size_t elemSize = arrayClass->GetComponentSize();
53 auto *array = AllocateArray(arrayClass, elemSize, length, spaceType, pinned);
54 if (UNLIKELY(array == nullptr)) {
55 LOG(ERROR, RUNTIME) << "Failed to allocate array.";
56 return nullptr;
57 }
58 // Order is matters here: GC can read data before it copied if we set length first.
59 // length == 0 is guaranteed by AllocateArray
60 TSAN_ANNOTATE_IGNORE_WRITES_BEGIN();
61 array->SetLength(length);
62 memcpy_s(array->GetData(), array->GetLength() * elemSize, data, length * elemSize);
63 TSAN_ANNOTATE_IGNORE_WRITES_END();
64 // Witout full memory barrier it is possible that architectures with weak memory order can try fetching array
65 // legth before it's set
66 arch::FullMemoryBarrier();
67 return array;
68 }
69
70 /* static */
Create(ark::Class * arrayClass,ArraySizeT length,ark::SpaceType spaceType,bool pinned)71 Array *Array::Create(ark::Class *arrayClass, ArraySizeT length, ark::SpaceType spaceType, bool pinned)
72 {
73 size_t elemSize = arrayClass->GetComponentSize();
74 auto *array = AllocateArray(arrayClass, elemSize, length, spaceType, pinned);
75 if (array == nullptr) {
76 return nullptr;
77 }
78 // No need to memset - it is done in allocator
79 TSAN_ANNOTATE_IGNORE_WRITES_BEGIN();
80 array->SetLength(length);
81 TSAN_ANNOTATE_IGNORE_WRITES_END();
82 // Without full memory barrier it is possible that architectures with weak memory order can try fetching array
83 // length before it's set
84 arch::FullMemoryBarrier();
85 return array;
86 }
87
88 /* static */
Create(DynClass * dynarrayclass,ArraySizeT length,ark::SpaceType spaceType,bool pinned)89 Array *Array::Create(DynClass *dynarrayclass, ArraySizeT length, ark::SpaceType spaceType, bool pinned)
90 {
91 size_t elemSize = coretypes::TaggedValue::TaggedTypeSize();
92 HClass *arrayClass = dynarrayclass->GetHClass();
93 auto *array = AllocateArray(arrayClass, elemSize, length, spaceType, pinned);
94 if (array == nullptr) {
95 return nullptr;
96 }
97 // No need to memset - it is done in allocator
98 TSAN_ANNOTATE_IGNORE_WRITES_BEGIN();
99 array->SetLength(length);
100 TSAN_ANNOTATE_IGNORE_WRITES_END();
101 // Witout full memory barrier it is possible that architectures with weak memory order can try fetching array
102 // legth before it's set
103 arch::FullMemoryBarrier();
104 return array;
105 }
106
107 /* static */
CreateTagged(const PandaVM * vm,ark::BaseClass * arrayClass,ArraySizeT length,ark::SpaceType spaceType,TaggedValue initValue)108 Array *Array::CreateTagged(const PandaVM *vm, ark::BaseClass *arrayClass, ArraySizeT length, ark::SpaceType spaceType,
109 TaggedValue initValue)
110 {
111 size_t elemSize = coretypes::TaggedValue::TaggedTypeSize();
112 auto *array = AllocateArray(arrayClass, elemSize, length, spaceType, false, vm);
113 if (array == nullptr) {
114 return nullptr;
115 }
116 // Order is matters here: GC can read data before it copied if we set length first.
117 // length == 0 is guaranteed by AllocateArray
118 for (ArraySizeT i = 0; i < length; i++) {
119 array->Set<TaggedType, false, true>(i, initValue.GetRawData());
120 }
121 TSAN_ANNOTATE_IGNORE_WRITES_BEGIN();
122 array->SetLength(length);
123 TSAN_ANNOTATE_IGNORE_WRITES_END();
124 // Witout full memory barrier it is possible that architectures with weak memory order can try fetching array
125 // legth before it's set
126 arch::FullMemoryBarrier();
127 return array;
128 }
129
130 } // namespace ark::coretypes
131