1 // Copyright 2023 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef PARTITION_ALLOC_PARTITION_ALLOC_ALLOCATION_DATA_H_
6 #define PARTITION_ALLOC_PARTITION_ALLOC_ALLOCATION_DATA_H_
7 
8 #include <cstdint>
9 
10 #include "partition_alloc/partition_alloc_base/component_export.h"
11 #include "partition_alloc/partition_alloc_buildflags.h"
12 #include "partition_alloc/tagging.h"
13 
14 namespace partition_alloc {
15 
16 // Definitions of various parameters of override and observer hooks. Allocation
17 // and free path differ from each other in that the allocation override provides
18 // data to the caller (we have an out parameter there), whereas the free
19 // override just consumes the data.
20 
21 // AllocationNotificationData is the in-parameter of an allocation observer
22 // hook.
PA_COMPONENT_EXPORT(PARTITION_ALLOC)23 class PA_COMPONENT_EXPORT(PARTITION_ALLOC) AllocationNotificationData {
24  public:
25   AllocationNotificationData(void* address, size_t size, const char* type_name)
26       : address_(address), size_(size), type_name_(type_name) {}
27 
28   void* address() const { return address_; }
29   size_t size() const { return size_; }
30   const char* type_name() const { return type_name_; }
31 
32   // In the allocation observer path, it's interesting which reporting mode is
33   // enabled.
34 #if BUILDFLAG(HAS_MEMORY_TAGGING)
35   AllocationNotificationData& SetMteReportingMode(
36       TagViolationReportingMode mode) {
37     mte_reporting_mode_ = mode;
38     return *this;
39   }
40 
41   TagViolationReportingMode mte_reporting_mode() const {
42     return mte_reporting_mode_;
43   }
44 #else
45   constexpr TagViolationReportingMode mte_reporting_mode() const {
46     return TagViolationReportingMode::kUndefined;
47   }
48 #endif  // BUILDFLAG(HAS_MEMORY_TAGGING)
49 
50  private:
51   void* address_ = nullptr;
52   size_t size_ = 0;
53   const char* type_name_ = nullptr;
54 #if BUILDFLAG(HAS_MEMORY_TAGGING)
55   TagViolationReportingMode mte_reporting_mode_ =
56       TagViolationReportingMode::kUndefined;
57 #endif
58 };
59 
60 // FreeNotificationData is the in-parameter of a free observer hook.
PA_COMPONENT_EXPORT(PARTITION_ALLOC)61 class PA_COMPONENT_EXPORT(PARTITION_ALLOC) FreeNotificationData {
62  public:
63   constexpr explicit FreeNotificationData(void* address) : address_(address) {}
64 
65   void* address() const { return address_; }
66 
67   // In the free observer path, it's interesting which reporting mode is
68   // enabled.
69 #if BUILDFLAG(HAS_MEMORY_TAGGING)
70   FreeNotificationData& SetMteReportingMode(TagViolationReportingMode mode) {
71     mte_reporting_mode_ = mode;
72     return *this;
73   }
74 
75   TagViolationReportingMode mte_reporting_mode() const {
76     return mte_reporting_mode_;
77   }
78 #else
79   constexpr TagViolationReportingMode mte_reporting_mode() const {
80     return TagViolationReportingMode::kUndefined;
81   }
82 #endif  // BUILDFLAG(HAS_MEMORY_TAGGING)
83 
84  private:
85   void* address_ = nullptr;
86 #if BUILDFLAG(HAS_MEMORY_TAGGING)
87   TagViolationReportingMode mte_reporting_mode_ =
88       TagViolationReportingMode::kUndefined;
89 #endif  // BUILDFLAG(HAS_MEMORY_TAGGING)
90 };
91 
92 }  // namespace partition_alloc
93 #endif  // PARTITION_ALLOC_PARTITION_ALLOC_ALLOCATION_DATA_H_
94