• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "jsvaluerefisarray_fuzzer.h"
17 #include "ecmascript/ecma_string-inl.h"
18 #include "ecmascript/napi/include/jsnapi.h"
19 
20 using namespace panda;
21 using namespace panda::ecmascript;
22 
23 namespace OHOS {
IsInt8ArrayFuzztest(const uint8_t * data,size_t size)24 void IsInt8ArrayFuzztest(const uint8_t *data, size_t size)
25 {
26     RuntimeOption option;
27     option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
28     EcmaVM *vm = JSNApi::CreateJSVM(option);
29     if (data == nullptr || size <= 0) {
30         LOG_ECMA(ERROR) << "illegal input!";
31         return;
32     }
33     NativePointerCallback deleter = nullptr;
34     Local<ArrayBufferRef> arrayBuffer = ArrayBufferRef::New(vm, (void *)data, (int32_t)size, deleter, (void *)data);
35     Local<JSValueRef> typedArray = Int8ArrayRef::New(vm, arrayBuffer, 0, (int32_t)size);
36     typedArray->IsInt8Array(vm);
37     JSNApi::DestroyJSVM(vm);
38 }
39 
IsUint8ArrayFuzztest(const uint8_t * data,size_t size)40 void IsUint8ArrayFuzztest(const uint8_t *data, size_t size)
41 {
42     RuntimeOption option;
43     option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
44     EcmaVM *vm = JSNApi::CreateJSVM(option);
45     if (data == nullptr || size <= 0) {
46         LOG_ECMA(ERROR) << "illegal input!";
47         return;
48     }
49     NativePointerCallback deleter = nullptr;
50     Local<ArrayBufferRef> arrayBuffer = ArrayBufferRef::New(vm, (void *)data, (int32_t)size, deleter, (void *)data);
51     Local<JSValueRef> typedArray = Uint8ArrayRef::New(vm, arrayBuffer, 0, (int32_t)size);
52     typedArray->IsUint8Array(vm);
53     JSNApi::DestroyJSVM(vm);
54 }
55 
IsUint8ClampedArrayFuzztest(const uint8_t * data,size_t size)56 void IsUint8ClampedArrayFuzztest(const uint8_t *data, size_t size)
57 {
58     RuntimeOption option;
59     option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
60     EcmaVM *vm = JSNApi::CreateJSVM(option);
61     if (data == nullptr || size <= 0) {
62         LOG_ECMA(ERROR) << "illegal input!";
63         return;
64     }
65     NativePointerCallback deleter = nullptr;
66     Local<ArrayBufferRef> arrayBuffer = ArrayBufferRef::New(vm, (void *)data, (int32_t)size, deleter, (void *)data);
67     Local<JSValueRef> typedArray = Uint8ClampedArrayRef::New(vm, arrayBuffer, 0, (int32_t)size);
68     typedArray->IsUint8ClampedArray(vm);
69     JSNApi::DestroyJSVM(vm);
70 }
71 
IsInt16ArrayFuzztest(const uint8_t * data,size_t size)72 void IsInt16ArrayFuzztest(const uint8_t *data, size_t size)
73 {
74     RuntimeOption option;
75     option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
76     EcmaVM *vm = JSNApi::CreateJSVM(option);
77     if (data == nullptr || size <= 0) {
78         LOG_ECMA(ERROR) << "illegal input!";
79         return;
80     }
81     NativePointerCallback deleter = nullptr;
82     Local<ArrayBufferRef> arrayBuffer = ArrayBufferRef::New(vm, (void *)data, (int32_t)size, deleter, (void *)data);
83     int32_t length = size / sizeof(int16_t);
84     Local<JSValueRef> typedArray = Int16ArrayRef::New(vm, arrayBuffer, 0, length);
85     typedArray->IsInt16Array(vm);
86     JSNApi::DestroyJSVM(vm);
87 }
88 
IsUint16ArrayFuzztest(const uint8_t * data,size_t size)89 void IsUint16ArrayFuzztest(const uint8_t *data, size_t size)
90 {
91     RuntimeOption option;
92     option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
93     EcmaVM *vm = JSNApi::CreateJSVM(option);
94     if (data == nullptr || size <= 0) {
95         LOG_ECMA(ERROR) << "illegal input!";
96         return;
97     }
98     NativePointerCallback deleter = nullptr;
99     Local<ArrayBufferRef> arrayBuffer = ArrayBufferRef::New(vm, (void *)data, (int32_t)size, deleter, (void *)data);
100     int32_t length = size / sizeof(uint16_t);
101     Local<JSValueRef> typedArray = Uint16ArrayRef::New(vm, arrayBuffer, 0, length);
102     typedArray->IsUint16Array(vm);
103     JSNApi::DestroyJSVM(vm);
104 }
105 }
106 
107 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)108 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
109 {
110     // Run your code on data.
111     OHOS::IsInt8ArrayFuzztest(data, size);
112     OHOS::IsUint8ArrayFuzztest(data, size);
113     OHOS::IsUint8ClampedArrayFuzztest(data, size);
114     OHOS::IsInt16ArrayFuzztest(data, size);
115     OHOS::IsUint16ArrayFuzztest(data, size);
116     return 0;
117 }