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_camera_host_service_fuzzer.h"
17 #include "fuzz_base.h"
18
19 #include <cstddef>
20 #include <cstdint>
21
U32_AT(const uint8_t * ptr)22 static uint32_t U32_AT(const uint8_t *ptr)
23 {
24 return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
25 }
26
onRemoteRequest(int cmdId,MessageParcel & data)27 static int32_t onRemoteRequest(int cmdId, MessageParcel &data)
28 {
29 MessageParcel reply;
30 MessageOption option;
31 std::shared_ptr<CameraHostStub> IPCHost = std::make_shared<CameraHostStub>();
32 sleep(1);
33 auto ret = IPCHost->CameraHostServiceStubOnRemoteRequest(cmdId, data, reply, option);
34 return ret;
35 }
36
IpcFuzzService(const uint8_t * data,size_t size)37 static void IpcFuzzService(const uint8_t *data, size_t size)
38 {
39 MessageParcel reply;
40 MessageOption option;
41 MessageParcel dataMessageParcel;
42 uint32_t code = U32_AT(data);
43 uint8_t *number = data;
44 number = number + sizeof(uint32_t);
45 if (size > sizeof(uint32_t)) {
46 if ((code == 0) || (code == 3)) { // 0:code size 3:code size
47 return;
48 }
49 size_t length = size;
50 length = length - sizeof(uint32_t);
51 dataMessageParcel.WriteBuffer(number, length);
52 dataMessageParcel.RewindRead(0);
53 onRemoteRequest((int)code, dataMessageParcel);
54 }
55 }
56
OnRemoteRequestFunc(const uint8_t * data,size_t size)57 static void OnRemoteRequestFunc(const uint8_t *data, size_t size)
58 {
59 IpcFuzzService(data, size);
60 }
61
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)62 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
63 {
64 OnRemoteRequestFunc(data, size);
65 return 0;
66 }