• 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 "IPCCameraDeviceCallback_fuzzer.h"
17 #include "fuzz_base.h"
18 
19 #include <cstddef>
20 #include <cstdint>
21 
22 class IPCCameraDeviceCallbackFuzzer : public CameraDeviceCallbackStub {
23 public:
OnError(ErrorType type,int32_t errorCode)24     virtual void OnError(ErrorType type, int32_t errorCode) override {}
OnResult(uint64_t timestamp,const std::shared_ptr<CameraStandard::CameraMetadata> & result)25     virtual void OnResult(uint64_t timestamp, const std::shared_ptr<CameraStandard::CameraMetadata> &result) 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     IPCCameraDeviceCallbackFuzzer IPCDeviceCallback;
38 
39     auto ret = IPCDeviceCallback.OnRemoteRequest(code, data, reply, option);
40     return ret;
41 }
42 
fuzzAccountService(const uint8_t * data,size_t size)43 static void fuzzAccountService(const uint8_t *data, size_t size)
44 {
45     MessageParcel reply;
46     MessageOption option;
47     MessageParcel dataMessageParcel;
48     if (size > sizeof(uint32_t)) {
49         uint32_t code = U32_AT(data);
50         if (code == 1) { // 1:code size
51             return;
52         }
53         data = data + sizeof(uint32_t);
54         size = size - sizeof(uint32_t);
55         dataMessageParcel.WriteInterfaceToken(CameraDeviceCallbackStub::GetDescriptor());
56         dataMessageParcel.WriteBuffer(data, size);
57         dataMessageParcel.RewindRead(0);
58         onRemoteRequest(code, dataMessageParcel);
59     }
60 }
61 
OnRemoteRequestFunc(const uint8_t * data,size_t size)62 static void OnRemoteRequestFunc(const uint8_t *data, size_t size)
63 {
64     fuzzAccountService(data, size);
65 }
66 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)67 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
68 {
69     OnRemoteRequestFunc(data, size);
70     return 0;
71 }