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