| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
// Copyright 2019 the V8 project authors. All rights reserved. |
| 8 |
// Use of this source code is governed by a BSD-style license that can be |
| 9 |
// found in the LICENSE file. |
| 10 |
|
| 11 |
#ifndef RegexpShim_h |
| 12 |
#define RegexpShim_h |
| 13 |
|
| 14 |
#include "mozilla/Assertions.h" |
| 15 |
#include "mozilla/Attributes.h" |
| 16 |
#include "mozilla/MathAlgorithms.h" |
| 17 |
#include "mozilla/Maybe.h" |
| 18 |
#include "mozilla/SegmentedVector.h" |
| 19 |
#include "mozilla/Sprintf.h" |
| 20 |
#include "mozilla/Types.h" |
| 21 |
|
| 22 |
#include <algorithm> |
| 23 |
#include <cctype> |
| 24 |
#include <iterator> |
| 25 |
#include <optional> |
| 26 |
|
| 27 |
#include "irregexp/RegExpTypes.h" |
| 28 |
#include "irregexp/util/FlagsShim.h" |
| 29 |
#include "irregexp/util/VectorShim.h" |
| 30 |
#include "irregexp/util/ZoneShim.h" |
| 31 |
#include "jit/JitCode.h" |
| 32 |
#include "jit/Label.h" |
| 33 |
#include "jit/shared/Assembler-shared.h" |
| 34 |
#include "js/friend/StackLimits.h" // js::AutoCheckRecursionLimit |
| 35 |
#include "js/RegExpFlags.h" |
| 36 |
#include "js/Value.h" |
| 37 |
#include "threading/ExclusiveData.h" |
| 38 |
#include "util/DifferentialTesting.h" |
| 39 |
#include "vm/JSContext.h" |
| 40 |
#include "vm/MutexIDs.h" |
| 41 |
#include "vm/NativeObject.h" |
| 42 |
#include "vm/RegExpShared.h" |
| 43 |
|
| 44 |
// Forward declaration of classes |
| 45 |
namespace v8 { |
| 46 |
namespace internal { |
| 47 |
|
| 48 |
class Heap; |
| 49 |
class Isolate; |
| 50 |
class RegExpMatchInfo; |
| 51 |
class RegExpStack; |
| 52 |
|
| 53 |
template <typename T> |
| 54 |
class Handle; |
| 55 |
|
| 56 |
} // namespace internal |
| 57 |
} // namespace v8 |
| 58 |
|
| 59 |
#define V8_WARN_UNUSED_RESULT [[nodiscard]] |
| 60 |
#define V8_EXPORT_PRIVATE |
| 61 |
#define V8_FALLTHROUGH [[fallthrough]] |
| 62 |
#define V8_NODISCARD [[nodiscard]] |
| 63 |
#define V8_NOEXCEPT noexcept |
| 64 |
|
| 65 |
#define FATAL(x) MOZ_CRASH(x) |
| 66 |
#define UNREACHABLE() MOZ_CRASH("unreachable code") |
| 67 |
#define UNIMPLEMENTED() MOZ_CRASH("unimplemented code") |
| 68 |
#define STATIC_ASSERT(exp) static_assert(exp, #exp) |
| 69 |
|
| 70 |
#define DCHECK MOZ_ASSERT |
| 71 |
#define DCHECK_EQ(lhs, rhs) MOZ_ASSERT((lhs) == (rhs)) |
| 72 |
#define DCHECK_NE(lhs, rhs) MOZ_ASSERT((lhs) != (rhs)) |
| 73 |
#define DCHECK_GT(lhs, rhs) MOZ_ASSERT((lhs) > (rhs)) |
| 74 |
#define DCHECK_GE(lhs, rhs) MOZ_ASSERT((lhs) >= (rhs)) |
| 75 |
#define DCHECK_LT(lhs, rhs) MOZ_ASSERT((lhs) < (rhs)) |
| 76 |
#define DCHECK_LE(lhs, rhs) MOZ_ASSERT((lhs) <= (rhs)) |
| 77 |
#define DCHECK_NULL(val) MOZ_ASSERT((val) == nullptr) |
| 78 |
#define DCHECK_NOT_NULL(val) MOZ_ASSERT((val) != nullptr) |
| 79 |
#define DCHECK_IMPLIES(lhs, rhs) MOZ_ASSERT_IF(lhs, rhs) |
| 80 |
#define CHECK MOZ_RELEASE_ASSERT |
| 81 |
#define CHECK_EQ(lhs, rhs) MOZ_RELEASE_ASSERT((lhs) == (rhs)) |
| 82 |
#define CHECK_LE(lhs, rhs) MOZ_RELEASE_ASSERT((lhs) <= (rhs)) |
| 83 |
#define CHECK_GE(lhs, rhs) MOZ_RELEASE_ASSERT((lhs) >= (rhs)) |
| 84 |
#define CHECK_IMPLIES(lhs, rhs) MOZ_RELEASE_ASSERT(!(lhs) || (rhs)) |
| 85 |
#define CONSTEXPR_DCHECK MOZ_ASSERT |
| 86 |
|
| 87 |
// These assertions are necessary to preserve the soundness of the V8 |
| 88 |
// sandbox. In V8, they are debug-only if the sandbox is off, but release |
| 89 |
// asserts if the sandbox is turned on. We don't have an equivalent sandbox, |
| 90 |
// so they can be debug checks for now. |
| 91 |
#define SBXCHECK MOZ_ASSERT |
| 92 |
#define SBXCHECK_EQ(lhs, rhs) MOZ_ASSERT((lhs) == (rhs)) |
| 93 |
#define SBXCHECK_NE(lhs, rhs) MOZ_ASSERT((lhs) != (rhs)) |
| 94 |
#define SBXCHECK_GT(lhs, rhs) MOZ_ASSERT((lhs) > (rhs)) |
| 95 |
#define SBXCHECK_GE(lhs, rhs) MOZ_ASSERT((lhs) >= (rhs)) |
| 96 |
#define SBXCHECK_LT(lhs, rhs) MOZ_ASSERT((lhs) < (rhs)) |
| 97 |
#define SBXCHECK_LE(lhs, rhs) MOZ_ASSERT((lhs) <= (rhs)) |
| 98 |
|
| 99 |
#define MemCopy memcpy |
| 100 |
|
| 101 |
// Origin: |
| 102 |
// https://github.com/v8/v8/blob/855591a54d160303349a5f0a32fab15825c708d1/src/base/macros.h#L310-L319 |
| 103 |
// ptrdiff_t is 't' according to the standard, but MSVC uses 'I'. |
| 104 |
#ifdef _MSC_VER |
| 105 |
# define V8PRIxPTRDIFF "Ix" |
| 106 |
# define V8PRIdPTRDIFF "Id" |
| 107 |
# define V8PRIuPTRDIFF "Iu" |
| 108 |
#else |
| 109 |
# define V8PRIxPTRDIFF "tx" |
| 110 |
# define V8PRIdPTRDIFF "td" |
| 111 |
# define V8PRIuPTRDIFF "tu" |
| 112 |
#endif |
| 113 |
|
| 114 |
#define arraysize std::size |
| 115 |
|
| 116 |
// Explicitly declare the assignment operator as deleted. |
| 117 |
#define DISALLOW_ASSIGN(TypeName) TypeName& operator=(const TypeName&) = delete |
| 118 |
|
| 119 |
// Explicitly declare the copy constructor and assignment operator as deleted. |
| 120 |
// This also deletes the implicit move constructor and implicit move assignment |
| 121 |
// operator, but still allows to manually define them. |
| 122 |
#define DISALLOW_COPY_AND_ASSIGN(TypeName) \ |
| 123 |
TypeName(const TypeName&) = delete; \ |
| 124 |
DISALLOW_ASSIGN(TypeName) |
| 125 |
|
| 126 |
// Explicitly declare all implicit constructors as deleted, namely the |
| 127 |
// default constructor, copy constructor and operator= functions. |
| 128 |
// This is especially useful for classes containing only static methods. |
| 129 |
#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \ |
| 130 |
TypeName() = delete; \ |
| 131 |
DISALLOW_COPY_AND_ASSIGN(TypeName) |
| 132 |
|
| 133 |
namespace v8 { |
| 134 |
|
| 135 |
// Origin: |
| 136 |
// https://github.com/v8/v8/blob/855591a54d160303349a5f0a32fab15825c708d1/src/base/macros.h#L364-L367 |
| 137 |
template <typename T, typename U> |
| 138 |
constexpr inline bool IsAligned(T value, U alignment) { |
| 139 |
return (value & (alignment - 1)) == 0; |
| 140 |
} |
| 141 |
|
| 142 |
using Address = uintptr_t; |
| 143 |
static const Address kNullAddress = 0; |
| 144 |
|
| 145 |
inline uintptr_t GetCurrentStackPosition() { |
| 146 |
return reinterpret_cast<uintptr_t>(__builtin_frame_address(0)); |
| 147 |
} |
| 148 |
|
| 149 |
namespace base { |
| 150 |
|
| 151 |
// Latin1/UTF-16 constants |
| 152 |
// Code-point values in Unicode 4.0 are 21 bits wide. |
| 153 |
// Code units in UTF-16 are 16 bits wide. |
| 154 |
using uc16 = char16_t; |
| 155 |
using uc32 = uint32_t; |
| 156 |
|
| 157 |
constexpr int kUC16Size = sizeof(base::uc16); |
| 158 |
|
| 159 |
// Origin: |
| 160 |
// https://github.com/v8/v8/blob/855591a54d160303349a5f0a32fab15825c708d1/src/base/macros.h#L247-L258 |
| 161 |
// The USE(x, ...) template is used to silence C++ compiler warnings |
| 162 |
// issued for (yet) unused variables (typically parameters). |
| 163 |
// The arguments are guaranteed to be evaluated from left to right. |
| 164 |
struct Use { |
| 165 |
template <typename T> |
| 166 |
Use(T&&) {} // NOLINT(runtime/explicit) |
| 167 |
}; |
| 168 |
#define USE(...) \ |
| 169 |
do { \ |
| 170 |
::v8::base::Use unused_tmp_array_for_use_macro[]{__VA_ARGS__}; \ |
| 171 |
(void)unused_tmp_array_for_use_macro; \ |
| 172 |
} while (false) |
| 173 |
|
| 174 |
// Origin: |
| 175 |
// https://github.com/v8/v8/blob/855591a54d160303349a5f0a32fab15825c708d1/src/base/safe_conversions.h#L35-L39 |
| 176 |
// saturated_cast<> is analogous to static_cast<> for numeric types, except |
| 177 |
// that the specified numeric conversion will saturate rather than overflow or |
| 178 |
// underflow. |
| 179 |
template <typename Dst, typename Src> |
| 180 |
inline Dst saturated_cast(Src value); |
| 181 |
|
| 182 |
// These are the only specializations that are needed for regexp code. |
| 183 |
// Instead of pulling in dozens of lines of template goo |
| 184 |
// to derive it, I used the implementation from uint8_clamped in |
| 185 |
// ArrayBufferObject.h. |
| 186 |
template <> |
| 187 |
inline uint8_t saturated_cast<uint8_t, int>(int x) { |
| 188 |
return (x >= 0) ? ((x < 255) ? uint8_t(x) : 255) : 0; |
| 189 |
} |
| 190 |
template <> |
| 191 |
inline uint8_t saturated_cast<uint8_t, uint32_t>(uint32_t x) { |
| 192 |
return (x < 255) ? uint8_t(x) : 255; |
| 193 |
} |
| 194 |
|
| 195 |
// Origin: |
| 196 |
// https://github.com/v8/v8/blob/fc088cdaccadede84886eee881e67af9db53669a/src/base/bounds.h#L14-L28 |
| 197 |
// Checks if value is in range [lower_limit, higher_limit] using a single |
| 198 |
// branch. |
| 199 |
template <typename T, typename U> |
| 200 |
inline constexpr bool IsInRange(T value, U lower_limit, U higher_limit) { |
| 201 |
using unsigned_T = typename std::make_unsigned<T>::type; |
| 202 |
// Use static_cast to support enum classes. |
| 203 |
return static_cast<unsigned_T>(static_cast<unsigned_T>(value) - |
| 204 |
static_cast<unsigned_T>(lower_limit)) <= |
| 205 |
static_cast<unsigned_T>(static_cast<unsigned_T>(higher_limit) - |
| 206 |
static_cast<unsigned_T>(lower_limit)); |
| 207 |
} |
| 208 |
|
| 209 |
#define LAZY_INSTANCE_INITIALIZER \ |
| 210 |
{ \ |
| 211 |
} |
| 212 |
|
| 213 |
template <typename T> |
| 214 |
class LazyInstanceImpl { |
| 215 |
public: |
| 216 |
LazyInstanceImpl() : value_(js::mutexid::IrregexpLazyStatic) {} |
| 217 |
|
| 218 |
const T* Pointer() { |
| 219 |
auto val = value_.lock(); |
| 220 |
if (val->isNothing()) { |
| 221 |
val->emplace(); |
| 222 |
} |
| 223 |
return val->ptr(); |
| 224 |
} |
| 225 |
|
| 226 |
private: |
| 227 |
js::ExclusiveData<mozilla::Maybe<T>> value_; |
| 228 |
}; |
| 229 |
|
| 230 |
template <typename T> |
| 231 |
class LazyInstance { |
| 232 |
public: |
| 233 |
using type = LazyInstanceImpl<T>; |
| 234 |
}; |
| 235 |
|
| 236 |
// Origin: |
| 237 |
// https://github.com/v8/v8/blob/855591a54d160303349a5f0a32fab15825c708d1/src/utils/utils.h#L40-L48 |
| 238 |
// Returns the value (0 .. 15) of a hexadecimal character c. |
| 239 |
// If c is not a legal hexadecimal character, returns a value < 0. |
| 240 |
// Used in regexp-parser.cc |
| 241 |
inline int HexValue(base::uc32 c) { |
| 242 |
c -= '0'; |
| 243 |
if (static_cast<unsigned>(c) <= 9) return c; |
| 244 |
c = (c | 0x20) - ('a' - '0'); // detect 0x11..0x16 and 0x31..0x36. |
| 245 |
if (static_cast<unsigned>(c) <= 5) return c + 10; |
| 246 |
return -1; |
| 247 |
} |
| 248 |
|
| 249 |
template <typename... Args> |
| 250 |
[[nodiscard]] uint32_t hash_combine(uint32_t aHash, Args... aArgs) { |
| 251 |
return mozilla::AddToHash(aHash, aArgs...); |
| 252 |
} |
| 253 |
|
| 254 |
namespace bits { |
| 255 |
|
| 256 |
inline uint64_t CountTrailingZeros(uint64_t value) { |
| 257 |
return mozilla::CountTrailingZeroes64(value); |
| 258 |
} |
| 259 |
|
| 260 |
inline size_t RoundUpToPowerOfTwo32(size_t value) { |
| 261 |
return mozilla::RoundUpPow2(value); |
| 262 |
} |
| 263 |
|
| 264 |
template <typename T> |
| 265 |
constexpr bool IsPowerOfTwo(T value) { |
| 266 |
return value > 0 && (value & (value - 1)) == 0; |
| 267 |
} |
| 268 |
|
| 269 |
} // namespace bits |
| 270 |
} // namespace base |
| 271 |
|
| 272 |
namespace unibrow { |
| 273 |
|
| 274 |
using uchar = unsigned int; |
| 275 |
|
| 276 |
// Origin: |
| 277 |
// https://github.com/v8/v8/blob/1f1e4cdb04c75eab77adbecd5f5514ddc3eb56cf/src/strings/unicode.h#L133-L150 |
| 278 |
class Latin1 { |
| 279 |
public: |
| 280 |
static const base::uc16 kMaxChar = 0xff; |
| 281 |
|
| 282 |
// Convert the character to Latin-1 case equivalent if possible. |
| 283 |
static inline base::uc16 TryConvertToLatin1(base::uc16 c) { |
| 284 |
// "GREEK CAPITAL LETTER MU" case maps to "MICRO SIGN". |
| 285 |
// "GREEK SMALL LETTER MU" case maps to "MICRO SIGN". |
| 286 |
if (c == 0x039C || c == 0x03BC) { |
| 287 |
return 0xB5; |
| 288 |
} |
| 289 |
// "LATIN CAPITAL LETTER Y WITH DIAERESIS" case maps to "LATIN SMALL LETTER |
| 290 |
// Y WITH DIAERESIS". |
| 291 |
if (c == 0x0178) { |
| 292 |
return 0xFF; |
| 293 |
} |
| 294 |
return c; |
| 295 |
} |
| 296 |
}; |
| 297 |
|
| 298 |
// Origin: |
| 299 |
// https://github.com/v8/v8/blob/b4bfbce6f91fc2cc72178af42bb3172c5f5eaebb/src/strings/unicode.h#L99-L131 |
| 300 |
class Utf16 { |
| 301 |
public: |
| 302 |
static inline bool IsLeadSurrogate(int code) { |
| 303 |
return js::unicode::IsLeadSurrogate(code); |
| 304 |
} |
| 305 |
static inline bool IsTrailSurrogate(int code) { |
| 306 |
return js::unicode::IsTrailSurrogate(code); |
| 307 |
} |
| 308 |
static inline base::uc16 LeadSurrogate(uint32_t char_code) { |
| 309 |
return js::unicode::LeadSurrogate(char_code); |
| 310 |
} |
| 311 |
static inline base::uc16 TrailSurrogate(uint32_t char_code) { |
| 312 |
return js::unicode::TrailSurrogate(char_code); |
| 313 |
} |
| 314 |
static inline uint32_t CombineSurrogatePair(char16_t lead, char16_t trail) { |
| 315 |
return js::unicode::UTF16Decode(lead, trail); |
| 316 |
} |
| 317 |
static const uchar kMaxNonSurrogateCharCode = 0xffff; |
| 318 |
}; |
| 319 |
|
| 320 |
#ifndef V8_INTL_SUPPORT |
| 321 |
|
| 322 |
// A cache used in case conversion. It caches the value for characters |
| 323 |
// that either have no mapping or map to a single character independent |
| 324 |
// of context. Characters that map to more than one character or that |
| 325 |
// map differently depending on context are always looked up. |
| 326 |
// Origin: |
| 327 |
// https://github.com/v8/v8/blob/b4bfbce6f91fc2cc72178af42bb3172c5f5eaebb/src/strings/unicode.h#L64-L88 |
| 328 |
template <class T, int size = 256> |
| 329 |
class Mapping { |
| 330 |
public: |
| 331 |
inline Mapping() = default; |
| 332 |
inline int get(uchar c, uchar n, uchar* result) { |
| 333 |
CacheEntry entry = entries_[c & kMask]; |
| 334 |
if (entry.code_point_ == c) { |
| 335 |
if (entry.offset_ == 0) { |
| 336 |
return 0; |
| 337 |
} else { |
| 338 |
result[0] = c + entry.offset_; |
| 339 |
return 1; |
| 340 |
} |
| 341 |
} else { |
| 342 |
return CalculateValue(c, n, result); |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
private: |
| 347 |
int CalculateValue(uchar c, uchar n, uchar* result) { |
| 348 |
bool allow_caching = true; |
| 349 |
int length = T::Convert(c, n, result, &allow_caching); |
| 350 |
if (allow_caching) { |
| 351 |
if (length == 1) { |
| 352 |
entries_[c & kMask] = CacheEntry(c, result[0] - c); |
| 353 |
return 1; |
| 354 |
} else { |
| 355 |
entries_[c & kMask] = CacheEntry(c, 0); |
| 356 |
return 0; |
| 357 |
} |
| 358 |
} else { |
| 359 |
return length; |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
struct CacheEntry { |
| 364 |
inline CacheEntry() : code_point_(kNoChar), offset_(0) {} |
| 365 |
inline CacheEntry(uchar code_point, signed offset) |
| 366 |
: code_point_(code_point), offset_(offset) {} |
| 367 |
uchar code_point_; |
| 368 |
signed offset_; |
| 369 |
static const int kNoChar = (1 << 21) - 1; |
| 370 |
}; |
| 371 |
static const int kSize = size; |
| 372 |
static const int kMask = kSize - 1; |
| 373 |
CacheEntry entries_[kSize]; |
| 374 |
}; |
| 375 |
|
| 376 |
// Origin: |
| 377 |
// https://github.com/v8/v8/blob/b4bfbce6f91fc2cc72178af42bb3172c5f5eaebb/src/strings/unicode.h#L241-L252 |
| 378 |
struct Ecma262Canonicalize { |
| 379 |
static const int kMaxWidth = 1; |
| 380 |
static int Convert(uchar c, uchar n, uchar* result, bool* allow_caching_ptr); |
| 381 |
}; |
| 382 |
struct Ecma262UnCanonicalize { |
| 383 |
static const int kMaxWidth = 4; |
| 384 |
static int Convert(uchar c, uchar n, uchar* result, bool* allow_caching_ptr); |
| 385 |
}; |
| 386 |
struct CanonicalizationRange { |
| 387 |
static const int kMaxWidth = 1; |
| 388 |
static int Convert(uchar c, uchar n, uchar* result, bool* allow_caching_ptr); |
| 389 |
}; |
| 390 |
|
| 391 |
#endif // !V8_INTL_SUPPORT |
| 392 |
|
| 393 |
struct Letter { |
| 394 |
static bool Is(uchar c); |
| 395 |
}; |
| 396 |
|
| 397 |
} // namespace unibrow |
| 398 |
|
| 399 |
namespace internal { |
| 400 |
|
| 401 |
#define PRINTF_FORMAT(x, y) MOZ_FORMAT_PRINTF(x, y) |
| 402 |
void PRINTF_FORMAT(1, 2) PrintF(const char* format, ...); |
| 403 |
void PRINTF_FORMAT(2, 3) PrintF(FILE* out, const char* format, ...); |
| 404 |
|
| 405 |
// Superclass for classes only using static method functions. |
| 406 |
// The subclass of AllStatic cannot be instantiated at all. |
| 407 |
class AllStatic { |
| 408 |
#ifdef DEBUG |
| 409 |
public: |
| 410 |
AllStatic() = delete; |
| 411 |
#endif |
| 412 |
}; |
| 413 |
|
| 414 |
// Superclass for classes managed with new and delete. |
| 415 |
// In irregexp, this is only AlternativeGeneration (in regexp-compiler.cc) |
| 416 |
// Compare: |
| 417 |
// https://github.com/v8/v8/blob/7b3332844212d78ee87a9426f3a6f7f781a8fbfa/src/utils/allocation.cc#L88-L96 |
| 418 |
class Malloced { |
| 419 |
public: |
| 420 |
static void* operator new(size_t size) { |
| 421 |
js::AutoEnterOOMUnsafeRegion oomUnsafe; |
| 422 |
void* result = js_malloc(size); |
| 423 |
if (!result) { |
| 424 |
oomUnsafe.crash("Irregexp Malloced shim"); |
| 425 |
} |
| 426 |
return result; |
| 427 |
} |
| 428 |
static void operator delete(void* p) { js_free(p); } |
| 429 |
}; |
| 430 |
|
| 431 |
constexpr int32_t KB = 1024; |
| 432 |
constexpr int32_t MB = 1024 * 1024; |
| 433 |
|
| 434 |
#define kMaxInt JSVAL_INT_MAX |
| 435 |
#define kMinInt JSVAL_INT_MIN |
| 436 |
constexpr int kSystemPointerSize = sizeof(void*); |
| 437 |
|
| 438 |
// The largest integer n such that n and n + 1 are both exactly |
| 439 |
// representable as a Number value. ES6 section 20.1.2.6 |
| 440 |
constexpr uint64_t kMaxSafeIntegerUint64 = (uint64_t{1} << 53) - 1; |
| 441 |
constexpr double kMaxSafeInteger = static_cast<double>(kMaxSafeIntegerUint64); |
| 442 |
|
| 443 |
constexpr int kBitsPerByte = 8; |
| 444 |
constexpr int kBitsPerByteLog2 = 3; |
| 445 |
constexpr int kUInt16Size = sizeof(uint16_t); |
| 446 |
constexpr int kInt32Size = sizeof(int32_t); |
| 447 |
constexpr int kUInt32Size = sizeof(uint32_t); |
| 448 |
constexpr int kInt64Size = sizeof(int64_t); |
| 449 |
|
| 450 |
constexpr int kMaxUInt16 = (1 << 16) - 1; |
| 451 |
|
| 452 |
inline constexpr bool IsDecimalDigit(base::uc32 c) { |
| 453 |
return c >= '0' && c <= '9'; |
| 454 |
} |
| 455 |
|
| 456 |
inline constexpr int AsciiAlphaToLower(base::uc32 c) { return c | 0x20; } |
| 457 |
|
| 458 |
inline bool is_uint24(int64_t val) { return (val >> 24) == 0; } |
| 459 |
inline bool is_int24(int64_t val) { |
| 460 |
int64_t limit = int64_t(1) << 23; |
| 461 |
return (-limit <= val) && (val < limit); |
| 462 |
} |
| 463 |
|
| 464 |
inline bool IsIdentifierStart(base::uc32 c) { |
| 465 |
return js::unicode::IsIdentifierStart(char32_t(c)); |
| 466 |
} |
| 467 |
inline bool IsIdentifierPart(base::uc32 c) { |
| 468 |
return js::unicode::IsIdentifierPart(char32_t(c)); |
| 469 |
} |
| 470 |
|
| 471 |
// Wrappers to disambiguate char16_t and uc16. |
| 472 |
struct AsUC16 { |
| 473 |
explicit AsUC16(char16_t v) : value(v) {} |
| 474 |
char16_t value; |
| 475 |
}; |
| 476 |
|
| 477 |
struct AsUC32 { |
| 478 |
explicit AsUC32(int32_t v) : value(v) {} |
| 479 |
int32_t value; |
| 480 |
}; |
| 481 |
|
| 482 |
std::ostream& operator<<(std::ostream& os, const AsUC16& c); |
| 483 |
std::ostream& operator<<(std::ostream& os, const AsUC32& c); |
| 484 |
|
| 485 |
// This class is used for the output of trace-regexp-parser. V8 has |
| 486 |
// an elaborate implementation to ensure that the output gets to the |
| 487 |
// right place, even on Android. We just need something that will |
| 488 |
// print output (ideally to stderr, to match the rest of our tracing |
| 489 |
// code). This is an empty wrapper that will convert itself to |
| 490 |
// std::cerr when used. |
| 491 |
class StdoutStream { |
| 492 |
public: |
| 493 |
operator std::ostream&() const; |
| 494 |
template <typename T> |
| 495 |
std::ostream& operator<<(T t); |
| 496 |
}; |
| 497 |
|
| 498 |
// Reuse existing Maybe implementation |
| 499 |
using mozilla::Maybe; |
| 500 |
|
| 501 |
template <typename T> |
| 502 |
Maybe<T> Just(const T& value) { |
| 503 |
return mozilla::Some(value); |
| 504 |
} |
| 505 |
|
| 506 |
template <typename T> |
| 507 |
mozilla::Nothing Nothing() { |
| 508 |
return mozilla::Nothing(); |
| 509 |
} |
| 510 |
|
| 511 |
template <typename T> |
| 512 |
using PseudoHandle = mozilla::UniquePtr<T, JS::FreePolicy>; |
| 513 |
|
| 514 |
// Compare 8bit/16bit chars to 8bit/16bit chars. |
| 515 |
// Used indirectly by regexp-interpreter.cc |
| 516 |
// Taken from: https://github.com/v8/v8/blob/master/src/utils/utils.h |
| 517 |
template <typename lchar, typename rchar> |
| 518 |
inline int CompareCharsUnsigned(const lchar* lhs, const rchar* rhs, |
| 519 |
size_t chars) { |
| 520 |
const lchar* limit = lhs + chars; |
| 521 |
if (sizeof(*lhs) == sizeof(char) && sizeof(*rhs) == sizeof(char)) { |
| 522 |
// memcmp compares byte-by-byte, yielding wrong results for two-byte |
| 523 |
// strings on little-endian systems. |
| 524 |
return memcmp(lhs, rhs, chars); |
| 525 |
} |
| 526 |
while (lhs < limit) { |
| 527 |
int r = static_cast<int>(*lhs) - static_cast<int>(*rhs); |
| 528 |
if (r != 0) return r; |
| 529 |
++lhs; |
| 530 |
++rhs; |
| 531 |
} |
| 532 |
return 0; |
| 533 |
} |
| 534 |
template <typename lchar, typename rchar> |
| 535 |
inline int CompareChars(const lchar* lhs, const rchar* rhs, size_t chars) { |
| 536 |
DCHECK_LE(sizeof(lchar), 2); |
| 537 |
DCHECK_LE(sizeof(rchar), 2); |
| 538 |
if (sizeof(lchar) == 1) { |
| 539 |
if (sizeof(rchar) == 1) { |
| 540 |
return CompareCharsUnsigned(reinterpret_cast<const uint8_t*>(lhs), |
| 541 |
reinterpret_cast<const uint8_t*>(rhs), chars); |
| 542 |
} else { |
| 543 |
return CompareCharsUnsigned(reinterpret_cast<const uint8_t*>(lhs), |
| 544 |
reinterpret_cast<const char16_t*>(rhs), |
| 545 |
chars); |
| 546 |
} |
| 547 |
} else { |
| 548 |
if (sizeof(rchar) == 1) { |
| 549 |
return CompareCharsUnsigned(reinterpret_cast<const char16_t*>(lhs), |
| 550 |
reinterpret_cast<const uint8_t*>(rhs), chars); |
| 551 |
} else { |
| 552 |
return CompareCharsUnsigned(reinterpret_cast<const char16_t*>(lhs), |
| 553 |
reinterpret_cast<const char16_t*>(rhs), |
| 554 |
chars); |
| 555 |
} |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
// Compare 8bit/16bit chars to 8bit/16bit chars. |
| 560 |
template <typename lchar, typename rchar> |
| 561 |
inline bool CompareCharsEqualUnsigned(const lchar* lhs, const rchar* rhs, |
| 562 |
size_t chars) { |
| 563 |
STATIC_ASSERT(std::is_unsigned<lchar>::value); |
| 564 |
STATIC_ASSERT(std::is_unsigned<rchar>::value); |
| 565 |
if (sizeof(*lhs) == sizeof(*rhs)) { |
| 566 |
// memcmp compares byte-by-byte, but for equality it doesn't matter whether |
| 567 |
// two-byte char comparison is little- or big-endian. |
| 568 |
return memcmp(lhs, rhs, chars * sizeof(*lhs)) == 0; |
| 569 |
} |
| 570 |
for (const lchar* limit = lhs + chars; lhs < limit; ++lhs, ++rhs) { |
| 571 |
if (*lhs != *rhs) return false; |
| 572 |
} |
| 573 |
return true; |
| 574 |
} |
| 575 |
|
| 576 |
template <typename lchar, typename rchar> |
| 577 |
inline bool CompareCharsEqual(const lchar* lhs, const rchar* rhs, |
| 578 |
size_t chars) { |
| 579 |
using ulchar = typename std::make_unsigned<lchar>::type; |
| 580 |
using urchar = typename std::make_unsigned<rchar>::type; |
| 581 |
return CompareCharsEqualUnsigned(reinterpret_cast<const ulchar*>(lhs), |
| 582 |
reinterpret_cast<const urchar*>(rhs), chars); |
| 583 |
} |
| 584 |
|
| 585 |
// V8::Object ~= JS::Value |
| 586 |
class Object { |
| 587 |
public: |
| 588 |
// The default object constructor in V8 stores a nullptr, |
| 589 |
// which has its low bit clear and is interpreted as Smi(0). |
| 590 |
constexpr Object() : asBits_(JS::Int32Value(0).asRawBits()) {} |
| 591 |
|
| 592 |
Object(const JS::Value& value) : asBits_(value.asRawBits()) {} |
| 593 |
|
| 594 |
// This constructor is only used in an unused implementation of |
| 595 |
// IsCharacterInRangeArray in regexp-macro-assembler.cc. |
| 596 |
Object(uintptr_t raw) : asBits_(raw) { MOZ_CRASH("unused"); } |
| 597 |
|
| 598 |
JS::Value value() const { return JS::Value::fromRawBits(asBits_); } |
| 599 |
|
| 600 |
inline static Object cast(Object object) { return object; } |
| 601 |
|
| 602 |
protected: |
| 603 |
void setValue(const JS::Value& val) { asBits_ = val.asRawBits(); } |
| 604 |
uint64_t asBits_; |
| 605 |
} JS_HAZ_GC_POINTER; |
| 606 |
|
| 607 |
// Used in regexp-interpreter.cc to check the return value of |
| 608 |
// isolate->stack_guard()->HandleInterrupts(). We want to handle |
| 609 |
// interrupts in the caller, so we return a magic value from |
| 610 |
// HandleInterrupts and check for it here. |
| 611 |
inline bool IsException(Object obj, Isolate*) { |
| 612 |
return obj.value().isMagic(JS_INTERRUPT_REGEXP); |
| 613 |
} |
| 614 |
|
| 615 |
class Smi : public Object { |
| 616 |
public: |
| 617 |
static Smi FromInt(int32_t value) { |
| 618 |
Smi smi; |
| 619 |
smi.setValue(JS::Int32Value(value)); |
| 620 |
return smi; |
| 621 |
} |
| 622 |
static inline int32_t ToInt(const Object object) { |
| 623 |
return object.value().toInt32(); |
| 624 |
} |
| 625 |
}; |
| 626 |
|
| 627 |
// V8::HeapObject ~= GC thing |
| 628 |
class HeapObject : public Object { |
| 629 |
public: |
| 630 |
inline static HeapObject cast(Object object) { |
| 631 |
HeapObject h; |
| 632 |
h.setValue(object.value()); |
| 633 |
return h; |
| 634 |
} |
| 635 |
}; |
| 636 |
|
| 637 |
// V8's values use low-bit tagging. If the LSB is 0, it's a small |
| 638 |
// integer. If the LSB is 1, it's a pointer to some GC thing. In V8, |
| 639 |
// this wrapper class is used to represent a pointer that has the low |
| 640 |
// bit set, or a small integer that has been shifted left by one |
| 641 |
// bit. We don't use the same tagging system, so all we need is a |
| 642 |
// transparent wrapper that automatically converts to/from the wrapped |
| 643 |
// type. |
| 644 |
template <typename T> |
| 645 |
class Tagged { |
| 646 |
public: |
| 647 |
Tagged() {} |
| 648 |
MOZ_IMPLICIT Tagged(const T& value) : value_(value) {} |
| 649 |
MOZ_IMPLICIT Tagged(T&& value) : value_(std::move(value)) {} |
| 650 |
|
| 651 |
T* operator->() { return &value_; } |
| 652 |
constexpr operator T() const { return value_; } |
| 653 |
|
| 654 |
private: |
| 655 |
T value_; |
| 656 |
}; |
| 657 |
|
| 658 |
// Adapted from v8/src/objects/casting.h |
| 659 |
|
| 660 |
template <typename To, typename From> |
| 661 |
inline Tagged<To> UncheckedCast(Tagged<From> value) { |
| 662 |
return Tagged<To>(To::cast(value)); |
| 663 |
} |
| 664 |
|
| 665 |
template <typename To, typename From> |
| 666 |
inline Tagged<To> Cast(const From& value) { |
| 667 |
return UncheckedCast<To>(Tagged(value)); |
| 668 |
} |
| 669 |
|
| 670 |
// A fixed-size array with Objects (aka Values) as element types. |
| 671 |
// Implemented using the dense elements of an ArrayObject. |
| 672 |
// Used for named captures. |
| 673 |
class FixedArray : public HeapObject { |
| 674 |
public: |
| 675 |
inline void set(uint32_t index, Object value) { |
| 676 |
inner()->setDenseElement(index, value.value()); |
| 677 |
} |
| 678 |
inline static FixedArray cast(Object object) { |
| 679 |
FixedArray f; |
| 680 |
f.setValue(object.value()); |
| 681 |
return f; |
| 682 |
} |
| 683 |
js::NativeObject* inner() { |
| 684 |
return &value().toObject().as<js::NativeObject>(); |
| 685 |
} |
| 686 |
}; |
| 687 |
|
| 688 |
/* |
| 689 |
* Conceptually, ByteArrayData is a variable-size structure. To |
| 690 |
* implement this in a C++-approved way, we allocate a struct |
| 691 |
* containing the 32-bit length field, followed by additional memory |
| 692 |
* for the data. To access the data, we get a pointer to the next byte |
| 693 |
* after the length field and cast it to the correct type. |
| 694 |
*/ |
| 695 |
inline uint8_t* ByteArrayData::data() { |
| 696 |
static_assert(alignof(uint8_t) <= alignof(ByteArrayData), |
| 697 |
"The trailing data must be aligned to start immediately " |
| 698 |
"after the header with no padding."); |
| 699 |
ByteArrayData* immediatelyAfter = this + 1; |
| 700 |
return reinterpret_cast<uint8_t*>(immediatelyAfter); |
| 701 |
} |
| 702 |
|
| 703 |
template <typename T> |
| 704 |
T* ByteArrayData::typedData() { |
| 705 |
static_assert(alignof(T) <= alignof(ByteArrayData)); |
| 706 |
MOZ_ASSERT(uintptr_t(data()) % alignof(T) == 0); |
| 707 |
return reinterpret_cast<T*>(data()); |
| 708 |
} |
| 709 |
|
| 710 |
template <typename T> |
| 711 |
T ByteArrayData::getTyped(uint32_t index) { |
| 712 |
MOZ_ASSERT(index < length() / sizeof(T)); |
| 713 |
return typedData<T>()[index]; |
| 714 |
} |
| 715 |
|
| 716 |
template <typename T> |
| 717 |
void ByteArrayData::setTyped(uint32_t index, T value) { |
| 718 |
MOZ_ASSERT(index < length() / sizeof(T)); |
| 719 |
typedData<T>()[index] = value; |
| 720 |
} |
| 721 |
|
| 722 |
// A fixed-size array of bytes. |
| 723 |
class ByteArray : public HeapObject { |
| 724 |
protected: |
| 725 |
ByteArrayData* inner() const { |
| 726 |
return static_cast<ByteArrayData*>(value().toPrivate()); |
| 727 |
} |
| 728 |
friend bool IsByteArray(Object obj); |
| 729 |
|
| 730 |
public: |
| 731 |
PseudoHandle<ByteArrayData> takeOwnership(Isolate* isolate); |
| 732 |
PseudoHandle<ByteArrayData> maybeTakeOwnership(Isolate* isolate); |
| 733 |
|
| 734 |
uint8_t get(uint32_t index) { return inner()->get(index); } |
| 735 |
void set(uint32_t index, uint8_t val) { inner()->set(index, val); } |
| 736 |
|
| 737 |
uint32_t length() const { return inner()->length(); } |
| 738 |
uint8_t* begin() { return inner()->data(); } |
| 739 |
|
| 740 |
static ByteArray cast(Object object) { |
| 741 |
ByteArray b; |
| 742 |
b.setValue(object.value()); |
| 743 |
return b; |
| 744 |
} |
| 745 |
|
| 746 |
friend class SMRegExpMacroAssembler; |
| 747 |
}; |
| 748 |
|
| 749 |
// A byte array that can be trusted to not contain malicious data. |
| 750 |
// See https://issues.chromium.org/issues/40069826. |
| 751 |
class TrustedByteArray : public ByteArray { |
| 752 |
public: |
| 753 |
static TrustedByteArray cast(Object object) { |
| 754 |
TrustedByteArray b; |
| 755 |
b.setValue(object.value()); |
| 756 |
return b; |
| 757 |
} |
| 758 |
}; |
| 759 |
|
| 760 |
// This is only used in assertions. In debug builds, we put a magic value |
| 761 |
// in the header of each ByteArrayData, and assert here that it matches. |
| 762 |
inline bool IsByteArray(Object obj) { |
| 763 |
MOZ_ASSERT(ByteArray::cast(obj).inner()->magic() == |
| 764 |
ByteArrayData::ExpectedMagic); |
| 765 |
return true; |
| 766 |
} |
| 767 |
|
| 768 |
// This is a convenience class used in V8 for treating a ByteArray as an array |
| 769 |
// of fixed-size integers. This version supports integral types up to 32 bits. |
| 770 |
template <typename T> |
| 771 |
class FixedIntegerArray : public ByteArray { |
| 772 |
static_assert(alignof(T) <= alignof(ByteArrayData)); |
| 773 |
static_assert(std::is_integral<T>::value); |
| 774 |
|
| 775 |
public: |
| 776 |
static Handle<FixedIntegerArray<T>> New(Isolate* isolate, uint32_t length); |
| 777 |
|
| 778 |
T get(uint32_t index) { return inner()->template getTyped<T>(index); }; |
| 779 |
void set(uint32_t index, T value) { |
| 780 |
inner()->template setTyped<T>(index, value); |
| 781 |
} |
| 782 |
|
| 783 |
static FixedIntegerArray<T> cast(Object object) { |
| 784 |
FixedIntegerArray<T> f; |
| 785 |
f.setValue(object.value()); |
| 786 |
return f; |
| 787 |
} |
| 788 |
|
| 789 |
uint32_t length() const { |
| 790 |
uint32_t byteLength = ByteArray::length(); |
| 791 |
MOZ_ASSERT(byteLength % sizeof(T) == 0); |
| 792 |
return byteLength / sizeof(T); |
| 793 |
} |
| 794 |
}; |
| 795 |
|
| 796 |
using FixedUInt16Array = FixedIntegerArray<uint16_t>; |
| 797 |
|
| 798 |
// Like Handles in SM, V8 handles are references to marked pointers. |
| 799 |
// Unlike SM, where Rooted pointers are created individually on the |
| 800 |
// stack, the target of a V8 handle lives in an arena on the isolate |
| 801 |
// (~= JSContext). Whenever a Handle is created, a new "root" is |
| 802 |
// created at the end of the arena. |
| 803 |
// |
| 804 |
// HandleScopes are used to manage the lifetimes of these handles. A |
| 805 |
// HandleScope lives on the stack and stores the size of the arena at |
| 806 |
// the time of its creation. When the function returns and the |
| 807 |
// HandleScope is destroyed, the arena is truncated to its previous |
| 808 |
// size, clearing all roots that were created since the creation of |
| 809 |
// the HandleScope. |
| 810 |
// |
| 811 |
// In some cases, objects that are GC-allocated in V8 are not in SM. |
| 812 |
// In particular, irregexp allocates ByteArrays during code generation |
| 813 |
// to store lookup tables. This does not play nicely with the SM |
| 814 |
// macroassembler's requirement that no GC allocations take place |
| 815 |
// while it is on the stack. To work around this, this shim layer also |
| 816 |
// provides the ability to create pseudo-handles, which are not |
| 817 |
// managed by the GC but provide the same API to irregexp. The "root" |
| 818 |
// of a pseudohandle is a unique pointer living in a second arena. If |
| 819 |
// the allocated object should outlive the HandleScope, it must be |
| 820 |
// manually moved out of the arena using maybeTakeOwnership. |
| 821 |
// (If maybeTakeOwnership is called multiple times, it will return |
| 822 |
// a null pointer on subsequent calls.) |
| 823 |
|
| 824 |
class MOZ_STACK_CLASS HandleScope { |
| 825 |
public: |
| 826 |
HandleScope(Isolate* isolate); |
| 827 |
~HandleScope(); |
| 828 |
|
| 829 |
private: |
| 830 |
size_t level_ = 0; |
| 831 |
size_t non_gc_level_ = 0; |
| 832 |
Isolate* isolate_; |
| 833 |
|
| 834 |
friend class Isolate; |
| 835 |
}; |
| 836 |
|
| 837 |
// Origin: |
| 838 |
// https://github.com/v8/v8/blob/5792f3587116503fc047d2f68c951c72dced08a5/src/handles/handles.h#L88-L171 |
| 839 |
template <typename T> |
| 840 |
class MOZ_NONHEAP_CLASS Handle { |
| 841 |
public: |
| 842 |
Handle() : location_(nullptr) {} |
| 843 |
Handle(T object, Isolate* isolate); |
| 844 |
Handle(const JS::Value& value, Isolate* isolate); |
| 845 |
|
| 846 |
// Constructor for handling automatic up casting. |
| 847 |
template <typename S, |
| 848 |
typename = std::enable_if_t<std::is_convertible_v<S*, T*>>> |
| 849 |
inline Handle(Handle<S> handle) : location_(handle.location_) {} |
| 850 |
|
| 851 |
inline bool is_null() const { return location_ == nullptr; } |
| 852 |
|
| 853 |
inline T operator*() const { return T::cast(Object(*location_)); }; |
| 854 |
|
| 855 |
// {ObjectRef} is returned by {Handle::operator->}. It should never be stored |
| 856 |
// anywhere or used in any other code; no one should ever have to spell out |
| 857 |
// {ObjectRef} in code. Its only purpose is to be dereferenced immediately by |
| 858 |
// "operator-> chaining". Returning the address of the field is valid because |
| 859 |
// this object's lifetime only ends at the end of the full statement. |
| 860 |
// Origin: |
| 861 |
// https://github.com/v8/v8/blob/03aaa4b3bf4cb01eee1f223b252e6869b04ab08c/src/handles/handles.h#L91-L105 |
| 862 |
class MOZ_TEMPORARY_CLASS ObjectRef { |
| 863 |
public: |
| 864 |
T* operator->() { return &object_; } |
| 865 |
|
| 866 |
private: |
| 867 |
friend class Handle; |
| 868 |
explicit ObjectRef(T object) : object_(object) {} |
| 869 |
|
| 870 |
T object_; |
| 871 |
}; |
| 872 |
inline ObjectRef operator->() const { return ObjectRef{**this}; } |
| 873 |
|
| 874 |
static Handle<T> fromHandleValue(JS::HandleValue handle) { |
| 875 |
return Handle(handle.address()); |
| 876 |
} |
| 877 |
|
| 878 |
private: |
| 879 |
Handle(const JS::Value* location) : location_(location) {} |
| 880 |
|
| 881 |
template <typename> |
| 882 |
friend class Handle; |
| 883 |
template <typename> |
| 884 |
friend class MaybeHandle; |
| 885 |
|
| 886 |
const JS::Value* location_; |
| 887 |
}; |
| 888 |
|
| 889 |
// A Handle can be converted into a MaybeHandle. Converting a MaybeHandle |
| 890 |
// into a Handle requires checking that it does not point to nullptr. This |
| 891 |
// ensures nullptr checks before use. |
| 892 |
// |
| 893 |
// Also note that Handles do not provide default equality comparison or hashing |
| 894 |
// operators on purpose. Such operators would be misleading, because intended |
| 895 |
// semantics is ambiguous between Handle location and object identity. |
| 896 |
// Origin: |
| 897 |
// https://github.com/v8/v8/blob/5792f3587116503fc047d2f68c951c72dced08a5/src/handles/maybe-handles.h#L15-L78 |
| 898 |
template <typename T> |
| 899 |
class MOZ_NONHEAP_CLASS MaybeHandle final { |
| 900 |
public: |
| 901 |
MaybeHandle() : location_(nullptr) {} |
| 902 |
|
| 903 |
// Constructor for handling automatic up casting from Handle. |
| 904 |
// Ex. Handle<JSArray> can be passed when MaybeHandle<Object> is expected. |
| 905 |
template <typename S, |
| 906 |
typename = std::enable_if_t<std::is_convertible_v<S*, T*>>> |
| 907 |
MaybeHandle(Handle<S> handle) : location_(handle.location_) {} |
| 908 |
|
| 909 |
inline Handle<T> ToHandleChecked() const { |
| 910 |
MOZ_RELEASE_ASSERT(location_); |
| 911 |
return Handle<T>(location_); |
| 912 |
} |
| 913 |
|
| 914 |
// Convert to a Handle with a type that can be upcasted to. |
| 915 |
template <typename S> |
| 916 |
inline bool ToHandle(Handle<S>* out) const { |
| 917 |
if (location_) { |
| 918 |
*out = Handle<T>(location_); |
| 919 |
return true; |
| 920 |
} else { |
| 921 |
*out = Handle<T>(); |
| 922 |
return false; |
| 923 |
} |
| 924 |
} |
| 925 |
|
| 926 |
private: |
| 927 |
JS::Value* location_; |
| 928 |
}; |
| 929 |
|
| 930 |
// From v8/src/handles/handles-inl.h |
| 931 |
|
| 932 |
template <typename T> |
| 933 |
inline Handle<T> handle(T object, Isolate* isolate) { |
| 934 |
return Handle<T>(object, isolate); |
| 935 |
} |
| 936 |
|
| 937 |
// V8 is migrating to a conservative stack scanning approach. When that |
| 938 |
// is enabled, a DirectHandle points directly at the V8 heap, and an |
| 939 |
// IndirectHandle is an unmigrated old-style Handle with a layer of |
| 940 |
// indirection. When disabled (which matches our implementation) the two |
| 941 |
// types are the same. See: |
| 942 |
// https://github.com/v8/v8/blob/887ec63c43e23c4fefba1c52d4525654bdc71e5b/src/common/globals.h#L1000-L1012 |
| 943 |
template <typename T> |
| 944 |
using DirectHandle = Handle<T>; |
| 945 |
|
| 946 |
template <typename T> |
| 947 |
using IndirectHandle = Handle<T>; |
| 948 |
|
| 949 |
template <typename T> |
| 950 |
using MaybeDirectHandle = MaybeHandle<T>; |
| 951 |
|
| 952 |
// RAII Guard classes |
| 953 |
|
| 954 |
using DisallowGarbageCollection = JS::AutoAssertNoGC; |
| 955 |
|
| 956 |
// V8 uses this inside DisallowGarbageCollection regions to turn |
| 957 |
// allocation back on before throwing a stack overflow exception or |
| 958 |
// handling interrupts. AutoSuppressGC is sufficient for the former |
| 959 |
// case, but not for the latter: handling interrupts can execute |
| 960 |
// arbitrary script code, and V8 jumps through some scary hoops to |
| 961 |
// "manually relocate unhandlified references" afterwards. To keep |
| 962 |
// things sane, we don't try to handle interrupts while regex code is |
| 963 |
// still on the stack. Instead, we return EXCEPTION and handle |
| 964 |
// interrupts in the caller. (See RegExpShared::execute.) |
| 965 |
|
| 966 |
class AllowGarbageCollection { |
| 967 |
public: |
| 968 |
AllowGarbageCollection() {} |
| 969 |
}; |
| 970 |
|
| 971 |
// Origin: |
| 972 |
// https://github.com/v8/v8/blob/84f3877c15bc7f8956d21614da4311337525a3c8/src/objects/string.h#L83-L474 |
| 973 |
class String : public HeapObject { |
| 974 |
private: |
| 975 |
JSString* str() const { return value().toString(); } |
| 976 |
|
| 977 |
public: |
| 978 |
String() = default; |
| 979 |
String(JSString* str) { setValue(JS::StringValue(str)); } |
| 980 |
|
| 981 |
operator JSString*() const { return str(); } |
| 982 |
|
| 983 |
// Max char codes. |
| 984 |
static const int32_t kMaxOneByteCharCode = unibrow::Latin1::kMaxChar; |
| 985 |
static const uint32_t kMaxOneByteCharCodeU = unibrow::Latin1::kMaxChar; |
| 986 |
static const int kMaxUtf16CodeUnit = 0xffff; |
| 987 |
static const uint32_t kMaxUtf16CodeUnitU = kMaxUtf16CodeUnit; |
| 988 |
static const base::uc32 kMaxCodePoint = 0x10ffff; |
| 989 |
|
| 990 |
MOZ_ALWAYS_INLINE int length() const { return str()->length(); } |
| 991 |
bool IsFlat() { return str()->isLinear(); }; |
| 992 |
|
| 993 |
// Origin: |
| 994 |
// https://github.com/v8/v8/blob/84f3877c15bc7f8956d21614da4311337525a3c8/src/objects/string.h#L95-L152 |
| 995 |
class FlatContent { |
| 996 |
public: |
| 997 |
FlatContent(JSLinearString* string, const DisallowGarbageCollection& no_gc) |
| 998 |
: string_(string), no_gc_(no_gc) {} |
| 999 |
inline bool IsOneByte() const { return string_->hasLatin1Chars(); } |
| 1000 |
inline bool IsTwoByte() const { return !string_->hasLatin1Chars(); } |
| 1001 |
|
| 1002 |
base::Vector<const uint8_t> ToOneByteVector() const { |
| 1003 |
MOZ_ASSERT(IsOneByte()); |
| 1004 |
return base::Vector<const uint8_t>(string_->latin1Chars(no_gc_), |
| 1005 |
string_->length()); |
| 1006 |
} |
| 1007 |
base::Vector<const base::uc16> ToUC16Vector() const { |
| 1008 |
MOZ_ASSERT(IsTwoByte()); |
| 1009 |
return base::Vector<const base::uc16>(string_->twoByteChars(no_gc_), |
| 1010 |
string_->length()); |
| 1011 |
} |
| 1012 |
void UnsafeDisableChecksumVerification() { |
| 1013 |
// Intentional no-op. See the comment for AllowGarbageCollection above. |
| 1014 |
} |
| 1015 |
|
| 1016 |
private: |
| 1017 |
const JSLinearString* string_; |
| 1018 |
const JS::AutoAssertNoGC& no_gc_; |
| 1019 |
}; |
| 1020 |
FlatContent GetFlatContent(const DisallowGarbageCollection& no_gc) { |
| 1021 |
MOZ_ASSERT(IsFlat()); |
| 1022 |
return FlatContent(&str()->asLinear(), no_gc); |
| 1023 |
} |
| 1024 |
|
| 1025 |
static Handle<String> Flatten(Isolate* isolate, Handle<String> string); |
| 1026 |
|
| 1027 |
inline static String cast(Object object) { |
| 1028 |
String s; |
| 1029 |
MOZ_ASSERT(object.value().isString()); |
| 1030 |
s.setValue(object.value()); |
| 1031 |
return s; |
| 1032 |
} |
| 1033 |
|
| 1034 |
inline static bool IsOneByteRepresentationUnderneath(String string) { |
| 1035 |
return string.str()->hasLatin1Chars(); |
| 1036 |
} |
| 1037 |
inline bool IsOneByteRepresentation() const { |
| 1038 |
return str()->hasLatin1Chars(); |
| 1039 |
} |
| 1040 |
|
| 1041 |
std::unique_ptr<char[]> ToCString(); |
| 1042 |
|
| 1043 |
template <typename Char> |
| 1044 |
base::Vector<const Char> GetCharVector( |
| 1045 |
const DisallowGarbageCollection& no_gc); |
| 1046 |
|
| 1047 |
friend class RegExpUtils; |
| 1048 |
}; |
| 1049 |
|
| 1050 |
template <> |
| 1051 |
inline base::Vector<const uint8_t> String::GetCharVector( |
| 1052 |
const DisallowGarbageCollection& no_gc) { |
| 1053 |
String::FlatContent flat = GetFlatContent(no_gc); |
| 1054 |
MOZ_ASSERT(flat.IsOneByte()); |
| 1055 |
return flat.ToOneByteVector(); |
| 1056 |
} |
| 1057 |
|
| 1058 |
template <> |
| 1059 |
inline base::Vector<const base::uc16> String::GetCharVector( |
| 1060 |
const DisallowGarbageCollection& no_gc) { |
| 1061 |
String::FlatContent flat = GetFlatContent(no_gc); |
| 1062 |
MOZ_ASSERT(flat.IsTwoByte()); |
| 1063 |
return flat.ToUC16Vector(); |
| 1064 |
} |
| 1065 |
|
| 1066 |
using RegExpFlags = JS::RegExpFlags; |
| 1067 |
using RegExpFlag = JS::RegExpFlags::Flag; |
| 1068 |
|
| 1069 |
class JSRegExp { |
| 1070 |
public: |
| 1071 |
// Each capture (including the match itself) needs two registers. |
| 1072 |
static constexpr int RegistersForCaptureCount(int count) { |
| 1073 |
return (count + 1) * 2; |
| 1074 |
} |
| 1075 |
|
| 1076 |
static RegExpFlags AsRegExpFlags(RegExpFlags flags) { return flags; } |
| 1077 |
static RegExpFlags AsJSRegExpFlags(RegExpFlags flags) { return flags; } |
| 1078 |
|
| 1079 |
static Handle<String> StringFromFlags(Isolate* isolate, RegExpFlags flags); |
| 1080 |
|
| 1081 |
// ****************************** |
| 1082 |
// Static constants |
| 1083 |
// ****************************** |
| 1084 |
|
| 1085 |
static constexpr int kMaxCaptures = (1 << 15) - 1; |
| 1086 |
|
| 1087 |
static constexpr int kNoBacktrackLimit = 0; |
| 1088 |
}; |
| 1089 |
|
| 1090 |
class IrRegExpData : public HeapObject { |
| 1091 |
public: |
| 1092 |
IrRegExpData() : HeapObject() {} |
| 1093 |
IrRegExpData(js::RegExpShared* re) { setValue(JS::PrivateGCThingValue(re)); } |
| 1094 |
|
| 1095 |
// ****************************************************** |
| 1096 |
// Methods that are called from inside the implementation |
| 1097 |
// ****************************************************** |
| 1098 |
void TierUpTick() { inner()->tierUpTick(); } |
| 1099 |
|
| 1100 |
Tagged<TrustedByteArray> bytecode(bool is_latin1) const { |
| 1101 |
return TrustedByteArray::cast( |
| 1102 |
Object(JS::PrivateValue(inner()->getByteCode(is_latin1)))); |
| 1103 |
} |
| 1104 |
|
| 1105 |
// TODO: should we expose this? |
| 1106 |
uint32_t backtrack_limit() const { return 0; } |
| 1107 |
|
| 1108 |
static IrRegExpData cast(Object object) { |
| 1109 |
IrRegExpData regexp; |
| 1110 |
js::gc::Cell* regexpShared = object.value().toGCThing(); |
| 1111 |
MOZ_ASSERT(regexpShared->is<js::RegExpShared>()); |
| 1112 |
regexp.setValue(JS::PrivateGCThingValue(regexpShared)); |
| 1113 |
return regexp; |
| 1114 |
} |
| 1115 |
|
| 1116 |
inline uint32_t max_register_count() const { |
| 1117 |
return inner()->getMaxRegisters(); |
| 1118 |
} |
| 1119 |
|
| 1120 |
RegExpFlags flags() const { return inner()->getFlags(); } |
| 1121 |
|
| 1122 |
size_t capture_count() const { |
| 1123 |
// Subtract 1 because pairCount includes the implicit global capture. |
| 1124 |
return inner()->pairCount() - 1; |
| 1125 |
} |
| 1126 |
|
| 1127 |
private: |
| 1128 |
js::RegExpShared* inner() const { |
| 1129 |
return value().toGCThing()->as<js::RegExpShared>(); |
| 1130 |
} |
| 1131 |
}; |
| 1132 |
|
| 1133 |
inline bool IsUnicode(RegExpFlags flags) { return flags.unicode(); } |
| 1134 |
inline bool IsGlobal(RegExpFlags flags) { return flags.global(); } |
| 1135 |
inline bool IsIgnoreCase(RegExpFlags flags) { return flags.ignoreCase(); } |
| 1136 |
inline bool IsMultiline(RegExpFlags flags) { return flags.multiline(); } |
| 1137 |
inline bool IsDotAll(RegExpFlags flags) { return flags.dotAll(); } |
| 1138 |
inline bool IsSticky(RegExpFlags flags) { return flags.sticky(); } |
| 1139 |
inline bool IsUnicodeSets(RegExpFlags flags) { return flags.unicodeSets(); } |
| 1140 |
inline bool IsEitherUnicode(RegExpFlags flags) { |
| 1141 |
return flags.unicode() || flags.unicodeSets(); |
| 1142 |
} |
| 1143 |
|
| 1144 |
inline std::optional<RegExpFlag> TryRegExpFlagFromChar(char c) { |
| 1145 |
RegExpFlag flag; |
| 1146 |
|
| 1147 |
// The parser only calls this after verifying that it's a supported flag. |
| 1148 |
if (JS::MaybeParseRegExpFlag(c, &flag)) { |
| 1149 |
return flag; |
| 1150 |
} |
| 1151 |
|
| 1152 |
return std::optional<RegExpFlag>{}; |
| 1153 |
} |
| 1154 |
|
| 1155 |
inline bool operator==(const RegExpFlags& lhs, const int& rhs) { |
| 1156 |
return lhs.value() == rhs; |
| 1157 |
} |
| 1158 |
inline bool operator!=(const RegExpFlags& lhs, const int& rhs) { |
| 1159 |
return !(lhs == rhs); |
| 1160 |
} |
| 1161 |
|
| 1162 |
class Histogram { |
| 1163 |
public: |
| 1164 |
inline void AddSample(int sample) {} |
| 1165 |
}; |
| 1166 |
|
| 1167 |
class Counters { |
| 1168 |
public: |
| 1169 |
Histogram* regexp_backtracks() { return ®exp_backtracks_; } |
| 1170 |
|
| 1171 |
private: |
| 1172 |
Histogram regexp_backtracks_; |
| 1173 |
}; |
| 1174 |
|
| 1175 |
enum class AllocationType : uint8_t { |
| 1176 |
kYoung, // Allocate in the nursery |
| 1177 |
kOld, // Allocate in the tenured heap |
| 1178 |
}; |
| 1179 |
|
| 1180 |
using StackGuard = Isolate; |
| 1181 |
using Factory = Isolate; |
| 1182 |
|
| 1183 |
class Isolate { |
| 1184 |
public: |
| 1185 |
Isolate(JSContext* cx) : cx_(cx) {} |
| 1186 |
~Isolate(); |
| 1187 |
bool init(); |
| 1188 |
|
| 1189 |
size_t sizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const; |
| 1190 |
|
| 1191 |
//********** Isolate code **********// |
| 1192 |
RegExpStack* regexp_stack() const { return regexpStack_; } |
| 1193 |
|
| 1194 |
// This is called from inside no-GC code. Instead of suppressing GC |
| 1195 |
// to allocate the error, we return false from Execute and call |
| 1196 |
// ReportOverRecursed in the caller. |
| 1197 |
void StackOverflow() {} |
| 1198 |
|
| 1199 |
#ifndef V8_INTL_SUPPORT |
| 1200 |
unibrow::Mapping<unibrow::Ecma262UnCanonicalize>* jsregexp_uncanonicalize() { |
| 1201 |
return &jsregexp_uncanonicalize_; |
| 1202 |
} |
| 1203 |
unibrow::Mapping<unibrow::Ecma262Canonicalize>* |
| 1204 |
regexp_macro_assembler_canonicalize() { |
| 1205 |
return ®exp_macro_assembler_canonicalize_; |
| 1206 |
} |
| 1207 |
unibrow::Mapping<unibrow::CanonicalizationRange>* jsregexp_canonrange() { |
| 1208 |
return &jsregexp_canonrange_; |
| 1209 |
} |
| 1210 |
|
| 1211 |
private: |
| 1212 |
unibrow::Mapping<unibrow::Ecma262UnCanonicalize> jsregexp_uncanonicalize_; |
| 1213 |
unibrow::Mapping<unibrow::Ecma262Canonicalize> |
| 1214 |
regexp_macro_assembler_canonicalize_; |
| 1215 |
unibrow::Mapping<unibrow::CanonicalizationRange> jsregexp_canonrange_; |
| 1216 |
#endif // !V8_INTL_SUPPORT |
| 1217 |
|
| 1218 |
public: |
| 1219 |
// An empty stub for telemetry we don't support |
| 1220 |
void IncreaseTotalRegexpCodeGenerated(Handle<HeapObject> code) {} |
| 1221 |
|
| 1222 |
Counters* counters() { return &counters_; } |
| 1223 |
|
| 1224 |
//********** Factory code **********// |
| 1225 |
inline Factory* factory() { return this; } |
| 1226 |
|
| 1227 |
Handle<ByteArray> NewByteArray( |
| 1228 |
int length, AllocationType allocation = AllocationType::kYoung); |
| 1229 |
|
| 1230 |
Handle<TrustedByteArray> NewTrustedByteArray( |
| 1231 |
int length, AllocationType allocation = AllocationType::kYoung); |
| 1232 |
|
| 1233 |
// Allocates a fixed array initialized with undefined values. |
| 1234 |
Handle<FixedArray> NewFixedArray(int length); |
| 1235 |
|
| 1236 |
template <typename T> |
| 1237 |
Handle<FixedIntegerArray<T>> NewFixedIntegerArray(uint32_t length); |
| 1238 |
|
| 1239 |
template <typename Char> |
| 1240 |
Handle<String> InternalizeString(const base::Vector<const Char>& str); |
| 1241 |
|
| 1242 |
//********** Stack guard code **********// |
| 1243 |
inline StackGuard* stack_guard() { return this; } |
| 1244 |
|
| 1245 |
uintptr_t real_climit() { return cx_->stackLimit(JS::StackForSystemCode); } |
| 1246 |
|
| 1247 |
// This is called from inside no-GC code. V8 runs the interrupt |
| 1248 |
// inside the no-GC code and then "manually relocates unhandlified |
| 1249 |
// references" afterwards. We just return a magic value and let the |
| 1250 |
// caller handle interrupts. |
| 1251 |
Object HandleInterrupts() { |
| 1252 |
return Object(JS::MagicValue(JS_INTERRUPT_REGEXP)); |
| 1253 |
} |
| 1254 |
|
| 1255 |
JSContext* cx() const { return cx_; } |
| 1256 |
|
| 1257 |
void trace(JSTracer* trc); |
| 1258 |
|
| 1259 |
//********** Handle code **********// |
| 1260 |
|
| 1261 |
JS::Value* getHandleLocation(const JS::Value& value); |
| 1262 |
|
| 1263 |
private: |
| 1264 |
mozilla::SegmentedVector<JS::Value, 256> handleArena_; |
| 1265 |
mozilla::SegmentedVector<PseudoHandle<void>, 256> uniquePtrArena_; |
| 1266 |
|
| 1267 |
void* allocatePseudoHandle(size_t bytes); |
| 1268 |
|
| 1269 |
public: |
| 1270 |
template <typename T> |
| 1271 |
PseudoHandle<T> takeOwnership(void* ptr); |
| 1272 |
template <typename T> |
| 1273 |
PseudoHandle<T> maybeTakeOwnership(void* ptr); |
| 1274 |
|
| 1275 |
uint32_t liveHandles() const { return handleArena_.Length(); } |
| 1276 |
uint32_t livePseudoHandles() const { return uniquePtrArena_.Length(); } |
| 1277 |
|
| 1278 |
private: |
| 1279 |
void openHandleScope(HandleScope& scope) { |
| 1280 |
scope.level_ = handleArena_.Length(); |
| 1281 |
scope.non_gc_level_ = uniquePtrArena_.Length(); |
| 1282 |
} |
| 1283 |
void closeHandleScope(size_t prevLevel, size_t prevUniqueLevel) { |
| 1284 |
size_t currLevel = handleArena_.Length(); |
| 1285 |
handleArena_.PopLastN(currLevel - prevLevel); |
| 1286 |
|
| 1287 |
size_t currUniqueLevel = uniquePtrArena_.Length(); |
| 1288 |
uniquePtrArena_.PopLastN(currUniqueLevel - prevUniqueLevel); |
| 1289 |
} |
| 1290 |
friend class HandleScope; |
| 1291 |
|
| 1292 |
JSContext* cx_; |
| 1293 |
RegExpStack* regexpStack_{}; |
| 1294 |
Counters counters_{}; |
| 1295 |
#ifdef DEBUG |
| 1296 |
public: |
| 1297 |
uint32_t shouldSimulateInterrupt_ = 0; |
| 1298 |
#endif |
| 1299 |
}; |
| 1300 |
|
| 1301 |
// Origin: |
| 1302 |
// https://github.com/v8/v8/blob/50dcf2af54ce27801a71c47c1be1d2c5e36b0dd6/src/execution/isolate.h#L1909-L1931 |
| 1303 |
class StackLimitCheck { |
| 1304 |
public: |
| 1305 |
StackLimitCheck(Isolate* isolate) : cx_(isolate->cx()) {} |
| 1306 |
|
| 1307 |
// Use this to check for stack-overflows in C++ code. |
| 1308 |
bool HasOverflowed() { |
| 1309 |
js::AutoCheckRecursionLimit recursion(cx_); |
| 1310 |
bool overflowed = !recursion.checkDontReport(cx_); |
| 1311 |
if (overflowed && js::SupportDifferentialTesting()) { |
| 1312 |
// We don't report overrecursion here, but we throw an exception later |
| 1313 |
// and this still affects differential testing. Mimic ReportOverRecursed |
| 1314 |
// (the fuzzers check for this particular string). |
| 1315 |
fprintf(stderr, "ReportOverRecursed called\n"); |
| 1316 |
} |
| 1317 |
return overflowed; |
| 1318 |
} |
| 1319 |
|
| 1320 |
// Use this to check for interrupt request in C++ code. |
| 1321 |
bool InterruptRequested() { |
| 1322 |
return cx_->hasPendingInterrupt(js::InterruptReason::CallbackUrgent); |
| 1323 |
} |
| 1324 |
|
| 1325 |
// Use this to check for stack-overflow when entering runtime from JS code. |
| 1326 |
bool JsHasOverflowed() { |
| 1327 |
js::AutoCheckRecursionLimit recursion(cx_); |
| 1328 |
return !recursion.checkDontReport(cx_); |
| 1329 |
} |
| 1330 |
|
| 1331 |
private: |
| 1332 |
JSContext* cx_; |
| 1333 |
}; |
| 1334 |
|
| 1335 |
class ExternalReference { |
| 1336 |
public: |
| 1337 |
static const void* TopOfRegexpStack(Isolate* isolate); |
| 1338 |
static size_t SizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf, |
| 1339 |
RegExpStack* regexpStack); |
| 1340 |
}; |
| 1341 |
|
| 1342 |
class Code : public HeapObject { |
| 1343 |
public: |
| 1344 |
uint8_t* raw_instruction_start() { return inner()->raw(); } |
| 1345 |
|
| 1346 |
static Code cast(Object object) { |
| 1347 |
Code c; |
| 1348 |
js::gc::Cell* jitCode = object.value().toGCThing(); |
| 1349 |
MOZ_ASSERT(jitCode->is<js::jit::JitCode>()); |
| 1350 |
c.setValue(JS::PrivateGCThingValue(jitCode)); |
| 1351 |
return c; |
| 1352 |
} |
| 1353 |
js::jit::JitCode* inner() { |
| 1354 |
return value().toGCThing()->as<js::jit::JitCode>(); |
| 1355 |
} |
| 1356 |
}; |
| 1357 |
|
| 1358 |
// Only used in function signature of functions we don't implement |
| 1359 |
// (NativeRegExpMacroAssembler::CheckStackGuardState) |
| 1360 |
class InstructionStream {}; |
| 1361 |
|
| 1362 |
// Only used in the definition of RegExpGlobalExecRunner, which we don't use. |
| 1363 |
class RegExpResultVectorScope {}; |
| 1364 |
|
| 1365 |
// Origin: https://github.com/v8/v8/blob/master/src/codegen/label.h |
| 1366 |
class Label { |
| 1367 |
public: |
| 1368 |
Label() : inner_(js::jit::Label()) {} |
| 1369 |
|
| 1370 |
js::jit::Label* inner() { return &inner_; } |
| 1371 |
|
| 1372 |
void Unuse() { inner_.reset(); } |
| 1373 |
|
| 1374 |
bool is_linked() { return inner_.used(); } |
| 1375 |
bool is_bound() { return inner_.bound(); } |
| 1376 |
bool is_unused() { return !inner_.used() && !inner_.bound(); } |
| 1377 |
|
| 1378 |
int pos() { return inner_.offset(); } |
| 1379 |
void link_to(int pos) { inner_.use(pos); } |
| 1380 |
void bind_to(int pos) { inner_.bind(pos); } |
| 1381 |
|
| 1382 |
private: |
| 1383 |
js::jit::Label inner_; |
| 1384 |
js::jit::CodeOffset patchOffset_; |
| 1385 |
|
| 1386 |
friend class SMRegExpMacroAssembler; |
| 1387 |
}; |
| 1388 |
|
| 1389 |
class RegExpUtils { |
| 1390 |
public: |
| 1391 |
static uint64_t AdvanceStringIndex(Tagged<String> string, uint64_t index, |
| 1392 |
bool unicode); |
| 1393 |
}; |
| 1394 |
|
| 1395 |
#define v8_flags js::jit::JitOptions |
| 1396 |
|
| 1397 |
#define V8_USE_COMPUTED_GOTO 1 |
| 1398 |
#define COMPILING_IRREGEXP_FOR_EXTERNAL_EMBEDDER |
| 1399 |
|
| 1400 |
} // namespace internal |
| 1401 |
} // namespace v8 |
| 1402 |
|
| 1403 |
namespace V8 { |
| 1404 |
|
| 1405 |
inline void FatalProcessOutOfMemory(v8::internal::Isolate* isolate, |
| 1406 |
const char* msg) { |
| 1407 |
js::AutoEnterOOMUnsafeRegion oomUnsafe; |
| 1408 |
oomUnsafe.crash(msg); |
| 1409 |
} |
| 1410 |
|
| 1411 |
} // namespace V8 |
| 1412 |
|
| 1413 |
#endif // RegexpShim_h |