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 "arraybufferrefnewwithtwoparameters_fuzzer.h"
17
18 #include "ecmascript/ecma_string-inl.h"
19 #include "ecmascript/napi/include/jsnapi.h"
20
21 using namespace panda;
22 using namespace panda::ecmascript;
23
24 #define MAXBYTELEN sizeof(int32_t)
25
26 namespace OHOS {
ArrayBufferRefNewWithTwoParametersFuzzTest(const uint8_t * data,size_t size)27 void ArrayBufferRefNewWithTwoParametersFuzzTest(const uint8_t* data, size_t size)
28 {
29 RuntimeOption option;
30 option.SetLogLevel(common::LOG_LEVEL::ERROR);
31 EcmaVM *vm = JSNApi::CreateJSVM(option);
32 int32_t input;
33 if (size <= 0) {
34 return;
35 }
36 if (size > MAXBYTELEN) {
37 size = MAXBYTELEN;
38 }
39 if (memcpy_s(&input, MAXBYTELEN, data, size) != 0) {
40 std::cout << "memcpy_s failed!";
41 UNREACHABLE();
42 }
43 ArrayBufferRef::New(vm, input);
44 JSNApi::DestroyJSVM(vm);
45 }
46
ArrayBufferRef_New_IsDetach_Detach_ByteLength_GetBuffer_FuzzTest(const uint8_t * data,size_t size)47 void ArrayBufferRef_New_IsDetach_Detach_ByteLength_GetBuffer_FuzzTest(const uint8_t *data, size_t size)
48 {
49 RuntimeOption option;
50 option.SetLogLevel(common::LOG_LEVEL::ERROR);
51 EcmaVM *vm = JSNApi::CreateJSVM(option);
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 Local<ArrayBufferRef> arrayBuffer = ArrayBufferRef::New(vm, input);
64 arrayBuffer->IsDetach(vm);
65 arrayBuffer->Detach(vm);
66 arrayBuffer->ByteLength(vm);
67 JSNApi::DestroyJSVM(vm);
68 }
69 }
70
71 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)72 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
73 {
74 // Run your code on data.
75 OHOS::ArrayBufferRefNewWithTwoParametersFuzzTest(data, size);
76 OHOS::ArrayBufferRef_New_IsDetach_Detach_ByteLength_GetBuffer_FuzzTest(data, size);
77 return 0;
78 }