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 <fuzzer/FuzzedDataProvider.h>
17 #include "jsvaluerefislightweightmapandset_fuzzer.h"
18 #include "ecmascript/containers/containers_list.h"
19 #include "ecmascript/containers/containers_private.h"
20 #include "ecmascript/ecma_string-inl.h"
21 #include "ecmascript/ecma_vm.h"
22 #include "ecmascript/global_env.h"
23 #include "ecmascript/js_handle.h"
24 #include "ecmascript/js_tagged_value.h"
25 #include "ecmascript/napi/include/jsnapi.h"
26 #include "ecmascript/js_thread.h"
27 #include "ecmascript/js_global_object.h"
28 #include "ecmascript/napi/jsnapi_helper.h"
29 #include "ecmascript/linked_hash_table.h"
30 #include "ecmascript/ecma_runtime_call_info.h"
31 #include "ecmascript/containers/containers_hashset.h"
32 #include "ecmascript/common.h"
33 #include "ecmascript/frames.h"
34 #include "ecmascript/object_factory.h"
35 #include "ecmascript/js_set.h"
36 #include "ecmascript/js_set_iterator.h"
37 #include "ecmascript/js_map.h"
38 #include "ecmascript/js_weak_container.h"
39 #include "ecmascript/js_map_iterator.h"
40 #include "ecmascript/containers/containers_arraylist.h"
41 #include "ecmascript/js_api/js_api_arraylist.h"
42 #include "ecmascript/builtins/builtins_function.h"
43 #include "ecmascript/builtins/builtins.h"
44 #include "ecmascript/ecma_global_storage.h"
45 #include "ecmascript/js_bigint.h"
46 #include "ecmascript/js_runtime_options.h"
47 #include "ecmascript/object_factory.h"
48 #include "ecmascript/tagged_array.h"
49 #include "ecmascript/js_generator_object.h"
50 #include "ecmascript/js_string_iterator.h"
51 #include "ecmascript/js_date_time_format.h"
52 #include "ecmascript/js_tagged_number.h"
53 #include "ecmascript/js_api/js_api_hashmap.h"
54 #include "ecmascript/builtins/builtins_regexp.h"
55 #include "ecmascript/js_regexp.h"
56 #include "ecmascript/tagged_hash_array.h"
57 #include "ecmascript/containers/containers_lightweightmap.h"
58 #include "ecmascript/containers/containers_lightweightset.h"
59
60 using namespace panda;
61 using namespace panda::test;
62 using namespace panda::ecmascript;
63 using namespace panda::ecmascript::containers;
64
65 namespace OHOS {
CreateEcmaRuntimeCallInfo(JSThread * thread,JSTaggedValue newTgt,uint32_t argvLength)66 EcmaRuntimeCallInfo *CreateEcmaRuntimeCallInfo(JSThread *thread, JSTaggedValue newTgt, uint32_t argvLength)
67 {
68 const uint8_t testDecodedSize = 2;
69 int32_t numActualArgs = argvLength / testDecodedSize + 1;
70 JSTaggedType *sp = const_cast<JSTaggedType *>(thread->GetCurrentSPFrame());
71
72 size_t frameSize = 0;
73 if (thread->IsAsmInterpreter()) {
74 frameSize = InterpretedEntryFrame::NumOfMembers() + numActualArgs;
75 } else {
76 frameSize = InterpretedFrame::NumOfMembers() + numActualArgs;
77 }
78 JSTaggedType *newSp = sp - frameSize; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
79 for (int i = numActualArgs; i > 0; i--) {
80 newSp[i - 1] = JSTaggedValue::Undefined().GetRawData();
81 }
82 EcmaRuntimeCallInfo *ecmaRuntimeCallInfo = reinterpret_cast<EcmaRuntimeCallInfo *>(newSp - 2);
83 *(--newSp) = numActualArgs;
84 *(--newSp) = common::ToUintPtr(thread);
85 ecmaRuntimeCallInfo->SetNewTarget(newTgt);
86 return ecmaRuntimeCallInfo;
87 }
88
SetupFrame(JSThread * thread,EcmaRuntimeCallInfo * info)89 static JSTaggedType *SetupFrame(JSThread *thread, EcmaRuntimeCallInfo *info)
90 {
91 JSTaggedType *sp = const_cast<JSTaggedType *>(thread->GetCurrentSPFrame());
92 size_t frameSize = 0;
93 const int num = 2;
94 // 2 means thread and numArgs
95 if (thread->IsAsmInterpreter()) {
96 frameSize = InterpretedEntryFrame::NumOfMembers() + info->GetArgsNumber() + NUM_MANDATORY_JSFUNC_ARGS + num;
97 } else {
98 frameSize = InterpretedFrame::NumOfMembers() + info->GetArgsNumber() + NUM_MANDATORY_JSFUNC_ARGS + num;
99 }
100 JSTaggedType *newSp = sp - frameSize; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
101
102 InterpretedEntryFrame *state = reinterpret_cast<InterpretedEntryFrame *>(newSp) - 1;
103 state->base.type = ecmascript::FrameType::INTERPRETER_ENTRY_FRAME;
104 state->base.prev = sp;
105 state->pc = nullptr;
106 thread->SetCurrentSPFrame(newSp);
107 return sp;
108 }
109
TearDownFrame(JSThread * thread,JSTaggedType * prev)110 void TearDownFrame(JSThread *thread, JSTaggedType *prev)
111 {
112 thread->SetCurrentSPFrame(prev);
113 }
114
ConstructobjectLightWeightMap(JSThread * thread)115 JSHandle<JSAPILightWeightMap> ConstructobjectLightWeightMap(JSThread *thread)
116 {
117 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
118 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
119 JSHandle<JSTaggedValue> globalObject = env->GetJSGlobalObject();
120 JSHandle<JSTaggedValue> key(factory->NewFromASCII("ArkPrivate"));
121 JSHandle<JSTaggedValue> value =
122 JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(globalObject), key).GetValue();
123 auto objCallInfo1 = CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
124 objCallInfo1->SetFunction(JSTaggedValue::Undefined());
125 objCallInfo1->SetThis(value.GetTaggedValue());
126 objCallInfo1->SetCallArg(0, JSTaggedValue(static_cast<int>(containers::ContainerTag::LightWeightMap)));
127 [[maybe_unused]] auto prev1 = SetupFrame(thread, objCallInfo1);
128 JSTaggedValue result1 = ecmascript::containers::ContainersPrivate::Load(objCallInfo1);
129 JSHandle<JSFunction> newTarget(thread, result1);
130 auto objCallInfo = CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
131 objCallInfo->SetFunction(newTarget.GetTaggedValue());
132 objCallInfo->SetNewTarget(newTarget.GetTaggedValue());
133 objCallInfo->SetThis(JSTaggedValue::Undefined());
134 [[maybe_unused]] auto prev = SetupFrame(thread, objCallInfo);
135 JSTaggedValue result = ecmascript::containers::ContainersLightWeightMap::LightWeightMapConstructor(objCallInfo);
136 TearDownFrame(thread, prev);
137 JSHandle<JSAPILightWeightMap> mapHandle(thread, result);
138 return mapHandle;
139 }
140
ConstructobjectLightWeightSet(JSThread * thread)141 JSHandle<JSAPILightWeightSet> ConstructobjectLightWeightSet(JSThread *thread)
142 {
143 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
144 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
145 JSHandle<JSTaggedValue> globalObject = env->GetJSGlobalObject();
146 JSHandle<JSTaggedValue> key(factory->NewFromASCII("ArkPrivate"));
147 JSHandle<JSTaggedValue> value =
148 JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(globalObject), key).GetValue();
149 auto objCallInfo1 = CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
150 objCallInfo1->SetFunction(JSTaggedValue::Undefined());
151 objCallInfo1->SetThis(value.GetTaggedValue());
152 objCallInfo1->SetCallArg(0, JSTaggedValue(static_cast<int>(containers::ContainerTag::LightWeightSet)));
153 [[maybe_unused]] auto prev1 = SetupFrame(thread, objCallInfo1);
154 JSTaggedValue result1 = ecmascript::containers::ContainersPrivate::Load(objCallInfo1);
155 JSHandle<JSFunction> newTarget(thread, result1);
156 auto objCallInfo = CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
157 objCallInfo->SetFunction(newTarget.GetTaggedValue());
158 objCallInfo->SetNewTarget(newTarget.GetTaggedValue());
159 objCallInfo->SetThis(JSTaggedValue::Undefined());
160 [[maybe_unused]] auto prev = SetupFrame(thread, objCallInfo);
161 JSTaggedValue result = ecmascript::containers::ContainersLightWeightSet::LightWeightSetConstructor(objCallInfo);
162 TearDownFrame(thread, prev);
163 JSHandle<JSAPILightWeightSet> mapHandle(thread, result);
164 return mapHandle;
165 }
166
JSValueRefIsLightWeightMapFuzzTest(const uint8_t * data,size_t size)167 void JSValueRefIsLightWeightMapFuzzTest(const uint8_t *data, size_t size)
168 {
169 FuzzedDataProvider fdp(data, size);
170 const int arkProp = fdp.ConsumeIntegral<int>();
171 RuntimeOption option;
172 option.SetLogLevel(common::LOG_LEVEL::ERROR);
173 option.SetArkProperties(arkProp);
174 EcmaVM *vm = JSNApi::CreateJSVM(option);
175 {
176 JsiFastNativeScope scope(vm);
177 auto thread = vm->GetAssociatedJSThread();
178 JSHandle<JSAPILightWeightMap> mapHandle = ConstructobjectLightWeightMap(thread);
179 JSHandle<JSTaggedValue> jshashmap = JSHandle<JSTaggedValue>::Cast(mapHandle);
180 Local<JSValueRef> tag = JSNApiHelper::ToLocal<JSValueRef>(jshashmap);
181 tag->IsLightWeightMap(vm);
182 }
183 JSNApi::DestroyJSVM(vm);
184 }
185
JSValueRefIsLightWeightSetFuzzTest(const uint8_t * data,size_t size)186 void JSValueRefIsLightWeightSetFuzzTest(const uint8_t *data, size_t size)
187 {
188 FuzzedDataProvider fdp(data, size);
189 const int arkProp = fdp.ConsumeIntegral<int>();
190 RuntimeOption option;
191 option.SetLogLevel(common::LOG_LEVEL::ERROR);
192 option.SetArkProperties(arkProp);
193 EcmaVM *vm = JSNApi::CreateJSVM(option);
194 {
195 JsiFastNativeScope scope(vm);
196 auto thread = vm->GetAssociatedJSThread();
197 JSHandle<JSAPILightWeightSet> mapHandle = ConstructobjectLightWeightSet(thread);
198 JSHandle<JSTaggedValue> jshashmap = JSHandle<JSTaggedValue>::Cast(mapHandle);
199 Local<JSValueRef> tag = JSNApiHelper::ToLocal<JSValueRef>(jshashmap);
200 tag->IsLightWeightSet(vm);
201 }
202 JSNApi::DestroyJSVM(vm);
203 }
204 }
205
206 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)207 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
208 {
209 // Run your code on data.
210 OHOS::JSValueRefIsLightWeightMapFuzzTest(data, size);
211 OHOS::JSValueRefIsLightWeightSetFuzzTest(data, size);
212 return 0;
213 }
214