• 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 "arraywrapperfifth_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 #include "want_params_wrapper.h"
28 
29 using namespace OHOS::AAFwk;
30 
31 namespace OHOS {
32 namespace {
33 constexpr size_t U32_AT_SIZE = 4;
34 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)35 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
36 {
37     long longSize = (long)(size);
38     InterfaceID id;
39     std::shared_ptr<Array> array = std::make_shared<Array>(longSize, id);
40     std::string value(data, size);
41     sptr<IInterface> stringValue = String::Box(value);
42     for (size_t i = 0; i < longSize; i++) {
43         array->Set(i, stringValue);
44     }
45     array->ToString();
46     array->typeId_ = g_IID_IString;
47     array->ToString();
48     array->typeId_ = g_IID_IBoolean;
49     array->ToString();
50     array->typeId_ = g_IID_IByte;
51     array->ToString();
52     array->typeId_ = g_IID_IShort;
53     array->ToString();
54     array->typeId_ = g_IID_IInteger;
55     array->ToString();
56     array->typeId_ = g_IID_ILong;
57     array->ToString();
58     array->typeId_ = g_IID_IFloat;
59     array->ToString();
60     array->typeId_ = g_IID_IDouble;
61     array->ToString();
62     array->typeId_ = g_IID_IArray;
63     array->ToString();
64     array->typeId_ = g_IID_IChar;
65     array->ToString();
66     array->typeId_ = g_IID_IWantParams;
67     array->ToString();
68     return true;
69 }
70 }
71 
72 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)73 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
74 {
75     /* Run your code on data */
76     if (data == nullptr) {
77         std::cout << "invalid data" << std::endl;
78         return 0;
79     }
80 
81     /* Validate the length of size */
82     if (size < OHOS::U32_AT_SIZE) {
83         return 0;
84     }
85 
86     char* ch = (char *)malloc(size + 1);
87     if (ch == nullptr) {
88         std::cout << "malloc failed." << std::endl;
89         return 0;
90     }
91 
92     (void)memset_s(ch, size + 1, 0x00, size + 1);
93     if (memcpy_s(ch, size, data, size) != EOK) {
94         std::cout << "copy failed." << std::endl;
95         free(ch);
96         ch = nullptr;
97         return 0;
98     }
99 
100     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
101     free(ch);
102     ch = nullptr;
103     return 0;
104 }
105