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 "IPCCameraHostService_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
fuzzAccountService(const uint8_t * data,size_t size)37 static void fuzzAccountService(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 data = data + sizeof(uint32_t);
44 if (size > sizeof(uint32_t)) {
45 if ((code == 0) || (code == 3)) { // 0:code size 3:code size
46 return;
47 }
48 size = size - sizeof(uint32_t);
49 dataMessageParcel.WriteBuffer(data, size);
50 dataMessageParcel.RewindRead(0);
51 onRemoteRequest((int)code, dataMessageParcel);
52 }
53 }
54
OnRemoteRequestFunc(const uint8_t * data,size_t size)55 static void OnRemoteRequestFunc(const uint8_t *data, size_t size)
56 {
57 fuzzAccountService(data, size);
58 }
59
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)60 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
61 {
62 OnRemoteRequestFunc(data, size);
63 return 0;
64 }