• 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 "containershashmap_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         auto objCallInfo = CreateEcmaRuntimeCallInfo(thread, 6);
59         objCallInfo->SetFunction(JSTaggedValue::Undefined());
60         objCallInfo->SetThis(value.GetTaggedValue());
61         objCallInfo->SetCallArg(0, JSTaggedValue(static_cast<int>(ContainerTag::HashMap)));
62         JSTaggedValue result = ContainersPrivate::Load(objCallInfo);
63         return result;
64     }
65 
CreateJSAPIHashMap(JSThread * thread)66     JSHandle<JSAPIHashMap> CreateJSAPIHashMap(JSThread *thread)
67     {
68         JSHandle<JSFunction> newTarget(thread, InitializeHashMapConstructor(thread));
69         auto objCallInfo = CreateEcmaRuntimeCallInfo(thread, 4);
70         objCallInfo->SetFunction(newTarget.GetTaggedValue());
71         objCallInfo->SetNewTarget(newTarget.GetTaggedValue());
72         objCallInfo->SetThis(JSTaggedValue::Undefined());
73 
74         JSTaggedValue result = ContainersHashMap::HashMapConstructor(objCallInfo);
75         JSHandle<JSAPIHashMap> map(thread, result);
76         return map;
77     }
78 
ContainersHashMapFuzzTest(const uint8_t * data,size_t size)79     void ContainersHashMapFuzzTest(const uint8_t* data, size_t size)
80     {
81         RuntimeOption option;
82         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
83         EcmaVM *vm = JSNApi::CreateJSVM(option);
84         auto thread = vm->GetAssociatedJSThread();
85         if (size <= 0) {
86             return;
87         }
88         double input = 0;
89         if (size > MAXBYTELEN) {
90             size = MAXBYTELEN;
91         }
92         if (memcpy_s(&input, MAXBYTELEN, data, size) != 0) {
93             std::cout << "memcpy_s failed!";
94             UNREACHABLE();
95         }
96         JSHandle<JSAPIHashMap> stack = CreateJSAPIHashMap(thread);
97         EcmaRuntimeCallInfo *callInfo = CreateEcmaRuntimeCallInfo(thread, 6);
98         callInfo->SetFunction(JSTaggedValue::Undefined());
99         callInfo->SetThis(stack.GetTaggedValue());
100         callInfo->SetCallArg(0, JSTaggedValue(input));
101         ContainersHashMap::HashMapConstructor(callInfo);
102         JSNApi::DestroyJSVM(vm);
103     }
104 }
105 
106 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)107 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
108 {
109     // Run your code on data.
110     OHOS::ContainersHashMapFuzzTest(data, size);
111     return 0;
112 }