1 /*
2 * Copyright (c) 2022-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 "cmdserviceprocessdelclient_fuzzer.h"
17 #include <string>
18 #include "securec.h"
19 #include "control_fd.h"
20
21 namespace OHOS {
22 const uint8_t *BASE_DATA = nullptr;
23 size_t g_baseSize = 0;
24 size_t g_basePos = 0;
25
GetData()26 template <class T> T GetData()
27 {
28 T object{};
29 size_t objectSize = sizeof(object);
30 if ((BASE_DATA == nullptr) || (objectSize > g_baseSize - g_basePos)) {
31 return object;
32 }
33 errno_t ret = memcpy_s(&object, objectSize, BASE_DATA + g_basePos, objectSize);
34 if (ret != EOK) {
35 return {};
36 }
37 g_basePos += objectSize;
38 return object;
39 }
40
FuzzCmdServiceProcessDelClient(const uint8_t * data,size_t size)41 bool FuzzCmdServiceProcessDelClient(const uint8_t* data, size_t size)
42 {
43 BASE_DATA = data;
44 g_baseSize = size;
45 g_basePos = 0;
46 if (size > sizeof(pid_t)) {
47 pid_t pid = GetData<pid_t>();
48 CmdServiceProcessDelClient(pid);
49 }
50 return true;
51 }
52 }
53
54 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)55 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
56 {
57 /* Run your code on data */
58 OHOS::FuzzCmdServiceProcessDelClient(data, size);
59 return 0;
60 }
61