• 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 #include "containershashmapforeach_fuzzer.h"
16 
17 #include "ecmascript/containers/containers_hashmap.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::ecmascript;
27 using namespace panda::ecmascript::containers;
28 
29 #define MAXBYTELEN sizeof(uint32_t)
30 namespace OHOS {
JSObjectCreate(JSThread * thread)31     JSFunction *JSObjectCreate(JSThread *thread)
32     {
33         EcmaVM *ecmaVM = thread->GetEcmaVM();
34         JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
35         return globalEnv->GetObjectFunction().GetObject<JSFunction>();
36     }
37 
CreateEcmaRuntimeCallInfo(JSThread * thread,uint32_t numArgs)38     EcmaRuntimeCallInfo *CreateEcmaRuntimeCallInfo(JSThread *thread, uint32_t numArgs)
39     {
40         auto factory = thread->GetEcmaVM()->GetFactory();
41         JSHandle<JSTaggedValue> hclass(thread, JSObjectCreate(thread));
42         JSHandle<JSTaggedValue> callee(factory->NewJSObjectByConstructor(JSHandle<JSFunction>::Cast(hclass), hclass));
43         JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined();
44         EcmaRuntimeCallInfo *objCallInfo =
45             EcmaInterpreter::NewRuntimeCallInfo(thread, undefined, callee, undefined, numArgs);
46         return objCallInfo;
47     }
48 
InitializeHashMapConstructor(JSThread * thread)49     JSTaggedValue InitializeHashMapConstructor(JSThread *thread)
50     {
51         ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
52         JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
53 
54         JSHandle<JSTaggedValue> globalObject = env->GetJSGlobalObject();
55         JSHandle<JSTaggedValue> key(factory->NewFromASCII("ArkPrivate"));
56         JSHandle<JSTaggedValue> value =
57             JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(globalObject), key).GetValue();
58 
59         auto objCallInfo = CreateEcmaRuntimeCallInfo(thread, 6);
60         objCallInfo->SetFunction(JSTaggedValue::Undefined());
61         objCallInfo->SetThis(value.GetTaggedValue());
62         objCallInfo->SetCallArg(0, JSTaggedValue(static_cast<int>(ContainerTag::HashMap)));
63         JSTaggedValue result = ContainersPrivate::Load(objCallInfo);
64         return result;
65     }
66 
CreateJSAPIHashMap(JSThread * thread)67     JSHandle<JSAPIHashMap> CreateJSAPIHashMap(JSThread *thread)
68     {
69         JSHandle<JSFunction> newTarget(thread, InitializeHashMapConstructor(thread));
70         auto objCallInfo = CreateEcmaRuntimeCallInfo(thread, 4);
71         objCallInfo->SetFunction(newTarget.GetTaggedValue());
72         objCallInfo->SetNewTarget(newTarget.GetTaggedValue());
73         objCallInfo->SetThis(JSTaggedValue::Undefined());
74 
75         JSTaggedValue result = ContainersHashMap::HashMapConstructor(objCallInfo);
76         JSHandle<JSAPIHashMap> map(thread, result);
77         return map;
78     }
79 
ContainersHashMapForEachFuzzTest(const uint8_t * data,size_t size)80     void ContainersHashMapForEachFuzzTest(const uint8_t* data, size_t size)
81     {
82         RuntimeOption option;
83         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
84         EcmaVM *vm = JSNApi::CreateJSVM(option);
85         auto thread = vm->GetAssociatedJSThread();
86         if (size <= 0) {
87             return;
88         }
89         double input = 0;
90         if (size > MAXBYTELEN) {
91             size = MAXBYTELEN;
92         }
93         if (memcpy_s(&input, MAXBYTELEN, data, size) != 0) {
94             std::cout << "memcpy_s failed!";
95             UNREACHABLE();
96         }
97         JSHandle<JSAPIHashMap> tMap = CreateJSAPIHashMap(thread);
98         auto callInfo = CreateEcmaRuntimeCallInfo(thread, 8);
99         callInfo->SetFunction(JSTaggedValue::Undefined());
100         callInfo->SetThis(tMap.GetTaggedValue());
101         callInfo->SetCallArg(0, JSTaggedValue(input));
102         callInfo->SetCallArg(1, JSTaggedValue(input));
103         ContainersHashMap::Set(callInfo);
104         JSHandle<JSAPIHashMap> dMap = CreateJSAPIHashMap(thread);
105         auto callInfo1 = CreateEcmaRuntimeCallInfo(thread, 8);
106         callInfo1->SetFunction(JSTaggedValue::Undefined());
107         callInfo1->SetThis(tMap.GetTaggedValue());
108         callInfo1->SetCallArg(0, JSTaggedValue::Undefined());
109         callInfo1->SetCallArg(1, dMap.GetTaggedValue());
110         ContainersHashMap::ForEach(callInfo1);
111         JSNApi::DestroyJSVM(vm);
112     }
113 }
114 
115 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)116 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
117 {
118     // Run your code on data.
119     OHOS::ContainersHashMapForEachFuzzTest(data, size);
120     return 0;
121 }