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 "wantfifth_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 #include "string_wrapper.h"
27
28 using namespace OHOS::AAFwk;
29
30 namespace OHOS {
31 namespace {
32 constexpr size_t U32_AT_SIZE = 4;
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 }
39
DoSomethingInterestingWithMyAPI(const char * data,size_t size)40 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
41 {
42 std::shared_ptr<Want> want = std::make_shared<Want>();
43 std::string key(data, size);
44 std::vector<zchar> charVector;
45 want->SetParam(key, charVector);
46 want->GetCharArrayParam(key);
47 std::vector<int> intVector;
48 want->SetParam(key, intVector);
49 want->GetIntArrayParam(key);
50 double doubleValue = 0.0;
51 want->SetParam(key, doubleValue);
52 want->GetDoubleParam(key, doubleValue);
53 std::vector<double> doubleVector;
54 want->SetParam(key, doubleVector);
55 want->GetDoubleArrayParam(key);
56 float floatValue = 0.0;
57 want->SetParam(key, floatValue);
58 want->GetFloatParam(key, floatValue);
59 std::string action(data, size);
60 want->SetAction(action);
61 std::string uri(data, size);
62 want->SetUri(uri);
63 std::vector<std::string> entities;
64 entities.push_back(key);
65 want->SetEntities(entities);
66 std::string deviceId(data, size);
67 want->SetDeviceId(deviceId);
68 std::string bundleName(data, size);
69 std::string abilityName(data, size);
70 want->SetElementName(bundleName, abilityName);
71 unsigned int flags = static_cast<unsigned int>(GetU32Data(data));
72 want->SetFlags(flags);
73 std::string uriString(data, size);
74 want->ToUriStringInner(uriString);
75 return true;
76 }
77 }
78
79 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)80 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
81 {
82 /* Run your code on data */
83 if (data == nullptr) {
84 std::cout << "invalid data" << std::endl;
85 return 0;
86 }
87
88 /* Validate the length of size */
89 if (size < OHOS::U32_AT_SIZE) {
90 return 0;
91 }
92
93 char* ch = reinterpret_cast<char *>(malloc(size + 1));
94 if (ch == nullptr) {
95 std::cout << "malloc failed." << std::endl;
96 return 0;
97 }
98
99 (void)memset_s(ch, size + 1, 0x00, size + 1);
100 if (memcpy_s(ch, size, data, size) != EOK) {
101 std::cout << "copy failed." << std::endl;
102 free(ch);
103 ch = nullptr;
104 return 0;
105 }
106
107 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
108 free(ch);
109 ch = nullptr;
110 return 0;
111 }
112