1 /*
2 * Copyright (c) 2022 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 <cstddef>
17 #include <cstdint>
18 #include <iostream>
19 #include "hdf_log.h"
20 #include "codec_fuzzer.h"
21 #include "codec_component_manager_service.h"
22
23 using namespace OHOS;
24
25 namespace OHOS {
26 constexpr size_t THRESHOLD = 10;
27 constexpr int32_t OFFSET = 4;
28 const std::u16string CODEC_INTERFACE_TOKEN = u"ohos.hdi.codec_service";
29
Convert2Uint32(const uint8_t * ptr)30 uint32_t Convert2Uint32(const uint8_t* ptr)
31 {
32 if (ptr == nullptr) {
33 return 0;
34 }
35 /*
36 * Move the 0th digit 24 to the left, the first digit 16 to the left, the second digit 8 to the left,
37 * and the third digit no left
38 */
39 return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
40 }
41
CodecFuzzTest(const uint8_t * rawData,size_t size)42 bool CodecFuzzTest(const uint8_t* rawData, size_t size)
43 {
44 if (rawData == nullptr) {
45 HDF_LOGE("%{public}s: Failed to obtain rawData", __func__);
46 return false;
47 }
48 uint32_t code = Convert2Uint32(rawData);
49 rawData = rawData + OFFSET;
50 size = size - OFFSET;
51
52 struct HdfSBuf *data = HdfSbufTypedObtain(SBUF_IPC);
53 if (data == nullptr) {
54 HDF_LOGE("%{public}s: Failed to obtain data", __func__);
55 return false;
56 }
57
58 HdfSbufWriteBuffer(data, CODEC_INTERFACE_TOKEN.c_str(), CODEC_INTERFACE_TOKEN.length());
59 HdfSbufWriteBuffer(data, rawData, size);
60
61 struct HdfSBuf *reply = HdfSbufTypedObtain(SBUF_IPC);
62 if (reply == nullptr) {
63 HDF_LOGE("%{public}s: Failed to obtain reply", __func__);
64 HdfSbufRecycle(data);
65 return false;
66 }
67
68 CodecComponentManagerSerivce *service = nullptr;
69
70 service = CodecComponentManagerSerivceGet();
71 if (service == nullptr) {
72 HDF_LOGE("%{public}s:CodecComponentManagerSerivceGet failed.", __func__);
73 HdfSbufRecycle(data);
74 HdfSbufRecycle(reply);
75 return false;
76 }
77
78 service->stub.OnRemoteRequest((struct CodecComponentManager *)(&service->stub.interface), code, data, reply);
79
80 OmxComponentManagerSeriveRelease(service);
81 HdfSbufRecycle(data);
82 HdfSbufRecycle(reply);
83
84 return true;
85 }
86 }
87
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)88 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
89 {
90 if (size < OHOS::THRESHOLD) {
91 return 0;
92 }
93
94 OHOS::CodecFuzzTest(data, size);
95 return 0;
96 }
97