1 /*
2 * Copyright (c) 2022-2023 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 <cstddef>
17 #include <cstdint>
18 #include <iostream>
19 #include <memory>
20
21 #include "securec.h"
22 #include "avsession_callback_proxy.h"
23 #include "iavcontroller_callback.h"
24 #include "iremote_proxy.h"
25 #include "avsession_log.h"
26 #include "avsession_errors.h"
27 #include "avsession_callback_proxy_fuzzer.h"
28
29 using namespace std;
30 using namespace OHOS;
31 using namespace OHOS::AVSession;
32
33 static const int32_t MAX_CODE_TEST = 13;
34 static const int32_t MIN_SIZE_NUM = 10;
35 static const uint8_t *RAW_DATA = nullptr;
36 static size_t g_totalSize = 0;
37 static size_t g_sizePos;
38
39 namespace {
40 /*
41 * describe: get data from FUZZ untrusted data(RAW_DATA) which size is according to sizeof(T)
42 * tips: only support basic type
43 */
44 template<class T>
GetData()45 T GetData()
46 {
47 T object {};
48 size_t objectSize = sizeof(object);
49 if (RAW_DATA == nullptr || objectSize > g_totalSize - g_sizePos) {
50 return object;
51 }
52 errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_sizePos, objectSize);
53 if (ret != EOK) {
54 return {};
55 }
56 g_sizePos += objectSize;
57 return object;
58 }
59
60 template<class T>
GetArrLength(T & arr)61 uint32_t GetArrLength(T& arr)
62 {
63 if (arr == nullptr) {
64 SLOGE("%{public}s: The array length is equal to 0", __func__);
65 return 0;
66 }
67 return sizeof(arr) / sizeof(arr[0]);
68 }
69
70 typedef void (*TestFuncs[1])();
71
72 TestFuncs g_allFuncs = {
73 AvsessionCallbackProxySendRequestTest
74 };
75
FuzzTest(const uint8_t * rawData,size_t size)76 bool FuzzTest(const uint8_t* rawData, size_t size)
77 {
78 if (rawData == nullptr) {
79 return false;
80 }
81
82 // initialize data
83 RAW_DATA = rawData;
84 g_totalSize = size;
85 g_sizePos = 0;
86
87 uint32_t code = GetData<uint32_t>();
88 uint32_t len = GetArrLength(g_allFuncs);
89 if (len > 0) {
90 g_allFuncs[code % len]();
91 } else {
92 SLOGE("%{public}s: The len length is equal to 0", __func__);
93 }
94
95 return true;
96 }
97 }
98
FuzzSendRequest()99 bool AvsessionCallbackProxyFuzzer::FuzzSendRequest()
100 {
101 uint32_t code = GetData<uint32_t>();
102 if (code >= MAX_CODE_TEST) {
103 return false;
104 }
105
106 sptr<IRemoteObject> remoteObject = nullptr;
107 std::shared_ptr<AvsessionCallbackProxyFuzzerTest> avSessionCallbackProxy =
108 std::make_shared<AvsessionCallbackProxyFuzzerTest>(remoteObject);
109 if (!avSessionCallbackProxy) {
110 SLOGI("avSessionCallbackProxy is null");
111 return false;
112 }
113 MessageParcel request;
114 CHECK_AND_PRINT_LOG(request.WriteInterfaceToken(avSessionCallbackProxy->GetDescriptor()),
115 "write interface token failed");
116 MessageParcel reply;
117 MessageOption option = { MessageOption::TF_ASYNC};
118 auto remote = avSessionCallbackProxy->GetRemote();
119 if (!remote) {
120 SLOGI("remote is null");
121 return false;
122 }
123 request.WriteBuffer(RAW_DATA, g_sizePos);
124 g_sizePos += sizeof(uint32_t);
125 request.RewindRead(0);
126 int32_t result = AVSESSION_ERROR;
127 CHECK_AND_PRINT_LOG((result = remote->SendRequest(code, request, reply, option)) == 0, "send request failed");
128 return result == AVSESSION_SUCCESS;
129 }
130
AvsessionCallbackProxySendRequestTest()131 void OHOS::AVSession::AvsessionCallbackProxySendRequestTest()
132 {
133 auto avSessionCallbackProxy = std::make_unique<AvsessionCallbackProxyFuzzer>();
134 if (avSessionCallbackProxy == nullptr) {
135 return;
136 }
137 avSessionCallbackProxy->FuzzSendRequest();
138 }
139
140 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)141 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
142 {
143 if (size < MIN_SIZE_NUM) {
144 return 0;
145 }
146 /* Run your code on data */
147 FuzzTest(data, size);
148 return 0;
149 }