1 /*
2 * Copyright (c) 2023 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 <fuzzer/FuzzedDataProvider.h>
17 #include "jsvaluerefishashmap_fuzzer.h"
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 #include "ecmascript/js_thread.h"
27 #include "ecmascript/js_global_object.h"
28 #include "ecmascript/napi/jsnapi_helper.h"
29 #include "ecmascript/linked_hash_table.h"
30 #include "ecmascript/ecma_runtime_call_info.h"
31 #include "ecmascript/common.h"
32 #include "ecmascript/frames.h"
33 #include "ecmascript/object_factory.h"
34 #include "ecmascript/js_set.h"
35 #include "ecmascript/js_set_iterator.h"
36 #include "ecmascript/js_map.h"
37 #include "ecmascript/js_weak_container.h"
38 #include "ecmascript/js_map_iterator.h"
39 #include "ecmascript/containers/containers_arraylist.h"
40 #include "ecmascript/js_api/js_api_arraylist.h"
41
42 using namespace panda;
43 using namespace panda::test;
44 using namespace panda::ecmascript;
45 using namespace panda::ecmascript::containers;
46
47 namespace OHOS {
CreateEcmaRuntimeCallInfo(JSThread * thread,JSTaggedValue newTgt,uint32_t argvLength)48 EcmaRuntimeCallInfo *CreateEcmaRuntimeCallInfo(JSThread *thread, JSTaggedValue newTgt, uint32_t argvLength)
49 {
50 const uint8_t testDecodedSize = 2;
51 int32_t numActualArgs = argvLength / testDecodedSize + 1;
52 JSTaggedType *sp = const_cast<JSTaggedType *>(thread->GetCurrentSPFrame());
53
54 size_t frameSize = 0;
55 if (thread->IsAsmInterpreter()) {
56 frameSize = InterpretedEntryFrame::NumOfMembers() + numActualArgs;
57 } else {
58 frameSize = InterpretedFrame::NumOfMembers() + numActualArgs;
59 }
60 JSTaggedType *newSp = sp - frameSize; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
61 for (int i = numActualArgs; i > 0; i--) {
62 newSp[i - 1] = JSTaggedValue::Undefined().GetRawData();
63 }
64 EcmaRuntimeCallInfo *ecmaRuntimeCallInfo = reinterpret_cast<EcmaRuntimeCallInfo *>(newSp - 2);
65 *(--newSp) = numActualArgs;
66 *(--newSp) = common::ToUintPtr(thread);
67 ecmaRuntimeCallInfo->SetNewTarget(newTgt);
68 return ecmaRuntimeCallInfo;
69 }
70
SetupFrame(JSThread * thread,EcmaRuntimeCallInfo * info)71 static JSTaggedType *SetupFrame(JSThread *thread, EcmaRuntimeCallInfo *info)
72 {
73 JSTaggedType *sp = const_cast<JSTaggedType *>(thread->GetCurrentSPFrame());
74 size_t frameSize = 0;
75 const int num = 2;
76 // 2 means thread and numArgs
77 if (thread->IsAsmInterpreter()) {
78 frameSize = InterpretedEntryFrame::NumOfMembers() + info->GetArgsNumber() + NUM_MANDATORY_JSFUNC_ARGS + num;
79 } else {
80 frameSize = InterpretedFrame::NumOfMembers() + info->GetArgsNumber() + NUM_MANDATORY_JSFUNC_ARGS + num;
81 }
82 JSTaggedType *newSp = sp - frameSize; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
83
84 InterpretedEntryFrame *state = reinterpret_cast<InterpretedEntryFrame *>(newSp) - 1;
85 state->base.type = ecmascript::FrameType::INTERPRETER_ENTRY_FRAME;
86 state->base.prev = sp;
87 state->pc = nullptr;
88 thread->SetCurrentSPFrame(newSp);
89 return sp;
90 }
91
TearDownFrame(JSThread * thread,JSTaggedType * prev)92 void TearDownFrame(JSThread *thread, JSTaggedType *prev)
93 {
94 thread->SetCurrentSPFrame(prev);
95 }
96
ConstructobjectHashMap(JSThread * thread)97 JSHandle<JSAPIHashMap> ConstructobjectHashMap(JSThread *thread)
98 {
99 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
100 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
101
102 JSHandle<JSTaggedValue> globalObject = env->GetJSGlobalObject();
103 JSHandle<JSTaggedValue> key(factory->NewFromASCII("ArkPrivate"));
104 JSHandle<JSTaggedValue> value =
105 JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(globalObject), key).GetValue();
106 auto objCallInfo = CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
107 objCallInfo->SetFunction(JSTaggedValue::Undefined());
108 objCallInfo->SetThis(value.GetTaggedValue());
109 objCallInfo->SetCallArg(0, JSTaggedValue(static_cast<int>(containers::ContainerTag::HashMap)));
110 [[maybe_unused]] auto prev = SetupFrame(thread, objCallInfo);
111 JSTaggedValue result = containers::ContainersPrivate::Load(objCallInfo);
112 TearDownFrame(thread, prev);
113 JSHandle<JSTaggedValue> constructor(thread, result);
114 JSHandle<JSAPIHashMap> map(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
115 return map;
116 }
117
118
JSValueRefIsHashMapFuzzTest(const uint8_t * data,size_t size)119 void JSValueRefIsHashMapFuzzTest(const uint8_t *data, size_t size)
120 {
121 FuzzedDataProvider fdp(data, size);
122 const int arkProp = fdp.ConsumeIntegral<int>();
123 RuntimeOption option;
124 option.SetLogLevel(common::LOG_LEVEL::ERROR);
125 option.SetArkProperties(arkProp);
126 EcmaVM *vm = JSNApi::CreateJSVM(option);
127 {
128 JsiFastNativeScope scope(vm);
129 auto thread = vm->GetAssociatedJSThread();
130 JSHandle<JSAPIHashMap> map = ConstructobjectHashMap(thread);
131 JSHandle<JSTaggedValue> jshashmap = JSHandle<JSTaggedValue>::Cast(map);
132 Local<JSValueRef> tag = JSNApiHelper::ToLocal<JSValueRef>(jshashmap);
133 tag->IsHashMap(vm);
134 }
135 JSNApi::DestroyJSVM(vm);
136 }
137 }
138
139 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)140 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
141 {
142 // Run your code on data.
143 OHOS::JSValueRefIsHashMapFuzzTest(data, size);
144 return 0;
145 }
146