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.
|
|
|
|
|
|
|
|
#ifndef SANDBOXED_API_VAR_PTR_H_
|
|
|
|
#define SANDBOXED_API_VAR_PTR_H_
|
|
|
|
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstring>
|
2019-03-19 00:21:48 +08:00
|
|
|
#include <memory>
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <string>
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2024-01-29 19:22:32 +08:00
|
|
|
#include "absl/base/attributes.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "absl/base/macros.h"
|
|
|
|
#include "absl/strings/str_format.h"
|
2021-12-02 01:54:09 +08:00
|
|
|
#include "sandboxed_api/var_abstract.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/var_reg.h"
|
|
|
|
|
2019-11-20 20:39:44 +08:00
|
|
|
namespace sapi::v {
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Class representing a pointer. Takes both Var* and regular pointers in the
|
|
|
|
// initializers.
|
2021-12-02 01:54:09 +08:00
|
|
|
class Ptr : public Reg<Var*> {
|
2019-03-19 00:21:48 +08:00
|
|
|
public:
|
|
|
|
Ptr() = delete;
|
|
|
|
|
2021-10-11 20:37:34 +08:00
|
|
|
explicit Ptr(Var* value, SyncType sync_type) : sync_type_(sync_type) {
|
|
|
|
Reg<Var*>::SetValue(value);
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Var* GetPointedVar() const { return Reg<Var*>::GetValue(); }
|
|
|
|
|
2021-10-11 20:37:34 +08:00
|
|
|
void SetValue(Var* ptr) final { value_->SetRemote(ptr); }
|
2020-07-20 22:05:24 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
Var* GetValue() const final {
|
2021-10-11 20:37:34 +08:00
|
|
|
return reinterpret_cast<Var*>(value_->GetRemote());
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const void* GetDataPtr() final {
|
|
|
|
remote_ptr_cache_ = GetValue();
|
|
|
|
return &remote_ptr_cache_;
|
|
|
|
}
|
2020-07-20 22:05:24 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
void SetDataFromPtr(const void* ptr, size_t max_sz) final {
|
|
|
|
void* tmp;
|
|
|
|
memcpy(&tmp, ptr, std::min(sizeof(tmp), max_sz));
|
|
|
|
SetValue(reinterpret_cast<Var*>(tmp));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Getter/Setter for the sync_type_ field.
|
|
|
|
SyncType GetSyncType() { return sync_type_; }
|
|
|
|
void SetSyncType(SyncType sync_type) { sync_type_ = sync_type; }
|
|
|
|
|
|
|
|
std::string ToString() const final {
|
2020-07-20 22:05:24 +08:00
|
|
|
Var* var = GetPointedVar();
|
2019-03-19 00:21:48 +08:00
|
|
|
return absl::StrFormat(
|
|
|
|
"Ptr to obj:%p (type:'%s' val:'%s'), local:%p, remote:%p, size:%tx",
|
2020-07-20 22:05:24 +08:00
|
|
|
var, var->GetTypeString(), var->ToString(), var->GetLocal(),
|
|
|
|
var->GetRemote(), var->GetSize());
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2021-10-11 20:37:34 +08:00
|
|
|
private:
|
2019-03-19 00:21:48 +08:00
|
|
|
// GetDataPtr() interface requires of us to return a pointer to the data
|
|
|
|
// (variable) that can be copied. We cannot get pointer to pointer with
|
|
|
|
// Var::GetRemote(), hence we cache it, and return pointer to it.
|
|
|
|
void* remote_ptr_cache_;
|
|
|
|
|
|
|
|
// Shall we synchronize the underlying object before/after call.
|
|
|
|
SyncType sync_type_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Good, old nullptr
|
2024-01-29 19:22:32 +08:00
|
|
|
class ABSL_DEPRECATED(
|
|
|
|
"Use regular `nullptr` or `NULL` instead. This class will eventually get "
|
|
|
|
"removed") NullPtr : public Ptr {
|
2019-03-19 00:21:48 +08:00
|
|
|
public:
|
2020-07-20 22:05:24 +08:00
|
|
|
NullPtr() : Ptr(&void_obj_, SyncType::kSyncNone) {}
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
Reg<void*> void_obj_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Pointer, which can only point to remote memory, and is never synchronized.
|
|
|
|
class RemotePtr : public Ptr {
|
|
|
|
public:
|
|
|
|
explicit RemotePtr(void* remote_addr)
|
2020-07-20 22:05:24 +08:00
|
|
|
: Ptr(&pointed_obj_, SyncType::kSyncNone) {
|
2019-03-19 00:21:48 +08:00
|
|
|
pointed_obj_.SetRemote(remote_addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Reg<void*> pointed_obj_;
|
|
|
|
};
|
|
|
|
|
2019-11-20 20:39:44 +08:00
|
|
|
} // namespace sapi::v
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
#endif // SANDBOXED_API_VAR_PTR_H_
|