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_REG_H_
|
|
|
|
#define SANDBOXED_API_VAR_REG_H_
|
|
|
|
|
|
|
|
#include <iostream>
|
2020-07-22 16:20:20 +08:00
|
|
|
#include <type_traits>
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
#include <glog/logging.h>
|
2020-07-22 16:20:20 +08:00
|
|
|
#include "absl/strings/str_cat.h"
|
|
|
|
#include "absl/strings/str_format.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/var_abstract.h"
|
|
|
|
|
2019-11-20 20:39:44 +08:00
|
|
|
namespace sapi::v {
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// The super-class for Reg. Specified as a class, so it can be used as a
|
|
|
|
// type specifier in methods.
|
|
|
|
class Callable : public Var {
|
|
|
|
public:
|
|
|
|
Callable(const Callable&) = delete;
|
|
|
|
Callable& operator=(const Callable&) = delete;
|
|
|
|
|
|
|
|
// Get pointer to the stored data.
|
|
|
|
virtual const void* GetDataPtr() = 0;
|
2020-07-22 16:20:20 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// Set internal data from ptr.
|
|
|
|
virtual void SetDataFromPtr(const void* ptr, size_t max_sz) = 0;
|
2020-07-22 16:20:20 +08:00
|
|
|
|
|
|
|
// Get data from internal ptr.
|
2019-03-19 00:21:48 +08:00
|
|
|
void GetDataFromPtr(void* ptr, size_t max_sz) {
|
|
|
|
size_t min_sz = std::min<size_t>(GetSize(), max_sz);
|
|
|
|
memcpy(ptr, GetDataPtr(), min_sz);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
Callable() = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
// class Reg represents register-sized variables.
|
|
|
|
template <typename T>
|
|
|
|
class Reg : public Callable {
|
2020-07-20 18:07:15 +08:00
|
|
|
public:
|
2020-07-22 16:20:20 +08:00
|
|
|
static_assert(std::is_integral_v<T> || std::is_floating_point_v<T> ||
|
|
|
|
std::is_pointer_v<T> || std::is_enum_v<T>,
|
2019-03-19 00:21:48 +08:00
|
|
|
"Only register-sized types are allowed as template argument "
|
|
|
|
"for class Reg.");
|
|
|
|
|
2021-10-11 20:37:34 +08:00
|
|
|
explicit Reg(const T value = {}) {
|
|
|
|
value_ = value;
|
|
|
|
SetLocal(&value_);
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Getter/Setter for the stored value.
|
2021-10-11 20:37:34 +08:00
|
|
|
virtual T GetValue() const { return value_; }
|
|
|
|
virtual void SetValue(T value) { value_ = value; }
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
const void* GetDataPtr() override {
|
2021-10-11 20:37:34 +08:00
|
|
|
return reinterpret_cast<const void*>(&value_);
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
void SetDataFromPtr(const void* ptr, size_t max_sz) override {
|
2021-10-11 20:37:34 +08:00
|
|
|
memcpy(&value_, ptr, std::min(GetSize(), max_sz));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t GetSize() const override { return sizeof(T); }
|
|
|
|
|
2020-07-22 16:20:20 +08:00
|
|
|
Type GetType() const override;
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2020-07-22 16:20:20 +08:00
|
|
|
std::string GetTypeString() const override;
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2020-07-22 16:20:20 +08:00
|
|
|
std::string ToString() const override;
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
protected:
|
2021-10-11 20:37:34 +08:00
|
|
|
T value_; // The stored value.
|
2020-07-22 16:20:20 +08:00
|
|
|
};
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2020-07-22 16:20:20 +08:00
|
|
|
template <typename T>
|
|
|
|
Type Reg<T>::GetType() const {
|
|
|
|
if constexpr (std::is_integral_v<T> || std::is_enum_v<T>) {
|
|
|
|
return Type::kInt;
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
2020-07-22 16:20:20 +08:00
|
|
|
if constexpr (std::is_floating_point_v<T>) {
|
|
|
|
return Type::kFloat;
|
|
|
|
}
|
|
|
|
if constexpr (std::is_pointer_v<T>) {
|
|
|
|
return Type::kPointer;
|
|
|
|
}
|
|
|
|
// Not reached
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
std::string Reg<T>::GetTypeString() const {
|
|
|
|
if constexpr (std::is_integral_v<T> || std::is_enum_v<T>) {
|
|
|
|
return "Integer";
|
|
|
|
}
|
|
|
|
if constexpr (std::is_floating_point_v<T>) {
|
|
|
|
return "Floating-point";
|
|
|
|
}
|
|
|
|
if constexpr (std::is_pointer_v<T>) {
|
|
|
|
return "Pointer";
|
|
|
|
}
|
|
|
|
// Not reached
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
std::string Reg<T>::ToString() const {
|
|
|
|
if constexpr (std::is_integral_v<T> || std::is_enum_v<T>) {
|
2021-10-11 20:37:34 +08:00
|
|
|
return absl::StrCat(value_);
|
2020-07-22 16:20:20 +08:00
|
|
|
}
|
|
|
|
if constexpr (std::is_floating_point_v<T>) {
|
2021-10-11 20:37:34 +08:00
|
|
|
return absl::StrFormat("%.10f", value_);
|
2020-07-22 16:20:20 +08:00
|
|
|
}
|
|
|
|
if constexpr (std::is_pointer<T>::value) {
|
2021-10-11 20:37:34 +08:00
|
|
|
return absl::StrFormat("%p", value_);
|
2020-07-22 16:20:20 +08:00
|
|
|
}
|
|
|
|
// Not reached.
|
|
|
|
}
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2019-11-20 20:39:44 +08:00
|
|
|
} // namespace sapi::v
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
#endif // SANDBOXED_API_VAR_REG_H_
|