Clean up fuzz helper classes

- Remove OEMCrypto state changes hidden in constructors and destructors.
- Use composition instead of inheritance to structure classes.
- Avoid calling non-trivial destructors for objects with static
  lifetime.

Merged from https://widevine-internal-review.googlesource.com/168497
Merged from https://widevine-internal-review.googlesource.com/171170
Merged from https://widevine-internal-review.googlesource.com/171171
Merged from https://widevine-internal-review.googlesource.com/171870

Change-Id: I20476a7b1132d11f011b8650ec01e3c2dc3fc0e8
This commit is contained in:
Ian Benz
2023-03-20 17:46:15 +00:00
committed by Robert Shih
parent af070601b0
commit 53fe55cb72
30 changed files with 518 additions and 398 deletions

View File

@@ -7,31 +7,39 @@
#include "oemcrypto_fuzz_helper.h"
#include "oemcrypto_fuzz_structs.h"
namespace wvoec {
namespace {
// Avoid calling non-trivial destructor.
wvoec::OEMCryptoLicenseAPIFuzz& license_api_fuzz =
*new wvoec::OEMCryptoLicenseAPIFuzz;
} // namespace
extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv) {
wvoec::RedirectStdoutToFile();
license_api_fuzz.Initialize();
return 0;
}
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
// Redirect printf and log statements from oemcrypto functions to a file to
// reduce noise
RedirectStdoutToFile();
// OEMCrypto_DestBufferDesc and a buffer from which data needs to be copied
// are expected as inputs to copy buffer API.
// Input fuzzed data is interpreted as:
// (OEMCrypto_DestBufferDesc | subsample_flags | input_buffer)
OEMCrypto_Copy_Buffer_Fuzz fuzzed_structure;
wvoec::OEMCrypto_Copy_Buffer_Fuzz fuzzed_structure;
if (size < sizeof(fuzzed_structure)) {
return 0;
}
FuzzedDataProvider fuzzed_data(data, size);
fuzzed_data.ConsumeData(&fuzzed_structure, sizeof(fuzzed_structure));
ConvertDataToValidEnum(OEMCrypto_BufferType_MaxValue,
&fuzzed_structure.dest_buffer_desc.type);
fuzzed_structure.dest_buffer_desc.buffer_config %= MAX_FUZZ_OUTPUT_LENGTH + 1;
wvoec::ConvertDataToValidEnum(OEMCrypto_BufferType_MaxValue,
&fuzzed_structure.dest_buffer_desc.type);
fuzzed_structure.dest_buffer_desc.buffer_config %=
wvoec::MAX_FUZZ_OUTPUT_LENGTH + 1;
const std::vector<OEMCrypto_SharedMemory> input_buffer =
fuzzed_data.ConsumeRemainingBytes<OEMCrypto_SharedMemory>();
OEMCryptoLicenseAPIFuzz license_api_fuzz;
const uint32_t session_id = license_api_fuzz.session()->session_id();
const uint32_t session_id = license_api_fuzz.session().session_id();
// Initialize output buffer.
OEMCrypto_DestBufferDesc dest_buffer_desc;
@@ -79,5 +87,3 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
return 0;
}
} // namespace wvoec