2020-01-17 21:05:03 +08:00
|
|
|
// Copyright 2019 Google LLC
|
2019-03-19 00:21:48 +08:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#ifndef SANDBOXED_API_VAR_ABSTRACT_H_
|
|
|
|
#define SANDBOXED_API_VAR_ABSTRACT_H_
|
|
|
|
|
|
|
|
#include <memory>
|
2019-03-26 22:54:02 +08:00
|
|
|
#include <string>
|
2019-03-19 00:21:48 +08:00
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
#include "absl/base/macros.h"
|
|
|
|
#include "sandboxed_api/var_type.h"
|
|
|
|
#include "sandboxed_api/util/status.h"
|
|
|
|
|
|
|
|
namespace sandbox2 {
|
|
|
|
class Comms;
|
2019-11-20 20:39:44 +08:00
|
|
|
} // namespace sandbox2
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
namespace sapi {
|
|
|
|
class Sandbox;
|
|
|
|
class RPCChannel;
|
2019-11-20 20:39:44 +08:00
|
|
|
} // namespace sapi
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2019-11-20 20:39:44 +08:00
|
|
|
namespace sapi::v {
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
class Ptr;
|
|
|
|
|
|
|
|
// An abstract class representing variables.
|
|
|
|
class Var {
|
|
|
|
public:
|
|
|
|
Var(const Var&) = delete;
|
|
|
|
Var& operator=(const Var&) = delete;
|
|
|
|
|
|
|
|
// Returns the address of the storage (remote side).
|
|
|
|
virtual void* GetRemote() const { return remote_; }
|
|
|
|
|
|
|
|
// Sets the address of the remote storage.
|
|
|
|
virtual void SetRemote(void* remote) { remote_ = remote; }
|
|
|
|
|
|
|
|
// Returns the address of the storage (local side).
|
|
|
|
virtual void* GetLocal() const { return local_; }
|
|
|
|
|
|
|
|
// Returns the size of the local variable storage.
|
|
|
|
virtual size_t GetSize() const = 0;
|
|
|
|
|
|
|
|
// Returns the type of the variable.
|
|
|
|
virtual Type GetType() const = 0;
|
|
|
|
|
2019-03-26 22:54:02 +08:00
|
|
|
// Returns a string representation of the variable type.
|
2019-03-19 00:21:48 +08:00
|
|
|
virtual std::string GetTypeString() const = 0;
|
|
|
|
|
2019-03-26 22:54:02 +08:00
|
|
|
// Returns a string representation of the variable value.
|
2019-03-19 00:21:48 +08:00
|
|
|
virtual std::string ToString() const = 0;
|
|
|
|
|
|
|
|
virtual ~Var();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
Var() : local_(nullptr), remote_(nullptr), free_rpc_channel_(nullptr) {}
|
|
|
|
|
|
|
|
// Set pointer to local storage class.
|
|
|
|
void SetLocal(void* local) { local_ = local; }
|
|
|
|
|
|
|
|
// Setter/Getter for the address of a Comms object which can be used to
|
2019-11-20 20:39:44 +08:00
|
|
|
// remotely free allocated memory backing up this variable, upon this
|
|
|
|
// object's end of life-time
|
2019-03-19 00:21:48 +08:00
|
|
|
void SetFreeRPCChannel(RPCChannel* rpc_channel) {
|
|
|
|
free_rpc_channel_ = rpc_channel;
|
|
|
|
}
|
|
|
|
RPCChannel* GetFreeRPCChannel() { return free_rpc_channel_; }
|
|
|
|
|
|
|
|
// Allocates the local variable on the remote side. The 'automatic_free'
|
|
|
|
// argument dictates whether the remote memory should be freed upon end of
|
|
|
|
// this object's lifetime.
|
|
|
|
virtual sapi::Status Allocate(RPCChannel* rpc_channel, bool automatic_free);
|
|
|
|
|
|
|
|
// Frees the local variable on the remote side.
|
|
|
|
virtual sapi::Status Free(RPCChannel* rpc_channel);
|
|
|
|
|
|
|
|
// Transfers the variable to the sandboxee's address space, has to be
|
|
|
|
// allocated there first.
|
|
|
|
virtual sapi::Status TransferToSandboxee(RPCChannel* rpc_channel, pid_t pid);
|
|
|
|
|
|
|
|
// Transfers the variable from the sandboxee's address space.
|
|
|
|
virtual sapi::Status TransferFromSandboxee(RPCChannel* rpc_channel,
|
|
|
|
pid_t pid);
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Pointer to local storage of the variable.
|
|
|
|
void* local_;
|
|
|
|
// Pointer to remote storage of the variable.
|
|
|
|
void* remote_;
|
|
|
|
|
|
|
|
// Comms which can be used to free resources allocated in the sandboxer upon
|
|
|
|
// this process' end of lifetime.
|
|
|
|
RPCChannel* free_rpc_channel_;
|
|
|
|
|
|
|
|
// Invokes Allocate()/Free()/Transfer*Sandboxee().
|
|
|
|
friend class ::sapi::Sandbox;
|
|
|
|
};
|
|
|
|
|
2019-11-20 20:39:44 +08:00
|
|
|
} // namespace sapi::v
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
#endif // SANDBOXED_API_VAR_ABSTRACT_H_
|