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 "dataviewref_fuzzer.h"
17 #include "ecmascript/base/string_helper.h"
18 #include "ecmascript/base/utf_helper.h"
19 #include "ecmascript/napi/include/jsnapi.h"
20
21 using namespace panda;
22 using namespace panda::ecmascript;
23
24 namespace OHOS {
DataViewRefByteOffSetFuzzTest(const uint8_t * data,size_t size)25 void DataViewRefByteOffSetFuzzTest([[maybe_unused]]const uint8_t *data, size_t size)
26 {
27 RuntimeOption option;
28 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
29 EcmaVM *vm = JSNApi::CreateJSVM(option);
30 const int32_t length = 15;
31 Local<ArrayBufferRef> arrayBuffer = ArrayBufferRef::New(vm, length);
32 const uint32_t byteOffset = 5;
33 const uint32_t byteLength = 7;
34 Local<DataViewRef> dataView = DataViewRef::New(vm, arrayBuffer, byteOffset, byteLength);
35 if (size <= 0) {
36 LOG_ECMA(ERROR) << "illegal input!";
37 return;
38 }
39 (void)dataView->ByteOffset();
40 JSNApi::DestroyJSVM(vm);
41 }
42
DataViewRefByteLengthFuzzTest(const uint8_t * data,size_t size)43 void DataViewRefByteLengthFuzzTest([[maybe_unused]]const uint8_t *data, size_t size)
44 {
45 RuntimeOption option;
46 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
47 EcmaVM *vm = JSNApi::CreateJSVM(option);
48 const int32_t length = 15;
49 Local<ArrayBufferRef> arrayBuffer = ArrayBufferRef::New(vm, length);
50 const uint32_t byteOffset = 5;
51 const uint32_t byteLength = 7;
52 Local<DataViewRef> dataView = DataViewRef::New(vm, arrayBuffer, byteOffset, byteLength);
53 if (size <= 0) {
54 LOG_ECMA(ERROR) << "illegal input!";
55 return;
56 }
57 (void)dataView->ByteLength();
58 JSNApi::DestroyJSVM(vm);
59 }
60
DataViewRefGetArrayBufferFuzzTest(const uint8_t * data,size_t size)61 void DataViewRefGetArrayBufferFuzzTest([[maybe_unused]]const uint8_t *data, size_t size)
62 {
63 RuntimeOption option;
64 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
65 EcmaVM *vm = JSNApi::CreateJSVM(option);
66 const int32_t length = 15;
67 Local<ArrayBufferRef> arrayBuffer = ArrayBufferRef::New(vm, length);
68 const uint32_t byteOffset = 5;
69 const uint32_t byteLength = 7;
70 Local<DataViewRef> dataView = DataViewRef::New(vm, arrayBuffer, byteOffset, byteLength);
71 if (size <= 0) {
72 LOG_ECMA(ERROR) << "illegal input!";
73 return;
74 }
75 (void)dataView->GetArrayBuffer(vm);
76 JSNApi::DestroyJSVM(vm);
77 }
78 }
79
80 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)81 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
82 {
83 // Run your code on data.
84 OHOS::DataViewRefByteOffSetFuzzTest(data, size);
85 OHOS::DataViewRefByteLengthFuzzTest(data, size);
86 OHOS::DataViewRefGetArrayBufferFuzzTest(data, size);
87 return 0;
88 }