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
|
|
|
|
//
|
2022-01-28 17:38:27 +08:00
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2019-03-19 00:21:48 +08:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
// Provides a class to marshall protobufs in and out of the sandbox
|
|
|
|
|
|
|
|
#ifndef SANDBOXED_API_VAR_PROTO_H_
|
|
|
|
#define SANDBOXED_API_VAR_PROTO_H_
|
|
|
|
|
|
|
|
#include <cinttypes>
|
2019-09-13 17:28:09 +08:00
|
|
|
#include <cstdint>
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <ctime>
|
2022-10-12 20:22:51 +08:00
|
|
|
#include <memory>
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <string>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <utility>
|
2019-09-13 17:28:09 +08:00
|
|
|
#include <vector>
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2023-08-24 21:23:03 +08:00
|
|
|
#include "absl/base/attributes.h"
|
2019-09-13 17:28:09 +08:00
|
|
|
#include "absl/base/macros.h"
|
2023-08-24 21:23:03 +08:00
|
|
|
#include "absl/log/log.h"
|
|
|
|
#include "absl/status/status.h"
|
2020-10-27 00:08:06 +08:00
|
|
|
#include "absl/status/statusor.h"
|
2021-07-30 18:54:45 +08:00
|
|
|
#include "absl/utility/utility.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/proto_helper.h"
|
2021-01-22 22:01:05 +08:00
|
|
|
#include "sandboxed_api/util/status_macros.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/var_lenval.h"
|
|
|
|
#include "sandboxed_api/var_ptr.h"
|
|
|
|
|
2019-11-20 20:39:44 +08:00
|
|
|
namespace sapi::v {
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
template <typename T>
|
2021-12-02 01:54:09 +08:00
|
|
|
class Proto : public Var {
|
2019-03-19 00:21:48 +08:00
|
|
|
public:
|
2022-03-03 20:47:46 +08:00
|
|
|
static_assert(std::is_base_of<google::protobuf::MessageLite, T>::value,
|
2019-09-13 17:28:09 +08:00
|
|
|
"Template argument must be a proto message");
|
|
|
|
|
|
|
|
ABSL_DEPRECATED("Use Proto<>::FromMessage() instead")
|
|
|
|
explicit Proto(const T& proto)
|
2020-04-02 22:42:17 +08:00
|
|
|
: wrapped_var_(SerializeProto(proto).value()) {}
|
2019-09-13 17:28:09 +08:00
|
|
|
|
2020-09-02 23:46:48 +08:00
|
|
|
static absl::StatusOr<Proto<T>> FromMessage(const T& proto) {
|
2019-09-13 17:28:09 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(std::vector<uint8_t> len_val, SerializeProto(proto));
|
2021-07-30 18:54:45 +08:00
|
|
|
return absl::StatusOr<Proto<T>>(absl::in_place, proto);
|
2019-09-13 17:28:09 +08:00
|
|
|
}
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
size_t GetSize() const final { return wrapped_var_.GetSize(); }
|
|
|
|
Type GetType() const final { return Type::kProto; }
|
|
|
|
std::string GetTypeString() const final { return "Protobuf"; }
|
|
|
|
std::string ToString() const final { return "Protobuf"; }
|
|
|
|
|
|
|
|
void* GetRemote() const override { return wrapped_var_.GetRemote(); }
|
|
|
|
void* GetLocal() const override { return wrapped_var_.GetLocal(); }
|
|
|
|
|
|
|
|
// Returns a copy of the stored protobuf object.
|
2020-09-02 23:46:48 +08:00
|
|
|
absl::StatusOr<T> GetMessage() const {
|
2019-09-13 17:28:09 +08:00
|
|
|
return DeserializeProto<T>(
|
|
|
|
reinterpret_cast<const char*>(wrapped_var_.GetData()),
|
|
|
|
wrapped_var_.GetDataSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
ABSL_DEPRECATED("Use GetMessage() instead")
|
2019-03-19 00:21:48 +08:00
|
|
|
std::unique_ptr<T> GetProtoCopy() const {
|
2021-07-30 18:54:45 +08:00
|
|
|
if (auto proto = GetMessage(); proto.ok()) {
|
2022-10-12 20:22:51 +08:00
|
|
|
return std::make_unique<T>(*std::move(proto));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
2019-09-13 17:28:09 +08:00
|
|
|
return nullptr;
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2019-09-13 17:28:09 +08:00
|
|
|
void SetRemote(void* /* remote */) override {
|
2019-03-19 00:21:48 +08:00
|
|
|
// We do not support that much indirection (pointer to a pointer to a
|
|
|
|
// protobuf) as it is unlikely that this is wanted behavior. If you expect
|
|
|
|
// this to work, please get in touch with us.
|
|
|
|
LOG(FATAL) << "SetRemote not supported on protobufs.";
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// Forward a couple of function calls to the actual var.
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status Allocate(RPCChannel* rpc_channel, bool automatic_free) override {
|
2019-03-19 00:21:48 +08:00
|
|
|
return wrapped_var_.Allocate(rpc_channel, automatic_free);
|
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status Free(RPCChannel* rpc_channel) override {
|
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status TransferToSandboxee(RPCChannel* rpc_channel,
|
2019-03-19 00:21:48 +08:00
|
|
|
pid_t pid) override {
|
|
|
|
return wrapped_var_.TransferToSandboxee(rpc_channel, pid);
|
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status TransferFromSandboxee(RPCChannel* rpc_channel,
|
2019-03-19 00:21:48 +08:00
|
|
|
pid_t pid) override {
|
|
|
|
return wrapped_var_.TransferFromSandboxee(rpc_channel, pid);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-07-30 18:54:45 +08:00
|
|
|
friend class absl::StatusOr<Proto<T>>;
|
|
|
|
|
|
|
|
explicit Proto(std::vector<uint8_t> data) : wrapped_var_(std::move(data)) {}
|
2019-09-13 17:28:09 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// The management of reading/writing the data to the sandboxee is handled by
|
|
|
|
// the LenVal class.
|
|
|
|
LenVal wrapped_var_;
|
|
|
|
};
|
|
|
|
|
2019-11-20 20:39:44 +08:00
|
|
|
} // namespace sapi::v
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
#endif // SANDBOXED_API_VAR_PROTO_H_
|