• 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 "arraywrapperthird_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <iostream>
21 
22 #define private public
23 #include "array_wrapper.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 constexpr char LEFT_BRACE_STRING = '{';
34 }
GetU32Data(const char * ptr)35 uint32_t GetU32Data(const char* ptr)
36 {
37     // convert fuzz input data to an integer
38     return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
39 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)40 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
41 {
42     long longSize = 0;
43     InterfaceID id;
44     std::shared_ptr<Array> array = std::make_shared<Array>(longSize, id);
45     std::string values(data, size);
46     array->ParseDouble(values, longSize);
47     array->ParseChar(values, longSize);
48     array->ParseArray(values, longSize);
49     array->ParseWantParams(values, longSize);
50     IArray* arrayptr = nullptr;
51     std::function<sptr<IInterface>(std::string)> func;
52     array->ParseElement(arrayptr, func, values, longSize);
53     std::string errorString(data, longSize);
54     array->Parse(errorString);
55     errorString.insert(errorString.begin(), String::SIGNATURE);
56     array->Parse(errorString);
57     errorString.insert(errorString.begin(), LEFT_BRACE_STRING);
58     array->Parse(errorString);
59     long lengthSize = (long)(size);
60     array->ParseElement(arrayptr, func, values, lengthSize);
61     std::shared_ptr<Array> otherArray = std::make_shared<Array>(longSize, id);
62     sptr<IInterface> stringValue = String::Box(values);
63     for (size_t i = 0; i < longSize; i++) {
64         otherArray->Set(i, stringValue);
65     }
66     array->IsStringArray(otherArray.get());
67     std::function<void(IInterface*)> function;
68     array->ForEach(otherArray.get(), function);
69     return true;
70 }
71 }
72 
73 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)74 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
75 {
76     /* Run your code on data */
77     if (data == nullptr) {
78         std::cout << "invalid data" << std::endl;
79         return 0;
80     }
81 
82     /* Validate the length of size */
83     if (size < OHOS::U32_AT_SIZE) {
84         return 0;
85     }
86 
87     char* ch = (char *)malloc(size + 1);
88     if (ch == nullptr) {
89         std::cout << "malloc failed." << std::endl;
90         return 0;
91     }
92 
93     (void)memset_s(ch, size + 1, 0x00, size + 1);
94     if (memcpy_s(ch, size, data, size) != EOK) {
95         std::cout << "copy failed." << std::endl;
96         free(ch);
97         ch = nullptr;
98         return 0;
99     }
100 
101     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
102     free(ch);
103     ch = nullptr;
104     return 0;
105 }
106