1 /*
2 * Copyright (c) 2025 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 <unistd.h>
17 #include <fcntl.h>
18 #include <string>
19 #include <memory>
20 #include "dump_fuzzer.h"
21 #include "init_utils.h"
22 #include "securec.h"
23 #define protected public
24 #include "watcher_manager.h"
25 #undef private
26 using namespace OHOS::init_param;
27
28 namespace OHOS {
ParseFuzzDataToArgs(const uint8_t * data,size_t size)29 std::vector<std::u16string> ParseFuzzDataToArgs(const uint8_t* data, size_t size)
30 {
31 std::vector<std::u16string> args;
32 if (size == 0) return args;
33
34 std::string input(reinterpret_cast<const char*>(data), size);
35 size_t start = 0;
36 size_t end = input.find('\n');
37 while (end != std::string::npos) {
38 std::string token = input.substr(start, end - start);
39 if (!token.empty()) {
40 args.emplace_back(token.begin(), token.end());
41 }
42 start = end + 1;
43 end = input.find('\n', start);
44 }
45 if (start < input.size()) {
46 std::string token = input.substr(start);
47 args.emplace_back(token.begin(), token.end());
48 }
49 return args;
50 }
51
FuzzAddDUMP(const uint8_t * data,size_t size)52 bool FuzzAddDUMP(const uint8_t* data, size_t size)
53 {
54 std::unique_ptr<WatcherManager> watcherManager = std::make_unique<WatcherManager>(0, true);
55 int fd = open("/dev/null", O_WRONLY);
56 std::vector<std::u16string> args = ParseFuzzDataToArgs(data, size);
57 watcherManager->Dump(fd, args);
58 close(fd);
59 return true;
60 }
61 }
62
63 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)64 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
65 {
66 /* Run your code on data */
67 OHOS::FuzzAddDUMP(data, size);
68 return 0;
69 }