1 /*
2 * Copyright (c) 2023 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 "destroy_fuzzer.h"
17
18 #include "hichain.h"
19 #include "distribution.h"
20 #include "securec.h"
21
22 namespace OHOS {
TransmitCb(const struct session_identity * identity,const void * data,uint32_t length)23 static void TransmitCb(const struct session_identity *identity, const void *data, uint32_t length)
24 {
25 return;
26 }
27
ConfirmReceiveRequestFunc(const struct session_identity * identity,int32_t operationCode)28 static int32_t ConfirmReceiveRequestFunc(const struct session_identity *identity, int32_t operationCode)
29 {
30 return HC_OK;
31 }
32
GetProtocolParamsCb(const struct session_identity * identity,int32_t operationCode,struct hc_pin * pin,struct operation_parameter * para)33 static void GetProtocolParamsCb(const struct session_identity *identity, int32_t operationCode,
34 struct hc_pin *pin, struct operation_parameter *para)
35 {
36 return;
37 }
38
SetSessionKeyFunc(const struct session_identity * identity,const struct hc_session_key * sessionKey)39 static void SetSessionKeyFunc(const struct session_identity *identity, const struct hc_session_key *sessionKey)
40 {
41 return;
42 }
43
SetServiceResultFunc(const struct session_identity * identity,int32_t result)44 static void SetServiceResultFunc(const struct session_identity *identity, int32_t result)
45 {
46 return;
47 }
48
49 static hc_call_back callback = {
50 .transmit = TransmitCb,
51 .get_protocol_params = GetProtocolParamsCb,
52 .set_session_key = SetSessionKeyFunc,
53 .set_service_result = SetServiceResultFunc,
54 .confirm_receive_request = ConfirmReceiveRequestFunc,
55 };
56
57 static struct session_identity identity = {
58 1,
59 {sizeof("hicar"), "hicar"},
60 {sizeof("CarDevice"), "CarDevice"},
61 0
62 };
63
DestroyFuzz(const uint8_t * data,size_t size)64 bool DestroyFuzz(const uint8_t *data, size_t size)
65 {
66 if ((data == nullptr) || (size < sizeof(int32_t))) {
67 return false;
68 }
69 hc_handle handle = get_instance(&identity, HC_CENTRE, &callback);
70 struct hichain *hichain = static_cast<struct hichain *>(handle);
71 hichain->operation_code = *reinterpret_cast<const int32_t *>(data);
72 hc_handle server = static_cast<void *>(hichain);
73 destroy(&server);
74 return true;
75 }
76 }
77
78 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)79 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
80 {
81 OHOS::DestroyFuzz(data, size);
82 return 0;
83 }
84