• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 constexpr size_t U32_AT_SIZE = 4;
27 constexpr uint8_t ENABLE = 2;
DoSomethingInterestingWithMyAPI(const char * data,size_t size)28 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
29 {
30     std::string name = std::string(data, size);
31     int64_t time = reinterpret_cast<uintptr_t>(data);
32     int32_t startCount = 0;
33     AgingBundleInfo agingBundleInfo{name, time, startCount};
34     agingBundleInfo.GetBundleName();
35     agingBundleInfo.GetRecentlyUsedTime();
36     agingBundleInfo.GetStartCount();
37     return true;
38 }
39 } // namespace OHOS
40 
41 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)42 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
43 {
44     /* Run your code on data */
45     if (data == nullptr) {
46         return 0;
47     }
48 
49     /* Validate the length of size */
50     if (size < OHOS::U32_AT_SIZE) {
51         return 0;
52     }
53 
54     char* ch = (char*)malloc(size + 1);
55     if (ch == nullptr) {
56         std::cout << "malloc failed." << std::endl;
57         return 0;
58     }
59 
60     (void)memset_s(ch, size + 1, 0x00, size + 1);
61     if (memcpy_s(ch, size + 1, data, size) != EOK) {
62         std::cout << "copy failed." << std::endl;
63         free(ch);
64         ch = nullptr;
65         return 0;
66     }
67 
68     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
69     free(ch);
70     ch = nullptr;
71     return 0;
72 }