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