• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "processwatchermessage_fuzzer.h"
16 #include <string>
17 #include <memory>
18 #include "init_utils.h"
19 #include "securec.h"
20 #define private public
21 #include "watcher_manager.h"
22 #undef private
23 using namespace OHOS::init_param;
24 
25 namespace OHOS {
FuzzProcessWatcherMessage(const uint8_t * data,size_t size)26 bool FuzzProcessWatcherMessage(const uint8_t* data, size_t size)
27     {
28         constexpr size_t minsize = sizeof(int) + sizeof(uint32_t) + 1;
29         if (size < minsize) {
30             return false;
31         }
32 
33         int type = 0;
34         if (memcpy_s(&type, sizeof(int), data, sizeof(int)) != 0) {
35             return false;
36         }
37 
38         uint32_t msgSize = 0;
39         if (memcpy_s(&msgSize, sizeof(uint32_t), data + sizeof(int), sizeof(uint32_t)) != 0) {
40             return false;
41         }
42 
43         const char* name = reinterpret_cast<const char*>(data + sizeof(int) + sizeof(uint32_t));
44         size_t namelen = size - sizeof(int) - sizeof(uint32_t);
45 
46         std::unique_ptr<char[]> safename = std::make_unique<char[]>(namelen + 1);
47         if (memcpy_s(safename.get(), namelen + 1, name, namelen) != 0) {
48             return false;
49         }
50         safename[namelen] = '\0';
51 
52         ParamMessage* message = CreateParamMessage(type, safename.get(), msgSize);
53         if (message == nullptr) {
54             return false;
55         }
56         std::unique_ptr<WatcherManager> watcherManager = std::make_unique<WatcherManager>(0, true);
57         watcherManager->ProcessWatcherMessage(message);
58         free(message);
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::FuzzProcessWatcherMessage(data, size);
68     return 0;
69 }