/* * 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 "i_audio_renderer_sink.h" #include "audio_manager_base.h" #include "audio_policy_manager_listener_stub.h" #include "audio_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"IStandardAudioService"; const int32_t SYSTEM_ABILITY_ID = 3001; 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_MIN = 0; const uint32_t LIMIT_MAX = 30; 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 AudioServerFuzzTest(const uint8_t *rawData, size_t size) { if (rawData == nullptr || size < LIMITSIZE) { return; } uint32_t code = Convert2Uint32(rawData) % (LIMIT_MAX - LIMIT_MIN + 1) + LIMIT_MIN; 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 AudioServerPtr = std::make_shared(SYSTEM_ABILITY_ID, RUN_ON_CREATE); if (code == static_cast(AudioServerInterfaceCode::SET_PARAMETER_CALLBACK)) { sptr focusListenerStub = new(std::nothrow) AudioPolicyManagerListenerStub(); sptr object = focusListenerStub->AsObject(); AudioServerPtr->SetParameterCallback(object); return; } AudioServerPtr->OnRemoteRequest(code, data, reply, option); if (size < LIMITSIZE) { return; } std::string netWorkId(reinterpret_cast(rawData), size - 1); AudioParamKey key = *reinterpret_cast(rawData); std::string condition(reinterpret_cast(rawData), size - 1); std::string value(reinterpret_cast(rawData), size - 1); AudioServerPtr->OnAudioParameterChange(netWorkId, key, condition, value); } void AudioServerCaptureSilentlyFuzzTest(const uint8_t *rawData, size_t size) { if (rawData == nullptr || size < LIMITSIZE) { return; } MessageParcel data; data.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN); data.WriteBuffer(rawData, size); data.RewindRead(0); MessageParcel reply; MessageOption option; std::shared_ptr AudioServerPtr = std::make_shared(SYSTEM_ABILITY_ID, RUN_ON_CREATE); AudioServerPtr->OnRemoteRequest(static_cast(AudioServerInterfaceCode::SET_CAPTURE_SILENT_STATE), 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::AudioServerFuzzTest(data, size); OHOS::AudioStandard::AudioServerCaptureSilentlyFuzzTest(data, size); return 0; }