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