-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathuserptr.hh
More file actions
181 lines (153 loc) · 4.84 KB
/
Copy pathuserptr.hh
File metadata and controls
181 lines (153 loc) · 4.84 KB
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
#pragma once
#include <cstddef>
#include <memory>
// XXX(Austin) Avoiding TOCTTOU bugs when dealing with user-provided
// pointers in the presence of shared memory requires copying. A
// possibly better approach would be to force some region of the
// address space to be thread-local and unshared and to require
// system call arguments to point in to this region. We still have to
// validate such pointers (that the pages are mapped and that their
// length doesn't extend out of the region), but we wouldn't need to
// copy them in the kernel.
// A protected pointer to user data. These act like pointers (and
// their in-memory representation is identical), but userptr's cannot
// be dereferenced without explicit checks.
template<typename T>
class userptr
{
T* ptr;
public:
userptr(std::nullptr_t n) : ptr(nullptr) { }
explicit userptr(T* p) : ptr(p) { }
userptr() = default;
userptr(const userptr<T> &o) = default;
userptr& operator=(const userptr& o) = default;
// Up-conversion
template<typename U, typename = typename
std::enable_if<std::is_convertible<U*, T*>::value>::type>
userptr(const userptr<U> &o) : ptr(o.ptr) { }
template<typename U, typename = typename
std::enable_if<std::is_convertible<U*, T*>::value>::type>
userptr& operator=(const userptr<U>& o)
{
ptr = o.ptr;
}
T* unsafe_get() const
{
return ptr;
}
// Note that this has to be explicit or C++ will use this as the
// conversion to *any* integral type, first converting through bool.
explicit operator bool() const
{
return ptr != nullptr;
}
explicit operator uptr () const
{
return (uptr)ptr;
}
userptr operator+(ptrdiff_t x) const
{
return userptr(unsafe_get() + x);
}
// Store a value to this pointer. Returns true if successful, false
// if the user pointer is illegal.
bool store(const T *val) const
{
return !putmem(unsafe_get(), val, sizeof(T));
}
bool store(const T *val, std::size_t count) const
{
return !putmem(unsafe_get(), val, sizeof(T) * count);
}
// Load a value from this pointer. Returns true if successful,
// false if the user pointer is illegal.
bool load(T *val) const
{
if (sizeof(T) == sizeof(uint64_t))
return !fetchint64((uptr)*this, reinterpret_cast<uint64_t*>(val));
else
return !fetchmem(val, unsafe_get(), sizeof(T));
}
bool load(T *val, std::size_t count) const
{
return fetchmem((void*)val, unsafe_get(), sizeof(T) * count) >= 0;
}
// Allocate memory for T[count] and copy into it. If the pointer is
// out of bounds, returns nullptr. If memory allocation fails,
// throws bad_alloc.
std::unique_ptr<T[]>
load_alloc(std::size_t count) const
{
std::unique_ptr<T[]> res(new T[count]);
if (!load(res.get(), count))
return nullptr;
return res;
}
};
// Specialization of userptr<void>
template<>
class userptr<void>
{
void *ptr;
public:
userptr(std::nullptr_t n) : ptr(nullptr) { }
explicit userptr(void* p) : ptr(p) { }
userptr() = default;
userptr(const userptr<void> &o) = default;
userptr& operator=(const userptr& o) = default;
void *unsafe_get() const
{
return ptr;
}
explicit operator bool() const
{
return ptr != nullptr;
}
explicit operator uptr () const
{
return (uptr)ptr;
}
bool store_bytes(const void *val, std::size_t bytes) const
{
return !putmem(unsafe_get(), val, bytes);
}
bool load_bytes(void *val, std::size_t bytes) const
{
return !fetchmem(val, unsafe_get(), bytes);
}
};
// For userptr to be passed like a regular pointer, its representation
// must be the same as a pointer (obviously) and, furthermore, the
// AMD64 ABI requires that it have a trivial copy construct and
// trivial destructor.
static_assert(sizeof(userptr<void>) == sizeof(void*), "userptr is wrong size");
static_assert(__is_pod(userptr<void>), "userptr is not a POD");
// A protected pointer to a string from user space.
class userptr_str
{
userptr<const char> ptr;
public:
userptr_str(std::nullptr_t n) : ptr(nullptr) { }
explicit userptr_str(const char* p) : ptr(p) { }
userptr_str() = default;
userptr_str(const userptr_str &o) = default;
userptr_str(userptr_str &&o) = default;
userptr_str& operator=(const userptr_str& o) = default;
explicit operator bool() const
{
return (bool)ptr;
}
bool load(char *dst, std::size_t size)
{
extern int fetchstr(char* dst, const char* usrc, u64 size);
return !fetchstr(dst, ptr.unsafe_get(), size);
}
// Allocate memory for this string and copy into it. If the pointer
// is out of bounds or the string is longer than limit returns
// nullptr. If memory allocation fails, throws bad_alloc. If
// len_out is not nullptr, it will be set to strlen of the returned
// string.
std::unique_ptr<char[]> load_alloc(
std::size_t limit, std::size_t *len_out = nullptr) const;
};