• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_callback_fuzzer.h"
17 #include "fuzz_base.h"
18 
19 #include <cstddef>
20 #include <cstdint>
21 
22 class IPCCameraHostCallbackFuzzer : public CameraHostCallbackStub {
23 public:
OnCameraStatus(const std::string & cameraId,CameraStatus status)24     void OnCameraStatus(const std::string &cameraId, CameraStatus status) override {}
OnFlashlightStatus(const std::string & cameraId,FlashlightStatus status)25     void OnFlashlightStatus(const std::string &cameraId, FlashlightStatus status) override {}
26 };
27 
U32_AT(const uint8_t * ptr)28 static uint32_t U32_AT(const uint8_t *ptr)
29 {
30     return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
31 }
32 
onRemoteRequest(uint32_t code,MessageParcel & data)33 static int32_t onRemoteRequest(uint32_t code, MessageParcel &data)
34 {
35     MessageParcel reply;
36     MessageOption option;
37     std::shared_ptr<IPCCameraHostCallbackFuzzer> IPCHostCall = std::make_shared<IPCCameraHostCallbackFuzzer>();
38     auto ret = IPCHostCall->OnRemoteRequest(code, data, reply, option);
39     return ret;
40 }
41 
IpcFuzzService(const uint8_t * data,size_t size)42 static void IpcFuzzService(const uint8_t *data, size_t size)
43 {
44     MessageParcel reply;
45     MessageOption option;
46     MessageParcel dataMessageParcel;
47     if (size > sizeof(uint32_t)) {
48         uint32_t code = U32_AT(data);
49         uint8_t *number = data;
50         number = number + sizeof(uint32_t);
51         size_t length = size;
52         length = length - sizeof(uint32_t);
53         dataMessageParcel.WriteInterfaceToken(CameraHostCallbackStub::GetDescriptor());
54         dataMessageParcel.WriteBuffer(number, length);
55         dataMessageParcel.RewindRead(0);
56         onRemoteRequest(code, dataMessageParcel);
57     }
58 }
59 
OnRemoteRequestFunc(const uint8_t * data,size_t size)60 static void OnRemoteRequestFunc(const uint8_t *data, size_t size)
61 {
62     IpcFuzzService(data, size);
63 }
64 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)65 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
66 {
67     OnRemoteRequestFunc(data, size);
68     return 0;
69 }