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 "containersstackforeach_fuzzer.h"
17 #include "ecmascript/containers/containers_stack.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/napi/include/jsnapi.h"
24 
25 using namespace panda;
26 using namespace panda::test;
27 using namespace panda::ecmascript;
28 using namespace panda::ecmascript::containers;
29 
30 #define MAXBYTELEN sizeof(uint32_t)
31 
32 namespace OHOS {
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 
InitializeStackConstructor(JSThread *thread)51     JSTaggedValue InitializeStackConstructor(JSThread *thread)
52     {
53         auto factory = thread->GetEcmaVM()->GetFactory();
54         JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
55         JSHandle<JSTaggedValue> globalObject = env->GetJSGlobalObject();
56         JSHandle<JSTaggedValue> key(factory->NewFromASCII("ArkPrivate"));
57         JSHandle<JSTaggedValue> value =
58             JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(globalObject), key).GetValue();
59         auto objCallInfo = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length
60         objCallInfo->SetFunction(JSTaggedValue::Undefined());
61         objCallInfo->SetThis(value.GetTaggedValue());
62         objCallInfo->SetCallArg(0, JSTaggedValue(static_cast<int>(ContainerTag::Stack)));
63         JSTaggedValue result = ContainersPrivate::Load(objCallInfo);
64         return result;
65     }
66 
TestForEachFunc([[maybe_unused]] EcmaRuntimeCallInfo *argv)67     JSTaggedValue TestForEachFunc([[maybe_unused]] EcmaRuntimeCallInfo *argv)
68     {
69         return JSTaggedValue::True();
70     }
71 
CreateJSAPIStack(JSThread *thread, JSTaggedValue compare = JSTaggedValue::Undefined())72     JSHandle<JSAPIStack> CreateJSAPIStack(JSThread *thread, JSTaggedValue compare = JSTaggedValue::Undefined())
73     {
74         JSHandle<JSTaggedValue> compareHandle(thread, compare);
75         JSHandle<JSFunction> newTarget(thread, InitializeStackConstructor(thread));
76         auto objCallInfo = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length
77         objCallInfo->SetFunction(newTarget.GetTaggedValue());
78         objCallInfo->SetNewTarget(newTarget.GetTaggedValue());
79         objCallInfo->SetThis(JSTaggedValue::Undefined());
80         objCallInfo->SetCallArg(0, compareHandle.GetTaggedValue());
81         JSTaggedValue result = ContainersStack::StackConstructor(objCallInfo);
82         JSHandle<JSAPIStack> stack(thread, result);
83         return stack;
84     }
85 
ContainersStackForEachFuzzTest(const uint8_t* data, size_t size)86     void ContainersStackForEachFuzzTest(const uint8_t* data, size_t size)
87     {
88         RuntimeOption option;
89         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
90         EcmaVM *vm = JSNApi::CreateJSVM(option);
91         {
92             JsiFastNativeScope scope(vm);
93             auto thread = vm->GetAssociatedJSThread();
94 
95             if (size <= 0) {
96                 return;
97             }
98             double input = 0;
99             if (size > MAXBYTELEN) {
100                 size = MAXBYTELEN;
101             }
102             if (memcpy_s(&input, MAXBYTELEN, data, size) != 0) {
103                 std::cout << "memcpy_s failed!";
104                 UNREACHABLE();
105             }
106 
107             JSHandle<JSAPIStack> stack = CreateJSAPIStack(thread);
108             {
109                 EcmaRuntimeCallInfo *callInfo = CreateEcmaRuntimeCallInfo(thread, 8); // 8 : means the argv length
110                 callInfo->SetFunction(JSTaggedValue::Undefined());
111                 callInfo->SetThis(stack.GetTaggedValue());
112                 callInfo->SetCallArg(0, JSTaggedValue(input));
113                 ContainersStack::Push(callInfo);
114             }
115 
116             ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
117             JSHandle<JSAPIStack> dlist = CreateJSAPIStack(thread);
118             {
119                 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
120                 JSHandle<JSFunction> func = factory->NewJSFunction(env, reinterpret_cast<void *>(TestForEachFunc));
121                 EcmaRuntimeCallInfo *callInfo = CreateEcmaRuntimeCallInfo(thread, 8); // 8 : means the argv length
122                 callInfo->SetFunction(JSTaggedValue::Undefined());
123                 callInfo->SetThis(stack.GetTaggedValue());
124                 callInfo->SetCallArg(0, func.GetTaggedValue());
125                 callInfo->SetCallArg(1, dlist.GetTaggedValue());
126                 ContainersStack::ForEach(callInfo);
127             }
128         }
129         JSNApi::DestroyJSVM(vm);
130     }
131 }
132 
133 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)134 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
135 {
136     // Run your code on data.
137     OHOS::ContainersStackForEachFuzzTest(data, size);
138     return 0;
139 }