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