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 "jsnapiinformation_fuzzer.h"
17 #include "ecmascript/base/string_helper.h"
18 #include "ecmascript/napi/include/jsnapi.h"
19 #include "ecmascript/napi/include/dfx_jsnapi.h"
20
21 using namespace panda;
22 using namespace panda::ecmascript;
23
24 namespace OHOS {
JSNApiCheckSecureMemFuzzTest(const uint8_t * data,size_t size)25 void JSNApiCheckSecureMemFuzzTest(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 if (data == nullptr || size <= 0) {
31 LOG_ECMA(ERROR) << "illegal input!";
32 return;
33 }
34 uintptr_t value = reinterpret_cast<uintptr_t>(data);
35 JSNApi::CheckSecureMem(value);
36 JSNApi::DestroyJSVM(vm);
37 }
38
JSNApiCreateEcmaVMFuzzTest(const uint8_t * data,size_t size)39 void JSNApiCreateEcmaVMFuzzTest([[maybe_unused]]const uint8_t *data, size_t size)
40 {
41 RuntimeOption option;
42 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
43 EcmaVM *vm = JSNApi::CreateJSVM(option);
44 if (size <= 0) {
45 return;
46 }
47 JSRuntimeOptions runtimeOptions;
48 EcmaVM *workerVm = JSNApi::CreateEcmaVM(runtimeOptions);
49 JSNApi::DestroyJSVM(workerVm);
50 JSNApi::DestroyJSVM(vm);
51 }
52
JSNApiEnableUserUncaughtErrorHandlerFuzzTest(const uint8_t * data,size_t size)53 void JSNApiEnableUserUncaughtErrorHandlerFuzzTest([[maybe_unused]]const uint8_t *data, size_t size)
54 {
55 RuntimeOption option;
56 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
57 EcmaVM *vm = JSNApi::CreateJSVM(option);
58 if (size <= 0) {
59 return;
60 }
61 JSNApi::EnableUserUncaughtErrorHandler(vm);
62 JSNApi::DestroyJSVM(vm);
63 }
64
JSNApiFunctionFuzzTest(const uint8_t * data,size_t size)65 void JSNApiFunctionFuzzTest([[maybe_unused]]const uint8_t *data, size_t size)
66 {
67 RuntimeOption option;
68 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
69 EcmaVM *vm = JSNApi::CreateJSVM(option);
70 if (size <= 0) {
71 return;
72 }
73 std::function<bool(std::string dirPath, uint8_t * *buff, size_t * buffSize)> cb = [](const std::string &inputPath,
74 uint8_t **buff, size_t *buffSize) -> bool {
75 if (inputPath.empty() || buff == nullptr || buffSize == nullptr) {
76 return false;
77 }
78 return false;
79 };
80 JSNApi::DestroyJSVM(vm);
81 }
82
JSNApiIsMixedDebugEnabledFuzzTest(const uint8_t * data,size_t size)83 void JSNApiIsMixedDebugEnabledFuzzTest([[maybe_unused]]const uint8_t *data, size_t size)
84 {
85 RuntimeOption option;
86 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
87 EcmaVM *vm = JSNApi::CreateJSVM(option);
88 if (size <= 0) {
89 return;
90 }
91 JSNApi::IsMixedDebugEnabled(vm);
92 JSNApi::DestroyJSVM(vm);
93 }
94
JSNApiNotifyNativeCallingFuzzTest(const uint8_t * data,size_t size)95 void JSNApiNotifyNativeCallingFuzzTest(const uint8_t *data, size_t size)
96 {
97 RuntimeOption option;
98 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
99 EcmaVM *vm = JSNApi::CreateJSVM(option);
100 if (data == nullptr || size <= 0) {
101 LOG_ECMA(ERROR) << "illegal input!";
102 return;
103 }
104 JSNApi::NotifyNativeCalling(vm, (void *)data);
105 JSNApi::DestroyJSVM(vm);
106 }
107 }
108
109 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)110 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
111 {
112 // Run your code on data.
113 OHOS::JSNApiCheckSecureMemFuzzTest(data, size);
114 OHOS::JSNApiCreateEcmaVMFuzzTest(data, size);
115 OHOS::JSNApiEnableUserUncaughtErrorHandlerFuzzTest(data, size);
116 OHOS::JSNApiFunctionFuzzTest(data, size);
117 OHOS::JSNApiIsMixedDebugEnabledFuzzTest(data, size);
118 OHOS::JSNApiNotifyNativeCallingFuzzTest(data, size);
119 return 0;
120 }