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 <iostream>
17 #include <cstddef>
18 #include <cstdint>
19 #include <memory>
20
21 #include "avcontroller_callback_proxy.h"
22 #include "iavcontroller_callback.h"
23 #include "iremote_proxy.h"
24 #include "avsession_log.h"
25 #include "avsession_errors.h"
26 #include "avcontroller_callbackproxy_fuzzer.h"
27
28 using namespace std;
29 using namespace OHOS;
30 using namespace OHOS::AVSession;
31
32 static const int32_t MAX_CODE_TEST = 5;
33 static const int32_t MAX_CODE_LEN = 512;
34 static const int32_t MIN_SIZE_NUM = 4;
35
FuzzSendRequest(uint8_t * data,size_t size)36 bool AvControllerCallbackProxyFuzzer::FuzzSendRequest(uint8_t* data, size_t size)
37 {
38 if ((data == nullptr) || (size > MAX_CODE_LEN) || size < MIN_SIZE_NUM) {
39 return false;
40 }
41
42 uint32_t cmdCode = *(reinterpret_cast<const uint32_t*>(data));
43 if (cmdCode >= MAX_CODE_TEST) {
44 return false;
45 }
46
47 sptr<IRemoteObject> remoteObject = nullptr;
48 std::shared_ptr<AVControllerCallbackProxyFuzzerTest> avControllerCallbackProxy =
49 std::make_shared<AVControllerCallbackProxyFuzzerTest>(remoteObject);
50
51 if (!avControllerCallbackProxy) {
52 return false;
53 }
54 MessageParcel parcel;
55 CHECK_AND_PRINT_LOG(parcel.WriteInterfaceToken(avControllerCallbackProxy->GetDescriptor()),
56 "write interface token failed");
57
58 MessageParcel reply;
59 MessageOption option { MessageOption::TF_ASYNC };
60 auto remote = avControllerCallbackProxy->GetRemote();
61 if (remote == nullptr) {
62 SLOGD("remote is null");
63 return false;
64 }
65 size -= sizeof(uint32_t);
66 parcel.WriteBuffer(data + sizeof(uint32_t), size);
67 parcel.RewindRead(0);
68 int32_t result = AVSESSION_ERROR;
69 CHECK_AND_PRINT_LOG(remote != nullptr, "get remote service failed");
70 CHECK_AND_PRINT_LOG((result = remote->SendRequest(cmdCode, parcel, reply, option)) == 0,
71 "send request failed");
72 return result == AVSESSION_SUCCESS;
73 }
74
AVControllerCallbackProxySendRequest(uint8_t * data,size_t size)75 bool OHOS::AVSession::AVControllerCallbackProxySendRequest(uint8_t* data, size_t size)
76 {
77 auto avControllerCallbackProxy = std::make_unique<AvControllerCallbackProxyFuzzer>();
78 if (avControllerCallbackProxy == nullptr) {
79 return false;
80 }
81 return avControllerCallbackProxy->FuzzSendRequest(data, size);
82 }
83
84 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)85 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
86 {
87 /* Run your code on data */
88 OHOS::AVSession::AVControllerCallbackProxySendRequest(const_cast<uint8_t*>(data), size);
89 return 0;
90 }
91
92