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 "wantseventh_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <iostream>
21
22 #define private public
23 #include "want.h"
24 #undef private
25 #include "securec.h"
26
27 using namespace OHOS::AAFwk;
28
29 namespace OHOS {
30 namespace {
31 constexpr size_t U32_AT_SIZE = 4;
32 constexpr uint8_t ENABLE = 2;
33 }
GetU32Data(const char * ptr)34 uint32_t GetU32Data(const char* ptr)
35 {
36 // convert fuzz input data to an integer
37 return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
38 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)39 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
40 {
41 std::shared_ptr<Want> want = std::make_shared<Want>();
42 unsigned int flags = static_cast<unsigned int>(GetU32Data(data));
43 want->AddFlags(flags);
44 want->GetFlags();
45 std::string bundleName(data, size);
46 std::string abilityName(data, size);
47 want->SetElementName(bundleName, abilityName);
48 want->GetElement();
49 want->GetEntities();
50 want->GetBundle();
51 std::string type(data, size);
52 want->SetType(type);
53 want->GetType();
54 std::string action(data, size);
55 want->SetAction(action);
56 want->GetAction();
57 WantParams wantparams;
58 want->SetParams(wantparams);
59 want->GetParams();
60 std::string key(data, size);
61 bool state = *data % ENABLE;
62 want->SetParam(key, state);
63 want->GetBoolParam(key, state);
64 int iValue = static_cast<int>(GetU32Data(data));
65 want->GetIntParam(key, iValue);
66 want->SetParam(key, iValue);
67 long lValue = static_cast<long>(GetU32Data(data));
68 want->GetLongParam(key, lValue);
69 want->SetParam(key, lValue);
70 long long llValue = static_cast<long long>(GetU32Data(data));
71 want->SetParam(key, llValue);
72 want->GetStringParam(key);
73 want->GetStringArrayParam(key);
74 return true;
75 }
76 }
77
78 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)79 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
80 {
81 /* Run your code on data */
82 if (data == nullptr) {
83 std::cout << "invalid data" << std::endl;
84 return 0;
85 }
86
87 /* Validate the length of size */
88 if (size < OHOS::U32_AT_SIZE) {
89 return 0;
90 }
91
92 char* ch = reinterpret_cast<char *>(malloc(size + 1));
93 if (ch == nullptr) {
94 std::cout << "malloc failed." << std::endl;
95 return 0;
96 }
97
98 (void)memset_s(ch, size + 1, 0x00, size + 1);
99 if (memcpy_s(ch, size, data, size) != EOK) {
100 std::cout << "copy failed." << std::endl;
101 free(ch);
102 ch = nullptr;
103 return 0;
104 }
105
106 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
107 free(ch);
108 ch = nullptr;
109 return 0;
110 }
111