sandboxed-api/sandboxed_api/var_lenval.h
Christian Blichmann 177b969e8c
Sandboxed API OSS release.
PiperOrigin-RevId: 238996664
Change-Id: I9646527e2be68ee0b6b371572b7aafe967102e57

Signed-off-by: Christian Blichmann <cblichmann@google.com>
2019-03-18 19:00:48 +01:00

86 lines
2.9 KiB
C++

// Copyright 2019 Google LLC. All Rights Reserved.
//
// 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_LENVAL_H_
#define SANDBOXED_API_VAR_LENVAL_H_
#include <sys/uio.h>
#include <memory>
#include "absl/base/macros.h"
#include "sandboxed_api/lenval_core.h"
#include "sandboxed_api/var_abstract.h"
#include "sandboxed_api/var_array.h"
#include "sandboxed_api/var_pointable.h"
#include "sandboxed_api/var_ptr.h"
#include "sandboxed_api/var_struct.h"
namespace sapi {
namespace v {
template <class T>
class Proto;
// Length + value container. Represents a pointer to a LenValStruct inside the
// sandboxee which allows the bidirectional synchronization data structures with
// changing lengths (e.g. protobuf structures). You probably want to directly
// use protobufs as they are easier to handle.
class LenVal : public Var, public Pointable {
public:
explicit LenVal(const char* data, uint64_t size)
: array_(const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(data)),
size),
struct_(size, nullptr) {}
explicit LenVal(const std::vector<uint8_t>& data)
: array_(data.size()), struct_(data.size(), nullptr) {
memcpy(array_.GetData(), data.data(), data.size());
}
explicit LenVal(size_t size) : array_(size), struct_(size, nullptr) {}
Type GetType() const final { return Type::kLenVal; }
std::string GetTypeString() const final { return "LengthValue"; }
std::string ToString() const final { return "LenVal"; }
Ptr* CreatePtr(Pointable::SyncType type) override {
return new Ptr(this, type);
}
sapi::Status ResizeData(RPCChannel* rpc_channel, size_t size);
size_t GetDataSize() const { return struct_.data().size; }
uint8_t* GetData() const { return array_.GetData(); }
void* GetRemote() const final { return struct_.GetRemote(); }
protected:
size_t GetSize() const final { return 0; }
sapi::Status Allocate(RPCChannel* rpc_channel, bool automatic_free) override;
sapi::Status Free(RPCChannel* rpc_channel) override;
sapi::Status TransferToSandboxee(RPCChannel* rpc_channel, pid_t pid) override;
sapi::Status TransferFromSandboxee(RPCChannel* rpc_channel,
pid_t pid) override;
Array<uint8_t> array_;
Struct<LenValStruct> struct_;
template <class T>
friend class Proto;
};
} // namespace v
} // namespace sapi
#endif // SANDBOXED_API_VAR_LENVAL_H_