• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "builtinsarraybufferarraybufferconstructor_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(int32_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 
BuiltinsArrayBufferArrayBufferConstructorFuzzTest(const uint8_t * data,size_t size)46     void BuiltinsArrayBufferArrayBufferConstructorFuzzTest(const uint8_t* data, size_t size)
47     {
48         RuntimeOption option;
49         option.SetLogLevel(RuntimeOption::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             const int32_t MaxMenory = 1048576;
68             if (input > MaxMenory) {
69                 input = MaxMenory;
70             }
71 
72             JSHandle<JSFunction> arrayBuffer(thread, env->GetArrayBufferFunction().GetTaggedValue());
73             JSHandle<JSObject> globalObject(thread, env->GetGlobalObject());
74 
75             auto ecmaRuntimeCallInfo = CreateEcmaRuntimeCallInfo(thread, arrayBuffer.GetTaggedValue(), 6);
76             ecmaRuntimeCallInfo->SetFunction(arrayBuffer.GetTaggedValue());
77             ecmaRuntimeCallInfo->SetThis(globalObject.GetTaggedValue());
78             ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(input));
79             builtins::BuiltinsArrayBuffer::ArrayBufferConstructor(ecmaRuntimeCallInfo);
80         }
81         JSNApi::DestroyJSVM(vm);
82     }
83 }
84 
85 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)86 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
87 {
88     // Run your code on data.
89     OHOS::BuiltinsArrayBufferArrayBufferConstructorFuzzTest(data, size);
90     return 0;
91 }