• 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 <fuzzer/FuzzedDataProvider.h>
17 #include "promiserejectinfo_fuzzer.h"
18 #include "ecmascript/base/string_helper.h"
19 #include "common_components/base/utf_helper.h"
20 #include "ecmascript/napi/include/jsnapi.h"
21 
22 using namespace panda;
23 using namespace panda::ecmascript;
24 using namespace common::utf_helper;
25 
26 namespace OHOS {
PromiseRejectInfoNewFuzzTest(const uint8_t * data,size_t size)27     void PromiseRejectInfoNewFuzzTest(const uint8_t* data, size_t size)
28     {
29         FuzzedDataProvider fdp(data, size);
30         RuntimeOption option;
31         option.SetLogLevel(common::LOG_LEVEL::ERROR);
32         EcmaVM *vm = JSNApi::CreateJSVM(option);
33         std::string str = fdp.ConsumeRandomLengthString(1024);
34         Local<StringRef> promiseStirng = StringRef::NewFromUtf8(vm, str.data());
35         Local<JSValueRef> promise(promiseStirng);
36         Local<StringRef> reasonString = StringRef::NewFromUtf8(vm, str.data());
37         Local<JSValueRef> reason(reasonString);
38         void *data2 = static_cast<void*>(new std::string("test"));
39         [[maybe_unused]]PromiseRejectInfo promiseReject(promise, reason,
40             PromiseRejectInfo::PROMISE_REJECTION_EVENT::REJECT, data2);
41         JSNApi::DestroyJSVM(vm);
42     }
43 
GetDataFuzzTest(const uint8_t * data,size_t size)44     void GetDataFuzzTest(const uint8_t* data, size_t size)
45     {
46         FuzzedDataProvider fdp(data, size);
47         RuntimeOption option;
48         option.SetLogLevel(common::LOG_LEVEL::ERROR);
49         EcmaVM *vm = JSNApi::CreateJSVM(option);
50         std::string str = fdp.ConsumeRandomLengthString(1024);
51         Local<StringRef> promiseStirng = StringRef::NewFromUtf8(vm, str.data());
52         Local<JSValueRef> promise(promiseStirng);
53         Local<StringRef> reasonString = StringRef::NewFromUtf8(vm, str.data());
54         Local<JSValueRef> reason(reasonString);
55         void *data2 = static_cast<void*>(new std::string("test"));
56         PromiseRejectInfo promiseReject(promise, reason,
57             PromiseRejectInfo::PROMISE_REJECTION_EVENT::REJECT, data2);
58         promiseReject.GetData();
59         JSNApi::DestroyJSVM(vm);
60     }
61 
PromiseRejectInfoGetPromiseFuzzTest(const uint8_t * data,size_t size)62     void PromiseRejectInfoGetPromiseFuzzTest(const uint8_t* data, size_t size)
63     {
64         FuzzedDataProvider fdp(data, size);
65         RuntimeOption option;
66         option.SetLogLevel(common::LOG_LEVEL::ERROR);
67         EcmaVM *vm = JSNApi::CreateJSVM(option);
68         std::string str = fdp.ConsumeRandomLengthString(1024);
69         Local<StringRef> promiseStirng = StringRef::NewFromUtf8(vm, str.data());
70         Local<JSValueRef> promise(promiseStirng);
71         Local<StringRef> reasonString = StringRef::NewFromUtf8(vm, str.data());
72         Local<JSValueRef> reason(reasonString);
73         void *newdata = static_cast<void*>(new std::string("test"));
74         PromiseRejectInfo promiseReject(promise, reason, PromiseRejectInfo::PROMISE_REJECTION_EVENT::REJECT, newdata);
75         promiseReject.GetPromise();
76         JSNApi::DestroyJSVM(vm);
77     }
78 
PromiseRejectInfoGetReasonFuzzTest(const uint8_t * data,size_t size)79     void PromiseRejectInfoGetReasonFuzzTest(const uint8_t* data, size_t size)
80     {
81         FuzzedDataProvider fdp(data, size);
82         RuntimeOption option;
83         option.SetLogLevel(common::LOG_LEVEL::ERROR);
84         EcmaVM *vm = JSNApi::CreateJSVM(option);
85         std::string str = fdp.ConsumeRandomLengthString(1024);
86         Local<StringRef> promiseStirng = StringRef::NewFromUtf8(vm, str.data());
87         Local<JSValueRef> promise(promiseStirng);
88         Local<StringRef> reasonString = StringRef::NewFromUtf8(vm, str.data());
89         Local<JSValueRef> reason(reasonString);
90         void *data2 = static_cast<void*>(new std::string("test"));
91         PromiseRejectInfo promiseReject(promise, reason, PromiseRejectInfo::PROMISE_REJECTION_EVENT::REJECT, data2);
92         [[maybe_unused]]Local<JSValueRef> reason_res = promiseReject.GetReason();
93         JSNApi::DestroyJSVM(vm);
94     }
95 
PromiseRejectInfoGetOperationFuzzTest(const uint8_t * data,size_t size)96     void PromiseRejectInfoGetOperationFuzzTest(const uint8_t* data, size_t size)
97     {
98         FuzzedDataProvider fdp(data, size);
99         RuntimeOption option;
100         option.SetLogLevel(common::LOG_LEVEL::ERROR);
101         EcmaVM *vm = JSNApi::CreateJSVM(option);
102         std::string str = fdp.ConsumeRandomLengthString(1024);
103         Local<StringRef> promiseStirng = StringRef::NewFromUtf8(vm, str.data());
104         Local<JSValueRef> promise(promiseStirng);
105         Local<StringRef> reasonString = StringRef::NewFromUtf8(vm, str.data());
106         Local<JSValueRef> reason(reasonString);
107         void *data2 = static_cast<void*>(new std::string("test"));
108         PromiseRejectInfo promiseReject(promise, reason, PromiseRejectInfo::PROMISE_REJECTION_EVENT::REJECT, data2);
109         promiseReject.GetOperation();
110         JSNApi::DestroyJSVM(vm);
111     }
112 }
113 
114 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)115 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
116 {
117     // Run your code on data.
118     OHOS::PromiseRejectInfoNewFuzzTest(data, size);
119     OHOS::GetDataFuzzTest(data, size);
120     OHOS::PromiseRejectInfoGetPromiseFuzzTest(data, size);
121     OHOS::PromiseRejectInfoGetReasonFuzzTest(data, size);
122     OHOS::PromiseRejectInfoGetOperationFuzzTest(data, size);
123     return 0;
124 }