• 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_util.h"
22 #define private public
23 #define protected public
24 #include "free_install_status_callback_proxy.h"
25 #include "free_install_status_callback_stub.h"
26 #undef private
27 #undef protected
28 #include "securec.h"
29 
30 using namespace OHOS::AppExecFwk;
31 
32 namespace OHOS {
33 constexpr size_t FOO_MAX_LEN = 1024;
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     formUtil.CreateFormWant(formName, specificationId, isTemporaryForm, want);
56     std::string uri(data, size);
57     int32_t connectId = static_cast<int32_t>(GetU32Data(data));
58     formUtil.CreateDefaultFormWant(want, uri, connectId);
59     formUtil.GenerateUdid();
60     int64_t udidHash = static_cast<int64_t>(GetU32Data(data));
61     formUtil.GenerateFormId(udidHash);
62     uint64_t formId = static_cast<uint64_t>(GetU32Data(data));
63     uint64_t udidHashs = static_cast<uint64_t>(GetU32Data(data));
64     formUtil.PaddingUdidHash(formId, udidHashs);
65     formUtil.GetCurrentNanosecond();
66     formUtil.GetCurrentMillisecond();
67     formUtil.GetCurrentAccountId();
68     sptr<IRemoteObject> impl = nullptr;
69     FreeInstallStatusCallBackProxy freeInstallStatusCallBackProxy(impl);
70     FreeInstallStatusCallBackStubFuzzTest freeInstallStatusCallBackStubFuzzTest;
71     MessageParcel datas;
72     MessageParcel reply;
73     freeInstallStatusCallBackStubFuzzTest.OnInstallFinishedInner(datas, reply);
74     uint32_t code = static_cast<uint32_t>(GetU32Data(data));
75     MessageOption option;
76     freeInstallStatusCallBackStubFuzzTest.OnRemoteRequest(code, datas, reply, option);
77     return formUtil.GenerateUdidHash(udidHash);
78 }
79 }
80 
81 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)82 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
83 {
84     /* Run your code on data */
85     if (data == nullptr) {
86         return 0;
87     }
88 
89     if (size < OHOS::U32_AT_SIZE) {
90         return 0;
91     }
92 
93     /* Validate the length of size */
94     if (size == 0 || size > OHOS::FOO_MAX_LEN) {
95         return 0;
96     }
97 
98     char* ch = (char *)malloc(size + 1);
99     if (ch == nullptr) {
100         return 0;
101     }
102 
103     (void)memset_s(ch, size + 1, 0x00, size + 1);
104     if (memcpy_s(ch, size, data, size) != EOK) {
105         free(ch);
106         ch = nullptr;
107         return 0;
108     }
109 
110     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
111     free(ch);
112     ch = nullptr;
113     return 0;
114 }
115 
116