• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2024 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 "jsnapiset_fuzzer.h"
17 #include "ecmascript/base/string_helper.h"
18 #include "ecmascript/ecma_vm.h"
19 #include "ecmascript/napi/include/dfx_jsnapi.h"
20 #include "ecmascript/napi/include/jsnapi.h"
21 
22 using namespace panda;
23 using namespace panda::ecmascript;
24 
25 namespace OHOS {
JSNApiSetAssetPathFuzzTest(const uint8_t * data,size_t size)26 void JSNApiSetAssetPathFuzzTest([[maybe_unused]]const uint8_t *data, size_t size)
27 {
28     RuntimeOption option;
29     option.SetLogLevel(common::LOG_LEVEL::ERROR);
30     EcmaVM *vm = JSNApi::CreateJSVM(option);
31     if (size <= 0) {
32         return;
33     }
34     std::string str = "/data/storage/el1/bundle/";
35     str += std::string((char *)data, size);
36     str += "/ets/modules.abc";
37     JSNApi::SetAssetPath(vm, str);
38     JSNApi::DestroyJSVM(vm);
39 }
40 
JSNApiSetBundleFuzzTest(const uint8_t * data,size_t size)41 void JSNApiSetBundleFuzzTest(const uint8_t *data, size_t size)
42 {
43     RuntimeOption option;
44     option.SetLogLevel(common::LOG_LEVEL::ERROR);
45     EcmaVM *vm = JSNApi::CreateJSVM(option);
46     if (data == nullptr || size <= 0) {
47         LOG_ECMA(ERROR) << "illegal input!";
48         return;
49     }
50     uint8_t *ptr = nullptr;
51     ptr = const_cast<uint8_t *>(data);
52     JSNApi::SetBundle(vm, (*data & size) > 0 ? true : false);
53     JSNApi::DestroyJSVM(vm);
54 }
55 
JSNApiSetHostEnqueueJobFuzzTest(const uint8_t * data,size_t size)56 void JSNApiSetHostEnqueueJobFuzzTest(const uint8_t *data, size_t size)
57 {
58     RuntimeOption option;
59     option.SetLogLevel(common::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     Local<JSValueRef> key = StringRef::NewFromUtf8(vm, (char *)data, (int)size);
66     JSNApi::SetHostEnqueueJob(vm, key);
67     JSNApi::DestroyJSVM(vm);
68 }
69 
JSNApiSetMockModuleListFuzzTest(const uint8_t * data,size_t size)70 void JSNApiSetMockModuleListFuzzTest(const uint8_t *data, size_t size)
71 {
72     RuntimeOption option;
73     option.SetLogLevel(common::LOG_LEVEL::ERROR);
74     EcmaVM *vm = JSNApi::CreateJSVM(option);
75     if (size <= 0) {
76         return;
77     }
78     std::map<std::string, std::string> str = { { (char *)data, "20" } };
79     JSNApi::SetMockModuleList(vm, str);
80     JSNApi::DestroyJSVM(vm);
81 }
82 }
83 
84 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)85 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
86 {
87     // Run your code on data.
88     OHOS::JSNApiSetBundleFuzzTest(data, size);
89     OHOS::JSNApiSetHostEnqueueJobFuzzTest(data, size);
90     OHOS::JSNApiSetMockModuleListFuzzTest(data, size);
91     return 0;
92 }