xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/var_lenval.cc (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "sandboxed_api/var_lenval.h"
16 
17 #include <sys/types.h>
18 
19 #include <cstddef>
20 
21 #include "absl/status/status.h"
22 #include "sandboxed_api/rpcchannel.h"
23 #include "sandboxed_api/util/status_macros.h"
24 
25 namespace sapi::v {
26 
Allocate(RPCChannel * rpc_channel,bool automatic_free)27 absl::Status LenVal::Allocate(RPCChannel* rpc_channel, bool automatic_free) {
28   SAPI_RETURN_IF_ERROR(struct_.Allocate(rpc_channel, automatic_free));
29   SAPI_RETURN_IF_ERROR(array_.Allocate(rpc_channel, true));
30 
31   // Set data pointer.
32   struct_.mutable_data()->data = array_.GetRemote();
33   return absl::OkStatus();
34 }
35 
Free(RPCChannel * rpc_channel)36 absl::Status LenVal::Free(RPCChannel* rpc_channel) {
37   SAPI_RETURN_IF_ERROR(array_.Free(rpc_channel));
38   SAPI_RETURN_IF_ERROR(struct_.Free(rpc_channel));
39   return absl::OkStatus();
40 }
41 
TransferToSandboxee(RPCChannel * rpc_channel,pid_t pid)42 absl::Status LenVal::TransferToSandboxee(RPCChannel* rpc_channel, pid_t pid) {
43   // Sync the structure and the underlying array.
44   SAPI_RETURN_IF_ERROR(struct_.TransferToSandboxee(rpc_channel, pid));
45   SAPI_RETURN_IF_ERROR(array_.TransferToSandboxee(rpc_channel, pid));
46   return absl::OkStatus();
47 }
48 
TransferFromSandboxee(RPCChannel * rpc_channel,pid_t pid)49 absl::Status LenVal::TransferFromSandboxee(RPCChannel* rpc_channel, pid_t pid) {
50   // Sync the structure back.
51   SAPI_RETURN_IF_ERROR(struct_.TransferFromSandboxee(rpc_channel, pid));
52 
53   // Resize the local array if required. Also make sure we own the buffer, this
54   // is the only way we can be sure that the buffer is writable.
55   size_t new_size = struct_.data().size;
56   SAPI_RETURN_IF_ERROR(array_.EnsureOwnedLocalBuffer(new_size));
57 
58   // Remote pointer might have changed, update it.
59   array_.SetRemote(struct_.data().data);
60   return array_.TransferFromSandboxee(rpc_channel, pid);
61 }
62 
ResizeData(RPCChannel * rpc_channel,size_t size)63 absl::Status LenVal::ResizeData(RPCChannel* rpc_channel, size_t size) {
64   SAPI_RETURN_IF_ERROR(array_.Resize(rpc_channel, size));
65   auto* struct_data = struct_.mutable_data();
66   struct_data->data = array_.GetRemote();
67   struct_data->size = size;
68   return absl::OkStatus();
69 }
70 
71 }  // namespace sapi::v
72