• 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 #include "abilitymgrpreloaduiextstateobserver_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include "securec.h"
21 #include <iostream>
22 #define private public
23 #include "preload_uiext_state_observer.h"
24 #undef private
25 
26 using namespace OHOS::AAFwk;
27 using namespace OHOS::AppExecFwk;
28 
29 namespace OHOS {
30 namespace {
31 constexpr size_t U32_AT_SIZE = 4;
32 } // namespace
33 
DoSomethingInterestingWithMyAPI(const char * data,size_t size)34 bool DoSomethingInterestingWithMyAPI(const char *data, size_t size)
35 {
36     std::weak_ptr<OHOS::AbilityRuntime::ExtensionRecord> extensionRecord;
37     std::shared_ptr<PreLoadUIExtStateObserver> preLoadUIExtStateObserver =
38         std::make_shared<PreLoadUIExtStateObserver>(extensionRecord);
39     ProcessData processData;
40     preLoadUIExtStateObserver-> OnProcessDied(processData);
41     return true;
42 }
43 } // namespace OHOS
44 
45 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)46 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
47 {
48     /* Run your code on data */
49     if (data == nullptr) {
50         std::cout << "invalid data" << std::endl;
51         return 0;
52     }
53 
54     /* Validate the length of size */
55     if (size < OHOS::U32_AT_SIZE) {
56         return 0;
57     }
58 
59     char *ch = (char *)malloc(size + 1);
60     if (ch == nullptr) {
61         std::cout << "malloc failed." << std::endl;
62         return 0;
63     }
64 
65     (void)memset_s(ch, size + 1, 0x00, size + 1);
66     if (memcpy_s(ch, size + 1, data, size) != EOK) {
67         std::cout << "copy failed." << std::endl;
68         free(ch);
69         ch = nullptr;
70         return 0;
71     }
72 
73     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
74     free(ch);
75     ch = nullptr;
76     return 0;
77 }