1 /*
2 * Copyright (c) 2023 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 "jsvaluerefislinkedlistiterator_fuzzer.h"
17 #include "ecmascript/containers/containers_list.h"
18 #include "ecmascript/containers/containers_private.h"
19 #include "ecmascript/ecma_string-inl.h"
20 #include "ecmascript/ecma_vm.h"
21 #include "ecmascript/global_env.h"
22 #include "ecmascript/js_handle.h"
23 #include "ecmascript/js_tagged_value.h"
24 #include "ecmascript/napi/include/jsnapi.h"
25 #include "ecmascript/js_thread.h"
26 #include "ecmascript/js_global_object.h"
27 #include "ecmascript/napi/jsnapi_helper.h"
28 #include "ecmascript/linked_hash_table.h"
29 #include "ecmascript/ecma_runtime_call_info.h"
30 #include "ecmascript/containers/containers_hashset.h"
31 #include "ecmascript/common.h"
32 #include "ecmascript/frames.h"
33 #include "ecmascript/tagged_list.h"
34 #include "ecmascript/linked_hash_table.h"
35 #include "ecmascript/js_api/js_api_list.h"
36 #include "ecmascript/js_api/js_api_linked_list.h"
37 #include "ecmascript/js_api/js_api_linked_list_iterator.h"
38 #include "ecmascript/object_factory.h"
39 #include "ecmascript/js_set.h"
40 #include "ecmascript/js_set_iterator.h"
41 #include "ecmascript/js_map.h"
42 #include "ecmascript/js_weak_container.h"
43 #include "ecmascript/js_map_iterator.h"
44 #include "ecmascript/containers/containers_arraylist.h"
45 #include "ecmascript/js_api/js_api_arraylist.h"
46 #include "ecmascript/builtins/builtins_function.h"
47 #include "ecmascript/builtins/builtins.h"
48 #include "ecmascript/ecma_global_storage.h"
49 #include "ecmascript/js_bigint.h"
50 #include "ecmascript/js_runtime_options.h"
51 #include "ecmascript/object_factory.h"
52 #include "ecmascript/tagged_array.h"
53 #include "ecmascript/js_generator_object.h"
54 #include "ecmascript/js_string_iterator.h"
55 #include "ecmascript/js_date_time_format.h"
56 #include "ecmascript/js_tagged_number.h"
57 #include "ecmascript/js_api/js_api_hashmap.h"
58 #include "ecmascript/builtins/builtins_regexp.h"
59 #include "ecmascript/js_regexp.h"
60 #include "ecmascript/tagged_hash_array.h"
61 #include "ecmascript/containers/containers_lightweightmap.h"
62 #include "ecmascript/containers/containers_lightweightset.h"
63 #include "ecmascript/js_api/js_api_plain_array.h"
64
65 using namespace panda;
66 using namespace panda::test;
67 using namespace panda::ecmascript;
68 using namespace panda::ecmascript::containers;
69
70 namespace OHOS {
CreateEcmaRuntimeCallInfo(JSThread * thread,JSTaggedValue newTgt,uint32_t argvLength)71 EcmaRuntimeCallInfo *CreateEcmaRuntimeCallInfo(JSThread *thread, JSTaggedValue newTgt, uint32_t argvLength)
72 {
73 const uint8_t testDecodedSize = 2;
74 int32_t numActualArgs = argvLength / testDecodedSize + 1;
75 JSTaggedType *sp = const_cast<JSTaggedType *>(thread->GetCurrentSPFrame());
76
77 size_t frameSize = 0;
78 if (thread->IsAsmInterpreter()) {
79 frameSize = InterpretedEntryFrame::NumOfMembers() + numActualArgs;
80 } else {
81 frameSize = InterpretedFrame::NumOfMembers() + numActualArgs;
82 }
83 JSTaggedType *newSp = sp - frameSize; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
84 for (int i = numActualArgs; i > 0; i--) {
85 newSp[i - 1] = JSTaggedValue::Undefined().GetRawData();
86 }
87 EcmaRuntimeCallInfo *ecmaRuntimeCallInfo = reinterpret_cast<EcmaRuntimeCallInfo *>(newSp - 2);
88 *(--newSp) = numActualArgs;
89 *(--newSp) = panda::ecmascript::ToUintPtr(thread);
90 ecmaRuntimeCallInfo->SetNewTarget(newTgt);
91 return ecmaRuntimeCallInfo;
92 }
93
SetupFrame(JSThread * thread,EcmaRuntimeCallInfo * info)94 static JSTaggedType *SetupFrame(JSThread *thread, EcmaRuntimeCallInfo *info)
95 {
96 JSTaggedType *sp = const_cast<JSTaggedType *>(thread->GetCurrentSPFrame());
97 size_t frameSize = 0;
98 const int num = 2;
99 // 2 means thread and numArgs
100 if (thread->IsAsmInterpreter()) {
101 frameSize = InterpretedEntryFrame::NumOfMembers() + info->GetArgsNumber() + NUM_MANDATORY_JSFUNC_ARGS + num;
102 } else {
103 frameSize = InterpretedFrame::NumOfMembers() + info->GetArgsNumber() + NUM_MANDATORY_JSFUNC_ARGS + num;
104 }
105 JSTaggedType *newSp = sp - frameSize; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
106
107 InterpretedEntryFrame *state = reinterpret_cast<InterpretedEntryFrame *>(newSp) - 1;
108 state->base.type = ecmascript::FrameType::INTERPRETER_ENTRY_FRAME;
109 state->base.prev = sp;
110 state->pc = nullptr;
111 thread->SetCurrentSPFrame(newSp);
112 return sp;
113 }
114
TearDownFrame(JSThread * thread,JSTaggedType * prev)115 void TearDownFrame(JSThread *thread, JSTaggedType *prev)
116 {
117 thread->SetCurrentSPFrame(prev);
118 }
119
JSValueRefIsLinkedListIteratorFuzzTest(const uint8_t * data,size_t size)120 void JSValueRefIsLinkedListIteratorFuzzTest([[maybe_unused]] const uint8_t *data, size_t size)
121 {
122 RuntimeOption option;
123 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
124 EcmaVM *vm = JSNApi::CreateJSVM(option);
125 {
126 JsiFastNativeScope scope(vm);
127 if (size <= 0) {
128 return;
129 }
130 auto thread = vm->GetAssociatedJSThread();
131 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
132 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
133 JSHandle<JSTaggedValue> globalObject = env->GetJSGlobalObject();
134 JSHandle<JSTaggedValue> key(factory->NewFromASCII("ArkPrivate"));
135 JSHandle<JSTaggedValue> tagvalue =
136 JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(globalObject), key).GetValue();
137 auto objCallInfo = CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
138 objCallInfo->SetFunction(JSTaggedValue::Undefined());
139 objCallInfo->SetThis(tagvalue.GetTaggedValue());
140 objCallInfo->SetCallArg(0, JSTaggedValue(static_cast<int>(containers::ContainerTag::LinkedList)));
141 [[maybe_unused]] auto prev = SetupFrame(thread, objCallInfo);
142 JSHandle<JSTaggedValue> contianer =
143 JSHandle<JSTaggedValue>(thread, containers::ContainersPrivate::Load(objCallInfo));
144 auto obj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(contianer), contianer);
145 JSHandle<JSAPILinkedList> linkedList = JSHandle<JSAPILinkedList>::Cast(obj);
146 JSTaggedValue doubleList = TaggedDoubleList::Create(thread);
147 linkedList->SetDoubleList(thread, doubleList);
148 uint32_t elementsNum = 256;
149 for (uint32_t i = 0; i < elementsNum; i++) {
150 JSHandle<JSTaggedValue> taggedvalue(thread, JSTaggedValue(i));
151 JSAPILinkedList::Add(thread, linkedList, taggedvalue);
152 }
153 JSHandle<JSTaggedValue> taggedValueHandle(thread, linkedList.GetTaggedValue());
154 JSHandle<JSAPILinkedListIterator> linkedListIterator(thread,
155 JSAPILinkedListIterator::CreateLinkedListIterator(thread, taggedValueHandle).GetTaggedValue());
156 JSHandle<JSTaggedValue> linkedListIteratortag = JSHandle<JSTaggedValue>::Cast(linkedListIterator);
157 Local<JSValueRef> object = JSNApiHelper::ToLocal<JSValueRef>(linkedListIteratortag);
158 object->IsLinkedListIterator(vm);
159 }
160 JSNApi::DestroyJSVM(vm);
161 }
162 }
163
164 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)165 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
166 {
167 // Run your code on data.
168 OHOS::JSValueRefIsLinkedListIteratorFuzzTest(data, size);
169 return 0;
170 }