1 /*
2 * Copyright (c) 2022 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 "formutil_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20
21 #include "form_constants.h"
22 #include "form_util.h"
23 #define private public
24 #define protected public
25 #include "free_install_status_callback_proxy.h"
26 #include "free_install_status_callback_stub.h"
27 #undef private
28 #undef protected
29 #include "securec.h"
30
31 using namespace OHOS::AppExecFwk;
32
33 namespace OHOS {
34 constexpr size_t FOO_MAX_LEN = 1024;
35 constexpr size_t U32_AT_SIZE = 4;
36 constexpr uint8_t ENABLE = 2;
37 class FreeInstallStatusCallBackStubFuzzTest : public FreeInstallStatusCallBackStub {
38 public:
39 FreeInstallStatusCallBackStubFuzzTest() = default;
40 virtual ~FreeInstallStatusCallBackStubFuzzTest() = default;
OnInstallFinished(int32_t resultCode,const Want & want,int32_t userId)41 void OnInstallFinished(int32_t resultCode, const Want &want, int32_t userId) override
42 {}
43 };
GetU32Data(const char * ptr)44 uint32_t GetU32Data(const char* ptr)
45 {
46 // convert fuzz input data to an integer
47 return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
48 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)49 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
50 {
51 FormUtil formUtil;
52 std::string formName(data, size);
53 int32_t specificationId = static_cast<int32_t>(GetU32Data(data));
54 bool isTemporaryForm = *data % ENABLE;
55 Want want;
56 want.SetParam(Constants::PARAM_FORM_NAME_KEY, formName);
57 want.SetParam(Constants::PARAM_FORM_DIMENSION_KEY, specificationId);
58 want.SetParam(Constants::PARAM_FORM_TEMPORARY_KEY, isTemporaryForm);
59
60 std::string uri(data, size);
61 int32_t connectId = static_cast<int32_t>(GetU32Data(data));
62 want.SetParam(Constants::FORM_CONNECT_ID, connectId);
63 want.SetParam(Constants::FORM_SUPPLY_INFO, uri);
64
65 int64_t udidHash = static_cast<int64_t>(GetU32Data(data));
66 formUtil.GenerateFormId(udidHash);
67 uint64_t formId = static_cast<uint64_t>(GetU32Data(data));
68 uint64_t udidHashs = static_cast<uint64_t>(GetU32Data(data));
69 formUtil.PaddingUdidHash(formId, udidHashs);
70 formUtil.GetCurrentNanosecond();
71 formUtil.GetCurrentMillisecond();
72 formUtil.GetCurrentAccountId();
73 formUtil.GetNowMillisecond();
74 std::string strInfo(data, size);
75 int64_t convertValue = static_cast<int64_t>(GetU32Data(data));
76 formUtil.ConvertStringToInt64(strInfo, convertValue);
77 sptr<IRemoteObject> impl = nullptr;
78 FreeInstallStatusCallBackProxy freeInstallStatusCallBackProxy(impl);
79 FreeInstallStatusCallBackStubFuzzTest freeInstallStatusCallBackStubFuzzTest;
80 MessageParcel datas;
81 MessageParcel reply;
82 freeInstallStatusCallBackStubFuzzTest.OnInstallFinishedInner(datas, reply);
83 uint32_t code = static_cast<uint32_t>(GetU32Data(data));
84 MessageOption option;
85 freeInstallStatusCallBackStubFuzzTest.OnRemoteRequest(code, datas, reply, option);
86 return formUtil.GenerateUdidHash(udidHash);
87 }
88 }
89
90 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)91 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
92 {
93 /* Run your code on data */
94 if (data == nullptr) {
95 return 0;
96 }
97
98 if (size < OHOS::U32_AT_SIZE) {
99 return 0;
100 }
101
102 /* Validate the length of size */
103 if (size == 0 || size > OHOS::FOO_MAX_LEN) {
104 return 0;
105 }
106
107 char* ch = static_cast<char*>(malloc(size + 1));
108 if (ch == nullptr) {
109 return 0;
110 }
111
112 (void)memset_s(ch, size + 1, 0x00, size + 1);
113 if (memcpy_s(ch, size + 1, data, size) != EOK) {
114 free(ch);
115 ch = nullptr;
116 return 0;
117 }
118
119 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
120 free(ch);
121 ch = nullptr;
122 return 0;
123 }
124
125