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 #include "sandboxed_api/var_lenval.h"
16*ec63e07aSXin Li
17*ec63e07aSXin Li #include <sys/types.h>
18*ec63e07aSXin Li
19*ec63e07aSXin Li #include <cstddef>
20*ec63e07aSXin Li
21*ec63e07aSXin Li #include "absl/status/status.h"
22*ec63e07aSXin Li #include "sandboxed_api/rpcchannel.h"
23*ec63e07aSXin Li #include "sandboxed_api/util/status_macros.h"
24*ec63e07aSXin Li
25*ec63e07aSXin Li namespace sapi::v {
26*ec63e07aSXin Li
Allocate(RPCChannel * rpc_channel,bool automatic_free)27*ec63e07aSXin Li absl::Status LenVal::Allocate(RPCChannel* rpc_channel, bool automatic_free) {
28*ec63e07aSXin Li SAPI_RETURN_IF_ERROR(struct_.Allocate(rpc_channel, automatic_free));
29*ec63e07aSXin Li SAPI_RETURN_IF_ERROR(array_.Allocate(rpc_channel, true));
30*ec63e07aSXin Li
31*ec63e07aSXin Li // Set data pointer.
32*ec63e07aSXin Li struct_.mutable_data()->data = array_.GetRemote();
33*ec63e07aSXin Li return absl::OkStatus();
34*ec63e07aSXin Li }
35*ec63e07aSXin Li
Free(RPCChannel * rpc_channel)36*ec63e07aSXin Li absl::Status LenVal::Free(RPCChannel* rpc_channel) {
37*ec63e07aSXin Li SAPI_RETURN_IF_ERROR(array_.Free(rpc_channel));
38*ec63e07aSXin Li SAPI_RETURN_IF_ERROR(struct_.Free(rpc_channel));
39*ec63e07aSXin Li return absl::OkStatus();
40*ec63e07aSXin Li }
41*ec63e07aSXin Li
TransferToSandboxee(RPCChannel * rpc_channel,pid_t pid)42*ec63e07aSXin Li absl::Status LenVal::TransferToSandboxee(RPCChannel* rpc_channel, pid_t pid) {
43*ec63e07aSXin Li // Sync the structure and the underlying array.
44*ec63e07aSXin Li SAPI_RETURN_IF_ERROR(struct_.TransferToSandboxee(rpc_channel, pid));
45*ec63e07aSXin Li SAPI_RETURN_IF_ERROR(array_.TransferToSandboxee(rpc_channel, pid));
46*ec63e07aSXin Li return absl::OkStatus();
47*ec63e07aSXin Li }
48*ec63e07aSXin Li
TransferFromSandboxee(RPCChannel * rpc_channel,pid_t pid)49*ec63e07aSXin Li absl::Status LenVal::TransferFromSandboxee(RPCChannel* rpc_channel, pid_t pid) {
50*ec63e07aSXin Li // Sync the structure back.
51*ec63e07aSXin Li SAPI_RETURN_IF_ERROR(struct_.TransferFromSandboxee(rpc_channel, pid));
52*ec63e07aSXin Li
53*ec63e07aSXin Li // Resize the local array if required. Also make sure we own the buffer, this
54*ec63e07aSXin Li // is the only way we can be sure that the buffer is writable.
55*ec63e07aSXin Li size_t new_size = struct_.data().size;
56*ec63e07aSXin Li SAPI_RETURN_IF_ERROR(array_.EnsureOwnedLocalBuffer(new_size));
57*ec63e07aSXin Li
58*ec63e07aSXin Li // Remote pointer might have changed, update it.
59*ec63e07aSXin Li array_.SetRemote(struct_.data().data);
60*ec63e07aSXin Li return array_.TransferFromSandboxee(rpc_channel, pid);
61*ec63e07aSXin Li }
62*ec63e07aSXin Li
ResizeData(RPCChannel * rpc_channel,size_t size)63*ec63e07aSXin Li absl::Status LenVal::ResizeData(RPCChannel* rpc_channel, size_t size) {
64*ec63e07aSXin Li SAPI_RETURN_IF_ERROR(array_.Resize(rpc_channel, size));
65*ec63e07aSXin Li auto* struct_data = struct_.mutable_data();
66*ec63e07aSXin Li struct_data->data = array_.GetRemote();
67*ec63e07aSXin Li struct_data->size = size;
68*ec63e07aSXin Li return absl::OkStatus();
69*ec63e07aSXin Li }
70*ec63e07aSXin Li
71*ec63e07aSXin Li } // namespace sapi::v
72