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 <iostream>
17 #include <cstddef>
18 #include <cstdint>
19
20 #include "audio_server.h"
21 #include "message_parcel.h"
22 using namespace std;
23
24 namespace OHOS {
25 namespace AudioStandard {
26 constexpr int32_t OFFSET = 4;
27 const std::u16string FORMMGR_INTERFACE_TOKEN = u"IStandardAudioService";
28 const int32_t SYSTEM_ABILITY_ID = 3001;
29 const bool RUN_ON_CREATE = false;
30 const int32_t LIMITSIZE = 4;
31 const int32_t SHIFT_LEFT_8 = 8;
32 const int32_t SHIFT_LEFT_16 = 16;
33 const int32_t SHIFT_LEFT_24 = 24;
34
Convert2Uint32(const uint8_t * ptr)35 uint32_t Convert2Uint32(const uint8_t *ptr)
36 {
37 if (ptr == nullptr) {
38 return 0;
39 }
40 /* Move the 0th digit to the left by 24 bits, the 1st digit to the left by 16 bits,
41 the 2nd digit to the left by 8 bits, and the 3rd digit not to the left */
42 return (ptr[0] << SHIFT_LEFT_24) | (ptr[1] << SHIFT_LEFT_16) | (ptr[2] << SHIFT_LEFT_8) | (ptr[3]);
43 }
44
AudioServerFuzzTest(const uint8_t * rawData,size_t size)45 void AudioServerFuzzTest(const uint8_t *rawData, size_t size)
46 {
47 if (rawData == nullptr || size < LIMITSIZE) {
48 return;
49 }
50 uint32_t code = Convert2Uint32(rawData);
51 rawData = rawData + OFFSET;
52 size = size - OFFSET;
53
54 MessageParcel data;
55 data.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN);
56 data.WriteBuffer(rawData, size);
57 data.RewindRead(0);
58 MessageParcel reply;
59 MessageOption option;
60
61 std::shared_ptr<AudioServer> AudioServerPtr =
62 std::make_shared<AudioServer>(SYSTEM_ABILITY_ID, RUN_ON_CREATE);
63 AudioServerPtr->OnRemoteRequest(code, data, reply, option);
64 }
65 } // namespace AudioStandard
66 } // namesapce OHOS
67
68 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)69 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
70 {
71 /* Run your code on data */
72 OHOS::AudioStandard::AudioServerFuzzTest(data, size);
73 return 0;
74 }