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 "containerslistconverttoarray_fuzzer.h"
17 
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 
27 #define MAXBYTELEN sizeof(uint32_t)
28 
29 using namespace panda;
30 using namespace panda::test;
31 using namespace panda::ecmascript;
32 using namespace panda::ecmascript::containers;
33 
34 namespace OHOS {
JSObjectCreate(JSThread *thread)35     JSFunction *JSObjectCreate(JSThread *thread)
36     {
37         EcmaVM *ecmaVM = thread->GetEcmaVM();
38         JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
39         return globalEnv->GetObjectFunction().GetObject<JSFunction>();
40     }
41 
CreateEcmaRuntimeCallInfo(JSThread *thread, uint32_t numArgs)42     EcmaRuntimeCallInfo *CreateEcmaRuntimeCallInfo(JSThread *thread, uint32_t numArgs)
43     {
44         auto factory = thread->GetEcmaVM()->GetFactory();
45         JSHandle<JSTaggedValue> hclass(thread, JSObjectCreate(thread));
46         JSHandle<JSTaggedValue> callee(factory->NewJSObjectByConstructor(JSHandle<JSFunction>::Cast(hclass), hclass));
47         JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined();
48         EcmaRuntimeCallInfo *objCallInfo =
49             EcmaInterpreter::NewRuntimeCallInfo(thread, undefined, callee, undefined, numArgs);
50         return objCallInfo;
51     }
52 
CreateEcmaRuntimeCallInfo(JSThread *thread, JSTaggedValue newTgt, uint32_t argvLength)53     EcmaRuntimeCallInfo* CreateEcmaRuntimeCallInfo(JSThread *thread, JSTaggedValue newTgt, uint32_t argvLength)
54     {
55         const uint8_t testDecodedSize = 2;
56         int32_t numActualArgs = argvLength / testDecodedSize + 1;
57         JSTaggedType *sp = const_cast<JSTaggedType *>(thread->GetCurrentSPFrame());
58 
59         size_t frameSize = 0;
60         if (thread->IsAsmInterpreter()) {
61             frameSize = InterpretedEntryFrame::NumOfMembers() + numActualArgs;
62         } else {
63             frameSize = InterpretedFrame::NumOfMembers() + numActualArgs;
64         }
65         JSTaggedType *newSp = sp - frameSize;
66         for (int i = numActualArgs; i > 0; i--) {
67             newSp[i - 1] = JSTaggedValue::Undefined().GetRawData();
68         }
69         EcmaRuntimeCallInfo *ecmaRuntimeCallInfo = reinterpret_cast<EcmaRuntimeCallInfo *>(newSp - 2);
70         *(--newSp) = numActualArgs;
71         *(--newSp) = panda::ecmascript::ToUintPtr(thread);
72         ecmaRuntimeCallInfo->SetNewTarget(newTgt);
73         return ecmaRuntimeCallInfo;
74     }
75 
InitializeContainersList(JSThread *thread)76     JSTaggedValue InitializeContainersList(JSThread *thread)
77     {
78         auto factory = thread->GetEcmaVM()->GetFactory();
79         JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
80         JSHandle<JSTaggedValue> globalObject = env->GetJSGlobalObject();
81         JSHandle<JSTaggedValue> key(factory->NewFromASCII("ArkPrivate"));
82         JSHandle<JSTaggedValue> value =
83             JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(globalObject), key).GetValue();
84 
85         auto objCallInfo = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length
86         objCallInfo->SetFunction(JSTaggedValue::Undefined());
87         objCallInfo->SetThis(value.GetTaggedValue());
88         objCallInfo->SetCallArg(0, JSTaggedValue(static_cast<int>(ContainerTag::List))); // 0 means the argument
89         JSTaggedValue result = ContainersPrivate::Load(objCallInfo);
90 
91         return result;
92     }
93 
CreateJSAPIList(JSThread *thread)94     JSHandle<JSAPIList> CreateJSAPIList(JSThread *thread)
95     {
96         JSHandle<JSFunction> newTarget(thread, InitializeContainersList(thread));
97         auto objCallInfo = CreateEcmaRuntimeCallInfo(thread, 4); // 4 : means the argv length
98         objCallInfo->SetFunction(newTarget.GetTaggedValue());
99         objCallInfo->SetNewTarget(newTarget.GetTaggedValue());
100         objCallInfo->SetThis(JSTaggedValue::Undefined());
101         JSTaggedValue result = ContainersList::Add(objCallInfo);
102         JSHandle<JSAPIList> list(thread, result);
103         return list;
104     }
105 
ContainerslistConvertToArrayFuzzTest(const uint8_t* data, size_t size)106     void ContainerslistConvertToArrayFuzzTest(const uint8_t* data, size_t size)
107     {
108         RuntimeOption option;
109         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
110         EcmaVM *vm = JSNApi::CreateJSVM(option);
111         {
112             JsiFastNativeScope scope(vm);
113             auto thread = vm->GetAssociatedJSThread();
114 
115             if (size <= 0) {
116                 return;
117             }
118             double input = 0;
119             if (size > MAXBYTELEN) {
120                 size = MAXBYTELEN;
121             }
122             if (memcpy_s(&input, MAXBYTELEN, data, size) != 0) {
123                 std::cout << "memcpy_s failed!";
124                 UNREACHABLE();
125             }
126             JSHandle<JSAPIList> list = CreateJSAPIList(thread);
127             auto callInfo = CreateEcmaRuntimeCallInfo(thread, 8); // 8 : means the argv length
128             callInfo->SetFunction(JSTaggedValue::Undefined());
129             callInfo->SetThis(list.GetTaggedValue());
130             callInfo->SetCallArg(0, JSTaggedValue(input));
131             ContainersList::Add(callInfo);
132 
133             auto callInfo1 = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length
134             callInfo1->SetFunction(JSTaggedValue::Undefined());
135             callInfo1->SetThis(list.GetTaggedValue());
136             ContainersList::ConvertToArray(callInfo1);
137         }
138         JSNApi::DestroyJSVM(vm);
139     }
140 }
141 
142 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)143 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
144 {
145     // Run your code on data.
146     OHOS::ContainerslistConvertToArrayFuzzTest(data, size);
147     return 0;
148 }
149