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