1 /*
2 * Copyright (c) 2022-2024 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 "builtinsarraybufferisview_fuzzer.h"
17
18 #include "ecmascript/builtins/builtins_arraybuffer.h"
19 #include "ecmascript/ecma_string-inl.h"
20 #include "ecmascript/ecma_vm.h"
21 #include "ecmascript/global_env.h"
22 #include "ecmascript/interpreter/interpreter.h"
23 #include "ecmascript/js_function.h"
24 #include "ecmascript/js_global_object.h"
25 #include "ecmascript/js_handle.h"
26 #include "ecmascript/js_thread.h"
27 #include "ecmascript/napi/include/jsnapi.h"
28
29 using namespace panda;
30 using namespace panda::ecmascript;
31
32 #define MAXBYTELEN sizeof(uint16_t)
33
34 namespace OHOS {
CreateEcmaRuntimeCallInfo(JSThread * thread,JSTaggedValue newTgt,uint32_t numArgs)35 EcmaRuntimeCallInfo *CreateEcmaRuntimeCallInfo(JSThread *thread, JSTaggedValue newTgt, uint32_t numArgs)
36 {
37 auto factory = thread->GetEcmaVM()->GetFactory();
38 JSHandle<JSTaggedValue> hclass(thread, newTgt);
39 JSHandle<JSTaggedValue> callee(factory->NewJSObjectByConstructor(JSHandle<JSFunction>::Cast(hclass), hclass));
40 JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined();
41 EcmaRuntimeCallInfo *objCallInfo =
42 EcmaInterpreter::NewRuntimeCallInfo(thread, undefined, callee, undefined, numArgs);
43 return objCallInfo;
44 }
45
BuiltinsArrayBufferIsViewFuzzTest(const uint8_t * data,size_t size)46 void BuiltinsArrayBufferIsViewFuzzTest(const uint8_t* data, size_t size)
47 {
48 RuntimeOption option;
49 option.SetLogLevel(common::LOG_LEVEL::ERROR);
50 EcmaVM *vm = JSNApi::CreateJSVM(option);
51 {
52 JsiFastNativeScope scope(vm);
53 JSThread *thread = vm->GetJSThread();
54 JSHandle<GlobalEnv> env = vm->GetGlobalEnv();
55
56 int32_t input;
57 if (size <= 0) {
58 return;
59 }
60 if (size > MAXBYTELEN) {
61 size = MAXBYTELEN;
62 }
63 if (memcpy_s(&input, MAXBYTELEN, data, size) != 0) {
64 std::cout << "memcpy_s failed!";
65 UNREACHABLE();
66 }
67
68 JSHandle<JSFunction> arrayBuffer(thread, env->GetArrayBufferFunction().GetTaggedValue());
69 JSHandle<JSObject> globalObject(thread, env->GetGlobalObject());
70
71 auto ecmaRuntimeCallInfo = CreateEcmaRuntimeCallInfo(thread, arrayBuffer.GetTaggedValue(), 6);
72 ecmaRuntimeCallInfo->SetFunction(arrayBuffer.GetTaggedValue());
73 ecmaRuntimeCallInfo->SetThis(globalObject.GetTaggedValue());
74 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(input));
75
76 builtins::BuiltinsArrayBuffer::IsView(ecmaRuntimeCallInfo);
77 }
78 JSNApi::DestroyJSVM(vm);
79 }
80 }
81
82 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)83 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
84 {
85 // Run your code on data.
86 OHOS::BuiltinsArrayBufferIsViewFuzzTest(data, size);
87 return 0;
88 }