1 /*
2  * Copyright (c) 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 #define private public
17 #include "agingutil_fuzzer.h"
18 
19 #include "aging/aging_constants.h"
20 #include "aging_bundle_info.h"
21 #include "aging_util.h"
22 #include "securec.h"
23 
24 using namespace OHOS::AppExecFwk;
25 namespace OHOS {
26 
27 constexpr size_t FOO_MAX_LEN = 1024;
28 constexpr size_t U32_AT_SIZE = 4;
29 constexpr uint8_t ENABLE = 2;
DoSomethingInterestingWithMyAPI(const char * data,size_t size)30 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
31 {
32     AgingBundleInfo agingBundleInfo;
33     agingBundleInfo.GetBundleName();
34     agingBundleInfo.GetRecentlyUsedTime();
35     agingBundleInfo.GetStartCount();
36     return true;
37 }
38 } // namespace OHOS
39 
40 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)41 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
42 {
43     /* Run your code on data */
44     if (data == nullptr) {
45         return 0;
46     }
47 
48     /* Validate the length of size */
49     if (size < OHOS::U32_AT_SIZE || size > OHOS::FOO_MAX_LEN) {
50         return 0;
51     }
52 
53     char* ch = (char*)malloc(size + 1);
54     if (ch == nullptr) {
55         std::cout << "malloc failed." << std::endl;
56         return 0;
57     }
58 
59     (void)memset_s(ch, size + 1, 0x00, size + 1);
60     if (memcpy_s(ch, size + 1, data, size) != EOK) {
61         std::cout << "copy failed." << std::endl;
62         free(ch);
63         ch = nullptr;
64         return 0;
65     }
66 
67     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
68     free(ch);
69     ch = nullptr;
70     return 0;
71 }