• 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 "containerslightweightmapat_fuzzer.h"
17 
18 #include "ecmascript/containers/containers_lightweightmap.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/napi/include/jsnapi.h"
25 
26 using namespace panda;
27 using namespace panda::test;
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 
InitializeLightWeightMapConstructor(JSThread * thread)51     JSTaggedValue InitializeLightWeightMapConstructor(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 
60         auto objCallInfo = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length
61         objCallInfo->SetFunction(JSTaggedValue::Undefined());
62         objCallInfo->SetThis(value.GetTaggedValue());
63         objCallInfo->SetCallArg(0, JSTaggedValue(static_cast<int>(ContainerTag::LightWeightMap)));
64         JSTaggedValue result = ContainersPrivate::Load(objCallInfo);
65 
66         return result;
67     }
68 
CreateJSAPILightWeightMap(JSThread * thread)69     JSHandle<JSAPILightWeightMap> CreateJSAPILightWeightMap(JSThread *thread)
70     {
71         JSHandle<JSFunction> newTarget(thread, InitializeLightWeightMapConstructor(thread));
72         auto objCallInfo = CreateEcmaRuntimeCallInfo(thread, 4);
73         objCallInfo->SetFunction(newTarget.GetTaggedValue());
74         objCallInfo->SetNewTarget(newTarget.GetTaggedValue());
75         objCallInfo->SetThis(JSTaggedValue::Undefined());
76 
77         JSTaggedValue result = ContainersLightWeightMap::LightWeightMapConstructor(objCallInfo);
78         JSHandle<JSAPILightWeightMap> map(thread, result);
79         return map;
80     }
81 
ContainersLightWeightMapAtFuzzTest(const uint8_t * data,size_t size)82     void ContainersLightWeightMapAtFuzzTest(const uint8_t* data, size_t size)
83     {
84         RuntimeOption option;
85         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
86         EcmaVM *vm = JSNApi::CreateJSVM(option);
87         auto thread = vm->GetAssociatedJSThread();
88 
89         if (size <= 0) {
90             return;
91         }
92         double input = 0;
93         const double maxByteLen = 4;
94         if (size > maxByteLen) {
95             size = maxByteLen;
96         }
97         if (memcpy_s(&input, maxByteLen, data, size) != 0) {
98             std::cout << "memcpy_s failed!";
99             UNREACHABLE();
100         }
101         JSHandle<JSAPILightWeightMap> lightWeightMap = CreateJSAPILightWeightMap(thread);
102 
103         EcmaRuntimeCallInfo *callInfo1 = CreateEcmaRuntimeCallInfo(thread, 8);
104         callInfo1->SetFunction(JSTaggedValue::Undefined());
105         callInfo1->SetThis(lightWeightMap.GetTaggedValue());
106         callInfo1->SetCallArg(0, JSTaggedValue(input));
107         callInfo1->SetCallArg(1, JSTaggedValue(input + 1));
108         ContainersLightWeightMap::Set(callInfo1);
109 
110         {
111             EcmaRuntimeCallInfo *callInfo = CreateEcmaRuntimeCallInfo(thread, 6);
112             callInfo->SetFunction(JSTaggedValue::Undefined());
113             callInfo->SetThis(lightWeightMap.GetTaggedValue());
114             callInfo->SetCallArg(0, JSTaggedValue(input));
115             ContainersLightWeightMap::GetKeyAt(callInfo);
116         }
117 
118         {
119             EcmaRuntimeCallInfo *callInfo = CreateEcmaRuntimeCallInfo(thread, 6);
120             callInfo->SetFunction(JSTaggedValue::Undefined());
121             callInfo->SetThis(lightWeightMap.GetTaggedValue());
122             callInfo->SetCallArg(0, JSTaggedValue(input));
123             ContainersLightWeightMap::GetValueAt(callInfo);
124         }
125 
126         {
127             EcmaRuntimeCallInfo *callInfo = CreateEcmaRuntimeCallInfo(thread, 6);
128             callInfo->SetFunction(JSTaggedValue::Undefined());
129             callInfo->SetThis(lightWeightMap.GetTaggedValue());
130             callInfo->SetCallArg(0, JSTaggedValue(input));
131             ContainersLightWeightMap::SetValueAt(callInfo);
132         }
133 
134         {
135             EcmaRuntimeCallInfo *callInfo = CreateEcmaRuntimeCallInfo(thread, 6);
136             callInfo->SetFunction(JSTaggedValue::Undefined());
137             callInfo->SetThis(lightWeightMap.GetTaggedValue());
138             callInfo->SetCallArg(0, JSTaggedValue(input));
139             ContainersLightWeightMap::RemoveAt(callInfo);
140         }
141         JSNApi::DestroyJSVM(vm);
142     }
143 }
144 
145 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)146 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
147 {
148     // Run your code on data.
149     OHOS::ContainersLightWeightMapAtFuzzTest(data, size);
150     return 0;
151 }