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