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