• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 "src/tracing/ipc/posix_shared_memory.h"
18 
19 #if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) ||   \
20     PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) || \
21     PERFETTO_BUILDFLAG(PERFETTO_OS_APPLE) ||   \
22     PERFETTO_BUILDFLAG(PERFETTO_OS_FUCHSIA) || \
23     PERFETTO_BUILDFLAG(PERFETTO_OS_WASM)
24 
25 #include <fcntl.h>
26 #include <stdint.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <sys/mman.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
32 
33 #include <memory>
34 #include <utility>
35 
36 #include "perfetto/base/compiler.h"
37 #include "perfetto/base/logging.h"
38 #include "perfetto/ext/base/temp_file.h"
39 #include "src/tracing/ipc/memfd.h"
40 
41 namespace perfetto {
42 
43 namespace {
44 int kFileSeals = F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_SEAL;
45 }  // namespace
46 
47 // static
Create(size_t size)48 std::unique_ptr<PosixSharedMemory> PosixSharedMemory::Create(size_t size) {
49   base::ScopedFile fd =
50       CreateMemfd("perfetto_shmem", MFD_CLOEXEC | MFD_ALLOW_SEALING);
51   bool is_memfd = !!fd;
52 
53   // In-tree builds only allow mem_fd, so we can inspect the seals to verify the
54   // fd is appropriately sealed. We'll crash in the PERFETTO_CHECK(fd) below if
55   // memfd_create failed.
56 #if !PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD)
57   if (!fd) {
58     // TODO: if this fails on Android we should fall back on ashmem.
59     PERFETTO_DPLOG("memfd_create() failed");
60     fd = base::TempFile::CreateUnlinked().ReleaseFD();
61   }
62 #endif
63 
64   PERFETTO_CHECK(fd);
65   int res = ftruncate(fd.get(), static_cast<off_t>(size));
66   PERFETTO_CHECK(res == 0);
67 
68   if (is_memfd) {
69     // When memfd is supported, file seals should be, too.
70     res = fcntl(*fd, F_ADD_SEALS, kFileSeals);
71     PERFETTO_DCHECK(res == 0);
72   }
73 
74   return MapFD(std::move(fd), size);
75 }
76 
77 // static
AttachToFd(base::ScopedFile fd,bool require_seals_if_supported)78 std::unique_ptr<PosixSharedMemory> PosixSharedMemory::AttachToFd(
79     base::ScopedFile fd,
80     bool require_seals_if_supported) {
81   bool requires_seals = require_seals_if_supported;
82 
83 #if PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD)
84   // In-tree kernels all support memfd.
85   PERFETTO_CHECK(HasMemfdSupport());
86 #else
87   // In out-of-tree builds, we only require seals if the kernel supports memfd.
88   if (requires_seals)
89     requires_seals = HasMemfdSupport();
90 #endif
91 
92   if (requires_seals) {
93     // If the system supports memfd, we require a sealed memfd.
94     int res = fcntl(*fd, F_GET_SEALS);
95     if (res == -1 || (res & kFileSeals) != kFileSeals) {
96       PERFETTO_PLOG("Couldn't verify file seals on shmem FD");
97       return nullptr;
98     }
99   }
100 
101   struct stat stat_buf = {};
102   int res = fstat(fd.get(), &stat_buf);
103   PERFETTO_CHECK(res == 0 && stat_buf.st_size > 0);
104   return MapFD(std::move(fd), static_cast<size_t>(stat_buf.st_size));
105 }
106 
107 // static
MapFD(base::ScopedFile fd,size_t size)108 std::unique_ptr<PosixSharedMemory> PosixSharedMemory::MapFD(base::ScopedFile fd,
109                                                             size_t size) {
110   PERFETTO_DCHECK(fd);
111   PERFETTO_DCHECK(size > 0);
112   void* start =
113       mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd.get(), 0);
114   PERFETTO_CHECK(start != MAP_FAILED);
115   return std::unique_ptr<PosixSharedMemory>(
116       new PosixSharedMemory(start, size, std::move(fd)));
117 }
118 
PosixSharedMemory(void * start,size_t size,base::ScopedFile fd)119 PosixSharedMemory::PosixSharedMemory(void* start,
120                                      size_t size,
121                                      base::ScopedFile fd)
122     : start_(start), size_(size), fd_(std::move(fd)) {}
123 
~PosixSharedMemory()124 PosixSharedMemory::~PosixSharedMemory() {
125   munmap(start(), size());
126 }
127 
~Factory()128 PosixSharedMemory::Factory::~Factory() {}
129 
CreateSharedMemory(size_t size)130 std::unique_ptr<SharedMemory> PosixSharedMemory::Factory::CreateSharedMemory(
131     size_t size) {
132   return PosixSharedMemory::Create(size);
133 }
134 
135 }  // namespace perfetto
136 
137 #endif  // OS_LINUX || OS_ANDROID || OS_APPLE
138