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_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"
|
|
|
|
|
2019-11-20 20:39:44 +08:00
|
|
|
namespace sapi::v {
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status ResizeData(RPCChannel* rpc_channel, size_t size);
|
2019-03-19 00:21:48 +08:00
|
|
|
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; }
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status Allocate(RPCChannel* rpc_channel, bool automatic_free) override;
|
|
|
|
absl::Status Free(RPCChannel* rpc_channel) override;
|
|
|
|
absl::Status TransferToSandboxee(RPCChannel* rpc_channel, pid_t pid) override;
|
|
|
|
absl::Status TransferFromSandboxee(RPCChannel* rpc_channel,
|
2019-03-19 00:21:48 +08:00
|
|
|
pid_t pid) override;
|
|
|
|
|
|
|
|
Array<uint8_t> array_;
|
|
|
|
Struct<LenValStruct> struct_;
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
friend class Proto;
|
|
|
|
};
|
|
|
|
|
2019-11-20 20:39:44 +08:00
|
|
|
} // namespace sapi::v
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
#endif // SANDBOXED_API_VAR_LENVAL_H_
|