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_ARRAY_H_
|
|
|
|
#define SANDBOXED_API_VAR_ARRAY_H_
|
|
|
|
|
2020-09-18 22:22:51 +08:00
|
|
|
#include <algorithm>
|
2019-03-19 00:21:48 +08:00
|
|
|
#include <cstring>
|
|
|
|
#include <memory>
|
|
|
|
|
2020-09-18 22:22:51 +08:00
|
|
|
#include <glog/logging.h>
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "absl/base/macros.h"
|
2020-02-28 01:23:44 +08:00
|
|
|
#include "absl/status/status.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "absl/strings/str_cat.h"
|
2020-10-14 17:02:44 +08:00
|
|
|
#include "absl/strings/string_view.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/rpcchannel.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_abstract.h"
|
|
|
|
#include "sandboxed_api/var_pointable.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
|
|
|
|
|
|
|
// Class representing an array.
|
|
|
|
template <class T>
|
|
|
|
class Array : public Var, public Pointable {
|
|
|
|
public:
|
|
|
|
// The array is not owned by this object.
|
|
|
|
Array(T* arr, size_t nelem)
|
|
|
|
: arr_(arr),
|
|
|
|
nelem_(nelem),
|
|
|
|
total_size_(nelem_ * sizeof(T)),
|
|
|
|
buffer_owned_(false) {
|
2020-09-18 22:22:51 +08:00
|
|
|
SetLocal(const_cast<std::remove_const_t<T>*>(arr_));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
2021-10-11 20:37:34 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// The array is allocated and owned by this object.
|
|
|
|
explicit Array(size_t nelem)
|
2020-09-18 22:22:51 +08:00
|
|
|
: nelem_(nelem), total_size_(nelem_ * sizeof(T)), buffer_owned_(true) {
|
|
|
|
void* storage = malloc(sizeof(T) * nelem);
|
|
|
|
CHECK(storage != nullptr);
|
|
|
|
SetLocal(storage);
|
|
|
|
arr_ = static_cast<T*>(storage);
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
2021-10-11 20:37:34 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
virtual ~Array() {
|
|
|
|
if (buffer_owned_) {
|
2020-09-18 22:22:51 +08:00
|
|
|
free(const_cast<std::remove_const_t<T>*>(arr_));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
T& operator[](size_t v) const { return arr_[v]; }
|
|
|
|
T* GetData() const { return arr_; }
|
|
|
|
|
|
|
|
size_t GetNElem() const { return nelem_; }
|
|
|
|
size_t GetSize() const final { return total_size_; }
|
|
|
|
Type GetType() const final { return Type::kArray; }
|
|
|
|
std::string GetTypeString() const final { return "Array"; }
|
|
|
|
std::string ToString() const override {
|
|
|
|
return absl::StrCat("Array, elem size: ", sizeof(T),
|
|
|
|
" B., total size: ", total_size_,
|
|
|
|
" B., nelems: ", GetNElem());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resizes the local and remote buffer using realloc(). Note that this will
|
|
|
|
// make all pointers to the current data (inside and outside of the sandbox)
|
|
|
|
// invalid.
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status Resize(RPCChannel* rpc_channel, size_t nelems) {
|
2019-03-19 00:21:48 +08:00
|
|
|
size_t absolute_size = sizeof(T) * nelems;
|
|
|
|
// Resize local buffer.
|
|
|
|
SAPI_RETURN_IF_ERROR(EnsureOwnedLocalBuffer(absolute_size));
|
|
|
|
|
|
|
|
// Resize remote buffer and update local pointer.
|
|
|
|
void* new_addr;
|
|
|
|
|
|
|
|
SAPI_RETURN_IF_ERROR(
|
|
|
|
rpc_channel->Reallocate(GetRemote(), absolute_size, &new_addr));
|
|
|
|
if (!new_addr) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("Reallocate() returned nullptr");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
SetRemote(new_addr);
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-10-11 20:37:34 +08:00
|
|
|
friend class LenVal;
|
|
|
|
|
|
|
|
Ptr* CreatePtr(Pointable::SyncType type) override {
|
|
|
|
return new Ptr(this, type);
|
|
|
|
}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// Resizes the internal storage.
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status EnsureOwnedLocalBuffer(size_t size) {
|
2019-03-19 00:21:48 +08:00
|
|
|
if (size % sizeof(T)) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError(
|
2019-03-19 00:21:48 +08:00
|
|
|
"Array size not a multiple of the item size");
|
|
|
|
}
|
|
|
|
// Do not (re-)allocate memory if the new size matches our size - except
|
|
|
|
// when we don't own that buffer.
|
|
|
|
if (size == total_size_ && buffer_owned_) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
void* new_addr = nullptr;
|
|
|
|
if (buffer_owned_) {
|
|
|
|
new_addr = realloc(arr_, size);
|
|
|
|
} else {
|
|
|
|
new_addr = malloc(size);
|
|
|
|
if (new_addr) {
|
2021-02-03 15:58:41 +08:00
|
|
|
memcpy(new_addr, arr_, std::min(size, total_size_));
|
2019-03-19 00:21:48 +08:00
|
|
|
buffer_owned_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!new_addr) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("(Re-)malloc failed");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
arr_ = static_cast<T*>(new_addr);
|
|
|
|
total_size_ = size;
|
|
|
|
nelem_ = size / sizeof(T);
|
2020-09-18 22:22:51 +08:00
|
|
|
SetLocal(new_addr);
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pointer to the data, owned by the object if buffer_owned_ is 'true'.
|
|
|
|
T* arr_;
|
2021-10-11 20:37:34 +08:00
|
|
|
size_t nelem_; // Number of elements
|
|
|
|
size_t total_size_; // Total size in bytes
|
|
|
|
bool buffer_owned_; // Whether we own the buffer
|
2019-03-19 00:21:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Specialized Array class for representing NUL-terminated C-style strings. The
|
|
|
|
// buffer is owned by the class, and is mutable.
|
|
|
|
class CStr : public Array<char> {
|
|
|
|
public:
|
2020-10-14 17:02:44 +08:00
|
|
|
explicit CStr(absl::string_view cstr) : Array<char>(cstr.size() + 1) {
|
|
|
|
std::copy(cstr.begin(), cstr.end(), GetData());
|
|
|
|
GetData()[cstr.size()] = '\0';
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string ToString() const final {
|
|
|
|
return absl::StrCat("CStr: len(w/o NUL):", strlen(GetData()), ", ['",
|
|
|
|
GetData(), "']");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Specialized Array class for representing NUL-terminated C-style strings. The
|
|
|
|
// buffer is not owned by the class and is not mutable.
|
|
|
|
class ConstCStr : public Array<const char> {
|
|
|
|
public:
|
|
|
|
explicit ConstCStr(const char* cstr)
|
|
|
|
: Array<const char>(cstr, strlen(cstr) + 1) {}
|
|
|
|
|
|
|
|
std::string ToString() const final {
|
2020-09-18 22:22:51 +08:00
|
|
|
return absl::StrCat("ConstCStr: len(w/o NUL):", strlen(GetData()), ", ['",
|
2019-03-19 00:21:48 +08:00
|
|
|
GetData(), "']");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-11-20 20:39:44 +08:00
|
|
|
} // namespace sapi::v
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
#endif // SANDBOXED_API_VAR_ARRAY_H_
|