1 /*
2 * Copyright (c) 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 "containerstreesetforeach_fuzzer.h"
17
18 #include "ecmascript/containers/containers_private.h"
19 #include "ecmascript/containers/containers_treeset.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/napi/include/jsnapi.h"
25 #include "ecmascript/object_factory.h"
26
27 using namespace panda;
28 using namespace panda::ecmascript;
29 using namespace panda::ecmascript::containers;
30
31 namespace OHOS {
32
JSObjectCreate(JSThread *thread)33 JSFunction *JSObjectCreate(JSThread *thread)
34 {
35 EcmaVM *ecmaVM = thread->GetEcmaVM();
36 JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
37 return globalEnv->GetObjectFunction().GetObject<JSFunction>();
38 }
39
CreateEcmaRuntimeCallInfo(JSThread *thread, uint32_t numArgs)40 EcmaRuntimeCallInfo *CreateEcmaRuntimeCallInfo(JSThread *thread, uint32_t numArgs)
41 {
42 auto factory = thread->GetEcmaVM()->GetFactory();
43 JSHandle<JSTaggedValue> hclass(thread, JSObjectCreate(thread));
44 JSHandle<JSTaggedValue> callee(factory->NewJSObjectByConstructor(JSHandle<JSFunction>::Cast(hclass), hclass));
45 JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined();
46 EcmaRuntimeCallInfo *objCallInfo =
47 EcmaInterpreter::NewRuntimeCallInfo(thread, undefined, callee, undefined, numArgs);
48 return objCallInfo;
49 }
50
InitializeTreeSetConstructor(JSThread *thread)51 JSTaggedValue InitializeTreeSetConstructor(JSThread *thread)
52 {
53 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
54 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
55
56 JSHandle<JSTaggedValue> globalObject = env->GetJSGlobalObject();
57 JSHandle<JSTaggedValue> key(factory->NewFromASCII("ArkPrivate"));
58 JSHandle<JSTaggedValue> value =
59 JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(globalObject), key).GetValue();
60
61 auto objCallInfo = CreateEcmaRuntimeCallInfo(thread, 6);
62 objCallInfo->SetFunction(JSTaggedValue::Undefined());
63 objCallInfo->SetThis(value.GetTaggedValue());
64 objCallInfo->SetCallArg(0, JSTaggedValue(static_cast<int>(ContainerTag::TreeSet)));
65 JSTaggedValue result = ContainersPrivate::Load(objCallInfo);
66 return result;
67 }
68
CreateJSAPITreeSet(JSThread *thread)69 JSHandle<JSAPITreeSet> CreateJSAPITreeSet(JSThread *thread)
70 {
71 JSHandle<JSFunction> newTarget(thread, InitializeTreeSetConstructor(thread));
72 auto objCallInfo = CreateEcmaRuntimeCallInfo(thread, 6);
73 objCallInfo->SetFunction(newTarget.GetTaggedValue());
74 objCallInfo->SetNewTarget(newTarget.GetTaggedValue());
75 objCallInfo->SetThis(JSTaggedValue::Undefined());
76
77 JSTaggedValue result = ContainersTreeSet::TreeSetConstructor(objCallInfo);
78 JSHandle<JSAPITreeSet> set(thread, result);
79 return set;
80 }
81
TestForEachFunc([[maybe_unused]] EcmaRuntimeCallInfo *argv)82 JSTaggedValue TestForEachFunc([[maybe_unused]] EcmaRuntimeCallInfo *argv)
83 {
84 return JSTaggedValue::True();
85 }
86
ContainersTreeSetForEachFuzzTest(const uint8_t* data, size_t size)87 void ContainersTreeSetForEachFuzzTest(const uint8_t* data, size_t size)
88 {
89 RuntimeOption option;
90 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
91 EcmaVM *vm = JSNApi::CreateJSVM(option);
92 {
93 JsiFastNativeScope scope(vm);
94 JSThread *thread = vm->GetJSThread();
95 if (size <= 0) {
96 return;
97 }
98 int32_t key = 0;
99 size_t maxByteLen = 4;
100 if (size > maxByteLen) {
101 size = maxByteLen;
102 }
103 if (memcpy_s(&key, maxByteLen, data, size) != EOK) {
104 std::cout << "memcpy_s failed!";
105 UNREACHABLE();
106 }
107
108 JSHandle<JSAPITreeSet> tset = CreateJSAPITreeSet(thread);
109 auto callInfo = CreateEcmaRuntimeCallInfo(thread, 6);
110 callInfo->SetFunction(JSTaggedValue::Undefined());
111 callInfo->SetThis(tset.GetTaggedValue());
112 callInfo->SetCallArg(0, JSTaggedValue(key));
113 ContainersTreeSet::Add(callInfo);
114
115 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
116 JSHandle<JSAPITreeSet> dset = CreateJSAPITreeSet(thread);
117 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
118 JSHandle<JSFunction> func = factory->NewJSFunction(env, reinterpret_cast<void *>(TestForEachFunc));
119 auto objCallInfo = CreateEcmaRuntimeCallInfo(thread, 8);
120 objCallInfo->SetFunction(JSTaggedValue::Undefined());
121 objCallInfo->SetThis(tset.GetTaggedValue());
122 objCallInfo->SetCallArg(0, func.GetTaggedValue());
123 objCallInfo->SetCallArg(1, dset.GetTaggedValue());
124 ContainersTreeSet::ForEach(objCallInfo);
125 }
126 JSNApi::DestroyJSVM(vm);
127 }
128 }
129
130 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)131 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
132 {
133 // Run your code on data.
134 OHOS::ContainersTreeSetForEachFuzzTest(data, size);
135 return 0;
136 }