1 /**
2 * Copyright (c) 2021-2025 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
27 // CC-OFFNXT(G.FUN.01) solid logic
AllocateArray(ark::BaseClass * arrayClass,size_t elemSize,ArraySizeT length,ark::SpaceType spaceType,bool pinned=false,const PandaVM * vm=Thread::GetCurrent ()->GetVM ())28 static Array *AllocateArray(ark::BaseClass *arrayClass, size_t elemSize, ArraySizeT length, ark::SpaceType spaceType,
29 bool pinned = false, const PandaVM *vm = Thread::GetCurrent()->GetVM())
30 {
31 size_t size = Array::ComputeSize(elemSize, length);
32 if (UNLIKELY(size == 0)) {
33 LOG(ERROR, RUNTIME) << "Illegal array size: element size: " << elemSize << " array length: " << length;
34 ThrowOutOfMemoryError("OOM when allocating array");
35 return nullptr;
36 }
37 if (LIKELY(spaceType == ark::SpaceType::SPACE_TYPE_OBJECT)) {
38 return static_cast<coretypes::Array *>(
39 vm->GetHeapManager()->AllocateObject(arrayClass, size, DEFAULT_ALIGNMENT, ManagedThread::GetCurrent(),
40 mem::ObjectAllocatorBase::ObjMemInitPolicy::REQUIRE_INIT, pinned));
41 }
42 if (spaceType == ark::SpaceType::SPACE_TYPE_NON_MOVABLE_OBJECT) {
43 return static_cast<coretypes::Array *>(vm->GetHeapManager()->AllocateNonMovableObject(
44 arrayClass, size, DEFAULT_ALIGNMENT, ManagedThread::GetCurrent()));
45 }
46 UNREACHABLE();
47 }
48
49 /* static */
Create(ark::Class * arrayClass,const uint8_t * data,ArraySizeT length,ark::SpaceType spaceType,bool pinned)50 Array *Array::Create(ark::Class *arrayClass, const uint8_t *data, ArraySizeT length, ark::SpaceType spaceType,
51 bool pinned)
52 {
53 size_t elemSize = arrayClass->GetComponentSize();
54 auto *array = AllocateArray(arrayClass, elemSize, length, spaceType, pinned);
55 if (UNLIKELY(array == nullptr)) {
56 LOG(ERROR, RUNTIME) << "Failed to allocate array.";
57 return nullptr;
58 }
59 // Order is matters here: GC can read data before it copied if we set length first.
60 // length == 0 is guaranteed by AllocateArray
61 TSAN_ANNOTATE_IGNORE_WRITES_BEGIN();
62 array->SetLength(length);
63 memcpy_s(array->GetData(), array->GetLength() * elemSize, data, length * elemSize);
64 TSAN_ANNOTATE_IGNORE_WRITES_END();
65 // Witout full memory barrier it is possible that architectures with weak memory order can try fetching array
66 // legth before it's set
67 arch::FullMemoryBarrier();
68 return array;
69 }
70
71 /* static */
Create(ark::Class * arrayClass,ArraySizeT length,ark::SpaceType spaceType,bool pinned)72 Array *Array::Create(ark::Class *arrayClass, ArraySizeT length, ark::SpaceType spaceType, bool pinned)
73 {
74 ASSERT(arrayClass != nullptr);
75 size_t elemSize = arrayClass->GetComponentSize();
76 auto *array = AllocateArray(arrayClass, elemSize, length, spaceType, pinned);
77 if (array == nullptr) {
78 return nullptr;
79 }
80 // No need to memset - it is done in allocator
81 TSAN_ANNOTATE_IGNORE_WRITES_BEGIN();
82 array->SetLength(length);
83 TSAN_ANNOTATE_IGNORE_WRITES_END();
84 // Without full memory barrier it is possible that architectures with weak memory order can try fetching array
85 // length before it's set
86 arch::FullMemoryBarrier();
87 return array;
88 }
89
90 /* static */
Create(DynClass * dynarrayclass,ArraySizeT length,ark::SpaceType spaceType,bool pinned)91 Array *Array::Create(DynClass *dynarrayclass, ArraySizeT length, ark::SpaceType spaceType, bool pinned)
92 {
93 size_t elemSize = coretypes::TaggedValue::TaggedTypeSize();
94 HClass *arrayClass = dynarrayclass->GetHClass();
95 auto *array = AllocateArray(arrayClass, elemSize, length, spaceType, pinned);
96 if (array == nullptr) {
97 return nullptr;
98 }
99 // No need to memset - it is done in allocator
100 TSAN_ANNOTATE_IGNORE_WRITES_BEGIN();
101 array->SetLength(length);
102 TSAN_ANNOTATE_IGNORE_WRITES_END();
103 // Witout full memory barrier it is possible that architectures with weak memory order can try fetching array
104 // legth before it's set
105 arch::FullMemoryBarrier();
106 return array;
107 }
108
109 /* static */
CreateTagged(const PandaVM * vm,ark::BaseClass * arrayClass,ArraySizeT length,ark::SpaceType spaceType,TaggedValue initValue)110 Array *Array::CreateTagged(const PandaVM *vm, ark::BaseClass *arrayClass, ArraySizeT length, ark::SpaceType spaceType,
111 TaggedValue initValue)
112 {
113 size_t elemSize = coretypes::TaggedValue::TaggedTypeSize();
114 auto *array = AllocateArray(arrayClass, elemSize, length, spaceType, false, vm);
115 if (array == nullptr) {
116 return nullptr;
117 }
118 // Order is matters here: GC can read data before it copied if we set length first.
119 // length == 0 is guaranteed by AllocateArray
120 for (ArraySizeT i = 0; i < length; i++) {
121 array->Set<TaggedType, false, true>(i, initValue.GetRawData());
122 }
123 TSAN_ANNOTATE_IGNORE_WRITES_BEGIN();
124 array->SetLength(length);
125 TSAN_ANNOTATE_IGNORE_WRITES_END();
126 // Witout full memory barrier it is possible that architectures with weak memory order can try fetching array
127 // legth before it's set
128 arch::FullMemoryBarrier();
129 return array;
130 }
131
132 } // namespace ark::coretypes
133