xref: /aosp_15_r20/external/perfetto/src/tracing/ipc/posix_shared_memory.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
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 #ifndef SRC_TRACING_IPC_POSIX_SHARED_MEMORY_H_
18 #define SRC_TRACING_IPC_POSIX_SHARED_MEMORY_H_
19 
20 #include "perfetto/base/build_config.h"
21 
22 #if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) ||   \
23     PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) || \
24     PERFETTO_BUILDFLAG(PERFETTO_OS_APPLE) ||   \
25     PERFETTO_BUILDFLAG(PERFETTO_OS_FUCHSIA) || \
26     PERFETTO_BUILDFLAG(PERFETTO_OS_WASM)
27 
28 #include <stddef.h>
29 
30 #include <memory>
31 
32 #include "perfetto/base/build_config.h"
33 #include "perfetto/ext/base/scoped_file.h"
34 #include "perfetto/ext/tracing/core/shared_memory.h"
35 
36 namespace perfetto {
37 
38 // Implements the SharedMemory and its factory for the posix-based transport.
39 class PosixSharedMemory : public SharedMemory {
40  public:
41   class Factory : public SharedMemory::Factory {
42    public:
43     ~Factory() override;
44     std::unique_ptr<SharedMemory> CreateSharedMemory(size_t) override;
45   };
46 
47   // Create a brand new SHM region.
48   static std::unique_ptr<PosixSharedMemory> Create(size_t size);
49 
50   // Mmaps a file descriptor to an existing SHM region. If
51   // |require_seals_if_supported| is true and the system supports
52   // memfd_create(), the FD is required to be a sealed memfd with F_SEAL_SEAL,
53   // F_SEAL_GROW, and F_SEAL_SHRINK seals set (otherwise, nullptr is returned).
54   // May also return nullptr if mapping fails for another reason (e.g. OOM).
55   static std::unique_ptr<PosixSharedMemory> AttachToFd(
56       base::ScopedFile,
57       bool require_seals_if_supported = true);
58 
59   ~PosixSharedMemory() override;
60 
fd()61   int fd() const { return fd_.get(); }
62 
63   // SharedMemory implementation.
64   using SharedMemory::start;  // Equal priority to const and non-const versions
start()65   const void* start() const override { return start_; }
size()66   size_t size() const override { return size_; }
67 
68  private:
69   static std::unique_ptr<PosixSharedMemory> MapFD(base::ScopedFile, size_t);
70 
71   PosixSharedMemory(void* start, size_t size, base::ScopedFile);
72   PosixSharedMemory(const PosixSharedMemory&) = delete;
73   PosixSharedMemory& operator=(const PosixSharedMemory&) = delete;
74 
75   void* const start_;
76   const size_t size_;
77   base::ScopedFile fd_;
78 };
79 
80 }  // namespace perfetto
81 
82 #endif  // OS_LINUX || OS_ANDROID || OS_APPLE
83 #endif  // SRC_TRACING_IPC_POSIX_SHARED_MEMORY_H_
84