• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "containerslistgetiteratorobj_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 using namespace panda;
28 using namespace panda::test;
29 using namespace panda::ecmascript;
30 using namespace panda::ecmascript::containers;
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 
CreateEcmaRuntimeCallInfo(JSThread * thread,JSTaggedValue newTgt,uint32_t argvLength)51     EcmaRuntimeCallInfo* CreateEcmaRuntimeCallInfo(JSThread *thread, JSTaggedValue newTgt, uint32_t argvLength)
52     {
53         const uint8_t testDecodedSize = 2;
54         int32_t numActualArgs = argvLength / testDecodedSize + 1;
55         JSTaggedType *sp = const_cast<JSTaggedType *>(thread->GetCurrentSPFrame());
56 
57         size_t frameSize = 0;
58         if (thread->IsAsmInterpreter()) {
59             frameSize = InterpretedEntryFrame::NumOfMembers() + numActualArgs;
60         } else {
61             frameSize = InterpretedFrame::NumOfMembers() + numActualArgs;
62         }
63         JSTaggedType *newSp = sp - frameSize;  // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
64         for (int i = numActualArgs; i > 0; i--) {
65             newSp[i - 1] = JSTaggedValue::Undefined().GetRawData();
66         }
67         EcmaRuntimeCallInfo *ecmaRuntimeCallInfo = reinterpret_cast<EcmaRuntimeCallInfo *>(newSp - 2);
68         *(--newSp) = numActualArgs;
69         *(--newSp) = panda::ecmascript::ToUintPtr(thread);
70         ecmaRuntimeCallInfo->SetNewTarget(newTgt);
71         return ecmaRuntimeCallInfo;
72     }
73 
InitializeContainersList(JSThread * thread)74     JSTaggedValue InitializeContainersList(JSThread *thread)
75     {
76         auto factory = thread->GetEcmaVM()->GetFactory();
77         JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
78         JSHandle<JSTaggedValue> globalObject = env->GetJSGlobalObject();
79         JSHandle<JSTaggedValue> key(factory->NewFromASCII("ArkPrivate"));
80         JSHandle<JSTaggedValue> value =
81             JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(globalObject), key).GetValue();
82 
83         auto objCallInfo = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length
84         objCallInfo->SetFunction(JSTaggedValue::Undefined());
85         objCallInfo->SetThis(value.GetTaggedValue());
86         objCallInfo->SetCallArg(0, JSTaggedValue(static_cast<int>(ContainerTag::List))); // 0 means the argument
87         JSTaggedValue result = ContainersPrivate::Load(objCallInfo);
88 
89         return result;
90     }
91 
CreateJSAPIList(JSThread * thread)92     JSHandle<JSAPIList> CreateJSAPIList(JSThread *thread)
93     {
94         JSHandle<JSFunction> newTarget(thread, InitializeContainersList(thread));
95         auto objCallInfo = CreateEcmaRuntimeCallInfo(thread, 4); // 4 : means the argv length
96         objCallInfo->SetFunction(newTarget.GetTaggedValue());
97         objCallInfo->SetNewTarget(newTarget.GetTaggedValue());
98         objCallInfo->SetThis(JSTaggedValue::Undefined());
99         JSTaggedValue result = ContainersList::Add(objCallInfo);
100         JSHandle<JSAPIList> map(thread, result);
101         return map;
102     }
103 
ContainerslistGetIteratorObjFuzzTest(const uint8_t * data,size_t size)104     void ContainerslistGetIteratorObjFuzzTest(const uint8_t* data, size_t size)
105     {
106         RuntimeOption option;
107         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
108         EcmaVM *vm = JSNApi::CreateJSVM(option);
109         auto thread = vm->GetAssociatedJSThread();
110 
111         if (size <= 0) {
112             return;
113         }
114         double input = 0;
115         const double MAXBYTELEN = 8;
116         if (size > MAXBYTELEN) {
117             size = MAXBYTELEN;
118         }
119         if (JSTaggedValue::IsImpureNaN(input)) {
120             return;
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, 6); // 6 : means the argv length
128         callInfo->SetFunction(JSTaggedValue::Undefined());
129         callInfo->SetThis(list.GetTaggedValue());
130         callInfo->SetCallArg(0, JSTaggedValue(input));
131         callInfo->SetCallArg(1, JSTaggedValue(input));
132         ContainersList::Add(callInfo);
133         auto callInfo1 = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length
134         callInfo1->SetFunction(JSTaggedValue::Undefined());
135         callInfo1->SetThis(list.GetTaggedValue());
136         ContainersList::GetIteratorObj(callInfo1);
137         JSNApi::DestroyJSVM(vm);
138     }
139 }
140 
141 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)142 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
143 {
144     // Run your code on data.
145     OHOS::ContainerslistGetIteratorObjFuzzTest(data, size);
146     return 0;
147 }