forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcasting.h
More file actions
472 lines (436 loc) · 18.1 KB
/
Copy pathcasting.h
File metadata and controls
472 lines (436 loc) · 18.1 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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
// Copyright 2024 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_OBJECTS_CASTING_H_
#define V8_OBJECTS_CASTING_H_
#include <type_traits>
#include "include/v8-source-location.h"
#include "src/base/logging.h"
#include "src/objects/tagged.h"
#include "src/sandbox/check.h"
namespace v8::internal {
// CastTraits<T> is a type trait that defines type checking behaviour for
// tagged object casting. The expected specialization is:
//
// template<>
// struct CastTraits<SomeObject> {
// template<typename From>
// static bool AllowFrom(Tagged<From> value) {
// return IsSomeObject(value);
// }
// };
//
// or, likely, just specializations of AllowFrom for Object and HeapObject,
// under the assumption that the HeapObject implementation is the same for all
// HeapObjects and the Object implementation has additional overhead in Smi
// checks.
//
// struct CastTraits<Object> {
// static bool AllowFrom(Tagged<HeapObject> value) {
// return IsSomeObject(value);
// }
// static bool AllowFrom(Tagged<Object> value) {
// return IsSomeObject(value);
// }
// };
//
template <typename To>
struct CastTraits;
template <typename T>
concept NotGCedType = !std::is_base_of_v<HeapObject, T> &&
!std::is_base_of_v<HeapObjectLayout, T>;
// `Is<T>(value)` checks whether `value` is a tagged object of type `T`.
template <typename T, typename U>
inline bool Is(Tagged<U> value) {
return CastTraits<T>::AllowFrom(value);
}
template <typename T, typename U>
inline bool Is(IndirectHandle<U> value);
template <typename T, typename U>
inline bool Is(DirectHandle<U> value);
// Is<T> checks for MaybeHandles are forbidden -- you need to first convert to
// a Handle with ToHandle or ToHandleChecked.
template <typename T, typename U>
bool Is(MaybeIndirectHandle<U> value) = delete;
template <typename T, typename U>
bool Is(MaybeDirectHandle<U> value) = delete;
// C++ versions for `Is<T>` that accept pointers and references.
template <typename To, typename From>
requires NotGCedType<To>
bool Is(const From& value) {
return CastTraits<std::remove_const_t<To>>::AllowFrom(value);
}
template <typename To, typename From>
requires NotGCedType<To>
bool Is(From& value) {
return Is<To>(const_cast<const From&>(value));
}
template <typename To, typename From>
requires NotGCedType<To>
bool Is(const From* value) {
return value && Is<To>(*value);
}
template <typename To, typename From>
requires NotGCedType<To>
bool Is(From* value) {
return value && Is<To>(*static_cast<const From*>(value));
}
// `UncheckedCast<T>(value)` casts `value` to a tagged object of type `T`,
// without checking the type of value.
template <typename To, typename From>
inline Tagged<To> UncheckedCast(Tagged<From> value) {
return Tagged<To>(value.ptr());
}
template <typename To, typename From>
inline IndirectHandle<To> UncheckedCast(IndirectHandle<From> value);
template <typename To, typename From>
inline MaybeIndirectHandle<To> UncheckedCast(MaybeIndirectHandle<From> value);
template <typename To, typename From>
inline DirectHandle<To> UncheckedCast(DirectHandle<From> value);
template <typename To, typename From>
inline MaybeDirectHandle<To> UncheckedCast(MaybeDirectHandle<From> value);
// HasCastImplementation is a concept that checks for the existence of
// Is<To>(Holder<From>) and UncheckedCast<To>(Holder<From>).
template <template <typename> class Holder, typename To, typename From>
concept HasTryCastImplementation = requires(Holder<From> value) {
{ Is<To>(value) } -> std::same_as<bool>;
{ UncheckedCast<To>(value) } -> std::same_as<Holder<To>>;
};
template <typename Derived, typename Base>
concept HasCppRefTryCastImplementation =
NotGCedType<Derived> && requires(Base& value) {
{ Is<Derived>(value) } -> std::same_as<bool>;
};
template <typename Derived, typename Base>
concept HasCppPointerTryCastImplementation =
NotGCedType<Derived> && requires(Base* value) {
{ Is<Derived>(value) } -> std::same_as<bool>;
};
// `TryCast<T>(value, &out)` casts `value` to a tagged object of type `T` and
// writes the value to `out`, returning true if the cast succeeded and false if
// it failed.
template <typename To, typename From, template <typename> class Holder>
requires HasTryCastImplementation<Holder, To, From>
inline bool TryCast(Holder<From> value, Holder<To>* out) {
if (!Is<To>(value)) return false;
*out = UncheckedCast<To>(value);
return true;
}
template <typename Derived, typename Base>
requires HasCppRefTryCastImplementation<Derived, Base>
inline bool TryCast(Base& value, Derived** out) {
if (!Is<Derived>(value)) return false;
*out = static_cast<Derived*>(&value);
return true;
}
template <typename Derived, typename Base>
requires HasCppPointerTryCastImplementation<Derived, Base>
inline bool TryCast(Base* value, Derived** out) {
if (!Is<Derived>(value)) return false;
*out = static_cast<Derived*>(value);
return true;
}
#ifdef DEBUG
template <typename T>
bool GCAwareObjectTypeCheck(Tagged<Object> object, const Heap* heap);
#endif // DEBUG
// `GCSafeCast<T>(value)` casts `object` to a tagged object of type `T` and
// should be used when the cast can be called from within a GC. The case all
// includes a debug check that `object` is either a tagged object of type `T`,
// or one of few special cases possible during GC (see GCAwareObjectTypeCheck):
// 1) `object` was already evacuated and the forwarding address refers to a
// tagged object of type `T`.
// 2) During Scavenger, `object` is a large object.
// 3) During a conservative Scavenger, `object` is a pinned object.
template <typename T>
Tagged<T> GCSafeCast(Tagged<Object> object, const Heap* heap) {
DCHECK(GCAwareObjectTypeCheck<T>(object, heap));
return UncheckedCast<T>(object);
}
// Check if a value holder is null -- used for Cast DCHECKs, so that null
// handles pass the check.
template <typename T, typename U>
inline bool NullOrIs(Tagged<U> value) {
// Raw tagged values are not allowed to be null, just check Is<T>.
return Is<T>(value);
}
template <typename T, typename U>
inline bool NullOrIs(IndirectHandle<U> value) {
return value.is_null() || Is<T>(value);
}
template <typename T, typename U>
inline bool NullOrIs(DirectHandle<U> value) {
return value.is_null() || Is<T>(value);
}
template <typename T, typename U>
inline bool NullOrIs(MaybeIndirectHandle<U> value) {
IndirectHandle<U> handle;
return !value.ToHandle(&handle) || Is<T>(handle);
}
template <typename T, typename U>
inline bool NullOrIs(MaybeDirectHandle<U> value) {
DirectHandle<U> handle;
return !value.ToHandle(&handle) || Is<T>(handle);
}
template <typename Derived, typename Base>
bool NullOrIs(Base& value) {
return Is<Derived>(value);
}
template <typename Derived, typename Base>
bool NullOrIs(Base* value) {
return !value || Is<Derived>(*value);
}
// HasCastImplementation is a concept that checks for the existence of
// NullOrIs<To>(Holder<From>) and UncheckedCast<To>(Holder<From>).
template <template <typename> class Holder, typename To, typename From>
concept HasCastImplementation = requires(Holder<From> value) {
{ NullOrIs<To>(value) } -> std::same_as<bool>;
{ UncheckedCast<To>(value) } -> std::same_as<Holder<To>>;
};
template <typename Derived, typename Base>
concept HasCppRefCastImplementation =
NotGCedType<Derived> && requires(Base& value) {
{ Is<Derived>(value) } -> std::same_as<bool>;
};
template <typename Derived, typename Base>
concept HasCppPointerCastImplementation =
NotGCedType<Derived> && requires(Base* value) {
{ Is<Derived>(value) } -> std::same_as<bool>;
};
// `TrustedCast<T>(value)` casts `value` to a tagged object of type `T`, only
// doing a debug check that `value` is a tagged object of type `T`. Down-casts
// to trusted objects are allowed for callers which already ensured their
// correctness. Null-valued holders are allowed.
template <typename To, typename From, template <typename> class Holder>
requires HasCastImplementation<Holder, To, From>
inline Holder<To> TrustedCast(
Holder<From> value, SourceLocation loc = SourceLocation::CurrentIfDebug()) {
// Casting pointers to in-sandbox objects is allowed even with an active
// DisallowSandboxAccess scope (as casting itself doesn't access any memory).
// However, in debug builds the DCHECK below will need to access the Map of
// the object, so we need to temporarily enable sandbox access. If no
// DisallowSandboxAccess scope is active, this is a no-op.
DCHECK_WITH_SANDBOX_ACCESS_AND_MSG_AND_LOC(
NullOrIs<To>(value), V8_PRETTY_FUNCTION_VALUE_OR("Cast type check"), loc);
return UncheckedCast<To>(value);
}
// `SbxCast<T>(value)` casts `value` to a tagged object of trusted type `T`,
// with a sandbox-only dynamic type check ensuring that `value` is a tagged
// object of type `T`. Null-valued holders are allowed.
template <typename To, typename From, template <typename> class Holder>
requires HasCastImplementation<Holder, To, From>
inline Holder<To> SbxCast(
Holder<From> value, SourceLocation loc = SourceLocation::CurrentIfDebug()) {
// Under the attacker model of the sandbox we cannot trust the type of
// in-sandbox objects for sandbox checks as these objects can be arbitrarily
// tampered with.
static_assert(is_subtype_v<To, TrustedObject>,
"Type of untrusted objects cannot be checked reliably");
DCHECK_WITH_MSG_AND_LOC(NullOrIs<To>(value),
V8_PRETTY_FUNCTION_VALUE_OR("Cast type check"), loc);
SBXCHECK(NullOrIs<To>(value));
return UncheckedCast<To>(value);
}
template <typename Derived, typename Base>
requires HasCppRefCastImplementation<Derived, Base>
inline Derived& SbxCast(Base& from,
SourceLocation loc = SourceLocation::CurrentIfDebug()) {
DCHECK_WITH_MSG_AND_LOC(NullOrIs<Derived>(from),
V8_PRETTY_FUNCTION_VALUE_OR("Cast type check"), loc);
SBXCHECK(NullOrIs<Derived>(from));
return static_cast<Derived&>(from);
}
template <typename Derived, typename Base>
requires HasCppPointerCastImplementation<Derived, Base>
inline Derived* SbxCast(Base* from,
SourceLocation loc = SourceLocation::CurrentIfDebug()) {
DCHECK_WITH_MSG_AND_LOC(NullOrIs<Derived>(from),
V8_PRETTY_FUNCTION_VALUE_OR("Cast type check"), loc);
SBXCHECK(NullOrIs<Derived>(from));
return static_cast<Derived*>(from);
}
// `CheckedCast<T>(value)` casts `value` to a tagged object of type `T`,
// with a always-on dynamic type check ensuring that `value` is a tagged object
// of type `T`. Null-valued holders are allowed.
template <typename To, typename From, template <typename> class Holder>
requires HasCastImplementation<Holder, To, From>
inline Holder<To> CheckedCast(
Holder<From> value, SourceLocation loc = SourceLocation::CurrentIfDebug()) {
DCHECK_WITH_MSG_AND_LOC(NullOrIs<To>(value),
V8_PRETTY_FUNCTION_VALUE_OR("Cast type check"), loc);
CHECK(NullOrIs<To>(value));
return UncheckedCast<To>(value);
}
template <typename Derived, typename Base>
requires HasCppRefCastImplementation<Derived, Base>
inline Derived& CheckedCast(
Base& from, SourceLocation loc = SourceLocation::CurrentIfDebug()) {
DCHECK_WITH_MSG_AND_LOC(NullOrIs<Derived>(from),
V8_PRETTY_FUNCTION_VALUE_OR("Cast type check"), loc);
CHECK(NullOrIs<Derived>(from));
return static_cast<Derived&>(from);
}
template <typename Derived, typename Base>
requires HasCppPointerCastImplementation<Derived, Base>
inline Derived* CheckedCast(
Base* from, SourceLocation loc = SourceLocation::CurrentIfDebug()) {
DCHECK_WITH_MSG_AND_LOC(NullOrIs<Derived>(from),
V8_PRETTY_FUNCTION_VALUE_OR("Cast type check"), loc);
CHECK(NullOrIs<Derived>(from));
return static_cast<Derived*>(from);
}
// `Cast<T>(value)` casts `value` to a tagged object of type `T`, with a debug
// check that `value` is a tagged object of type `T`. Null-valued holders are
// allowed. Down-casts to trusted objects are not allowed to prevent mistakes.
template <typename To, typename From, template <typename> class Holder>
requires HasCastImplementation<Holder, To, From>
inline Holder<To> Cast(Holder<From> value,
SourceLocation loc = SourceLocation::CurrentIfDebug()) {
static_assert(
!is_subtype_v<To, TrustedObject> || is_subtype_v<From, To>,
"Down-casting to trusted objects is verboten. Pick one of:\n"
"* TryCast if the use is guarded by a test\n"
" if (Trusted t; TryCast(obj, &t)) { t->foo(); }\n"
"* (Checked|Sbx)Cast if the object is expected to be of a certain type\n"
" Tagged<Trusted> t = SbxCast<Trusted>(obj);\n"
"* TrustedCast if correct by construction (or other deprecated usage)\n"
" Tagged<Trusted> t = "
"TrustedCast<Trusted>(obj->ReadTrustedPointerField<kTrustedTag>(...);\n");
return TrustedCast<To>(value, loc);
}
// TODO(leszeks): Figure out a way to make these cast to actual pointers rather
// than Tagged.
template <typename To, typename From>
requires std::is_base_of_v<HeapObjectLayout, From>
inline Tagged<To> UncheckedCast(const From* value) {
return UncheckedCast<To>(Tagged(value));
}
template <typename To, typename From>
requires std::is_base_of_v<HeapObjectLayout, From>
inline Tagged<To> TrustedCast(const From* value) {
return TrustedCast<To>(Tagged(value));
}
template <typename To, typename From>
requires std::is_base_of_v<HeapObjectLayout, From>
inline Tagged<To> CheckedCast(const From* value) {
return CheckedCast<To>(Tagged(value));
}
template <typename To, typename From>
requires std::is_base_of_v<HeapObjectLayout, From>
inline Tagged<To> SbxCast(const From* value) {
return SbxCast<To>(Tagged(value));
}
template <typename To, typename From>
requires std::is_base_of_v<HeapObjectLayout, From>
inline Tagged<To> Cast(const From* value,
SourceLocation loc = SourceLocation::CurrentIfDebug()) {
return Cast<To>(Tagged(value), loc);
}
template <typename To, typename From>
requires(std::is_base_of_v<HeapObject, From>)
inline Tagged<To> UncheckedCast(From value) {
return UncheckedCast<To>(Tagged(value));
}
template <typename To, typename From>
requires(std::is_base_of_v<HeapObject, From>)
inline Tagged<To> TrustedCast(From value) {
return TrustedCast<To>(Tagged(value));
}
template <typename To, typename From>
requires(std::is_base_of_v<HeapObject, From>)
inline Tagged<To> CheckedCast(From value) {
return CheckedCast<To>(Tagged(value));
}
template <typename To, typename From>
requires(std::is_base_of_v<HeapObject, From>)
inline Tagged<To> SbxCast(From value) {
return SbxCast<To>(Tagged(value));
}
template <typename To, typename From>
requires(std::is_base_of_v<HeapObject, From>)
inline Tagged<To> Cast(From value,
SourceLocation loc = SourceLocation::CurrentIfDebug()) {
return Cast<To>(Tagged(value), loc);
}
template <typename Derived, typename Base>
requires HasCppRefCastImplementation<Derived, Base>
inline Derived& Cast(Base& from,
SourceLocation loc = SourceLocation::CurrentIfDebug()) {
DCHECK_WITH_MSG_AND_LOC(NullOrIs<Derived>(from),
V8_PRETTY_FUNCTION_VALUE_OR("Cast type check"), loc);
return static_cast<Derived&>(from);
}
template <typename Derived, typename Base>
requires HasCppPointerCastImplementation<Derived, Base>
inline Derived* Cast(Base* from,
SourceLocation loc = SourceLocation::CurrentIfDebug()) {
DCHECK_WITH_MSG_AND_LOC(NullOrIs<Derived>(from),
V8_PRETTY_FUNCTION_VALUE_OR("Cast type check"), loc);
return static_cast<Derived*>(from);
}
// `Is<T>(maybe_weak_value)` specialization for possible weak values and strong
// target `T`, that additionally first checks whether `maybe_weak_value` is
// actually a strong value (or a Smi, which can't be weak).
template <typename T, typename U>
inline bool Is(Tagged<MaybeWeak<U>> value) {
// Cast from maybe weak to strong needs to be strong or smi.
if constexpr (!is_maybe_weak_v<T>) {
if (!value.IsStrongOrSmi()) return false;
return CastTraits<T>::AllowFrom(Tagged<U>(value.ptr()));
} else {
// Dispatches to CastTraits<MaybeWeak<T>> below.
return CastTraits<T>::AllowFrom(value);
}
}
template <typename T, typename... U>
constexpr inline bool Is(Tagged<Union<U...>> value) {
using UnionU = Union<U...>;
if constexpr (is_subtype_v<UnionU, HeapObject>) {
return Is<T>(Tagged<HeapObject>(value));
} else if constexpr (is_subtype_v<UnionU, MaybeWeak<HeapObject>>) {
return Is<T>(Tagged<MaybeWeak<HeapObject>>(value));
} else if constexpr (is_subtype_v<UnionU, Object>) {
return Is<T>(Tagged<Object>(value));
} else {
static_assert(is_subtype_v<UnionU, MaybeWeak<Object>>);
return Is<T>(Tagged<MaybeWeak<Object>>(value));
}
}
// Specialization for maybe weak cast targets, which first converts the incoming
// value to a strong reference and then checks if the cast to the strong T
// is allowed. Cleared weak references always return true.
template <typename T>
struct CastTraits<MaybeWeak<T>> {
template <typename U>
static bool AllowFrom(Tagged<U> value) {
if constexpr (is_maybe_weak_v<U>) {
// Cleared values are always ok.
if (value.IsCleared()) return true;
// TODO(leszeks): Skip Smi check for values that are known to not be Smi.
if (value.IsSmi()) {
return CastTraits<T>::AllowFrom(Tagged<Smi>(value.ptr()));
}
return CastTraits<T>::AllowFrom(MakeStrong(value));
} else {
return CastTraits<T>::AllowFrom(value);
}
}
};
template <>
struct CastTraits<Object> {
static inline bool AllowFrom(Tagged<Object> value) { return true; }
};
template <>
struct CastTraits<Smi> {
static inline bool AllowFrom(Tagged<Object> value) { return value.IsSmi(); }
static inline bool AllowFrom(Tagged<HeapObject> value) { return false; }
};
template <>
struct CastTraits<HeapObject> {
static inline bool AllowFrom(Tagged<Object> value) {
return value.IsHeapObject();
}
static inline bool AllowFrom(Tagged<HeapObject> value) { return true; }
};
} // namespace v8::internal
#endif // V8_OBJECTS_CASTING_H_