• 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 "objectgetinformation_fuzzer.h"
17 #include "ecmascript/base/string_helper.h"
18 #include "ecmascript/napi/include/jsnapi.h"
19 
20 using namespace panda;
21 using namespace panda::ecmascript;
22 
23 namespace OHOS {
ObjectGetAllPropertyNamesFuzzTest(const uint8_t * data,size_t size)24 void ObjectGetAllPropertyNamesFuzzTest(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     int32_t index = 0;
34     size_t maxByteLen1 = 4;
35     if (size > maxByteLen1) {
36         size = maxByteLen1;
37     }
38     if (memcpy_s(&index, maxByteLen1, data, size) != EOK) {
39         LOG_ECMA(ERROR) << "memcpy_s failed !";
40         UNREACHABLE();
41     }
42     uint32_t filter = 0;
43     size_t maxByteLen2 = 4;
44     if (size > maxByteLen2) {
45         size = maxByteLen2;
46     }
47     if (memcpy_s(&filter, maxByteLen2, data, size) != EOK) {
48         LOG_ECMA(ERROR) << "memcpy_s failed !";
49         UNREACHABLE();
50     }
51     Local<ObjectRef> object = ObjectRef::New(vm);
52     NativePointerCallback callBack = nullptr;
53     object->SetNativePointerField(vm, index, (void *)data, callBack, (void *)data);
54     object->GetAllPropertyNames(vm, filter);
55     JSNApi::DestroyJSVM(vm);
56 }
57 
ObjectGetNativePointerFieldCountFuzzTest(const uint8_t * data,size_t size)58 void ObjectGetNativePointerFieldCountFuzzTest(const uint8_t *data, size_t size)
59 {
60     RuntimeOption option;
61     option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
62     EcmaVM *vm = JSNApi::CreateJSVM(option);
63     if (data == nullptr || size <= 0) {
64         LOG_ECMA(ERROR) << "illegal input!";
65         return;
66     }
67     int32_t key = 0;
68     size_t maxByteLen = 4;
69     if (size > maxByteLen) {
70         size = maxByteLen;
71     }
72     if (memcpy_s(&key, maxByteLen, data, size) != EOK) {
73         LOG_ECMA(ERROR) << "memcpy_s failed !";
74         UNREACHABLE();
75     }
76     if (key <= 0 || key > 1024) { // 1024 : 1M in size
77         key = 1024;               // 1024 : 1M in size
78     }
79     Local<ObjectRef> object = ObjectRef::New(vm);
80     object->SetNativePointerFieldCount(vm, key);
81     object->GetNativePointerFieldCount();
82     JSNApi::DestroyJSVM(vm);
83 }
84 
ObjectGetOwnEnumerablePropertyNamesFuzzTest(const uint8_t * data,size_t size)85 void ObjectGetOwnEnumerablePropertyNamesFuzzTest(const uint8_t *data, size_t size)
86 {
87     RuntimeOption option;
88     option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
89     EcmaVM *vm = JSNApi::CreateJSVM(option);
90     if (data == nullptr || size <= 0) {
91         LOG_ECMA(ERROR) << "illegal input!";
92         return;
93     }
94     int32_t index = 0;
95     size_t maxByteLen = 4;
96     if (size > maxByteLen) {
97         size = maxByteLen;
98     }
99     if (memcpy_s(&index, maxByteLen, data, size) != EOK) {
100         LOG_ECMA(ERROR) << "memcpy_s failed !";
101         UNREACHABLE();
102     }
103     Local<ObjectRef> object = ObjectRef::New(vm);
104     NativePointerCallback callBack = nullptr;
105     object->SetNativePointerField(vm, index, (void *)data, callBack, (void *)data);
106     object->GetOwnEnumerablePropertyNames(vm);
107     JSNApi::DestroyJSVM(vm);
108 }
109 
ObjectGetOwnPropertyNamesFuzzTest(const uint8_t * data,size_t size)110 void ObjectGetOwnPropertyNamesFuzzTest(const uint8_t *data, size_t size)
111 {
112     RuntimeOption option;
113     option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
114     EcmaVM *vm = JSNApi::CreateJSVM(option);
115     if (data == nullptr || size <= 0) {
116         LOG_ECMA(ERROR) << "illegal input!";
117         return;
118     }
119     int32_t index = 0;
120     size_t maxByteLen = 4;
121     if (size > maxByteLen) {
122         size = maxByteLen;
123     }
124     if (memcpy_s(&index, maxByteLen, data, size) != EOK) {
125         LOG_ECMA(ERROR) << "memcpy_s failed !";
126         UNREACHABLE();
127     }
128     Local<ObjectRef> object = ObjectRef::New(vm);
129     NativePointerCallback callBack = nullptr;
130     object->SetNativePointerField(vm, index, (void *)data, callBack, (void *)data);
131     object->GetOwnPropertyNames(vm);
132     JSNApi::DestroyJSVM(vm);
133 }
134 }
135 
136 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)137 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
138 {
139     // Run your code on data.
140     OHOS::ObjectGetAllPropertyNamesFuzzTest(data, size);
141     OHOS::ObjectGetNativePointerFieldCountFuzzTest(data, size);
142     OHOS::ObjectGetOwnEnumerablePropertyNamesFuzzTest(data, size);
143     OHOS::ObjectGetOwnPropertyNamesFuzzTest(data, size);
144     return 0;
145 }