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
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 size_t elemSize = arrayClass->GetComponentSize();
75 auto *array = AllocateArray(arrayClass, elemSize, length, spaceType, pinned);
76 if (array == nullptr) {
77 return nullptr;
78 }
79 // No need to memset - it is done in allocator
80 TSAN_ANNOTATE_IGNORE_WRITES_BEGIN();
81 array->SetLength(length);
82 TSAN_ANNOTATE_IGNORE_WRITES_END();
83 // Without full memory barrier it is possible that architectures with weak memory order can try fetching array
84 // length before it's set
85 arch::FullMemoryBarrier();
86 return array;
87 }
88
89 /* static */
Create(DynClass * dynarrayclass,ArraySizeT length,ark::SpaceType spaceType,bool pinned)90 Array *Array::Create(DynClass *dynarrayclass, ArraySizeT length, ark::SpaceType spaceType, bool pinned)
91 {
92 size_t elemSize = coretypes::TaggedValue::TaggedTypeSize();
93 HClass *arrayClass = dynarrayclass->GetHClass();
94 auto *array = AllocateArray(arrayClass, elemSize, length, spaceType, pinned);
95 if (array == nullptr) {
96 return nullptr;
97 }
98 // No need to memset - it is done in allocator
99 TSAN_ANNOTATE_IGNORE_WRITES_BEGIN();
100 array->SetLength(length);
101 TSAN_ANNOTATE_IGNORE_WRITES_END();
102 // Witout full memory barrier it is possible that architectures with weak memory order can try fetching array
103 // legth before it's set
104 arch::FullMemoryBarrier();
105 return array;
106 }
107
108 /* static */
CreateTagged(const PandaVM * vm,ark::BaseClass * arrayClass,ArraySizeT length,ark::SpaceType spaceType,TaggedValue initValue)109 Array *Array::CreateTagged(const PandaVM *vm, ark::BaseClass *arrayClass, ArraySizeT length, ark::SpaceType spaceType,
110 TaggedValue initValue)
111 {
112 size_t elemSize = coretypes::TaggedValue::TaggedTypeSize();
113 auto *array = AllocateArray(arrayClass, elemSize, length, spaceType, false, vm);
114 if (array == nullptr) {
115 return nullptr;
116 }
117 // Order is matters here: GC can read data before it copied if we set length first.
118 // length == 0 is guaranteed by AllocateArray
119 for (ArraySizeT i = 0; i < length; i++) {
120 array->Set<TaggedType, false, true>(i, initValue.GetRawData());
121 }
122 TSAN_ANNOTATE_IGNORE_WRITES_BEGIN();
123 array->SetLength(length);
124 TSAN_ANNOTATE_IGNORE_WRITES_END();
125 // Witout full memory barrier it is possible that architectures with weak memory order can try fetching array
126 // legth before it's set
127 arch::FullMemoryBarrier();
128 return array;
129 }
130
131 } // namespace ark::coretypes
132