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 "codecsendcommand_fuzzer.h"
17 #include "codeccommon_fuzzer.h"
18
19 #include <securec.h>
20 #include <unistd.h>
21
22 using namespace OHOS::HDI::Codec::V3_0;
23 using OHOS::HDI::Codec::V3_0::CodecCommandType;
24 using OHOS::HDI::Codec::V3_0::CodecStateType;
25
26 namespace {
27 struct AllParameters {
28 enum CodecCommandType cmd;
29 uint32_t param;
30 int8_t *cmdData;
31 uint32_t cmdDataLen;
32 };
33 constexpr uint32_t WAIT_TIME = 1000;
34 constexpr uint32_t MAX_WAIT = 50;
35 }
36
37 namespace OHOS {
38 namespace Codec {
WaitState(CodecStateType objState)39 void WaitState(CodecStateType objState)
40 {
41 CodecStateType state = CODEC_STATE_INVALID;
42 uint32_t count = 0;
43 do {
44 usleep(WAIT_TIME);
45 g_component->GetState(state);
46 count++;
47 } while (state != objState && count <= MAX_WAIT);
48 }
49
CodecSendCommand(const uint8_t * data,size_t size)50 bool CodecSendCommand(const uint8_t *data, size_t size)
51 {
52 struct AllParameters params;
53 if (data == nullptr) {
54 return false;
55 }
56
57 if (size < sizeof(params)) {
58 return false;
59 }
60
61 if (memcpy_s(reinterpret_cast<void *>(¶ms), sizeof(params), data, sizeof(params)) != 0) {
62 HDF_LOGE("%{public}s: memcpy_s failed", __func__);
63 return false;
64 }
65
66 bool result = Preconditions();
67 if (!result) {
68 HDF_LOGE("%{public}s: Preconditions failed\n", __func__);
69 return false;
70 }
71
72 std::vector<int8_t> cmdData;
73 ObjectToVector(params.cmdData, cmdData);
74
75 int32_t ret = g_component->SendCommand(params.cmd, params.param, cmdData);
76 if (ret != HDF_SUCCESS) {
77 HDF_LOGE("%{public}s: SendCommand failed, ret is [%{public}x]\n", __func__, ret);
78 }
79 CodecStateType type = CodecStateType(params.param);
80 WaitState(type);
81
82 if (params.cmd == CODEC_COMMAND_STATE_SET && params.param == CODEC_STATE_IDLE) {
83 g_component->SendCommand(CODEC_COMMAND_STATE_SET, CODEC_STATE_LOADED, cmdData);
84 WaitState(CODEC_STATE_LOADED);
85 }
86
87 result = Destroy();
88 if (!result) {
89 HDF_LOGE("%{public}s: Destroy failed\n", __func__);
90 return false;
91 }
92
93 return true;
94 }
95 } // namespace codec
96 } // namespace OHOS
97
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)98 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
99 {
100 OHOS::Codec::CodecSendCommand(data, size);
101 return 0;
102 }
103