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