1 /*
2 * Copyright (c) 2021 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 "ipc_stream_operator_callback_fuzzer.h"
17 #include "fuzz_base.h"
18
19 #include <cstddef>
20 #include <cstdint>
21
22 class IPCStreamOperatorCallbackFuzzer : public StreamOperatorCallbackStub {
23 public:
OnCaptureStarted(int32_t captureId,const std::vector<int32_t> & streamIds)24 void OnCaptureStarted(int32_t captureId, const std::vector<int32_t> &streamIds) override {}
OnCaptureEnded(int32_t captureId,const std::vector<std::shared_ptr<CaptureEndedInfo>> & infos)25 void OnCaptureEnded(int32_t captureId,
26 const std::vector<std::shared_ptr<CaptureEndedInfo>> &infos) override {}
OnCaptureError(int32_t captureId,const std::vector<std::shared_ptr<CaptureErrorInfo>> & infos)27 void OnCaptureError(int32_t captureId,
28 const std::vector<std::shared_ptr<CaptureErrorInfo>> &infos) override {}
OnFrameShutter(int32_t captureId,const std::vector<int32_t> & streamIds,uint64_t timestamp)29 void OnFrameShutter(int32_t captureId,
30 const std::vector<int32_t> &streamIds, uint64_t timestamp) override {}
31 };
32
U32_AT(const uint8_t * ptr)33 static uint32_t U32_AT(const uint8_t *ptr)
34 {
35 return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
36 }
37
onRemoteRequest(uint32_t code,MessageParcel & data)38 static int32_t onRemoteRequest(uint32_t code, MessageParcel &data)
39 {
40 MessageParcel reply;
41 MessageOption option;
42 IPCStreamOperatorCallbackFuzzer IPCStreamSerCall;
43 auto ret = IPCStreamSerCall.OnRemoteRequest(code, data, reply, option);
44 return ret;
45 }
46
IpcFuzzService(const uint8_t * data,size_t size)47 static void IpcFuzzService(const uint8_t *data, size_t size)
48 {
49 MessageParcel reply;
50 MessageOption option;
51 MessageParcel dataMessageParcel;
52 if (size > sizeof(uint32_t)) {
53 uint32_t code = U32_AT(data);
54 uint8_t *number = data;
55 number = number + sizeof(uint32_t);
56 size_t length = size;
57 length = length - sizeof(uint32_t);
58 dataMessageParcel.WriteInterfaceToken(StreamOperatorCallbackStub::GetDescriptor());
59 dataMessageParcel.WriteBuffer(number, length);
60 dataMessageParcel.RewindRead(0);
61 onRemoteRequest(code, dataMessageParcel);
62 }
63 }
64
OnRemoteRequestFunc(const uint8_t * data,size_t size)65 static void OnRemoteRequestFunc(const uint8_t *data, size_t size)
66 {
67 IpcFuzzService(data, size);
68 }
69
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)70 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
71 {
72 OnRemoteRequestFunc(data, size);
73 return 0;
74 }