1 /**
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include <binder/IMemory.h>
17 #include <binder/MemoryDealer.h>
18 #include <private/android_filesystem_config.h>
19 #include "MediaLogService.h"
20 #include "fuzzer/FuzzedDataProvider.h"
21
22 constexpr const char* kWriterNames[2] = {"FastMixer", "FastCapture"};
23 constexpr size_t kMinSize = 0x100;
24 constexpr size_t kMaxSize = 0x10000;
25 constexpr size_t kLogMemorySize = 400 * 1024;
26 constexpr size_t kMaxNumLines = USHRT_MAX;
27
28 using namespace android;
29
30 class MediaLogFuzzer {
31 public:
32 void init();
33 void process(const uint8_t* data, size_t size);
34
35 private:
36 sp<MemoryDealer> mMemoryDealer = nullptr;
37 sp<MediaLogService> mService = nullptr;
38 };
39
init()40 void MediaLogFuzzer::init() {
41 setuid(AID_MEDIA);
42 mService = new MediaLogService();
43 mMemoryDealer = new MemoryDealer(kLogMemorySize, "MediaLogFuzzer", MemoryHeapBase::READ_ONLY);
44 }
45
process(const uint8_t * data,size_t size)46 void MediaLogFuzzer::process(const uint8_t* data, size_t size) {
47 FuzzedDataProvider fuzzedDataProvider(data, size);
48 size_t writerNameIdx =
49 fuzzedDataProvider.ConsumeIntegralInRange<size_t>(0, std::size(kWriterNames) - 1);
50 bool shouldDumpBeforeUnregister = fuzzedDataProvider.ConsumeBool();
51 size_t logSize = fuzzedDataProvider.ConsumeIntegralInRange<size_t>(kMinSize, kMaxSize);
52 sp<IMemory> logBuffer = mMemoryDealer->allocate(NBLog::Timeline::sharedSize(logSize));
53 Vector<String16> args;
54 size_t numberOfLines = fuzzedDataProvider.ConsumeIntegralInRange<size_t>(0, kMaxNumLines);
55 for (size_t lineIdx = 0; lineIdx < numberOfLines; ++lineIdx) {
56 args.add(static_cast<String16>(fuzzedDataProvider.ConsumeRandomLengthString().c_str()));
57 }
58 const char* fileName = "logDumpFile";
59 int fd = memfd_create(fileName, MFD_ALLOW_SEALING);
60 fuzzedDataProvider.ConsumeData(logBuffer->unsecurePointer(), logBuffer->size());
61 mService->registerWriter(logBuffer, logSize, kWriterNames[writerNameIdx]);
62 if (shouldDumpBeforeUnregister) {
63 mService->dump(fd, args);
64 mService->unregisterWriter(logBuffer);
65 } else {
66 mService->unregisterWriter(logBuffer);
67 mService->dump(fd, args);
68 }
69 }
70
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)71 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
72 MediaLogFuzzer mediaLogFuzzer = MediaLogFuzzer();
73 mediaLogFuzzer.init();
74 mediaLogFuzzer.process(data, size);
75 return 0;
76 }
77