xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/var_lenval.h (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1*ec63e07aSXin Li // Copyright 2019 Google LLC
2*ec63e07aSXin Li //
3*ec63e07aSXin Li // Licensed under the Apache License, Version 2.0 (the "License");
4*ec63e07aSXin Li // you may not use this file except in compliance with the License.
5*ec63e07aSXin Li // You may obtain a copy of the License at
6*ec63e07aSXin Li //
7*ec63e07aSXin Li //     https://www.apache.org/licenses/LICENSE-2.0
8*ec63e07aSXin Li //
9*ec63e07aSXin Li // Unless required by applicable law or agreed to in writing, software
10*ec63e07aSXin Li // distributed under the License is distributed on an "AS IS" BASIS,
11*ec63e07aSXin Li // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*ec63e07aSXin Li // See the License for the specific language governing permissions and
13*ec63e07aSXin Li // limitations under the License.
14*ec63e07aSXin Li 
15*ec63e07aSXin Li #ifndef SANDBOXED_API_VAR_LENVAL_H_
16*ec63e07aSXin Li #define SANDBOXED_API_VAR_LENVAL_H_
17*ec63e07aSXin Li 
18*ec63e07aSXin Li #include <sys/types.h>
19*ec63e07aSXin Li #include <sys/uio.h>
20*ec63e07aSXin Li 
21*ec63e07aSXin Li #include <cstring>
22*ec63e07aSXin Li #include <memory>
23*ec63e07aSXin Li #include <string>
24*ec63e07aSXin Li #include <vector>
25*ec63e07aSXin Li 
26*ec63e07aSXin Li #include "absl/base/macros.h"
27*ec63e07aSXin Li #include "absl/status/status.h"
28*ec63e07aSXin Li #include "sandboxed_api/lenval_core.h"
29*ec63e07aSXin Li #include "sandboxed_api/var_abstract.h"
30*ec63e07aSXin Li #include "sandboxed_api/var_array.h"
31*ec63e07aSXin Li #include "sandboxed_api/var_ptr.h"
32*ec63e07aSXin Li #include "sandboxed_api/var_struct.h"
33*ec63e07aSXin Li 
34*ec63e07aSXin Li namespace sapi::v {
35*ec63e07aSXin Li 
36*ec63e07aSXin Li template <class T>
37*ec63e07aSXin Li class Proto;
38*ec63e07aSXin Li 
39*ec63e07aSXin Li // Length + value container. Represents a pointer to a LenValStruct inside the
40*ec63e07aSXin Li // sandboxee which allows the bidirectional synchronization data structures with
41*ec63e07aSXin Li // changing lengths (e.g. protobuf structures). You probably want to directly
42*ec63e07aSXin Li // use protobufs as they are easier to handle.
43*ec63e07aSXin Li class LenVal : public Var {
44*ec63e07aSXin Li  public:
LenVal(const char * data,uint64_t size)45*ec63e07aSXin Li   explicit LenVal(const char* data, uint64_t size)
46*ec63e07aSXin Li       : array_(const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(data)),
47*ec63e07aSXin Li                size),
48*ec63e07aSXin Li         struct_(size, nullptr) {}
49*ec63e07aSXin Li 
LenVal(const std::vector<uint8_t> & data)50*ec63e07aSXin Li   explicit LenVal(const std::vector<uint8_t>& data)
51*ec63e07aSXin Li       : array_(data.size()), struct_(data.size(), nullptr) {
52*ec63e07aSXin Li     memcpy(array_.GetData(), data.data(), data.size());
53*ec63e07aSXin Li   }
54*ec63e07aSXin Li 
LenVal(size_t size)55*ec63e07aSXin Li   explicit LenVal(size_t size) : array_(size), struct_(size, nullptr) {}
56*ec63e07aSXin Li 
GetType()57*ec63e07aSXin Li   Type GetType() const final { return Type::kLenVal; }
GetTypeString()58*ec63e07aSXin Li   std::string GetTypeString() const final { return "LengthValue"; }
ToString()59*ec63e07aSXin Li   std::string ToString() const final { return "LenVal"; }
60*ec63e07aSXin Li 
61*ec63e07aSXin Li   absl::Status ResizeData(RPCChannel* rpc_channel, size_t size);
GetDataSize()62*ec63e07aSXin Li   size_t GetDataSize() const { return struct_.data().size; }
GetData()63*ec63e07aSXin Li   uint8_t* GetData() const { return array_.GetData(); }
GetRemote()64*ec63e07aSXin Li   void* GetRemote() const final { return struct_.GetRemote(); }
65*ec63e07aSXin Li 
66*ec63e07aSXin Li  protected:
GetSize()67*ec63e07aSXin Li   size_t GetSize() const final { return 0; }
68*ec63e07aSXin Li 
69*ec63e07aSXin Li   absl::Status Allocate(RPCChannel* rpc_channel, bool automatic_free) override;
70*ec63e07aSXin Li   absl::Status Free(RPCChannel* rpc_channel) override;
71*ec63e07aSXin Li   absl::Status TransferToSandboxee(RPCChannel* rpc_channel, pid_t pid) override;
72*ec63e07aSXin Li   absl::Status TransferFromSandboxee(RPCChannel* rpc_channel,
73*ec63e07aSXin Li                                      pid_t pid) override;
74*ec63e07aSXin Li 
75*ec63e07aSXin Li   Array<uint8_t> array_;
76*ec63e07aSXin Li   Struct<LenValStruct> struct_;
77*ec63e07aSXin Li 
78*ec63e07aSXin Li   template <class T>
79*ec63e07aSXin Li   friend class Proto;
80*ec63e07aSXin Li };
81*ec63e07aSXin Li 
82*ec63e07aSXin Li }  // namespace sapi::v
83*ec63e07aSXin Li 
84*ec63e07aSXin Li #endif  // SANDBOXED_API_VAR_LENVAL_H_
85