• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "operationsecond_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <iostream>
21 
22 #define private public
23 #include "operation.h"
24 #undef private
25 #include "securec.h"
26 #include "uri.h"
27 
28 using namespace OHOS::AAFwk;
29 
30 namespace OHOS {
31 namespace {
32 constexpr size_t U32_AT_SIZE = 4;
33 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)34 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
35 {
36     std::shared_ptr<Operation> operation = std::make_shared<Operation>();
37     std::shared_ptr<Operation> otherOperation = std::make_shared<Operation>();
38     std::string values(data, size);
39     otherOperation->SetBundleName(values);
40     if (*operation == *otherOperation) {};
41     operation = std::make_shared<Operation>(*otherOperation);
42     otherOperation->SetDeviceId(values);
43     if (*operation == *otherOperation) {};
44     operation = std::make_shared<Operation>(*otherOperation);
45     otherOperation->SetModuleName(values);
46     if (*operation == *otherOperation) {};
47     operation = std::make_shared<Operation>(*otherOperation);
48     std::vector<std::string> otherEntities;
49     otherEntities.insert(otherEntities.begin(), values);
50     otherOperation->SetEntities(otherEntities);
51     if (*operation == *otherOperation) {};
52     operation = std::make_shared<Operation>(*otherOperation);
53     operation->SetEntities(otherEntities);
54     if (*operation == *otherOperation) {};
55     operation = std::make_shared<Operation>(*otherOperation);
56     otherOperation->SetFlags(U32_AT_SIZE);
57     if (*operation == *otherOperation) {};
58     operation = std::make_shared<Operation>(*otherOperation);
59     Uri otherUri(values);
60     otherOperation->SetUri(otherUri);
61     if (*operation == *otherOperation) {};
62     if (*otherOperation == *otherOperation) {};
63     operation = std::make_shared<Operation>(*otherOperation);
64     std::string entity(data, size);
65     entity.append(values);
66     otherEntities.insert(otherEntities.begin(), entity);
67     otherOperation->SetEntities(otherEntities);
68     std::vector<std::string> entities = otherEntities;
69     entities[0] = values;
70     operation->SetEntities(entities);
71     if (*operation == *otherOperation) {};
72     Parcel parcel;
73     operation->Marshalling(parcel);
74     operation->ReadFromParcel(parcel);
75     operation->Unmarshalling(parcel);
76     operation->GetUri(otherUri);
77     operation->DumpInfo(size);
78     return true;
79 }
80 }
81 
82 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)83 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
84 {
85     /* Run your code on data */
86     if (data == nullptr) {
87         std::cout << "invalid data" << std::endl;
88         return 0;
89     }
90 
91     /* Validate the length of size */
92     if (size < OHOS::U32_AT_SIZE) {
93         return 0;
94     }
95 
96     char* ch = (char *)malloc(size + 1);
97     if (ch == nullptr) {
98         std::cout << "malloc failed." << std::endl;
99         return 0;
100     }
101 
102     (void)memset_s(ch, size + 1, 0x00, size + 1);
103     if (memcpy_s(ch, size, data, size) != EOK) {
104         std::cout << "copy failed." << std::endl;
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