xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/var_ptr.h (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1*ec63e07aSXin Li // Copyright 2019 Google LLC
2*ec63e07aSXin Li //
3*ec63e07aSXin Li // Licensed under the Apache License, Version 2.0 (the "License");
4*ec63e07aSXin Li // you may not use this file except in compliance with the License.
5*ec63e07aSXin Li // You may obtain a copy of the License at
6*ec63e07aSXin Li //
7*ec63e07aSXin Li //     https://www.apache.org/licenses/LICENSE-2.0
8*ec63e07aSXin Li //
9*ec63e07aSXin Li // Unless required by applicable law or agreed to in writing, software
10*ec63e07aSXin Li // distributed under the License is distributed on an "AS IS" BASIS,
11*ec63e07aSXin Li // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*ec63e07aSXin Li // See the License for the specific language governing permissions and
13*ec63e07aSXin Li // limitations under the License.
14*ec63e07aSXin Li 
15*ec63e07aSXin Li #ifndef SANDBOXED_API_VAR_PTR_H_
16*ec63e07aSXin Li #define SANDBOXED_API_VAR_PTR_H_
17*ec63e07aSXin Li 
18*ec63e07aSXin Li #include <algorithm>
19*ec63e07aSXin Li #include <cstring>
20*ec63e07aSXin Li #include <memory>
21*ec63e07aSXin Li #include <string>
22*ec63e07aSXin Li 
23*ec63e07aSXin Li #include "absl/base/attributes.h"
24*ec63e07aSXin Li #include "absl/base/macros.h"
25*ec63e07aSXin Li #include "absl/strings/str_format.h"
26*ec63e07aSXin Li #include "sandboxed_api/var_abstract.h"
27*ec63e07aSXin Li #include "sandboxed_api/var_reg.h"
28*ec63e07aSXin Li 
29*ec63e07aSXin Li namespace sapi::v {
30*ec63e07aSXin Li 
31*ec63e07aSXin Li // Class representing a pointer. Takes both Var* and regular pointers in the
32*ec63e07aSXin Li // initializers.
33*ec63e07aSXin Li class Ptr : public Reg<Var*> {
34*ec63e07aSXin Li  public:
35*ec63e07aSXin Li   Ptr() = delete;
36*ec63e07aSXin Li 
Ptr(Var * value,SyncType sync_type)37*ec63e07aSXin Li   explicit Ptr(Var* value, SyncType sync_type) : sync_type_(sync_type) {
38*ec63e07aSXin Li     Reg<Var*>::SetValue(value);
39*ec63e07aSXin Li   }
40*ec63e07aSXin Li 
GetPointedVar()41*ec63e07aSXin Li   Var* GetPointedVar() const { return Reg<Var*>::GetValue(); }
42*ec63e07aSXin Li 
SetValue(Var * ptr)43*ec63e07aSXin Li   void SetValue(Var* ptr) final { value_->SetRemote(ptr); }
44*ec63e07aSXin Li 
GetValue()45*ec63e07aSXin Li   Var* GetValue() const final {
46*ec63e07aSXin Li     return reinterpret_cast<Var*>(value_->GetRemote());
47*ec63e07aSXin Li   }
48*ec63e07aSXin Li 
GetDataPtr()49*ec63e07aSXin Li   const void* GetDataPtr() final {
50*ec63e07aSXin Li     remote_ptr_cache_ = GetValue();
51*ec63e07aSXin Li     return &remote_ptr_cache_;
52*ec63e07aSXin Li   }
53*ec63e07aSXin Li 
SetDataFromPtr(const void * ptr,size_t max_sz)54*ec63e07aSXin Li   void SetDataFromPtr(const void* ptr, size_t max_sz) final {
55*ec63e07aSXin Li     void* tmp;
56*ec63e07aSXin Li     memcpy(&tmp, ptr, std::min(sizeof(tmp), max_sz));
57*ec63e07aSXin Li     SetValue(reinterpret_cast<Var*>(tmp));
58*ec63e07aSXin Li   }
59*ec63e07aSXin Li 
60*ec63e07aSXin Li   // Getter/Setter for the sync_type_ field.
GetSyncType()61*ec63e07aSXin Li   SyncType GetSyncType() { return sync_type_; }
SetSyncType(SyncType sync_type)62*ec63e07aSXin Li   void SetSyncType(SyncType sync_type) { sync_type_ = sync_type; }
63*ec63e07aSXin Li 
ToString()64*ec63e07aSXin Li   std::string ToString() const final {
65*ec63e07aSXin Li     Var* var = GetPointedVar();
66*ec63e07aSXin Li     return absl::StrFormat(
67*ec63e07aSXin Li         "Ptr to obj:%p (type:'%s' val:'%s'), local:%p, remote:%p, size:%tx",
68*ec63e07aSXin Li         var, var->GetTypeString(), var->ToString(), var->GetLocal(),
69*ec63e07aSXin Li         var->GetRemote(), var->GetSize());
70*ec63e07aSXin Li   }
71*ec63e07aSXin Li 
72*ec63e07aSXin Li  private:
73*ec63e07aSXin Li   // GetDataPtr() interface requires of us to return a pointer to the data
74*ec63e07aSXin Li   // (variable) that can be copied. We cannot get pointer to pointer with
75*ec63e07aSXin Li   // Var::GetRemote(), hence we cache it, and return pointer to it.
76*ec63e07aSXin Li   void* remote_ptr_cache_;
77*ec63e07aSXin Li 
78*ec63e07aSXin Li   // Shall we synchronize the underlying object before/after call.
79*ec63e07aSXin Li   SyncType sync_type_;
80*ec63e07aSXin Li };
81*ec63e07aSXin Li 
82*ec63e07aSXin Li // Good, old nullptr
83*ec63e07aSXin Li class ABSL_DEPRECATED(
84*ec63e07aSXin Li     "Use regular `nullptr` or `NULL` instead. This class will eventually get "
85*ec63e07aSXin Li     "removed") NullPtr : public Ptr {
86*ec63e07aSXin Li  public:
NullPtr()87*ec63e07aSXin Li   NullPtr() : Ptr(&void_obj_, SyncType::kSyncNone) {}
88*ec63e07aSXin Li 
89*ec63e07aSXin Li  private:
90*ec63e07aSXin Li   Reg<void*> void_obj_;
91*ec63e07aSXin Li };
92*ec63e07aSXin Li 
93*ec63e07aSXin Li // Pointer, which can only point to remote memory, and is never synchronized.
94*ec63e07aSXin Li class RemotePtr : public Ptr {
95*ec63e07aSXin Li  public:
RemotePtr(void * remote_addr)96*ec63e07aSXin Li   explicit RemotePtr(void* remote_addr)
97*ec63e07aSXin Li       : Ptr(&pointed_obj_, SyncType::kSyncNone) {
98*ec63e07aSXin Li     pointed_obj_.SetRemote(remote_addr);
99*ec63e07aSXin Li   }
100*ec63e07aSXin Li 
101*ec63e07aSXin Li  private:
102*ec63e07aSXin Li   Reg<void*> pointed_obj_;
103*ec63e07aSXin Li };
104*ec63e07aSXin Li 
105*ec63e07aSXin Li }  // namespace sapi::v
106*ec63e07aSXin Li 
107*ec63e07aSXin Li #endif  // SANDBOXED_API_VAR_PTR_H_
108