xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/var_abstract.h (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef SANDBOXED_API_VAR_ABSTRACT_H_
16 #define SANDBOXED_API_VAR_ABSTRACT_H_
17 
18 #include <ctime>
19 #include <memory>
20 #include <string>
21 #include <type_traits>
22 
23 #include "absl/base/attributes.h"
24 #include "absl/base/macros.h"
25 #include "absl/status/status.h"
26 #include "sandboxed_api/var_type.h"
27 
28 namespace sandbox2 {
29 class Comms;
30 }  // namespace sandbox2
31 
32 namespace sapi {
33 class RPCChannel;
34 class Sandbox;
35 }  // namespace sapi
36 
37 namespace sapi::v {
38 
39 class Ptr;
40 
41 class ABSL_DEPRECATED(
42     "Use the Var::PtrXXX() family of methods instead") Pointable {
43  public:
44   enum SyncType {
45     // Do not synchronize the underlying object after/before calls.
46     kSyncNone = 0x0,
47     // Synchronize the underlying object (send the data to the sandboxee)
48     // before the call takes place.
49     kSyncBefore = 0x1,
50     // Synchronize the underlying object (retrieve data from the sandboxee)
51     // after the call has finished.
52     kSyncAfter = 0x2,
53     // Synchronize the underlying object with the remote object, by sending the
54     // data to the sandboxee before the call, and retrieving it from the
55     // sandboxee after the call has finished.
56     kSyncBoth = kSyncBefore | kSyncAfter,
57   };
58 
59   virtual ~Pointable() = default;
60 };
61 
62 // An abstract class representing variables.
63 class Var : public Pointable {
64  public:
65   Var(const Var&) = delete;
66   Var& operator=(const Var&) = delete;
67 
68   virtual ~Var();
69 
70   // Returns the address of the storage (remote side).
GetRemote()71   virtual void* GetRemote() const { return remote_; }
72 
73   // Sets the address of the remote storage.
SetRemote(void * remote)74   virtual void SetRemote(void* remote) { remote_ = remote; }
75 
76   // Returns the address of the storage (local side).
GetLocal()77   virtual void* GetLocal() const { return local_; }
78 
79   // Returns the size of the local variable storage.
80   virtual size_t GetSize() const = 0;
81 
82   // Returns the type of the variable.
83   virtual Type GetType() const = 0;
84 
85   // Returns a string representation of the variable type.
86   virtual std::string GetTypeString() const = 0;
87 
88   // Returns a string representation of the variable value.
89   virtual std::string ToString() const = 0;
90 
91   // Functions to get pointers with certain type of synchronization schemes.
92   Ptr* PtrNone();
93   Ptr* PtrBoth();
94   Ptr* PtrBefore();
95   Ptr* PtrAfter();
96 
97  protected:
98   Var() = default;
99 
100   // Set pointer to local storage class.
SetLocal(void * local)101   void SetLocal(void* local) { local_ = local; }
102 
103   // Setter/Getter for the address of a Comms object which can be used to
104   // remotely free allocated memory backing up this variable, upon this
105   // object's end of life-time
SetFreeRPCChannel(RPCChannel * rpc_channel)106   void SetFreeRPCChannel(RPCChannel* rpc_channel) {
107     free_rpc_channel_ = rpc_channel;
108   }
GetFreeRPCChannel()109   RPCChannel* GetFreeRPCChannel() { return free_rpc_channel_; }
110 
111   // Allocates the local variable on the remote side. The 'automatic_free'
112   // argument dictates whether the remote memory should be freed upon end of
113   // this object's lifetime.
114   virtual absl::Status Allocate(RPCChannel* rpc_channel, bool automatic_free);
115 
116   // Frees the local variable on the remote side.
117   virtual absl::Status Free(RPCChannel* rpc_channel);
118 
119   // Transfers the variable to the sandboxee's address space, has to be
120   // allocated there first.
121   virtual absl::Status TransferToSandboxee(RPCChannel* rpc_channel, pid_t pid);
122 
123   // Transfers the variable from the sandboxee's address space.
124   virtual absl::Status TransferFromSandboxee(RPCChannel* rpc_channel,
125                                              pid_t pid);
126 
127  private:
128   // Needed so that we can use unique_ptr with incomplete type.
129   struct PtrDeleter {
130     void operator()(Ptr* p);
131   };
132 
133   // Invokes Allocate()/Free()/Transfer*Sandboxee().
134   friend class ::sapi::Sandbox;
135 
136   std::unique_ptr<Ptr, PtrDeleter> ptr_none_;
137   std::unique_ptr<Ptr, PtrDeleter> ptr_both_;
138   std::unique_ptr<Ptr, PtrDeleter> ptr_before_;
139   std::unique_ptr<Ptr, PtrDeleter> ptr_after_;
140 
141   // Pointer to local storage of the variable.
142   void* local_ = nullptr;
143   // Pointer to remote storage of the variable.
144   void* remote_ = nullptr;
145 
146   // Comms which can be used to free resources allocated in the sandboxer upon
147   // this process' end of lifetime.
148   RPCChannel* free_rpc_channel_ = nullptr;
149 };
150 
151 }  // namespace sapi::v
152 
153 #endif  // SANDBOXED_API_VAR_ABSTRACT_H_
154