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