• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 U32_AT_SIZE = 4;
35 constexpr uint8_t ENABLE = 2;
36 class FreeInstallStatusCallBackStubFuzzTest : public FreeInstallStatusCallBackStub {
37 public:
38     FreeInstallStatusCallBackStubFuzzTest() = default;
39     virtual ~FreeInstallStatusCallBackStubFuzzTest() = default;
OnInstallFinished(int32_t resultCode,const Want & want,int32_t userId)40     void OnInstallFinished(int32_t resultCode, const Want &want, int32_t userId) override
41     {}
42 };
GetU32Data(const char * ptr)43 uint32_t GetU32Data(const char* ptr)
44 {
45     // convert fuzz input data to an integer
46     return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
47 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)48 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
49 {
50     FormUtil formUtil;
51     std::string formName(data, size);
52     int32_t specificationId = static_cast<int32_t>(GetU32Data(data));
53     bool isTemporaryForm = *data % ENABLE;
54     Want want;
55     want.SetParam(Constants::PARAM_FORM_NAME_KEY, formName);
56     want.SetParam(Constants::PARAM_FORM_DIMENSION_KEY, specificationId);
57     want.SetParam(Constants::PARAM_FORM_TEMPORARY_KEY, isTemporaryForm);
58 
59     std::string uri(data, size);
60     int32_t connectId = static_cast<int32_t>(GetU32Data(data));
61     want.SetParam(Constants::FORM_CONNECT_ID, connectId);
62     want.SetParam(Constants::FORM_SUPPLY_INFO, uri);
63 
64     int64_t udidHash = static_cast<int64_t>(GetU32Data(data));
65     formUtil.GenerateFormId(udidHash);
66     uint64_t formId = static_cast<uint64_t>(GetU32Data(data));
67     uint64_t udidHashs = static_cast<uint64_t>(GetU32Data(data));
68     formUtil.PaddingUdidHash(formId, udidHashs);
69     formUtil.GetCurrentNanosecond();
70     formUtil.GetCurrentMillisecond();
71     formUtil.GetCurrentAccountId();
72     formUtil.GetNowMillisecond();
73     std::string strInfo(data, size);
74     int64_t convertValue = static_cast<int64_t>(GetU32Data(data));
75     formUtil.ConvertStringToInt64(strInfo, convertValue);
76     sptr<IRemoteObject> impl = nullptr;
77     FreeInstallStatusCallBackProxy freeInstallStatusCallBackProxy(impl);
78     FreeInstallStatusCallBackStubFuzzTest freeInstallStatusCallBackStubFuzzTest;
79     MessageParcel datas;
80     MessageParcel reply;
81     freeInstallStatusCallBackStubFuzzTest.OnInstallFinishedInner(datas, reply);
82     uint32_t code = static_cast<uint32_t>(GetU32Data(data));
83     MessageOption option;
84     freeInstallStatusCallBackStubFuzzTest.OnRemoteRequest(code, datas, reply, option);
85     return formUtil.GenerateUdidHash(udidHash);
86 }
87 }
88 
89 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)90 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
91 {
92     /* Run your code on data */
93     if (data == nullptr) {
94         return 0;
95     }
96 
97     if (size < OHOS::U32_AT_SIZE) {
98         return 0;
99     }
100 
101     char* ch = static_cast<char*>(malloc(size + 1));
102     if (ch == nullptr) {
103         return 0;
104     }
105 
106     (void)memset_s(ch, size + 1, 0x00, size + 1);
107     if (memcpy_s(ch, size + 1, data, size) != EOK) {
108         free(ch);
109         ch = nullptr;
110         return 0;
111     }
112 
113     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
114     free(ch);
115     ch = nullptr;
116     return 0;
117 }
118 
119