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
RoundToPow2(size_t v)29 size_t RoundToPow2(size_t v) {
30 uint64_t x = static_cast<uint64_t>(v);
31 if (x < 2)
32 return 2;
33
34 x--;
35 x |= x >> 1;
36 x |= x >> 2;
37 x |= x >> 4;
38 x |= x >> 8;
39 x |= x >> 16;
40 x |= x >> 32;
41 x++;
42 return static_cast<size_t>(x);
43 }
44
FuzzRingBuffer(const uint8_t * data,size_t size)45 int FuzzRingBuffer(const uint8_t* data, size_t size) {
46 if (size <= sizeof(SharedRingBuffer::MetadataPage))
47 return 0;
48
49 auto fd = base::TempFile::CreateUnlinked().ReleaseFD();
50 PERFETTO_CHECK(fd);
51
52 // Use fuzzer input to first fill the SharedRingBuffer::MetadataPage in the
53 // first page, and then put the remainder into the data portion of the ring
54 // buffer (2nd+ pages).
55 size_t payload_size = size - sizeof(SharedRingBuffer::MetadataPage);
56 const uint8_t* payload = data + sizeof(SharedRingBuffer::MetadataPage);
57 size_t payload_size_pages =
58 (payload_size + base::kPageSize - 1) / base::kPageSize;
59 // Upsize test buffer to be 2^n data pages (precondition of the impl) + 1 page
60 // for the metadata.
61 size_t total_size_pages = 1 + RoundToPow2(payload_size_pages);
62
63 // Clear spinlock field, as otherwise the read will wait indefinitely (it
64 // defaults to indefinite blocking mode).
65 SharedRingBuffer::MetadataPage header = {};
66 memcpy(&header, data, sizeof(header));
67 header.spinlock.locked = false;
68 header.spinlock.poisoned = false;
69
70 PERFETTO_CHECK(ftruncate(*fd, static_cast<off_t>(total_size_pages *
71 base::kPageSize)) == 0);
72 PERFETTO_CHECK(base::WriteAll(*fd, &header, sizeof(header)) != -1);
73 PERFETTO_CHECK(lseek(*fd, base::kPageSize, SEEK_SET) != -1);
74 PERFETTO_CHECK(base::WriteAll(*fd, payload, payload_size) != -1);
75
76 auto buf = SharedRingBuffer::Attach(std::move(fd));
77 PERFETTO_CHECK(!!buf);
78
79 bool did_read;
80 do {
81 auto read_buf = buf->BeginRead();
82 did_read = bool(read_buf);
83 if (did_read) {
84 volatile uint8_t* v_data = read_buf.data;
85 // Assert we get a reference to valid memory.
86 for (size_t i = 0; i < read_buf.size; ++i)
87 v_data[i] = v_data[i];
88 }
89 buf->EndRead(std::move(read_buf));
90 } while (did_read);
91 return 0;
92 }
93
94 } // namespace
95 } // namespace profiling
96 } // namespace perfetto
97
98 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size);
99
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)100 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
101 return perfetto::profiling::FuzzRingBuffer(data, size);
102 }
103