libintrovirt v0.57.4
IntroVirt introspection library
Loading...
Searching...
No Matches
guest_member_ptr.hh
Go to the documentation of this file.
1/*
2 * Copyright 2021 Assured Information Security, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#pragma once
17
18#include "guest_ptr.hh"
19#include "guest_size_t_ptr.hh"
20
21namespace introvirt {
22
24template <typename _ResultType, typename _PtrType>
25class guest_member_ptr final {
26 using _basic_resulttype = std::remove_extent_t<_ResultType>;
27 using _basic_ptrtype = std::remove_extent_t<_PtrType>;
28
29 static_assert(std::is_same_v<_basic_ptrtype, uint32_t> ||
30 std::is_same_v<_basic_ptrtype, uint64_t>,
31 "Invalid PtrType");
32
33 static constexpr bool _is_result_array = std::is_array_v<_ResultType>;
34 static constexpr bool _is_ptrtype_array = std::is_array_v<_PtrType>;
35 static constexpr bool _is_pointer = std::is_pointer_v<_basic_resulttype>;
36 static constexpr bool _is_cstring = std::is_same_v<std::remove_const_t<_ResultType>, char[]>;
37 static constexpr bool _is_wstring =
38 std::is_same_v<std::remove_const_t<_ResultType>, char16_t[]>;
39
40 using _outptr_type =
42
43 inline void _check_bounds(size_t index) const {
44 constexpr bool length = sizeof(_PtrType) / sizeof(_basic_ptrtype);
45 introvirt_assert(index < length, "Accessing array out of bounds");
46 }
47
48 public:
49 explicit inline operator bool() const { return raw_ != 0; }
50 constexpr bool x64() const { return std::is_same_v<_basic_ptrtype, uint64_t>; }
51
53 void set(_basic_ptrtype in) {
54 static_assert(!_is_ptrtype_array, "set() requires a length for array types");
55 raw_ = in;
56 }
57 void set(_basic_ptrtype in, size_t index) {
58 static_assert(_is_ptrtype_array, "set() only requires a length for array types");
59 _check_bounds(index);
60 raw_[index] = in;
61 }
62 template <typename Tp, typename PtrType>
63 void set(const guest_ptr<Tp, PtrType>& in) {
64 static_assert(!_is_ptrtype_array, "set() requires a length for array types");
65 raw_ = in.address();
66 }
67 template <typename Tp, typename PtrType>
68 void set(const guest_ptr<Tp, PtrType>& in, size_t index) {
69 static_assert(_is_ptrtype_array, "set() only requires a length for array types");
70 _check_bounds(index);
71 raw_[index] = in.address();
72 }
73
76 // Neither the result nor the ptrtype is not an array
77 static_assert(!_is_result_array, "length is needed for arrays");
78 static_assert(!_is_ptrtype_array, "index needed for arrays");
79 context.reset(raw_);
80 return _outptr_type(context);
81 }
82 _outptr_type get(guest_ptr<void> context, size_t length) const {
83 // The ptrtype is not an array, but the result is
84 static_assert(_is_result_array, "length is only needed for arrays");
85 static_assert(!_is_ptrtype_array, "index needed for arrays");
86 context.reset(raw_);
87 return _outptr_type(context, length);
88 }
89 _outptr_type get(size_t index, guest_ptr<void> context) const {
90 // The ptrtype is an array, but the result is not
91 static_assert(!_is_result_array, "length is needed for arrays");
92 static_assert(_is_ptrtype_array, "index only needed for non arrays");
93 _check_bounds(index);
94 context.reset(raw_[index]);
95 return _outptr_type(context);
96 }
97 _outptr_type get(size_t index, guest_ptr<void> context, size_t length) const {
98 // Both the result and ptrtype are arrays
99 static_assert(_is_result_array, "length only needed for arrays");
100 static_assert(_is_ptrtype_array, "index only needed for non arrays");
101 _check_bounds(index);
102 context.reset(raw_[index]);
103 return _outptr_type(context, length);
104 }
105
107 guest_ptr<char[]> cstring(guest_ptr<void> context, size_t max_length = 0xFFFF) const {
108 static_assert(_is_cstring, "cstring() is only valid for char[]");
109 static_assert(!_is_ptrtype_array, "index is required for cstring() arrays");
110 context.reset(raw_);
111 return map_guest_cstring(context, max_length);
112 }
114 size_t max_length = 0xFFFF) const {
115 static_assert(_is_cstring, "cstring() is only valid for char[]");
116 static_assert(_is_ptrtype_array, "index is only required for cstring() arrays");
117 _check_bounds(index);
118 context.reset(raw_[index]);
119 return map_guest_cstring(context, max_length);
120 }
122 guest_ptr<char16_t[]> wstring(guest_ptr<void> context, size_t max_length = 0xFFFF) const {
123 static_assert(_is_wstring, "wstring() is only valid for char16_t[]");
124 static_assert(!_is_ptrtype_array, "index is required for cstring() arrays");
125 context.reset(raw_);
126 return map_guest_wstring(context, max_length);
127 }
129 size_t max_length = 0xFFFF) const {
130 static_assert(_is_wstring, "wstring() is only valid for char16_t[]");
131 static_assert(_is_ptrtype_array, "index is only required for cstring() arrays");
132 _check_bounds(index);
133 context.reset(raw_[index]);
134 return map_guest_wstring(context, max_length);
135 }
136
137 _basic_ptrtype raw() const {
138 static_assert(!_is_ptrtype_array, "Index required for arrays");
139 return raw_;
140 }
141
142 _basic_ptrtype raw(size_t index) const {
143 static_assert(_is_ptrtype_array, "Index only required for arrays");
144 _check_bounds(index);
145 return raw_[index];
146 }
147
148 private:
149 _PtrType raw_;
150};
151
152static_assert(sizeof(guest_member_ptr<void, uint32_t>) == sizeof(uint32_t),
153 "guest_ptr_member size mismatch");
154static_assert(sizeof(guest_member_ptr<void, uint64_t>) == sizeof(uint64_t),
155 "guest_ptr_member size mismatch");
156
157} // namespace introvirt
Definition guest_ptr.hh:88
uint64_t address() const
Functions for getting the underlying address.
Definition guest_ptr.hh:124
void reset()
Definition guest_ptr.hh:345
Helper class to embed in structs.
Definition guest_member_ptr.hh:25
void set(const guest_ptr< Tp, PtrType > &in, size_t index)
Definition guest_member_ptr.hh:68
guest_ptr< char[]> cstring(size_t index, guest_ptr< void > context, size_t max_length=0xFFFF) const
Definition guest_member_ptr.hh:113
_outptr_type get(size_t index, guest_ptr< void > context) const
Definition guest_member_ptr.hh:89
guest_ptr< char16_t[]> wstring(size_t index, guest_ptr< void > context, size_t max_length=0xFFFF) const
Definition guest_member_ptr.hh:128
void set(_basic_ptrtype in)
setters
Definition guest_member_ptr.hh:53
_basic_ptrtype raw() const
Definition guest_member_ptr.hh:137
void set(_basic_ptrtype in, size_t index)
Definition guest_member_ptr.hh:57
_outptr_type get(size_t index, guest_ptr< void > context, size_t length) const
Definition guest_member_ptr.hh:97
guest_ptr< char[]> cstring(guest_ptr< void > context, size_t max_length=0xFFFF) const
cstring helpers
Definition guest_member_ptr.hh:107
constexpr bool x64() const
Definition guest_member_ptr.hh:50
_outptr_type get(guest_ptr< void > context) const
Getters.
Definition guest_member_ptr.hh:75
_outptr_type get(guest_ptr< void > context, size_t length) const
Definition guest_member_ptr.hh:82
void set(const guest_ptr< Tp, PtrType > &in)
Definition guest_member_ptr.hh:63
guest_ptr< char16_t[]> wstring(guest_ptr< void > context, size_t max_length=0xFFFF) const
wstring helpers
Definition guest_member_ptr.hh:122
_basic_ptrtype raw(size_t index) const
Definition guest_member_ptr.hh:142
Type-safe guest virtual address pointer and guest_ptr template.
#define introvirt_assert(condition, msg)
Definition introvirt_assert.hh:32
Core IntroVirt classes.
Definition Cr0.hh:20
basic_guest_ptr< char16_t[], void, _Physical > map_guest_wstring(const basic_guest_ptr< _Tp, _PtrType, _Physical > &ptr, size_t max_length=0xFFFF)
Helper function for map_guest_str<char16_t>
Definition guest_ptr.hh:1417
basic_guest_ptr< char[], void, _Physical > map_guest_cstring(const basic_guest_ptr< _Tp, _PtrType, _Physical > &ptr, size_t max_length=0xFFFF)
Helper function for map_guest_str<char>
Definition guest_ptr.hh:1404