1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
#pragma once
// IWYU pragma: private, include "rlbox.hpp"
// IWYU pragma: friend "rlbox_.*\.hpp"
#include <cstring>
#include <functional>
#include <type_traits>
#include "rlbox_conversion.hpp"
#include "rlbox_helpers.hpp"
#include "rlbox_types.hpp"
#include "rlbox_wrapper_traits.hpp"
namespace rlbox::detail {
template<typename T, typename T_Sbx, typename T_Enable = void>
struct convert_to_sandbox_equivalent_helper;
template<typename T, typename T_Sbx>
struct convert_to_sandbox_equivalent_helper<
T,
T_Sbx,
std::enable_if_t<!std::is_class_v<T>>>
{
using type = typename rlbox_sandbox<
T_Sbx>::template convert_to_sandbox_equivalent_nonclass_t<T>;
};
template<typename T, typename T_Sbx>
using convert_to_sandbox_equivalent_t =
typename convert_to_sandbox_equivalent_helper<T, T_Sbx>::type;
// This is used by rlbox_load_structs_from_library to test the current namespace
struct markerStruct
{};
}
#define helper_create_converted_field(fieldType, fieldName, isFrozen) \
typename detail::convert_to_sandbox_equivalent_t<fieldType, T_Sbx> fieldName;
#define helper_no_op()
#define sandbox_equivalent_specialization(T, libId) \
template<typename T_Sbx> \
struct Sbx_##libId##_##T \
{ \
sandbox_fields_reflection_##libId##_class_##T( \
helper_create_converted_field, \
helper_no_op) \
}; \
\
/* add convert_to_sandbox_equivalent_t specialization for new struct */ \
namespace detail { \
template<typename T_Template, typename T_Sbx> \
struct convert_to_sandbox_equivalent_helper< \
T_Template, \
T_Sbx, \
std::enable_if_t<std::is_same_v<T_Template, T>>> \
{ \
using type = Sbx_##libId##_##T<T_Sbx>; \
}; \
}
#define helper_create_tainted_field( \
fieldType, fieldName, isFrozen, MaybeConst) \
MaybeConst tainted<fieldType, T_Sbx> fieldName;
#define helper_create_tainted_vol_field( \
fieldType, fieldName, isFrozen, MaybeConst) \
MaybeConst tainted_volatile<fieldType, T_Sbx> fieldName;
#define helper_convert_type(fieldType, fieldName, isFrozen) \
::rlbox::detail::convert_type<T_Sbx, Direction, Context>( \
lhs.fieldName, rhs.fieldName, example_unsandboxed_ptr, sandbox_ptr);
#define helper_find_example_pointer_or_null(fieldType, fieldName, isFrozen) \
{ \
const void* ret = fieldName.find_example_pointer_or_null(); \
if (ret != nullptr) { \
return ret; \
} \
}
#define tainted_data_specialization_helper(MaybeConst, T, libId) \
\
template<typename T_Sbx> \
class tainted_volatile<MaybeConst T, T_Sbx> \
{ \
KEEP_CLASSES_FRIENDLY \
KEEP_CAST_FRIENDLY \
\
private: \
inline MaybeConst Sbx_##libId##_##T<T_Sbx>& \
get_sandbox_value_ref() noexcept \
{ \
return *reinterpret_cast<MaybeConst Sbx_##libId##_##T<T_Sbx>*>(this); \
} \
\
inline const Sbx_##libId##_##T<T_Sbx>& get_sandbox_value_ref() \
const noexcept \
{ \
return *reinterpret_cast<const Sbx_##libId##_##T<T_Sbx>*>(this); \
} \
\
inline T get_raw_value() const noexcept \
{ \
T lhs; \
const auto& rhs = get_sandbox_value_ref(); \
constexpr auto Direction = \
detail::adjust_type_direction::TO_APPLICATION; \
constexpr auto Context = detail::adjust_type_context::EXAMPLE; \
/* This is a tainted_volatile, so its address is a valid example for use \
* as example_unsandboxed_ptr */ \
const void* example_unsandboxed_ptr = &rhs; \
rlbox_sandbox<T_Sbx>* sandbox_ptr = nullptr; \
sandbox_fields_reflection_##libId##_class_##T(helper_convert_type, \
helper_no_op) \
\
return lhs; \
} \
\
/* get_raw_sandbox_value has to return a custom struct to deal with the \
* adjusted machine model, to ensure */ \
inline Sbx_##libId##_##T<T_Sbx> get_raw_sandbox_value() const noexcept \
{ \
auto ret_ptr = reinterpret_cast<const Sbx_##libId##_##T<T_Sbx>*>(this); \
return *ret_ptr; \
} \
\
tainted_volatile() = default; \
tainted_volatile(const tainted_volatile<MaybeConst T, T_Sbx>& p) = \
default; \
\
public: \
sandbox_fields_reflection_##libId##_class_##T( \
helper_create_tainted_vol_field, \
helper_no_op, \
MaybeConst) \
\
inline tainted<MaybeConst T*, T_Sbx> \
operator&() const noexcept \
{ \
auto ref_cast = \
reinterpret_cast<MaybeConst T*>(&get_sandbox_value_ref()); \
auto ret = tainted<MaybeConst T*, T_Sbx>::internal_factory(ref_cast); \
return ret; \
} \
\
inline auto UNSAFE_unverified() const { return get_raw_value(); } \
inline auto UNSAFE_sandboxed(rlbox_sandbox<T_Sbx>& sandbox) const \
{ \
return get_raw_sandbox_value(sandbox); \
} \
\
template<size_t N> \
inline auto unverified_safe_because(const char (&reason)[N]) const \
{ \
RLBOX_UNUSED(reason); \
return UNSAFE_unverified(); \
} \
\
T copy_and_verify(std::function<T(tainted<T, T_Sbx>)> verifier) \
{ \
tainted<T, T_Sbx> val(*this); \
return verifier(val); \
} \
\
/* Can't define this yet due, to mutually dependent definition between \
tainted and tainted_volatile for structs */ \
inline tainted_volatile<MaybeConst T, T_Sbx>& operator=( \
const tainted<T, T_Sbx>& rhs); \
}; \
\
template<typename T_Sbx> \
class tainted<MaybeConst T, T_Sbx> \
{ \
KEEP_CLASSES_FRIENDLY \
KEEP_CAST_FRIENDLY \
\
private: \
inline MaybeConst T& get_raw_value_ref() noexcept \
{ \
return *reinterpret_cast<MaybeConst T*>(this); \
} \
\
inline const T& get_raw_value_ref() const noexcept \
{ \
return *reinterpret_cast<const T*>(this); \
} \
\
inline T get_raw_value() const noexcept \
{ \
auto ret_ptr = reinterpret_cast<const T*>(this); \
return *ret_ptr; \
} \
\
/* get_raw_sandbox_value has to return a custom struct to deal with the \
* adjusted machine model, to ensure */ \
inline Sbx_##libId##_##T<T_Sbx> get_raw_sandbox_value( \
rlbox_sandbox<T_Sbx>& sandbox) const noexcept \
{ \
Sbx_##libId##_##T<T_Sbx> lhs; \
const auto& rhs = get_raw_value_ref(); \
constexpr auto Direction = detail::adjust_type_direction::TO_SANDBOX; \
constexpr auto Context = detail::adjust_type_context::SANDBOX; \
const void* example_unsandboxed_ptr = nullptr; \
rlbox_sandbox<T_Sbx>* sandbox_ptr = &sandbox; \
sandbox_fields_reflection_##libId##_class_##T(helper_convert_type, \
helper_no_op) \
\
return lhs; \
} \
\
inline const void* find_example_pointer_or_null() const noexcept \
{ \
sandbox_fields_reflection_##libId##_class_##T( \
helper_find_example_pointer_or_null, helper_no_op) \
\
return nullptr; \
} \
\
public: \
sandbox_fields_reflection_##libId##_class_##T(helper_create_tainted_field, \
helper_no_op, \
MaybeConst) \
\
tainted() = default; \
tainted(const tainted<MaybeConst T, T_Sbx>& p) = default; \
\
tainted(const tainted_volatile<T, T_Sbx>& p) \
{ \
auto& lhs = get_raw_value_ref(); \
auto& rhs = p.get_sandbox_value_ref(); \
constexpr auto Direction = \
detail::adjust_type_direction::TO_APPLICATION; \
constexpr auto Context = detail::adjust_type_context::EXAMPLE; \
/* This is a tainted_volatile, so its address is a valid for use as */ \
/* example_unsandboxed_ptr */ \
const void* example_unsandboxed_ptr = &rhs; \
rlbox_sandbox<T_Sbx>* sandbox_ptr = nullptr; \
sandbox_fields_reflection_##libId##_class_##T(helper_convert_type, \
helper_no_op) \
} \
\
inline tainted_opaque<MaybeConst T, T_Sbx> to_opaque() \
{ \
return *reinterpret_cast<tainted_opaque<MaybeConst T, T_Sbx>*>(this); \
} \
\
inline auto UNSAFE_unverified() const { return get_raw_value(); } \
inline auto UNSAFE_sandboxed(rlbox_sandbox<T_Sbx>& sandbox) const \
{ \
return get_raw_sandbox_value(sandbox); \
} \
\
template<size_t N> \
inline auto unverified_safe_because(const char (&reason)[N]) const \
{ \
RLBOX_UNUSED(reason); \
return UNSAFE_unverified(); \
} \
\
T copy_and_verify(std::function<T(tainted<T, T_Sbx>)> verifier) \
{ \
return verifier(*this); \
} \
}; \
\
/* Had to delay the definition due, to mutually dependence between \
tainted and tainted_volatile for structs */ \
template<typename T_Sbx> \
inline tainted_volatile<MaybeConst T, T_Sbx>& \
tainted_volatile<MaybeConst T, T_Sbx>::operator=( \
const tainted<T, T_Sbx>& rhs_wrap) \
{ \
auto& lhs = get_sandbox_value_ref(); \
auto& rhs = rhs_wrap.get_raw_value_ref(); \
constexpr auto Direction = detail::adjust_type_direction::TO_SANDBOX; \
constexpr auto Context = detail::adjust_type_context::EXAMPLE; \
/* This is a tainted_volatile, so its address is a valid example for */ \
/* use as example_unsandboxed_ptr */ \
const void* example_unsandboxed_ptr = &lhs; \
rlbox_sandbox<T_Sbx>* sandbox_ptr = nullptr; \
sandbox_fields_reflection_##libId##_class_##T(helper_convert_type, \
helper_no_op) \
\
return *this; \
}
#define tainted_data_specialization(T, libId) \
tainted_data_specialization_helper(, T, libId) \
tainted_data_specialization_helper(const, T, libId)
#define convert_type_specialization(T, libId) \
namespace detail { \
template<typename T_Sbx, \
detail::adjust_type_direction Direction, \
adjust_type_context Context, \
typename T_From> \
class convert_type_class<T_Sbx, Direction, Context, T, T_From> \
{ \
public: \
static inline void run(T& lhs, \
const T_From& rhs, \
const void* example_unsandboxed_ptr, \
rlbox_sandbox<T_Sbx>* sandbox_ptr) \
{ \
sandbox_fields_reflection_##libId##_class_##T(helper_convert_type, \
helper_no_op) \
} \
}; \
\
template<typename T_Sbx, \
detail::adjust_type_direction Direction, \
adjust_type_context Context, \
typename T_From> \
class convert_type_class<T_Sbx, \
Direction, \
Context, \
Sbx_##libId##_##T<T_Sbx>, \
T_From> \
{ \
public: \
static inline void run(Sbx_##libId##_##T<T_Sbx>& lhs, \
const T_From& rhs, \
const void* example_unsandboxed_ptr, \
rlbox_sandbox<T_Sbx>* sandbox_ptr) \
{ \
sandbox_fields_reflection_##libId##_class_##T(helper_convert_type, \
helper_no_op) \
} \
}; \
}
// clang-format off
#define rlbox_load_structs_from_library(libId) \
namespace rlbox { \
/* check that this macro is called in a global namespace */ \
static_assert( \
::rlbox::detail::is_member_of_rlbox_detail<detail::markerStruct>, \
"Invoke rlbox_load_structs_from_library in the global namespace"); \
\
sandbox_fields_reflection_##libId##_allClasses( \
sandbox_equivalent_specialization) \
\
sandbox_fields_reflection_##libId##_allClasses( \
tainted_data_specialization) \
\
sandbox_fields_reflection_##libId##_allClasses( \
convert_type_specialization) \
} \
RLBOX_REQUIRE_SEMI_COLON
// clang-format on
|