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_offline_fuzzer.h"
17 #include "fuzz_base.h"
18
19 #include <cstddef>
20 #include <cstdint>
21
22 class IPCOfflineFuzzer : public OfflineStreamOperatorStub {
23 public:
CancelCapture(int captureId)24 virtual CamRetCode CancelCapture(int captureId)
25 {
26 static_cast<void>(captureId);
27 return OHOS::Camera::NO_ERROR;
28 }
ReleaseStreams(const std::vector<int> & streamIds)29 virtual CamRetCode ReleaseStreams(const std::vector<int>& streamIds)
30 {
31 return OHOS::Camera::NO_ERROR;
32 }
Release()33 virtual CamRetCode Release()
34 {
35 return OHOS::Camera::NO_ERROR;
36 }
37 };
38
U32_AT(const uint8_t * ptr)39 static uint32_t U32_AT(const uint8_t *ptr)
40 {
41 return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
42 }
43
onRemoteRequest(uint32_t code,MessageParcel & data)44 static int32_t onRemoteRequest(uint32_t code, MessageParcel &data)
45 {
46 MessageParcel reply;
47 MessageOption option;
48 IPCOfflineFuzzer IPCOfflineSer;
49 auto ret = IPCOfflineSer.OnRemoteRequest(code, data, reply, option);
50 return ret;
51 }
52
IpcFuzzService(const uint8_t * data,size_t size)53 static void IpcFuzzService(const uint8_t *data, size_t size)
54 {
55 MessageParcel reply;
56 MessageOption option;
57 MessageParcel dataMessageParcel;
58 uint32_t code = U32_AT(data);
59
60 uint8_t *number = data;
61 number = number + sizeof(uint32_t);
62 if (size > sizeof(uint32_t)) {
63 size_t length = size;
64 length = length - sizeof(uint32_t);
65 dataMessageParcel.WriteInterfaceToken(OfflineStreamOperatorStub::GetDescriptor());
66 dataMessageParcel.WriteBuffer(number, length);
67 dataMessageParcel.RewindRead(0);
68 onRemoteRequest(code, dataMessageParcel);
69 }
70 }
71
OnRemoteRequestFunc(const uint8_t * data,size_t size)72 static void OnRemoteRequestFunc(const uint8_t *data, size_t size)
73 {
74 IpcFuzzService(data, size);
75 }
76
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)77 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
78 {
79 OnRemoteRequestFunc(data, size);
80 return 0;
81 }