1 /*
2 * Copyright (C) 2019 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
17 #include <stddef.h>
18 #include <stdint.h>
19 #include <unistd.h>
20
21 #include "perfetto/ext/base/file_utils.h"
22 #include "perfetto/ext/base/temp_file.h"
23 #include "src/profiling/memory/shared_ring_buffer.h"
24
25 namespace perfetto {
26 namespace profiling {
27 namespace {
28
29 struct FuzzingInputHeader {
30 size_t write_size;
31 SharedRingBuffer::MetadataPage metadata_page;
32 };
33
RoundToPow2(size_t v)34 size_t RoundToPow2(size_t v) {
35 uint64_t x = static_cast<uint64_t>(v);
36 if (x < 2)
37 return 2;
38
39 x--;
40 x |= x >> 1;
41 x |= x >> 2;
42 x |= x >> 4;
43 x |= x >> 8;
44 x |= x >> 16;
45 x |= x >> 32;
46 x++;
47 return static_cast<size_t>(x);
48 }
49
FuzzRingBufferWrite(const uint8_t * data,size_t size)50 int FuzzRingBufferWrite(const uint8_t* data, size_t size) {
51 if (size <= sizeof(FuzzingInputHeader))
52 return 0;
53
54 auto fd = base::TempFile::CreateUnlinked().ReleaseFD();
55 PERFETTO_CHECK(fd);
56
57 // Prefill shared buffer with fuzzer input, then attempt to write.
58 // TODO(fmayer): Do we actually need to fill the buffer with payload, or
59 // should we only fuzz the metadata?
60 size_t payload_size = size - sizeof(FuzzingInputHeader);
61 const uint8_t* payload = data + sizeof(FuzzingInputHeader);
62 size_t payload_size_pages =
63 (payload_size + base::kPageSize - 1) / base::kPageSize;
64 // Upsize test buffer to be 2^n data pages (precondition of the impl) + 1 page
65 // for the metadata.
66 size_t total_size_pages = 1 + RoundToPow2(payload_size_pages);
67
68 // Clear spinlock field, as otherwise we will fail acquiring the lock below.
69 FuzzingInputHeader header = {};
70 memcpy(&header, data, sizeof(header));
71 SharedRingBuffer::MetadataPage& metadata_page = header.metadata_page;
72 metadata_page.spinlock.locked = false;
73 metadata_page.spinlock.poisoned = false;
74
75 PERFETTO_CHECK(ftruncate(*fd, static_cast<off_t>(total_size_pages *
76 base::kPageSize)) == 0);
77 PERFETTO_CHECK(base::WriteAll(*fd, &metadata_page, sizeof(metadata_page)) !=
78 -1);
79 PERFETTO_CHECK(lseek(*fd, base::kPageSize, SEEK_SET) != -1);
80 PERFETTO_CHECK(base::WriteAll(*fd, payload, payload_size) != -1);
81
82 auto buf = SharedRingBuffer::Attach(std::move(fd));
83 PERFETTO_CHECK(!!buf);
84
85 SharedRingBuffer::Buffer write_buf;
86 {
87 auto lock = buf->AcquireLock(ScopedSpinlock::Mode::Try);
88 PERFETTO_CHECK(lock.locked());
89 write_buf = buf->BeginWrite(lock, header.write_size);
90 }
91 if (!write_buf)
92 return 0;
93
94 memset(write_buf.data, '\0', write_buf.size);
95 buf->EndWrite(std::move(write_buf));
96 return 0;
97 }
98
99 } // namespace
100 } // namespace profiling
101 } // namespace perfetto
102
103 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size);
104
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)105 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
106 return perfetto::profiling::FuzzRingBufferWrite(data, size);
107 }
108