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