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 #include "audio_policy_server.h"
20 #include "message_parcel.h"
21 using namespace std;
22
23 namespace OHOS {
24 namespace AudioStandard {
25 constexpr int32_t OFFSET = 4;
26 const std::u16string FORMMGR_INTERFACE_TOKEN = u"IAudioPolicy";
27 const int32_t SYSTEM_ABILITY_ID = 3009;
28 const bool RUN_ON_CREATE = false;
29 const int32_t LIMITSIZE = 4;
30 const int32_t SHIFT_LEFT_8 = 8;
31 const int32_t SHIFT_LEFT_16 = 16;
32 const int32_t SHIFT_LEFT_24 = 24;
33
Convert2Uint32(const uint8_t * ptr)34 uint32_t Convert2Uint32(const uint8_t *ptr)
35 {
36 if (ptr == nullptr) {
37 return 0;
38 }
39 /* Move the 0th digit to the left by 24 bits, the 1st digit to the left by 16 bits,
40 the 2nd digit to the left by 8 bits, and the 3rd digit not to the left */
41 return (ptr[0] << SHIFT_LEFT_24) | (ptr[1] << SHIFT_LEFT_16) | (ptr[2] << SHIFT_LEFT_8) | (ptr[3]);
42 }
43
AudioPolicyFuzzTest(const uint8_t * rawData,size_t size)44 void AudioPolicyFuzzTest(const uint8_t *rawData, size_t size)
45 {
46 if (rawData == nullptr || size < LIMITSIZE) {
47 return;
48 }
49 uint32_t code = Convert2Uint32(rawData);
50 rawData = rawData + OFFSET;
51 size = size - OFFSET;
52
53 MessageParcel data;
54 data.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN);
55 data.WriteBuffer(rawData, size);
56 data.RewindRead(0);
57
58 MessageParcel reply;
59 MessageOption option;
60 std::shared_ptr<AudioPolicyServer> AudioPolicyServerPtr =
61 std::make_shared<AudioPolicyServer>(SYSTEM_ABILITY_ID, RUN_ON_CREATE);
62
63 AudioPolicyServerPtr->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::AudioPolicyFuzzTest(data, size);
73 return 0;
74 }
75