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"
|
2020-02-28 01:23:44 +08:00
|
|
|
#include "absl/status/status.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/var_type.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:
|
2020-07-20 18:07:15 +08:00
|
|
|
Var() = default;
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// 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.
|
2020-02-28 01:23:44 +08:00
|
|
|
virtual absl::Status Allocate(RPCChannel* rpc_channel, bool automatic_free);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Frees the local variable on the remote side.
|
2020-02-28 01:23:44 +08:00
|
|
|
virtual absl::Status Free(RPCChannel* rpc_channel);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Transfers the variable to the sandboxee's address space, has to be
|
|
|
|
// allocated there first.
|
2020-02-28 01:23:44 +08:00
|
|
|
virtual absl::Status TransferToSandboxee(RPCChannel* rpc_channel, pid_t pid);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Transfers the variable from the sandboxee's address space.
|
2020-02-28 01:23:44 +08:00
|
|
|
virtual absl::Status TransferFromSandboxee(RPCChannel* rpc_channel,
|
2019-03-19 00:21:48 +08:00
|
|
|
pid_t pid);
|
|
|
|
|
|
|
|
private:
|
2020-07-20 18:07:15 +08:00
|
|
|
// Invokes Allocate()/Free()/Transfer*Sandboxee().
|
|
|
|
friend class ::sapi::Sandbox;
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// Pointer to local storage of the variable.
|
2020-07-20 18:07:15 +08:00
|
|
|
void* local_ = nullptr;
|
2019-03-19 00:21:48 +08:00
|
|
|
// Pointer to remote storage of the variable.
|
2020-07-20 18:07:15 +08:00
|
|
|
void* remote_ = nullptr;
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Comms which can be used to free resources allocated in the sandboxer upon
|
|
|
|
// this process' end of lifetime.
|
2020-07-20 18:07:15 +08:00
|
|
|
RPCChannel* free_rpc_channel_ = nullptr;
|
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
|
|
|
|
|
|
|
#endif // SANDBOXED_API_VAR_ABSTRACT_H_
|