/* * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include #include "audio_policy_server.h" #include "message_parcel.h" using namespace std; namespace OHOS { namespace AudioStandard { constexpr int32_t OFFSET = 4; const std::u16string FORMMGR_INTERFACE_TOKEN = u"IAudioPolicy"; const int32_t SYSTEM_ABILITY_ID = 3009; const bool RUN_ON_CREATE = false; const int32_t LIMITSIZE = 4; const int32_t SHIFT_LEFT_8 = 8; const int32_t SHIFT_LEFT_16 = 16; const int32_t SHIFT_LEFT_24 = 24; const uint32_t LIMIT_ONE = 0; const uint32_t LIMIT_TWO = 30; const uint32_t LIMIT_THREE = 60; const uint32_t LIMIT_FOUR = 80; uint32_t Convert2Uint32(const uint8_t *ptr) { if (ptr == nullptr) { return 0; } /* Move the 0th digit to the left by 24 bits, the 1st digit to the left by 16 bits, the 2nd digit to the left by 8 bits, and the 3rd digit not to the left */ return (ptr[0] << SHIFT_LEFT_24) | (ptr[1] << SHIFT_LEFT_16) | (ptr[2] << SHIFT_LEFT_8) | (ptr[3]); } void AudioPolicyFuzzFirstLimitTest(const uint8_t *rawData, size_t size) { if (rawData == nullptr || size < LIMITSIZE) { return; } uint32_t code = Convert2Uint32(rawData) % (LIMIT_TWO - LIMIT_ONE + 1) + LIMIT_ONE; rawData = rawData + OFFSET; size = size - OFFSET; MessageParcel data; data.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN); data.WriteBuffer(rawData, size); data.RewindRead(0); MessageParcel reply; MessageOption option; std::shared_ptr AudioPolicyServerPtr = std::make_shared(SYSTEM_ABILITY_ID, RUN_ON_CREATE); AudioPolicyServerPtr->OnRemoteRequest(code, data, reply, option); } void AudioPolicyFuzzSecondLimitTest(const uint8_t *rawData, size_t size) { if (rawData == nullptr || size < LIMITSIZE) { return; } uint32_t code = Convert2Uint32(rawData) % (LIMIT_THREE - LIMIT_TWO + 1) + LIMIT_TWO; rawData = rawData + OFFSET; size = size - OFFSET; MessageParcel data; data.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN); data.WriteBuffer(rawData, size); data.RewindRead(0); MessageParcel reply; MessageOption option; std::shared_ptr AudioPolicyServerPtr = std::make_shared(SYSTEM_ABILITY_ID, RUN_ON_CREATE); AudioPolicyServerPtr->OnRemoteRequest(code, data, reply, option); } void AudioPolicyFuzzThirdLimitTest(const uint8_t *rawData, size_t size) { if (rawData == nullptr || size < LIMITSIZE) { return; } uint32_t code = Convert2Uint32(rawData) % (LIMIT_FOUR - LIMIT_THREE + 1) + LIMIT_THREE; rawData = rawData + OFFSET; size = size - OFFSET; MessageParcel data; data.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN); data.WriteBuffer(rawData, size); data.RewindRead(0); MessageParcel reply; MessageOption option; std::shared_ptr AudioPolicyServerPtr = std::make_shared(SYSTEM_ABILITY_ID, RUN_ON_CREATE); AudioPolicyServerPtr->OnRemoteRequest(code, data, reply, option); } } // namespace AudioStandard } // namesapce OHOS /* Fuzzer entry point */ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { /* Run your code on data */ OHOS::AudioStandard::AudioPolicyFuzzFirstLimitTest(data, size); OHOS::AudioStandard::AudioPolicyFuzzSecondLimitTest(data, size); OHOS::AudioStandard::AudioPolicyFuzzThirdLimitTest(data, size); return 0; }