Mufaddal Chakera | 253887f | 2020-12-09 17:17:37 +0530 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
| 3 | * Copyright (C) 2021 The Android Open Source Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * |
| 17 | ******************************************************************************/ |
| 18 | #include <stdint.h> |
| 19 | #include <sys/wait.h> |
| 20 | #include <unistd.h> |
| 21 | #include <algorithm> |
| 22 | #include <memory> |
| 23 | #include <string> |
| 24 | #include <utility> |
| 25 | #include <vector> |
| 26 | |
| 27 | #include <Serializer.h> |
| 28 | #include <android-base/file.h> |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 29 | #include <android/media/permission/Identity.h> |
Mufaddal Chakera | 253887f | 2020-12-09 17:17:37 +0530 | [diff] [blame] | 30 | #include <libxml/parser.h> |
| 31 | #include <libxml/xinclude.h> |
| 32 | #include <media/AudioPolicy.h> |
| 33 | #include <media/PatchBuilder.h> |
| 34 | #include <media/RecordingActivityTracker.h> |
| 35 | |
| 36 | #include <AudioPolicyInterface.h> |
| 37 | #include <android_audio_policy_configuration_V7_0-enums.h> |
| 38 | #include <fuzzer/FuzzedDataProvider.h> |
| 39 | #include <tests/AudioPolicyManagerTestClient.h> |
| 40 | #include <tests/AudioPolicyTestClient.h> |
| 41 | #include <tests/AudioPolicyTestManager.h> |
| 42 | #include <xsdc/XsdcSupport.h> |
| 43 | |
| 44 | using namespace android; |
| 45 | |
| 46 | namespace xsd { |
| 47 | using namespace ::android::audio::policy::configuration::V7_0; |
| 48 | } |
| 49 | |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 50 | using media::permission::Identity; |
| 51 | |
Mufaddal Chakera | 253887f | 2020-12-09 17:17:37 +0530 | [diff] [blame] | 52 | static const std::vector<audio_format_t> kAudioFormats = [] { |
| 53 | std::vector<audio_format_t> result; |
| 54 | for (const auto enumVal : xsdc_enum_range<xsd::AudioFormat>{}) { |
| 55 | audio_format_t audioFormatHal; |
| 56 | std::string audioFormat = toString(enumVal); |
| 57 | if (audio_format_from_string(audioFormat.c_str(), &audioFormatHal)) { |
| 58 | result.push_back(audioFormatHal); |
| 59 | } |
| 60 | } |
| 61 | return result; |
| 62 | }(); |
| 63 | |
| 64 | static const std::vector<audio_channel_mask_t> kAudioChannelOutMasks = [] { |
| 65 | std::vector<audio_channel_mask_t> result; |
| 66 | for (const auto enumVal : xsdc_enum_range<xsd::AudioChannelMask>{}) { |
| 67 | audio_channel_mask_t audioChannelMaskHal; |
| 68 | std::string audioChannelMask = toString(enumVal); |
| 69 | if (enumVal != xsd::AudioChannelMask::AUDIO_CHANNEL_NONE && |
| 70 | audioChannelMask.find("_IN_") == std::string::npos && |
| 71 | audio_channel_mask_from_string(audioChannelMask.c_str(), &audioChannelMaskHal)) { |
| 72 | result.push_back(audioChannelMaskHal); |
| 73 | } |
| 74 | } |
| 75 | return result; |
| 76 | }(); |
| 77 | |
| 78 | static const std::vector<audio_channel_mask_t> kAudioChannelInMasks = [] { |
| 79 | std::vector<audio_channel_mask_t> result; |
| 80 | for (const auto enumVal : xsdc_enum_range<xsd::AudioChannelMask>{}) { |
| 81 | audio_channel_mask_t audioChannelMaskHal; |
| 82 | std::string audioChannelMask = toString(enumVal); |
| 83 | if (enumVal != xsd::AudioChannelMask::AUDIO_CHANNEL_NONE && |
| 84 | audioChannelMask.find("_OUT_") == std::string::npos && |
| 85 | audio_channel_mask_from_string(audioChannelMask.c_str(), &audioChannelMaskHal)) { |
| 86 | result.push_back(audioChannelMaskHal); |
| 87 | } |
| 88 | } |
| 89 | return result; |
| 90 | }(); |
| 91 | |
| 92 | static const std::vector<audio_output_flags_t> kAudioOutputFlags = [] { |
| 93 | std::vector<audio_output_flags_t> result; |
| 94 | for (const auto enumVal : xsdc_enum_range<xsd::AudioInOutFlag>{}) { |
| 95 | audio_output_flags_t audioOutputFlagHal; |
| 96 | std::string audioOutputFlag = toString(enumVal); |
| 97 | if (audioOutputFlag.find("_OUTPUT_") != std::string::npos && |
| 98 | audio_output_flag_from_string(audioOutputFlag.c_str(), &audioOutputFlagHal)) { |
| 99 | result.push_back(audioOutputFlagHal); |
| 100 | } |
| 101 | } |
| 102 | return result; |
| 103 | }(); |
| 104 | |
| 105 | static const std::vector<audio_devices_t> kAudioDevices = [] { |
| 106 | std::vector<audio_devices_t> result; |
| 107 | for (const auto enumVal : xsdc_enum_range<xsd::AudioDevice>{}) { |
| 108 | audio_devices_t audioDeviceHal; |
| 109 | std::string audioDevice = toString(enumVal); |
| 110 | if (audio_device_from_string(audioDevice.c_str(), &audioDeviceHal)) { |
| 111 | result.push_back(audioDeviceHal); |
| 112 | } |
| 113 | } |
| 114 | return result; |
| 115 | }(); |
| 116 | |
| 117 | static const std::vector<audio_usage_t> kAudioUsages = [] { |
| 118 | std::vector<audio_usage_t> result; |
| 119 | for (const auto enumVal : xsdc_enum_range<xsd::AudioUsage>{}) { |
| 120 | audio_usage_t audioUsageHal; |
| 121 | std::string audioUsage = toString(enumVal); |
| 122 | if (audio_usage_from_string(audioUsage.c_str(), &audioUsageHal)) { |
| 123 | result.push_back(audioUsageHal); |
| 124 | } |
| 125 | } |
| 126 | return result; |
| 127 | }(); |
| 128 | |
| 129 | static const std::vector<audio_source_t> kAudioSources = [] { |
| 130 | std::vector<audio_source_t> result; |
| 131 | for (const auto enumVal : xsdc_enum_range<xsd::AudioSource>{}) { |
| 132 | audio_source_t audioSourceHal; |
| 133 | std::string audioSource = toString(enumVal); |
| 134 | if (audio_source_from_string(audioSource.c_str(), &audioSourceHal)) { |
| 135 | result.push_back(audioSourceHal); |
| 136 | } |
| 137 | } |
| 138 | return result; |
| 139 | }(); |
| 140 | |
| 141 | static const std::vector<audio_content_type_t> kAudioContentTypes = [] { |
| 142 | std::vector<audio_content_type_t> result; |
| 143 | for (const auto enumVal : xsdc_enum_range<xsd::AudioContentType>{}) { |
| 144 | audio_content_type_t audioContentTypeHal; |
| 145 | std::string audioContentType = toString(enumVal); |
| 146 | if (audio_content_type_from_string(audioContentType.c_str(), &audioContentTypeHal)) { |
| 147 | result.push_back(audioContentTypeHal); |
| 148 | } |
| 149 | } |
| 150 | return result; |
| 151 | }(); |
| 152 | |
| 153 | std::vector<int> kMixTypes = {MIX_TYPE_PLAYERS, MIX_TYPE_RECORDERS}; |
| 154 | |
| 155 | std::vector<int> kMixRouteFlags = {MIX_ROUTE_FLAG_RENDER, MIX_ROUTE_FLAG_LOOP_BACK, |
| 156 | MIX_ROUTE_FLAG_LOOP_BACK_AND_RENDER, MIX_ROUTE_FLAG_ALL}; |
| 157 | |
| 158 | std::vector<audio_flags_mask_t> kAudioFlagMasks = { |
| 159 | AUDIO_FLAG_NONE, AUDIO_FLAG_AUDIBILITY_ENFORCED, |
| 160 | AUDIO_FLAG_SECURE, AUDIO_FLAG_SCO, |
| 161 | AUDIO_FLAG_BEACON, AUDIO_FLAG_HW_AV_SYNC, |
| 162 | AUDIO_FLAG_HW_HOTWORD, AUDIO_FLAG_BYPASS_INTERRUPTION_POLICY, |
| 163 | AUDIO_FLAG_BYPASS_MUTE, AUDIO_FLAG_LOW_LATENCY, |
| 164 | AUDIO_FLAG_DEEP_BUFFER, AUDIO_FLAG_NO_MEDIA_PROJECTION, |
| 165 | AUDIO_FLAG_MUTE_HAPTIC, AUDIO_FLAG_NO_SYSTEM_CAPTURE, |
| 166 | AUDIO_FLAG_CAPTURE_PRIVATE}; |
| 167 | |
| 168 | std::vector<audio_policy_dev_state_t> kAudioPolicyDeviceStates = { |
| 169 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 170 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 171 | AUDIO_POLICY_DEVICE_STATE_CNT, |
| 172 | }; |
| 173 | |
| 174 | std::vector<uint32_t> kSamplingRates = {8000, 16000, 44100, 48000, 88200, 96000}; |
| 175 | |
| 176 | template <typename T> |
| 177 | T getValueFromVector(FuzzedDataProvider *fdp, std::vector<T> arr) { |
| 178 | if (fdp->ConsumeBool()) { |
| 179 | return arr[fdp->ConsumeIntegralInRange<int32_t>(0, arr.size() - 1)]; |
| 180 | } else { |
| 181 | return (T)fdp->ConsumeIntegral<uint32_t>(); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | class AudioPolicyManagerFuzzer { |
| 186 | public: |
| 187 | explicit AudioPolicyManagerFuzzer(FuzzedDataProvider *fdp); |
| 188 | virtual ~AudioPolicyManagerFuzzer() = default; |
| 189 | virtual bool initialize(); |
| 190 | virtual void SetUpManagerConfig(); |
| 191 | bool getOutputForAttr(audio_port_handle_t *selectedDeviceId, audio_format_t format, |
| 192 | audio_channel_mask_t channelMask, int sampleRate, |
| 193 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE, |
| 194 | audio_io_handle_t *output = nullptr, |
| 195 | audio_port_handle_t *portId = nullptr, audio_attributes_t attr = {}); |
| 196 | bool getInputForAttr(const audio_attributes_t &attr, audio_unique_id_t riid, |
| 197 | audio_port_handle_t *selectedDeviceId, audio_format_t format, |
| 198 | audio_channel_mask_t channelMask, int sampleRate, |
| 199 | audio_input_flags_t flags = AUDIO_INPUT_FLAG_NONE, |
| 200 | audio_port_handle_t *portId = nullptr); |
| 201 | bool findDevicePort(audio_port_role_t role, audio_devices_t deviceType, |
| 202 | const std::string &address, audio_port_v7 *foundPort); |
| 203 | static audio_port_handle_t getDeviceIdFromPatch(const struct audio_patch *patch); |
| 204 | audio_patch createFuzzedPatch(); |
| 205 | void fuzzPatchCreation(); |
| 206 | virtual void process(); |
| 207 | |
| 208 | protected: |
| 209 | std::unique_ptr<AudioPolicyManagerTestClient> mClient{new AudioPolicyManagerTestClient}; |
| 210 | std::unique_ptr<AudioPolicyTestManager> mManager{new AudioPolicyTestManager(mClient.get())}; |
| 211 | FuzzedDataProvider *mFdp; |
| 212 | }; |
| 213 | |
| 214 | AudioPolicyManagerFuzzer::AudioPolicyManagerFuzzer(FuzzedDataProvider *fdp) |
| 215 | : mFdp(fdp) {} |
| 216 | |
| 217 | bool AudioPolicyManagerFuzzer::initialize() { |
| 218 | if (mFdp->remaining_bytes() < 1) { |
| 219 | return false; |
| 220 | } |
| 221 | // init code |
| 222 | SetUpManagerConfig(); |
| 223 | |
| 224 | if (mManager->initialize() != NO_ERROR) { |
| 225 | return false; |
| 226 | } |
| 227 | if (mManager->initCheck() != NO_ERROR) { |
| 228 | return false; |
| 229 | } |
| 230 | return true; |
| 231 | } |
| 232 | |
| 233 | void AudioPolicyManagerFuzzer::SetUpManagerConfig() { mManager->getConfig().setDefault(); } |
| 234 | |
| 235 | bool AudioPolicyManagerFuzzer::getOutputForAttr( |
| 236 | audio_port_handle_t *selectedDeviceId, audio_format_t format, audio_channel_mask_t channelMask, |
| 237 | int sampleRate, audio_output_flags_t flags, audio_io_handle_t *output, |
| 238 | audio_port_handle_t *portId, audio_attributes_t attr) { |
| 239 | audio_io_handle_t localOutput; |
| 240 | if (!output) output = &localOutput; |
| 241 | *output = AUDIO_IO_HANDLE_NONE; |
| 242 | audio_stream_type_t stream = AUDIO_STREAM_DEFAULT; |
| 243 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 244 | config.sample_rate = sampleRate; |
| 245 | config.channel_mask = channelMask; |
| 246 | config.format = format; |
| 247 | audio_port_handle_t localPortId; |
| 248 | if (!portId) portId = &localPortId; |
| 249 | *portId = AUDIO_PORT_HANDLE_NONE; |
| 250 | AudioPolicyInterface::output_type_t outputType; |
| 251 | |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 252 | // TODO b/182392769: use identity util |
| 253 | Identity i; |
| 254 | i.uid = 0; |
| 255 | if (mManager->getOutputForAttr(&attr, output, AUDIO_SESSION_NONE, &stream, i, &config, |
Mufaddal Chakera | 253887f | 2020-12-09 17:17:37 +0530 | [diff] [blame] | 256 | &flags, selectedDeviceId, portId, {}, &outputType) != OK) { |
| 257 | return false; |
| 258 | } |
| 259 | if (*output == AUDIO_IO_HANDLE_NONE || *portId == AUDIO_PORT_HANDLE_NONE) { |
| 260 | return false; |
| 261 | } |
| 262 | return true; |
| 263 | } |
| 264 | |
| 265 | bool AudioPolicyManagerFuzzer::getInputForAttr( |
| 266 | const audio_attributes_t &attr, audio_unique_id_t riid, audio_port_handle_t *selectedDeviceId, |
| 267 | audio_format_t format, audio_channel_mask_t channelMask, int sampleRate, |
| 268 | audio_input_flags_t flags, audio_port_handle_t *portId) { |
| 269 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
| 270 | audio_config_base_t config = AUDIO_CONFIG_BASE_INITIALIZER; |
| 271 | config.sample_rate = sampleRate; |
| 272 | config.channel_mask = channelMask; |
| 273 | config.format = format; |
| 274 | audio_port_handle_t localPortId; |
| 275 | if (!portId) portId = &localPortId; |
| 276 | *portId = AUDIO_PORT_HANDLE_NONE; |
| 277 | AudioPolicyInterface::input_type_t inputType; |
| 278 | |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 279 | Identity i; |
| 280 | i.uid = 0; |
| 281 | if (mManager->getInputForAttr(&attr, &input, riid, AUDIO_SESSION_NONE, i, &config, |
Mufaddal Chakera | 253887f | 2020-12-09 17:17:37 +0530 | [diff] [blame] | 282 | flags, selectedDeviceId, &inputType, portId) != OK) { |
| 283 | return false; |
| 284 | } |
| 285 | if (*portId == AUDIO_PORT_HANDLE_NONE || input == AUDIO_IO_HANDLE_NONE) { |
| 286 | return false; |
| 287 | } |
| 288 | return true; |
| 289 | } |
| 290 | |
| 291 | bool AudioPolicyManagerFuzzer::findDevicePort(audio_port_role_t role, audio_devices_t deviceType, |
| 292 | const std::string &address, |
| 293 | audio_port_v7 *foundPort) { |
| 294 | uint32_t numPorts = 0; |
| 295 | uint32_t generation1; |
| 296 | status_t ret; |
| 297 | |
| 298 | ret = mManager->listAudioPorts(role, AUDIO_PORT_TYPE_DEVICE, &numPorts, nullptr, &generation1); |
| 299 | if (ret != NO_ERROR) { |
| 300 | return false; |
| 301 | } |
| 302 | |
| 303 | uint32_t generation2; |
| 304 | struct audio_port_v7 ports[numPorts]; |
| 305 | ret = mManager->listAudioPorts(role, AUDIO_PORT_TYPE_DEVICE, &numPorts, ports, &generation2); |
| 306 | if (ret != NO_ERROR) { |
| 307 | return false; |
| 308 | } |
| 309 | |
| 310 | for (const auto &port : ports) { |
| 311 | if (port.role == role && port.ext.device.type == deviceType && |
| 312 | (strncmp(port.ext.device.address, address.c_str(), AUDIO_DEVICE_MAX_ADDRESS_LEN) == |
| 313 | 0)) { |
| 314 | if (foundPort) *foundPort = port; |
| 315 | return true; |
| 316 | } |
| 317 | } |
| 318 | return false; |
| 319 | } |
| 320 | |
| 321 | audio_port_handle_t AudioPolicyManagerFuzzer::getDeviceIdFromPatch( |
| 322 | const struct audio_patch *patch) { |
| 323 | if (patch->num_sources != 0 && patch->num_sinks != 0) { |
| 324 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
| 325 | return patch->sinks[0].id; |
| 326 | } else { |
| 327 | return patch->sources[0].id; |
| 328 | } |
| 329 | } |
| 330 | return AUDIO_PORT_HANDLE_NONE; |
| 331 | } |
| 332 | |
| 333 | audio_patch AudioPolicyManagerFuzzer::createFuzzedPatch() { |
| 334 | audio_patch patch{}; |
| 335 | patch.id = mFdp->ConsumeIntegral<uint32_t>(); |
| 336 | patch.num_sources = mFdp->ConsumeIntegralInRange(0, AUDIO_PATCH_PORTS_MAX); |
| 337 | for (int i = 0; i < patch.num_sources; ++i) { |
| 338 | audio_port_config config{}; |
| 339 | std::vector<uint8_t> bytes = mFdp->ConsumeBytes<uint8_t>(sizeof(config)); |
| 340 | memcpy(reinterpret_cast<uint8_t *>(&config), &bytes[0], bytes.size()); |
| 341 | patch.sources[i] = config; |
| 342 | } |
| 343 | patch.num_sinks = mFdp->ConsumeIntegralInRange(0, AUDIO_PATCH_PORTS_MAX); |
| 344 | for (int i = 0; i < patch.num_sinks; ++i) { |
| 345 | audio_port_config config{}; |
| 346 | std::vector<uint8_t> bytes = mFdp->ConsumeBytes<uint8_t>(sizeof(config)); |
| 347 | memcpy(reinterpret_cast<uint8_t *>(&config), &bytes[0], bytes.size()); |
| 348 | patch.sinks[i] = config; |
| 349 | } |
| 350 | return patch; |
| 351 | } |
| 352 | |
| 353 | void AudioPolicyManagerFuzzer::fuzzPatchCreation() { |
| 354 | if (mFdp->remaining_bytes()) { |
| 355 | audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE; |
| 356 | uid_t uid = mFdp->ConsumeIntegral<uint32_t>(); |
| 357 | |
| 358 | // create a fuzzed patch |
| 359 | handle = AUDIO_PATCH_HANDLE_NONE; |
| 360 | audio_patch patch = createFuzzedPatch(); |
| 361 | uid = mFdp->ConsumeIntegral<uint32_t>(); |
| 362 | if (mManager->createAudioPatch(&patch, &handle, uid) == NO_ERROR) { |
| 363 | mManager->releaseAudioPatch(handle, uid); |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | void AudioPolicyManagerFuzzer::process() { |
| 369 | if (initialize()) { |
| 370 | fuzzPatchCreation(); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | class AudioPolicyManagerFuzzerWithConfigurationFile : public AudioPolicyManagerFuzzer { |
| 375 | public: |
| 376 | explicit AudioPolicyManagerFuzzerWithConfigurationFile(FuzzedDataProvider *fdp) |
| 377 | : AudioPolicyManagerFuzzer(fdp){}; |
| 378 | |
| 379 | protected: |
| 380 | void SetUpManagerConfig() override; |
| 381 | virtual std::string getConfigFile(); |
| 382 | void traverseAndFuzzXML(xmlDocPtr pDoc, xmlNodePtr curr); |
| 383 | std::string fuzzXML(std::string xmlPath); |
| 384 | |
| 385 | static inline const std::string sExecutableDir = base::GetExecutableDirectory() + "/"; |
| 386 | static inline const std::string sDefaultConfig = |
| 387 | sExecutableDir + "data/test_audio_policy_configuration.xml"; |
| 388 | static inline const std::string sFuzzedConfig = sExecutableDir + "fuzzed.xml";; |
| 389 | }; |
| 390 | |
| 391 | std::string AudioPolicyManagerFuzzerWithConfigurationFile::getConfigFile() { |
| 392 | return fuzzXML(sDefaultConfig); |
| 393 | } |
| 394 | |
| 395 | void AudioPolicyManagerFuzzerWithConfigurationFile::SetUpManagerConfig() { |
| 396 | deserializeAudioPolicyFile(getConfigFile().c_str(), &mManager->getConfig()); |
| 397 | } |
| 398 | |
| 399 | void AudioPolicyManagerFuzzerWithConfigurationFile::traverseAndFuzzXML(xmlDocPtr pDoc, |
| 400 | xmlNodePtr curr) { |
| 401 | if (curr == nullptr) { |
| 402 | return; |
| 403 | } |
| 404 | |
| 405 | xmlAttr *attribute = curr->properties; |
| 406 | while (attribute) { |
| 407 | if (!xmlStrcmp(attribute->name, reinterpret_cast<const xmlChar *>("format"))) { |
| 408 | const char *newFormat = |
| 409 | audio_format_to_string(getValueFromVector<audio_format_t>(mFdp, kAudioFormats)); |
| 410 | xmlSetProp(curr, attribute->name, reinterpret_cast<const xmlChar *>(newFormat)); |
| 411 | } |
| 412 | if (!xmlStrcmp(attribute->name, reinterpret_cast<const xmlChar *>("flags"))) { |
| 413 | std::string newFlag = ""; |
| 414 | uint16_t numFlags = std::max((uint16_t)1, mFdp->ConsumeIntegral<uint16_t>()); |
| 415 | for (uint16_t i = 0; i < numFlags; ++i) { |
| 416 | newFlag += std::string(audio_output_flag_to_string( |
| 417 | getValueFromVector<audio_output_flags_t>(mFdp, kAudioOutputFlags))); |
| 418 | if (i != (numFlags - 1)) { |
| 419 | newFlag += std::string("|"); |
| 420 | } |
| 421 | } |
| 422 | xmlSetProp(curr, attribute->name, reinterpret_cast<const xmlChar *>(newFlag.c_str())); |
| 423 | } |
| 424 | if (!xmlStrcmp(attribute->name, reinterpret_cast<const xmlChar *>("samplingRates"))) { |
| 425 | std::string newRate = ""; |
| 426 | uint16_t numRates = std::max((uint16_t)1, mFdp->ConsumeIntegral<uint16_t>()); |
| 427 | for (uint16_t i = 0; i < numRates; ++i) { |
| 428 | newRate += std::to_string(getValueFromVector<uint32_t>(mFdp, kSamplingRates)); |
| 429 | if (i != (numRates - 1)) { |
| 430 | newRate += std::string(","); |
| 431 | } |
| 432 | } |
| 433 | xmlSetProp(curr, attribute->name, reinterpret_cast<const xmlChar *>(newRate.c_str())); |
| 434 | } |
| 435 | if (!xmlStrcmp(attribute->name, reinterpret_cast<const xmlChar *>("channelMasks"))) { |
| 436 | int isOutMask = -1; |
| 437 | char *value = |
| 438 | reinterpret_cast<char *>(xmlNodeListGetString(pDoc, attribute->children, 1)); |
| 439 | if (std::string(value).find(std::string("_OUT_")) != std::string::npos) { |
| 440 | // OUT mask |
| 441 | isOutMask = 1; |
| 442 | } else if (std::string(value).find(std::string("_IN_")) != std::string::npos) { |
| 443 | // IN mask |
| 444 | isOutMask = 0; |
| 445 | } |
| 446 | if (isOutMask != -1) { |
| 447 | std::string newMask = ""; |
| 448 | uint16_t numMasks = std::max((uint16_t)1, mFdp->ConsumeIntegral<uint16_t>()); |
| 449 | for (uint16_t i = 0; i < numMasks; ++i) { |
| 450 | if (isOutMask) { |
| 451 | newMask += std::string(audio_channel_out_mask_to_string( |
| 452 | getValueFromVector<audio_channel_mask_t>(mFdp, kAudioChannelOutMasks))); |
| 453 | } else { |
| 454 | newMask += std::string(audio_channel_in_mask_to_string( |
| 455 | getValueFromVector<audio_channel_mask_t>(mFdp, kAudioChannelInMasks))); |
| 456 | } |
| 457 | if (i != (numMasks - 1)) { |
| 458 | newMask += std::string(","); |
| 459 | } |
| 460 | } |
| 461 | xmlSetProp(curr, attribute->name, |
| 462 | reinterpret_cast<const xmlChar *>(newMask.c_str())); |
| 463 | } |
| 464 | xmlFree(value); |
| 465 | } |
| 466 | attribute = attribute->next; |
| 467 | } |
| 468 | |
| 469 | curr = curr->xmlChildrenNode; |
| 470 | while (curr != nullptr) { |
| 471 | traverseAndFuzzXML(pDoc, curr); |
| 472 | curr = curr->next; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | std::string AudioPolicyManagerFuzzerWithConfigurationFile::fuzzXML(std::string xmlPath) { |
| 477 | std::string outPath = sFuzzedConfig; |
| 478 | |
| 479 | // Load in the xml file from disk |
| 480 | xmlDocPtr pDoc = xmlParseFile(xmlPath.c_str()); |
| 481 | xmlNodePtr root = xmlDocGetRootElement(pDoc); |
| 482 | |
| 483 | traverseAndFuzzXML(pDoc, root); |
| 484 | |
| 485 | // Save the document back out to disk. |
| 486 | xmlSaveFileEnc(outPath.c_str(), pDoc, "UTF-8"); |
| 487 | xmlFreeDoc(pDoc); |
| 488 | |
| 489 | return outPath; |
| 490 | } |
| 491 | |
| 492 | class AudioPolicyManagerFuzzerMsd : public AudioPolicyManagerFuzzerWithConfigurationFile { |
| 493 | public: |
| 494 | explicit AudioPolicyManagerFuzzerMsd(FuzzedDataProvider *fdp) |
| 495 | : AudioPolicyManagerFuzzerWithConfigurationFile(fdp) {} |
| 496 | |
| 497 | protected: |
| 498 | std::string getConfigFile() override; |
| 499 | |
| 500 | static inline const std::string sMsdConfig = |
| 501 | sExecutableDir + "data/test_audio_policy_msd_configuration.xml"; |
| 502 | }; |
| 503 | |
| 504 | std::string AudioPolicyManagerFuzzerMsd::getConfigFile() { return fuzzXML(sMsdConfig); } |
| 505 | |
| 506 | using PolicyMixTuple = std::tuple<audio_usage_t, audio_source_t, uint32_t>; |
| 507 | |
| 508 | class AudioPolicyManagerFuzzerDynamicPolicy : public AudioPolicyManagerFuzzerWithConfigurationFile { |
| 509 | public: |
| 510 | explicit AudioPolicyManagerFuzzerDynamicPolicy(FuzzedDataProvider *fdp) |
| 511 | : AudioPolicyManagerFuzzerWithConfigurationFile(fdp){}; |
| 512 | ~AudioPolicyManagerFuzzerDynamicPolicy() override; |
| 513 | void process() override; |
| 514 | |
| 515 | protected: |
| 516 | status_t addPolicyMix(int mixType, int mixFlag, audio_devices_t deviceType, |
| 517 | std::string mixAddress, const audio_config_t &audioConfig, |
| 518 | const std::vector<PolicyMixTuple> &rules); |
| 519 | void clearPolicyMix(); |
| 520 | void registerPolicyMixes(); |
| 521 | void unregisterPolicyMixes(); |
| 522 | |
| 523 | Vector<AudioMix> mAudioMixes; |
| 524 | const std::string mMixAddress = "remote_submix_media"; |
| 525 | }; |
| 526 | |
| 527 | AudioPolicyManagerFuzzerDynamicPolicy::~AudioPolicyManagerFuzzerDynamicPolicy() { |
| 528 | clearPolicyMix(); |
| 529 | } |
| 530 | |
| 531 | status_t AudioPolicyManagerFuzzerDynamicPolicy::addPolicyMix( |
| 532 | int mixType, int mixFlag, audio_devices_t deviceType, std::string mixAddress, |
| 533 | const audio_config_t &audioConfig, const std::vector<PolicyMixTuple> &rules) { |
| 534 | Vector<AudioMixMatchCriterion> myMixMatchCriteria; |
| 535 | |
| 536 | for (const auto &rule : rules) { |
| 537 | myMixMatchCriteria.add( |
| 538 | AudioMixMatchCriterion(std::get<0>(rule), std::get<1>(rule), std::get<2>(rule))); |
| 539 | } |
| 540 | |
| 541 | AudioMix myAudioMix(myMixMatchCriteria, mixType, audioConfig, mixFlag, |
| 542 | String8(mixAddress.c_str()), 0); |
| 543 | myAudioMix.mDeviceType = deviceType; |
| 544 | // Clear mAudioMix before add new one to make sure we don't add already existing mixes. |
| 545 | mAudioMixes.clear(); |
| 546 | mAudioMixes.add(myAudioMix); |
| 547 | |
| 548 | // As the policy mixes registration may fail at some case, |
| 549 | // caller need to check the returned status. |
| 550 | status_t ret = mManager->registerPolicyMixes(mAudioMixes); |
| 551 | return ret; |
| 552 | } |
| 553 | |
| 554 | void AudioPolicyManagerFuzzerDynamicPolicy::clearPolicyMix() { |
| 555 | if (mManager != nullptr) { |
| 556 | mManager->unregisterPolicyMixes(mAudioMixes); |
| 557 | } |
| 558 | mAudioMixes.clear(); |
| 559 | } |
| 560 | |
| 561 | void AudioPolicyManagerFuzzerDynamicPolicy::registerPolicyMixes() { |
| 562 | const uint32_t numPolicies = mFdp->ConsumeIntegralInRange<uint32_t>(1, MAX_MIXES_PER_POLICY); |
| 563 | |
| 564 | for (int i = 0; i < numPolicies; ++i) { |
| 565 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 566 | audioConfig.channel_mask = getValueFromVector<audio_channel_mask_t>( |
| 567 | mFdp, mFdp->ConsumeBool() ? kAudioChannelInMasks : kAudioChannelOutMasks); |
| 568 | audioConfig.format = getValueFromVector<audio_format_t>(mFdp, kAudioFormats); |
| 569 | audioConfig.sample_rate = getValueFromVector<uint32_t>(mFdp, kSamplingRates); |
| 570 | addPolicyMix(getValueFromVector<int>(mFdp, kMixTypes), |
| 571 | getValueFromVector<int>(mFdp, kMixRouteFlags), |
| 572 | getValueFromVector<audio_devices_t>(mFdp, kAudioDevices), "", audioConfig, |
| 573 | std::vector<PolicyMixTuple>()); |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | void AudioPolicyManagerFuzzerDynamicPolicy::unregisterPolicyMixes() { |
| 578 | mManager->unregisterPolicyMixes(mAudioMixes); |
| 579 | } |
| 580 | |
| 581 | void AudioPolicyManagerFuzzerDynamicPolicy::process() { |
| 582 | if (initialize()) { |
| 583 | registerPolicyMixes(); |
| 584 | fuzzPatchCreation(); |
| 585 | unregisterPolicyMixes(); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | class AudioPolicyManagerFuzzerDPNoRemoteSubmixModule |
| 590 | : public AudioPolicyManagerFuzzerDynamicPolicy { |
| 591 | public: |
| 592 | explicit AudioPolicyManagerFuzzerDPNoRemoteSubmixModule(FuzzedDataProvider *fdp) |
| 593 | : AudioPolicyManagerFuzzerDynamicPolicy(fdp){}; |
| 594 | |
| 595 | protected: |
| 596 | std::string getConfigFile() override; |
| 597 | |
| 598 | static inline const std::string sPrimaryOnlyConfig = |
| 599 | sExecutableDir + "data/test_audio_policy_primary_only_configuration.xml"; |
| 600 | }; |
| 601 | |
| 602 | std::string AudioPolicyManagerFuzzerDPNoRemoteSubmixModule::getConfigFile() { |
| 603 | return fuzzXML(sPrimaryOnlyConfig); |
| 604 | } |
| 605 | |
| 606 | class AudioPolicyManagerFuzzerDPPlaybackReRouting : public AudioPolicyManagerFuzzerDynamicPolicy { |
| 607 | public: |
| 608 | explicit AudioPolicyManagerFuzzerDPPlaybackReRouting(FuzzedDataProvider *fdp); |
| 609 | ~AudioPolicyManagerFuzzerDPPlaybackReRouting() override; |
| 610 | void process() override; |
| 611 | |
| 612 | protected: |
| 613 | bool initialize() override; |
| 614 | void playBackReRouting(); |
| 615 | |
| 616 | std::unique_ptr<RecordingActivityTracker> mTracker; |
| 617 | |
| 618 | std::vector<PolicyMixTuple> mUsageRules = { |
| 619 | {AUDIO_USAGE_MEDIA, AUDIO_SOURCE_DEFAULT, RULE_MATCH_ATTRIBUTE_USAGE}, |
| 620 | {AUDIO_USAGE_ALARM, AUDIO_SOURCE_DEFAULT, RULE_MATCH_ATTRIBUTE_USAGE}}; |
| 621 | |
| 622 | struct audio_port_v7 mInjectionPort; |
| 623 | audio_port_handle_t mPortId = AUDIO_PORT_HANDLE_NONE; |
| 624 | audio_config_t mAudioConfig; |
| 625 | }; |
| 626 | |
| 627 | AudioPolicyManagerFuzzerDPPlaybackReRouting::AudioPolicyManagerFuzzerDPPlaybackReRouting( |
| 628 | FuzzedDataProvider *fdp) |
| 629 | : AudioPolicyManagerFuzzerDynamicPolicy(fdp) { |
| 630 | const uint32_t numRules = mFdp->ConsumeIntegralInRange<uint32_t>(1, 10); |
| 631 | for (int i = 0; i < numRules; ++i) { |
| 632 | PolicyMixTuple rule = {getValueFromVector<audio_usage_t>(mFdp, kAudioUsages), |
| 633 | getValueFromVector<audio_source_t>(mFdp, kAudioSources), |
| 634 | RULE_MATCH_ATTRIBUTE_USAGE}; |
| 635 | mUsageRules.push_back(rule); |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | AudioPolicyManagerFuzzerDPPlaybackReRouting::~AudioPolicyManagerFuzzerDPPlaybackReRouting() { |
| 640 | mManager->stopInput(mPortId); |
| 641 | } |
| 642 | |
| 643 | bool AudioPolicyManagerFuzzerDPPlaybackReRouting::initialize() { |
Mufaddal Chakera | 5d9a9e0 | 2021-02-16 14:18:54 +0530 | [diff] [blame] | 644 | if (!AudioPolicyManagerFuzzerDynamicPolicy::initialize()) { |
| 645 | return false; |
| 646 | } |
Mufaddal Chakera | 253887f | 2020-12-09 17:17:37 +0530 | [diff] [blame] | 647 | mTracker.reset(new RecordingActivityTracker()); |
| 648 | |
| 649 | mAudioConfig = AUDIO_CONFIG_INITIALIZER; |
| 650 | mAudioConfig.channel_mask = |
| 651 | getValueFromVector<audio_channel_mask_t>(mFdp, kAudioChannelOutMasks); |
| 652 | mAudioConfig.format = getValueFromVector<audio_format_t>(mFdp, kAudioFormats); |
| 653 | mAudioConfig.sample_rate = getValueFromVector<uint32_t>(mFdp, kSamplingRates); |
| 654 | status_t ret = addPolicyMix(getValueFromVector<int>(mFdp, kMixTypes), |
| 655 | getValueFromVector<int>(mFdp, kMixRouteFlags), |
| 656 | getValueFromVector<audio_devices_t>(mFdp, kAudioDevices), |
| 657 | mMixAddress, mAudioConfig, mUsageRules); |
| 658 | if (ret != NO_ERROR) { |
| 659 | return false; |
| 660 | } |
| 661 | |
| 662 | struct audio_port_v7 extractionPort; |
| 663 | findDevicePort(AUDIO_PORT_ROLE_SOURCE, getValueFromVector<audio_devices_t>(mFdp, kAudioDevices), |
| 664 | mMixAddress, &extractionPort); |
| 665 | |
| 666 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 667 | audio_source_t source = getValueFromVector<audio_source_t>(mFdp, kAudioSources); |
| 668 | audio_attributes_t attr = {AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, source, |
| 669 | AUDIO_FLAG_NONE, ""}; |
| 670 | std::string tags = "addr=" + mMixAddress; |
| 671 | strncpy(attr.tags, tags.c_str(), AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1); |
| 672 | getInputForAttr(attr, mTracker->getRiid(), &selectedDeviceId, mAudioConfig.format, |
| 673 | mAudioConfig.channel_mask, mAudioConfig.sample_rate, AUDIO_INPUT_FLAG_NONE, |
| 674 | &mPortId); |
| 675 | |
| 676 | ret = mManager->startInput(mPortId); |
| 677 | if (ret != NO_ERROR) { |
| 678 | return false; |
| 679 | } |
| 680 | if (!findDevicePort(AUDIO_PORT_ROLE_SINK, |
| 681 | getValueFromVector<audio_devices_t>(mFdp, kAudioDevices), mMixAddress, |
| 682 | &mInjectionPort)) { |
| 683 | return false; |
| 684 | } |
| 685 | |
| 686 | return true; |
| 687 | } |
| 688 | |
| 689 | void AudioPolicyManagerFuzzerDPPlaybackReRouting::playBackReRouting() { |
| 690 | const uint32_t numTestCases = mFdp->ConsumeIntegralInRange<uint32_t>(1, 10); |
| 691 | for (int i = 0; i < numTestCases; ++i) { |
| 692 | audio_attributes_t attr; |
| 693 | attr.content_type = getValueFromVector<audio_content_type_t>(mFdp, kAudioContentTypes); |
| 694 | attr.usage = getValueFromVector<audio_usage_t>(mFdp, kAudioUsages); |
| 695 | attr.source = getValueFromVector<audio_source_t>(mFdp, kAudioSources); |
| 696 | attr.flags = getValueFromVector<audio_flags_mask_t>(mFdp, kAudioFlagMasks); |
| 697 | std::string tags(mFdp->ConsumeBool() ? "" : "addr=remote_submix_media"); |
| 698 | strncpy(attr.tags, tags.c_str(), AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1); |
| 699 | |
| 700 | audio_port_handle_t playbackRoutedPortId = AUDIO_PORT_HANDLE_NONE; |
| 701 | getOutputForAttr(&playbackRoutedPortId, mAudioConfig.format, mAudioConfig.channel_mask, |
| 702 | mAudioConfig.sample_rate, AUDIO_OUTPUT_FLAG_NONE, nullptr /*output*/, |
| 703 | nullptr /*portId*/, attr); |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | void AudioPolicyManagerFuzzerDPPlaybackReRouting::process() { |
| 708 | if (initialize()) { |
| 709 | playBackReRouting(); |
| 710 | registerPolicyMixes(); |
| 711 | fuzzPatchCreation(); |
| 712 | unregisterPolicyMixes(); |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | class AudioPolicyManagerFuzzerDPMixRecordInjection : public AudioPolicyManagerFuzzerDynamicPolicy { |
| 717 | public: |
| 718 | explicit AudioPolicyManagerFuzzerDPMixRecordInjection(FuzzedDataProvider *fdp); |
| 719 | ~AudioPolicyManagerFuzzerDPMixRecordInjection() override; |
| 720 | void process() override; |
| 721 | |
| 722 | protected: |
| 723 | bool initialize() override; |
| 724 | void recordingInjection(); |
| 725 | |
| 726 | std::unique_ptr<RecordingActivityTracker> mTracker; |
| 727 | |
| 728 | std::vector<PolicyMixTuple> mSourceRules = { |
| 729 | {AUDIO_USAGE_UNKNOWN, AUDIO_SOURCE_CAMCORDER, RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET}, |
| 730 | {AUDIO_USAGE_UNKNOWN, AUDIO_SOURCE_MIC, RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET}, |
| 731 | {AUDIO_USAGE_UNKNOWN, AUDIO_SOURCE_VOICE_COMMUNICATION, |
| 732 | RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET}}; |
| 733 | |
| 734 | struct audio_port_v7 mExtractionPort; |
| 735 | audio_port_handle_t mPortId = AUDIO_PORT_HANDLE_NONE; |
| 736 | audio_config_t mAudioConfig; |
| 737 | }; |
| 738 | |
| 739 | AudioPolicyManagerFuzzerDPMixRecordInjection::AudioPolicyManagerFuzzerDPMixRecordInjection( |
| 740 | FuzzedDataProvider *fdp) |
| 741 | : AudioPolicyManagerFuzzerDynamicPolicy(fdp) { |
| 742 | const uint32_t numRules = mFdp->ConsumeIntegralInRange<uint32_t>(1, 10); |
| 743 | for (int i = 0; i < numRules; ++i) { |
| 744 | PolicyMixTuple rule = {getValueFromVector<audio_usage_t>(mFdp, kAudioUsages), |
| 745 | getValueFromVector<audio_source_t>(mFdp, kAudioSources), |
| 746 | RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET}; |
| 747 | mSourceRules.push_back(rule); |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | AudioPolicyManagerFuzzerDPMixRecordInjection::~AudioPolicyManagerFuzzerDPMixRecordInjection() { |
| 752 | mManager->stopOutput(mPortId); |
| 753 | } |
| 754 | |
| 755 | bool AudioPolicyManagerFuzzerDPMixRecordInjection::initialize() { |
Mufaddal Chakera | 5d9a9e0 | 2021-02-16 14:18:54 +0530 | [diff] [blame] | 756 | if (!AudioPolicyManagerFuzzerDynamicPolicy::initialize()) { |
| 757 | return false; |
| 758 | } |
Mufaddal Chakera | 253887f | 2020-12-09 17:17:37 +0530 | [diff] [blame] | 759 | |
| 760 | mTracker.reset(new RecordingActivityTracker()); |
| 761 | |
| 762 | mAudioConfig = AUDIO_CONFIG_INITIALIZER; |
| 763 | mAudioConfig.channel_mask = |
| 764 | getValueFromVector<audio_channel_mask_t>(mFdp, kAudioChannelInMasks); |
| 765 | mAudioConfig.format = getValueFromVector<audio_format_t>(mFdp, kAudioFormats); |
| 766 | mAudioConfig.sample_rate = getValueFromVector<uint32_t>(mFdp, kSamplingRates); |
| 767 | status_t ret = addPolicyMix(getValueFromVector<int>(mFdp, kMixTypes), |
| 768 | getValueFromVector<int>(mFdp, kMixRouteFlags), |
| 769 | getValueFromVector<audio_devices_t>(mFdp, kAudioDevices), |
| 770 | mMixAddress, mAudioConfig, mSourceRules); |
| 771 | if (ret != NO_ERROR) { |
| 772 | return false; |
| 773 | } |
| 774 | |
| 775 | struct audio_port_v7 injectionPort; |
| 776 | findDevicePort(AUDIO_PORT_ROLE_SINK, getValueFromVector<audio_devices_t>(mFdp, kAudioDevices), |
| 777 | mMixAddress, &injectionPort); |
| 778 | |
| 779 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 780 | audio_usage_t usage = getValueFromVector<audio_usage_t>(mFdp, kAudioUsages); |
| 781 | audio_attributes_t attr = {AUDIO_CONTENT_TYPE_UNKNOWN, usage, AUDIO_SOURCE_DEFAULT, |
| 782 | AUDIO_FLAG_NONE, ""}; |
| 783 | std::string tags = std::string("addr=") + mMixAddress; |
| 784 | strncpy(attr.tags, tags.c_str(), AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1); |
| 785 | getOutputForAttr(&selectedDeviceId, mAudioConfig.format, mAudioConfig.channel_mask, |
| 786 | mAudioConfig.sample_rate /*sampleRate*/, AUDIO_OUTPUT_FLAG_NONE, |
| 787 | nullptr /*output*/, &mPortId, attr); |
| 788 | ret = mManager->startOutput(mPortId); |
| 789 | if (ret != NO_ERROR) { |
| 790 | return false; |
| 791 | } |
| 792 | getDeviceIdFromPatch(mClient->getLastAddedPatch()); |
| 793 | if (!findDevicePort(AUDIO_PORT_ROLE_SOURCE, |
| 794 | getValueFromVector<audio_devices_t>(mFdp, kAudioDevices), mMixAddress, |
| 795 | &mExtractionPort)) { |
| 796 | return false; |
| 797 | } |
| 798 | |
| 799 | return true; |
| 800 | } |
| 801 | |
| 802 | void AudioPolicyManagerFuzzerDPMixRecordInjection::recordingInjection() { |
| 803 | const uint32_t numTestCases = mFdp->ConsumeIntegralInRange<uint32_t>(1, 10); |
| 804 | for (int i = 0; i < numTestCases; ++i) { |
| 805 | audio_attributes_t attr; |
| 806 | attr.content_type = getValueFromVector<audio_content_type_t>(mFdp, kAudioContentTypes); |
| 807 | attr.usage = getValueFromVector<audio_usage_t>(mFdp, kAudioUsages); |
| 808 | attr.source = getValueFromVector<audio_source_t>(mFdp, kAudioSources); |
| 809 | attr.flags = getValueFromVector<audio_flags_mask_t>(mFdp, kAudioFlagMasks); |
| 810 | std::string tags(mFdp->ConsumeBool() ? "" : "addr=remote_submix_media"); |
| 811 | strncpy(attr.tags, tags.c_str(), AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1); |
| 812 | |
| 813 | audio_port_handle_t captureRoutedPortId = AUDIO_PORT_HANDLE_NONE; |
| 814 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE; |
| 815 | getInputForAttr(attr, mTracker->getRiid(), &captureRoutedPortId, mAudioConfig.format, |
| 816 | mAudioConfig.channel_mask, mAudioConfig.sample_rate, AUDIO_INPUT_FLAG_NONE, |
| 817 | &portId); |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | void AudioPolicyManagerFuzzerDPMixRecordInjection::process() { |
| 822 | if (initialize()) { |
| 823 | recordingInjection(); |
| 824 | registerPolicyMixes(); |
| 825 | fuzzPatchCreation(); |
| 826 | unregisterPolicyMixes(); |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | using DeviceConnectionTestParams = |
| 831 | std::tuple<audio_devices_t /*type*/, std::string /*name*/, std::string /*address*/>; |
| 832 | |
| 833 | class AudioPolicyManagerFuzzerDeviceConnection |
| 834 | : public AudioPolicyManagerFuzzerWithConfigurationFile { |
| 835 | public: |
| 836 | explicit AudioPolicyManagerFuzzerDeviceConnection(FuzzedDataProvider *fdp) |
| 837 | : AudioPolicyManagerFuzzerWithConfigurationFile(fdp){}; |
| 838 | void process() override; |
| 839 | |
| 840 | protected: |
| 841 | void setDeviceConnectionState(); |
| 842 | void explicitlyRoutingAfterConnection(); |
| 843 | }; |
| 844 | |
| 845 | void AudioPolicyManagerFuzzerDeviceConnection::setDeviceConnectionState() { |
| 846 | const uint32_t numTestCases = mFdp->ConsumeIntegralInRange<uint32_t>(1, 10); |
| 847 | for (int i = 0; i < numTestCases; ++i) { |
| 848 | const audio_devices_t type = getValueFromVector<audio_devices_t>(mFdp, kAudioDevices); |
| 849 | const std::string name = mFdp->ConsumeRandomLengthString(); |
| 850 | const std::string address = mFdp->ConsumeRandomLengthString(); |
| 851 | mManager->setDeviceConnectionState( |
| 852 | type, getValueFromVector<audio_policy_dev_state_t>(mFdp, kAudioPolicyDeviceStates), |
| 853 | address.c_str(), name.c_str(), getValueFromVector<audio_format_t>(mFdp, kAudioFormats)); |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | void AudioPolicyManagerFuzzerDeviceConnection::explicitlyRoutingAfterConnection() { |
| 858 | const uint32_t numTestCases = mFdp->ConsumeIntegralInRange<uint32_t>(1, 10); |
| 859 | for (int i = 0; i < numTestCases; ++i) { |
| 860 | const audio_devices_t type = getValueFromVector<audio_devices_t>(mFdp, kAudioDevices); |
| 861 | const std::string name = mFdp->ConsumeRandomLengthString(); |
| 862 | const std::string address = mFdp->ConsumeRandomLengthString(); |
| 863 | mManager->setDeviceConnectionState( |
| 864 | type, getValueFromVector<audio_policy_dev_state_t>(mFdp, kAudioPolicyDeviceStates), |
| 865 | address.c_str(), name.c_str(), getValueFromVector<audio_format_t>(mFdp, kAudioFormats)); |
| 866 | |
| 867 | audio_port_v7 devicePort; |
| 868 | const audio_port_role_t role = |
| 869 | audio_is_output_device(type) ? AUDIO_PORT_ROLE_SINK : AUDIO_PORT_ROLE_SOURCE; |
| 870 | findDevicePort(role, type, address, &devicePort); |
| 871 | |
| 872 | audio_port_handle_t routedPortId = devicePort.id; |
| 873 | // Try start input or output according to the device type |
| 874 | if (audio_is_output_devices(type)) { |
| 875 | getOutputForAttr(&routedPortId, getValueFromVector<audio_format_t>(mFdp, kAudioFormats), |
| 876 | getValueFromVector<audio_channel_mask_t>(mFdp, kAudioChannelOutMasks), |
| 877 | getValueFromVector<uint32_t>(mFdp, kSamplingRates), |
| 878 | AUDIO_OUTPUT_FLAG_NONE); |
| 879 | } else if (audio_is_input_device(type)) { |
| 880 | RecordingActivityTracker tracker; |
| 881 | getInputForAttr({}, tracker.getRiid(), &routedPortId, |
| 882 | getValueFromVector<audio_format_t>(mFdp, kAudioFormats), |
| 883 | getValueFromVector<audio_channel_mask_t>(mFdp, kAudioChannelInMasks), |
| 884 | getValueFromVector<uint32_t>(mFdp, kSamplingRates), |
| 885 | AUDIO_INPUT_FLAG_NONE); |
| 886 | } |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | void AudioPolicyManagerFuzzerDeviceConnection::process() { |
| 891 | if (initialize()) { |
| 892 | setDeviceConnectionState(); |
| 893 | explicitlyRoutingAfterConnection(); |
| 894 | fuzzPatchCreation(); |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | class AudioPolicyManagerTVFuzzer : public AudioPolicyManagerFuzzerWithConfigurationFile { |
| 899 | public: |
| 900 | explicit AudioPolicyManagerTVFuzzer(FuzzedDataProvider *fdp) |
| 901 | : AudioPolicyManagerFuzzerWithConfigurationFile(fdp){}; |
| 902 | void process() override; |
| 903 | |
| 904 | protected: |
| 905 | std::string getConfigFile(); |
| 906 | void testHDMIPortSelection(audio_output_flags_t flags); |
| 907 | |
| 908 | static inline const std::string sTvConfig = |
| 909 | AudioPolicyManagerTVFuzzer::sExecutableDir + "data/test_tv_apm_configuration.xml"; |
| 910 | }; |
| 911 | |
| 912 | std::string AudioPolicyManagerTVFuzzer::getConfigFile() { return fuzzXML(sTvConfig); } |
| 913 | |
| 914 | void AudioPolicyManagerTVFuzzer::testHDMIPortSelection(audio_output_flags_t flags) { |
| 915 | audio_devices_t audioDevice = getValueFromVector<audio_devices_t>(mFdp, kAudioDevices); |
| 916 | audio_format_t audioFormat = getValueFromVector<audio_format_t>(mFdp, kAudioFormats); |
| 917 | status_t ret = mManager->setDeviceConnectionState( |
| 918 | audioDevice, AUDIO_POLICY_DEVICE_STATE_AVAILABLE, "" /*address*/, "" /*name*/, audioFormat); |
| 919 | if (ret != NO_ERROR) { |
| 920 | return; |
| 921 | } |
| 922 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 923 | audio_io_handle_t output; |
| 924 | audio_port_handle_t portId; |
| 925 | getOutputForAttr(&selectedDeviceId, getValueFromVector<audio_format_t>(mFdp, kAudioFormats), |
| 926 | getValueFromVector<audio_channel_mask_t>(mFdp, kAudioChannelOutMasks), |
| 927 | getValueFromVector<uint32_t>(mFdp, kSamplingRates), flags, &output, &portId); |
| 928 | sp<SwAudioOutputDescriptor> outDesc = mManager->getOutputs().valueFor(output); |
| 929 | if (outDesc.get() == nullptr) { |
| 930 | return; |
| 931 | } |
| 932 | audio_port_v7 port = {}; |
| 933 | outDesc->toAudioPort(&port); |
| 934 | mManager->releaseOutput(portId); |
| 935 | mManager->setDeviceConnectionState(audioDevice, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 936 | "" /*address*/, "" /*name*/, audioFormat); |
| 937 | } |
| 938 | |
| 939 | void AudioPolicyManagerTVFuzzer::process() { |
| 940 | if (initialize()) { |
| 941 | testHDMIPortSelection(getValueFromVector<audio_output_flags_t>(mFdp, kAudioOutputFlags)); |
| 942 | fuzzPatchCreation(); |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
| 947 | if (size < 1) { |
| 948 | return 0; |
| 949 | } |
| 950 | FuzzedDataProvider fdp = FuzzedDataProvider(data, size); |
| 951 | while (fdp.remaining_bytes() > 0) { |
| 952 | AudioPolicyManagerFuzzer audioPolicyManagerFuzzer(&fdp); |
| 953 | audioPolicyManagerFuzzer.process(); |
| 954 | |
| 955 | AudioPolicyManagerFuzzerMsd audioPolicyManagerFuzzerMsd(&fdp); |
| 956 | audioPolicyManagerFuzzerMsd.process(); |
| 957 | |
| 958 | AudioPolicyManagerFuzzerWithConfigurationFile audioPolicyManagerFuzzerWithConfigurationFile( |
| 959 | &fdp); |
| 960 | audioPolicyManagerFuzzerWithConfigurationFile.process(); |
| 961 | |
| 962 | AudioPolicyManagerFuzzerDynamicPolicy audioPolicyManagerFuzzerDynamicPolicy(&fdp); |
| 963 | audioPolicyManagerFuzzerDynamicPolicy.process(); |
| 964 | |
| 965 | AudioPolicyManagerFuzzerDPNoRemoteSubmixModule |
| 966 | audioPolicyManagerFuzzerDPNoRemoteSubmixModule(&fdp); |
| 967 | audioPolicyManagerFuzzerDPNoRemoteSubmixModule.process(); |
| 968 | |
| 969 | AudioPolicyManagerFuzzerDPPlaybackReRouting audioPolicyManagerFuzzerDPPlaybackReRouting( |
| 970 | &fdp); |
| 971 | audioPolicyManagerFuzzerDPPlaybackReRouting.process(); |
| 972 | |
| 973 | AudioPolicyManagerFuzzerDPMixRecordInjection audioPolicyManagerFuzzerDPMixRecordInjection( |
| 974 | &fdp); |
| 975 | audioPolicyManagerFuzzerDPMixRecordInjection.process(); |
| 976 | |
| 977 | AudioPolicyManagerFuzzerDeviceConnection audioPolicyManagerFuzzerDeviceConnection(&fdp); |
| 978 | audioPolicyManagerFuzzerDeviceConnection.process(); |
| 979 | |
| 980 | AudioPolicyManagerTVFuzzer audioPolicyManagerTVFuzzer(&fdp); |
| 981 | audioPolicyManagerTVFuzzer.process(); |
| 982 | } |
| 983 | return 0; |
| 984 | } |