From fdb560b34d71fe82d025f65f92a4eef225ec2608 Mon Sep 17 00:00:00 2001 From: "Matt A. Tobin" Date: Mon, 9 Nov 2020 20:20:18 -0500 Subject: [PATCH 1/9] Issue #1677 - Part 3: Create shim definitions for V8-specific code in new regexp implementation --- js/src/regexp/property-sequences.h | 1 + js/src/regexp/regexp-ast.h | 1 + js/src/regexp/regexp-bytecode-peephole.h | 1 + js/src/regexp/regexp-bytecodes.h | 1 + js/src/regexp/regexp-dotprinter.h | 1 + js/src/regexp/regexp-macro-assembler-arch.h | 16 + js/src/regexp/regexp-macro-assembler.h | 1 + js/src/regexp/regexp-shim.cc | 72 ++ js/src/regexp/regexp-shim.h | 999 ++++++++++++++++++++ js/src/regexp/regexp-stack.h | 1 + js/src/regexp/regexp.h | 1 + js/src/regexp/util/flags.h | 93 ++ js/src/regexp/util/vector.h | 204 ++++ js/src/regexp/util/zone.h | 356 +++++++ 14 files changed, 1748 insertions(+) create mode 100644 js/src/regexp/regexp-macro-assembler-arch.h create mode 100644 js/src/regexp/regexp-shim.cc create mode 100644 js/src/regexp/regexp-shim.h create mode 100644 js/src/regexp/util/flags.h create mode 100644 js/src/regexp/util/vector.h create mode 100644 js/src/regexp/util/zone.h diff --git a/js/src/regexp/property-sequences.h b/js/src/regexp/property-sequences.h index cbf2ea1fc0..ed39e23795 100644 --- a/js/src/regexp/property-sequences.h +++ b/js/src/regexp/property-sequences.h @@ -7,6 +7,7 @@ #ifdef V8_INTL_SUPPORT +#include "regexp/regexp-shim.h" namespace v8 { namespace internal { diff --git a/js/src/regexp/regexp-ast.h b/js/src/regexp/regexp-ast.h index 540fdce80a..fe6913e1d4 100644 --- a/js/src/regexp/regexp-ast.h +++ b/js/src/regexp/regexp-ast.h @@ -5,6 +5,7 @@ #ifndef V8_REGEXP_REGEXP_AST_H_ #define V8_REGEXP_REGEXP_AST_H_ +#include "regexp/regexp-shim.h" namespace v8 { namespace internal { diff --git a/js/src/regexp/regexp-bytecode-peephole.h b/js/src/regexp/regexp-bytecode-peephole.h index 96349f52cf..31d5a2d480 100644 --- a/js/src/regexp/regexp-bytecode-peephole.h +++ b/js/src/regexp/regexp-bytecode-peephole.h @@ -5,6 +5,7 @@ #ifndef V8_REGEXP_REGEXP_BYTECODE_PEEPHOLE_H_ #define V8_REGEXP_REGEXP_BYTECODE_PEEPHOLE_H_ +#include "regexp/regexp-shim.h" namespace v8 { namespace internal { diff --git a/js/src/regexp/regexp-bytecodes.h b/js/src/regexp/regexp-bytecodes.h index 35f7a30dae..24d6925db9 100644 --- a/js/src/regexp/regexp-bytecodes.h +++ b/js/src/regexp/regexp-bytecodes.h @@ -5,6 +5,7 @@ #ifndef V8_REGEXP_REGEXP_BYTECODES_H_ #define V8_REGEXP_REGEXP_BYTECODES_H_ +#include "regexp/regexp-shim.h" namespace v8 { namespace internal { diff --git a/js/src/regexp/regexp-dotprinter.h b/js/src/regexp/regexp-dotprinter.h index b3b268f53a..e5781184c0 100644 --- a/js/src/regexp/regexp-dotprinter.h +++ b/js/src/regexp/regexp-dotprinter.h @@ -5,6 +5,7 @@ #ifndef V8_REGEXP_REGEXP_DOTPRINTER_H_ #define V8_REGEXP_REGEXP_DOTPRINTER_H_ +#include "regexp/regexp-shim.h" namespace v8 { namespace internal { diff --git a/js/src/regexp/regexp-macro-assembler-arch.h b/js/src/regexp/regexp-macro-assembler-arch.h new file mode 100644 index 0000000000..60b5c94de4 --- /dev/null +++ b/js/src/regexp/regexp-macro-assembler-arch.h @@ -0,0 +1,16 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * vim: set ts=8 sts=2 et sw=2 tw=80: + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +// This file implements the NativeRegExpMacroAssembler interface for +// SpiderMonkey. It provides the same interface as each of V8's +// architecture-specific implementations. + +#ifndef RegexpMacroAssemblerArch_h +#define RegexpMacroAssemblerArch_h + +#include "regexp/regexp-shim.h" + +#endif // RegexpMacroAssemblerArch_h diff --git a/js/src/regexp/regexp-macro-assembler.h b/js/src/regexp/regexp-macro-assembler.h index 67d97d1228..dd059a43d0 100644 --- a/js/src/regexp/regexp-macro-assembler.h +++ b/js/src/regexp/regexp-macro-assembler.h @@ -6,6 +6,7 @@ #define V8_REGEXP_REGEXP_MACRO_ASSEMBLER_H_ #include "regexp/regexp-ast.h" +#include "regexp/regexp-shim.h" #include "regexp/regexp.h" namespace v8 { diff --git a/js/src/regexp/regexp-shim.cc b/js/src/regexp/regexp-shim.cc new file mode 100644 index 0000000000..5e1da7a605 --- /dev/null +++ b/js/src/regexp/regexp-shim.cc @@ -0,0 +1,72 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * vim: set ts=8 sts=2 et sw=2 tw=80: + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +// Copyright 2019 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. + +#include "regexp/regexp-shim.h" + +namespace v8 { +namespace internal { + +void PrintF(const char* format, ...) { + va_list arguments; + va_start(arguments, format); + vprintf(format, arguments); + va_end(arguments); +} + +void PrintF(FILE* out, const char* format, ...) { + va_list arguments; + va_start(arguments, format); + vfprintf(out, format, arguments); + va_end(arguments); +} + +// Origin: +// https://github.com/v8/v8/blob/855591a54d160303349a5f0a32fab15825c708d1/src/utils/ostreams.cc#L120-L169 +// (This is a hand-simplified version.) +// Writes the given character to the output escaping everything outside +// of printable ASCII range. +std::ostream& operator<<(std::ostream& os, const AsUC16& c) { + uint16_t v = c.value; + bool isPrint = 0x20 < v && v <= 0x7e; + char buf[10]; + const char* format = isPrint ? "%c" : (v <= 0xFF) ? "\\x%02x" : "\\u%04x"; + snprintf(buf, sizeof(buf), format, v); + return os << buf; +} +std::ostream& operator<<(std::ostream& os, const AsUC32& c) { + int32_t v = c.value; + if (v <= String::kMaxUtf16CodeUnit) { + return os << AsUC16(v); + } + char buf[13]; + snprintf(buf, sizeof(buf), "\\u{%06x}", v); + return os << buf; +} + +DisallowJavascriptExecution::DisallowJavascriptExecution(Isolate* isolate) + : nojs_(isolate->cx()) {} + +// TODO: Map flags to jitoptions +bool FLAG_correctness_fuzzer_suppressions = false; +bool FLAG_enable_regexp_unaligned_accesses = false; +bool FLAG_harmony_regexp_sequence = false; +bool FLAG_regexp_interpret_all = false; +bool FLAG_regexp_mode_modifiers = false; +bool FLAG_regexp_optimization = false; +bool FLAG_regexp_peephole_optimization = false; +bool FLAG_regexp_possessive_quantifier = false; +bool FLAG_regexp_tier_up = false; +bool FLAG_trace_regexp_assembler = false; +bool FLAG_trace_regexp_bytecodes = false; +bool FLAG_trace_regexp_parser = false; +bool FLAG_trace_regexp_peephole_optimization = false; + +} // namespace internal +} // namespace v8 diff --git a/js/src/regexp/regexp-shim.h b/js/src/regexp/regexp-shim.h new file mode 100644 index 0000000000..0497ee552d --- /dev/null +++ b/js/src/regexp/regexp-shim.h @@ -0,0 +1,999 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * vim: set ts=8 sts=2 et sw=2 tw=80: + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +// Copyright 2019 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 RegexpShim_h +#define RegexpShim_h + +#include "mozilla/Assertions.h" +#include "mozilla/Attributes.h" +#include "mozilla/MathAlgorithms.h" +#include "mozilla/Maybe.h" +#include "mozilla/Types.h" + +#include + +#include "jit/Label.h" +#include "js/Value.h" +#include "regexp/util/flags.h" +#include "regexp/util/vector.h" +#include "regexp/util/zone.h" +#include "vm/NativeObject.h" + +// Forward declaration of classes +namespace v8 { +namespace internal { + +class Heap; +class Isolate; +class RegExpMatchInfo; +class RegExpStack; + +} // namespace internal +} // namespace v8 + +#define V8_WARN_UNUSED_RESULT MOZ_MUST_USE +#define V8_EXPORT_PRIVATE MOZ_EXPORT +#define V8_FALLTHROUGH MOZ_FALLTHROUGH + +#define FATAL(x) MOZ_CRASH(x) +#define UNREACHABLE() MOZ_CRASH("unreachable code") +#define UNIMPLEMENTED() MOZ_CRASH("unimplemented code") +#define STATIC_ASSERT(exp) static_assert(exp, #exp) + +#define DCHECK MOZ_ASSERT +#define DCHECK_EQ(lhs, rhs) MOZ_ASSERT((lhs) == (rhs)) +#define DCHECK_NE(lhs, rhs) MOZ_ASSERT((lhs) != (rhs)) +#define DCHECK_GT(lhs, rhs) MOZ_ASSERT((lhs) > (rhs)) +#define DCHECK_GE(lhs, rhs) MOZ_ASSERT((lhs) >= (rhs)) +#define DCHECK_LT(lhs, rhs) MOZ_ASSERT((lhs) < (rhs)) +#define DCHECK_LE(lhs, rhs) MOZ_ASSERT((lhs) <= (rhs)) +#define DCHECK_NULL(val) MOZ_ASSERT((val) == nullptr) +#define DCHECK_NOT_NULL(val) MOZ_ASSERT((val) != nullptr) +#define DCHECK_IMPLIES(lhs, rhs) MOZ_ASSERT_IF(lhs, rhs) +#define CHECK MOZ_RELEASE_ASSERT + +template +static constexpr inline T Min(T t1, T t2) { + return t1 < t2 ? t1 : t2; +} + +template +static constexpr inline T Max(T t1, T t2) { + return t1 > t2 ? t1 : t2; +} +#define MemCopy memcpy + +// Origin: +// https://github.com/v8/v8/blob/855591a54d160303349a5f0a32fab15825c708d1/src/base/macros.h#L310-L319 +// ptrdiff_t is 't' according to the standard, but MSVC uses 'I'. +#ifdef _MSC_VER +# define V8PRIxPTRDIFF "Ix" +# define V8PRIdPTRDIFF "Id" +# define V8PRIuPTRDIFF "Iu" +#else +# define V8PRIxPTRDIFF "tx" +# define V8PRIdPTRDIFF "td" +# define V8PRIuPTRDIFF "tu" +#endif + +// Origin: +// https://github.com/v8/v8/blob/855591a54d160303349a5f0a32fab15825c708d1/src/base/macros.h#L27-L38 +// The arraysize(arr) macro returns the # of elements in an array arr. +// The expression is a compile-time constant, and therefore can be +// used in defining new arrays, for example. If you use arraysize on +// a pointer by mistake, you will get a compile-time error. +#define arraysize(array) (sizeof(ArraySizeHelper(array))) + +// This template function declaration is used in defining arraysize. +// Note that the function doesn't need an implementation, as we only +// use its type. +template +char (&ArraySizeHelper(T (&array)[N]))[N]; + +// Explicitly declare the assignment operator as deleted. +#define DISALLOW_ASSIGN(TypeName) TypeName& operator=(const TypeName&) = delete + +// Explicitly declare the copy constructor and assignment operator as deleted. +// This also deletes the implicit move constructor and implicit move assignment +// operator, but still allows to manually define them. +#define DISALLOW_COPY_AND_ASSIGN(TypeName) \ + TypeName(const TypeName&) = delete; \ + DISALLOW_ASSIGN(TypeName) + +// Explicitly declare all implicit constructors as deleted, namely the +// default constructor, copy constructor and operator= functions. +// This is especially useful for classes containing only static methods. +#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \ + TypeName() = delete; \ + DISALLOW_COPY_AND_ASSIGN(TypeName) + +namespace v8 { + +// Origin: +// https://github.com/v8/v8/blob/855591a54d160303349a5f0a32fab15825c708d1/src/base/macros.h#L364-L367 +template +constexpr inline bool IsAligned(T value, U alignment) { + return (value & (alignment - 1)) == 0; +} + +using byte = uint8_t; +using Address = uintptr_t; +static const Address kNullAddress = 0; + +namespace base { + +// Origin: +// https://github.com/v8/v8/blob/855591a54d160303349a5f0a32fab15825c708d1/src/base/macros.h#L247-L258 +// The USE(x, ...) template is used to silence C++ compiler warnings +// issued for (yet) unused variables (typically parameters). +// The arguments are guaranteed to be evaluated from left to right. +struct Use { + template + Use(T&&) {} // NOLINT(runtime/explicit) +}; +#define USE(...) \ + do { \ + ::v8::base::Use unused_tmp_array_for_use_macro[]{__VA_ARGS__}; \ + (void)unused_tmp_array_for_use_macro; \ + } while (false) + +// Origin: +// https://github.com/v8/v8/blob/855591a54d160303349a5f0a32fab15825c708d1/src/base/safe_conversions.h#L35-L39 +// saturated_cast<> is analogous to static_cast<> for numeric types, except +// that the specified numeric conversion will saturate rather than overflow or +// underflow. +template +inline Dst saturated_cast(Src value); + +// This is the only specialization that is needed for regexp code. +// Instead of pulling in dozens of lines of template goo +// to derive it, I used the implementation from uint8_clamped in +// ArrayBufferObject.h. +template <> +inline uint8_t saturated_cast(int x) { + return (x >= 0) ? ((x < 255) ? uint8_t(x) : 255) : 0; +} + +namespace bits { + +inline uint64_t CountTrailingZeros(uint64_t value) { + return mozilla::CountTrailingZeroes64(value); +} + +} // namespace bits +} // namespace base + +namespace unibrow { + +using uchar = unsigned int; + +// Origin: +// https://github.com/v8/v8/blob/1f1e4cdb04c75eab77adbecd5f5514ddc3eb56cf/src/strings/unicode.h#L133-L150 +class Latin1 { + public: + static const uint16_t kMaxChar = 0xff; + + // Convert the character to Latin-1 case equivalent if possible. + static inline uint16_t TryConvertToLatin1(uint16_t c) { + // "GREEK CAPITAL LETTER MU" case maps to "MICRO SIGN". + // "GREEK SMALL LETTER MU" case maps to "MICRO SIGN". + if (c == 0x039C || c == 0x03BC) { + return 0xB5; + } + // "LATIN CAPITAL LETTER Y WITH DIAERESIS" case maps to "LATIN SMALL LETTER + // Y WITH DIAERESIS". + if (c == 0x0178) { + return 0xFF; + } + return c; + } +}; + +// Origin: +// https://github.com/v8/v8/blob/b4bfbce6f91fc2cc72178af42bb3172c5f5eaebb/src/strings/unicode.h#L99-L131 +class Utf16 { + public: + static inline bool IsLeadSurrogate(int code) { + return js::unicode::IsLeadSurrogate(code); + } + static inline bool IsTrailSurrogate(int code) { + return js::unicode::IsTrailSurrogate(code); + } + static inline uint16_t LeadSurrogate(uint32_t char_code) { + return js::unicode::LeadSurrogate(char_code); + } + static inline uint16_t TrailSurrogate(uint32_t char_code) { + return js::unicode::TrailSurrogate(char_code); + } + static inline uint32_t CombineSurrogatePair(char16_t lead, char16_t trail) { + return js::unicode::UTF16Decode(lead, trail); + } + static const uchar kMaxNonSurrogateCharCode = 0xffff; +}; + +// A cache used in case conversion. It caches the value for characters +// that either have no mapping or map to a single character independent +// of context. Characters that map to more than one character or that +// map differently depending on context are always looked up. +// Origin: +// https://github.com/v8/v8/blob/b4bfbce6f91fc2cc72178af42bb3172c5f5eaebb/src/strings/unicode.h#L64-L88 +template +class Mapping { + public: + inline Mapping() = default; + int get(uchar c, uchar n, uchar* result); + + private: + friend class Test; + int CalculateValue(uchar c, uchar n, uchar* result); + struct CacheEntry { + inline CacheEntry() : code_point_(kNoChar), offset_(0) {} + inline CacheEntry(uchar code_point, signed offset) + : code_point_(code_point), offset_(offset) {} + uchar code_point_; + signed offset_; + static const int kNoChar = (1 << 21) - 1; + }; + static const int kSize = size; + static const int kMask = kSize - 1; + CacheEntry entries_[kSize]; +}; + +// Origin: +// https://github.com/v8/v8/blob/b4bfbce6f91fc2cc72178af42bb3172c5f5eaebb/src/strings/unicode.h#L241-L252 +struct Ecma262Canonicalize { + static const int kMaxWidth = 1; + static int Convert(uchar c, uchar n, uchar* result, bool* allow_caching_ptr); +}; +struct Ecma262UnCanonicalize { + static const int kMaxWidth = 1; + static int Convert(uchar c, uchar n, uchar* result, bool* allow_caching_ptr); +}; +struct CanonicalizationRange { + static const int kMaxWidth = 1; + static int Convert(uchar c, uchar n, uchar* result, bool* allow_caching_ptr); +}; + +} // namespace unibrow + +namespace internal { + +#define PRINTF_FORMAT(x, y) MOZ_FORMAT_PRINTF(x, y) +void PRINTF_FORMAT(1, 2) PrintF(const char* format, ...); +void PRINTF_FORMAT(2, 3) PrintF(FILE* out, const char* format, ...); + +// Superclass for classes only using static method functions. +// The subclass of AllStatic cannot be instantiated at all. +class AllStatic { +#ifdef DEBUG + public: + AllStatic() = delete; +#endif +}; + +// Superclass for classes managed with new and delete. +// In irregexp, this is only AlternativeGeneration (in regexp-compiler.cc) +// Compare: +// https://github.com/v8/v8/blob/7b3332844212d78ee87a9426f3a6f7f781a8fbfa/src/utils/allocation.cc#L88-L96 +class Malloced { + public: + static void* operator new(size_t size) { + js::AutoEnterOOMUnsafeRegion oomUnsafe; + void* result = js_malloc(size); + if (!result) { + oomUnsafe.crash("Irregexp Malloced shim"); + } + return result; + } + static void operator delete(void* p) { js_free(p); } +}; + +constexpr int32_t KB = 1024; +constexpr int32_t MB = 1024 * 1024; + +#define kMaxInt JSVAL_INT_MAX +#define kMinInt JSVAL_INT_MIN +constexpr int kSystemPointerSize = sizeof(void*); + +// The largest integer n such that n and n + 1 are both exactly +// representable as a Number value. ES6 section 20.1.2.6 +constexpr double kMaxSafeInteger = 9007199254740991.0; // 2^53-1 + +// Latin1/UTF-16 constants +// Code-point values in Unicode 4.0 are 21 bits wide. +// Code units in UTF-16 are 16 bits wide. +using uc16 = uint16_t; +using uc32 = int32_t; + +constexpr int kBitsPerByte = 8; +constexpr int kBitsPerByteLog2 = 3; +constexpr int kUInt32Size = sizeof(uint32_t); +constexpr int kInt64Size = sizeof(int64_t); +constexpr int kUC16Size = sizeof(uc16); + +inline constexpr bool IsDecimalDigit(uc32 c) { return c >= '0' && c <= '9'; } +inline bool is_uint24(int val) { return (val & 0x00ffffff) == val; } + +inline bool IsIdentifierStart(uc32 c) { + return js::unicode::IsIdentifierStart(uint32_t(c)); +} +inline bool IsIdentifierPart(uc32 c) { + return js::unicode::IsIdentifierPart(uint32_t(c)); +} + +// Wrappers to disambiguate uint16_t and uc16. +struct AsUC16 { + explicit AsUC16(uint16_t v) : value(v) {} + uint16_t value; +}; + +struct AsUC32 { + explicit AsUC32(int32_t v) : value(v) {} + int32_t value; +}; + +class StdoutStream : public std::ostream {}; + +std::ostream& operator<<(std::ostream& os, const AsUC16& c); +std::ostream& operator<<(std::ostream& os, const AsUC32& c); + +// Reuse existing Maybe implementation +using mozilla::Maybe; + +template +Maybe Just(const T& value) { + return mozilla::Some(value); +} + +template +mozilla::Nothing Nothing() { + return mozilla::Nothing(); +} + +// Origin: +// https://github.com/v8/v8/blob/855591a54d160303349a5f0a32fab15825c708d1/src/utils/utils.h#L600-L642 +// Compare 8bit/16bit chars to 8bit/16bit chars. +// Used indirectly by regexp-interpreter.cc +template +inline int CompareCharsUnsigned(const lchar* lhs, const rchar* rhs, + size_t chars) { + const lchar* limit = lhs + chars; + if (sizeof(*lhs) == sizeof(char) && sizeof(*rhs) == sizeof(char)) { + // memcmp compares byte-by-byte, yielding wrong results for two-byte + // strings on little-endian systems. + return memcmp(lhs, rhs, chars); + } + while (lhs < limit) { + int r = static_cast(*lhs) - static_cast(*rhs); + if (r != 0) return r; + ++lhs; + ++rhs; + } + return 0; +} +template +inline int CompareChars(const lchar* lhs, const rchar* rhs, size_t chars) { + DCHECK_LE(sizeof(lchar), 2); + DCHECK_LE(sizeof(rchar), 2); + if (sizeof(lchar) == 1) { + if (sizeof(rchar) == 1) { + return CompareCharsUnsigned(reinterpret_cast(lhs), + reinterpret_cast(rhs), chars); + } else { + return CompareCharsUnsigned(reinterpret_cast(lhs), + reinterpret_cast(rhs), + chars); + } + } else { + if (sizeof(rchar) == 1) { + return CompareCharsUnsigned(reinterpret_cast(lhs), + reinterpret_cast(rhs), chars); + } else { + return CompareCharsUnsigned(reinterpret_cast(lhs), + reinterpret_cast(rhs), + chars); + } + } +} + +// Origin: +// https://github.com/v8/v8/blob/855591a54d160303349a5f0a32fab15825c708d1/src/utils/utils.h#L40-L48 +// Returns the value (0 .. 15) of a hexadecimal character c. +// If c is not a legal hexadecimal character, returns a value < 0. +// Used in regexp-parser.cc +inline int HexValue(uc32 c) { + c -= '0'; + if (static_cast(c) <= 9) return c; + c = (c | 0x20) - ('a' - '0'); // detect 0x11..0x16 and 0x31..0x36. + if (static_cast(c) <= 5) return c + 10; + return -1; +} + +// V8::Object ~= JS::Value +class Object { + public: + // The default object constructor in V8 stores a nullptr, + // which has its low bit clear and is interpreted as Smi(0). + constexpr Object() : value_(JS::Int32Value(0)) {} + + // Conversions to/from SpiderMonkey types + constexpr Object(JS::Value value) : value_(value) {} + operator JS::Value() const { return value_; } + + // Used in regexp-macro-assembler.cc and regexp-interpreter.cc to + // check the return value of isolate->stack_guard()->HandleInterrupts() + // In V8, this will be either an exception object or undefined. + // In SM, we store the exception in the context, so we can use our normal + // idiom: return false iff we are throwing an exception. + inline bool IsException(Isolate*) const { return !value_.toBoolean(); } + + // SpiderMonkey tries to avoid leaking the internal representation of its + // objects. V8 is not so strict. These functions are used when calling / + // being called by native code: objects are converted to Addresses for the + // call, then cast back to objects on the other side. + // We might be able to upstream a patch that eliminates the need for these. + Object(Address bits); + Address ptr() const; + + protected: + JS::Value value_; +}; + +class Smi : public Object { + public: + static Smi FromInt(int32_t value) { + Smi smi; + smi.value_ = JS::Int32Value(value); + return smi; + } + static inline int32_t ToInt(const Object object) { + return JS::Value(object).toInt32(); + } +}; + +// V8::HeapObject ~= JSObject +class HeapObject : public Object { +public: + // Only used for bookkeeping of total code generated in regexp-compiler. + // We may be able to refactor this away. + int Size() const; +}; + +// A fixed-size array with Objects (aka Values) as element types +// Implemented as a wrapper around a regular native object with dense elements. +class FixedArray : public HeapObject { + public: + inline void set(uint32_t index, Object value) { + JS::Value(*this).toObject().as().setDenseElement(index, + value); + } +}; + +// A fixed-size array of bytes. +// TODO: figure out the best implementation for this. Uint8Array might work, +// but it's not currently visible outside of TypedArrayObject.cpp. +class ByteArray : public HeapObject { + public: + uint8_t get(uint32_t index); + void set(uint32_t index, uint8_t val); + uint32_t length(); + byte* GetDataStartAddress(); + byte* GetDataEndAddress(); + + static ByteArray cast(Object object); +}; + +// Like Handles in SM, V8 handles are references to marked pointers. +// Unlike SM, where Rooted pointers are created individually on the +// stack, the target of a V8 handle lives in a HandleScope. +// HandleScopes are created on the stack and register themselves with +// the isolate (~= JSContext). Whenever a Handle is created, the +// outermost HandleScope is retrieved from the isolate, and a new root +// is created in that HandleScope. The Handle remains valid for the +// lifetime of the HandleScope. +class HandleScope { + public: + HandleScope(Isolate* isolate); +}; + +// Origin: +// https://github.com/v8/v8/blob/5792f3587116503fc047d2f68c951c72dced08a5/src/handles/handles.h#L88-L171 +template +class Handle { + public: + Handle(); + Handle(T object, Isolate* isolate); + + // Constructor for handling automatic up casting. + template ::value>::type> + /*inline*/ Handle(Handle handle); + + template + /*inline*/ static const Handle cast(Handle that); + + T* operator->() const; + T operator*() const; + + bool is_null() const; + + Address address(); + + private: + template + friend class Handle; + template + friend class MaybeHandle; +}; + +// A Handle can be converted into a MaybeHandle. Converting a MaybeHandle +// into a Handle requires checking that it does not point to nullptr. This +// ensures nullptr checks before use. +// +// Also note that Handles do not provide default equality comparison or hashing +// operators on purpose. Such operators would be misleading, because intended +// semantics is ambiguous between Handle location and object identity. +// Origin: +// https://github.com/v8/v8/blob/5792f3587116503fc047d2f68c951c72dced08a5/src/handles/maybe-handles.h#L15-L78 +template +class MaybeHandle final { + public: + MaybeHandle() = default; + + // Constructor for handling automatic up casting from Handle. + // Ex. Handle can be passed when MaybeHandle is expected. + template ::value>::type> + MaybeHandle(Handle handle); + + /*inline*/ Handle ToHandleChecked() const; + + // Convert to a Handle with a type that can be upcasted to. + template + /*inline*/ bool ToHandle(Handle* out) const; +}; + +// From v8/src/handles/handles-inl.h + +template +inline Handle handle(T object, Isolate* isolate) { + return Handle(object, isolate); +} + +// RAII Guard classes + +class DisallowHeapAllocation { + public: + DisallowHeapAllocation() {} + operator const JS::AutoAssertNoGC&() const { return no_gc_; } + + private: + const JS::AutoAssertNoGC no_gc_; +}; + +// This is used inside DisallowHeapAllocation regions to enable +// allocation just before throwing an exception, to allocate the +// exception object. Specifically, it only ever guards: +// - isolate->stack_guard()->HandleInterrupts() +// - isolate->StackOverflow() +// Those cases don't allocate in SpiderMonkey, so this can be a no-op. +class AllowHeapAllocation { + public: + // Empty constructor to avoid unused_variable warnings + AllowHeapAllocation() {} +}; + +class DisallowJavascriptExecution { + public: + DisallowJavascriptExecution(Isolate* isolate); + + private: + js::AutoAssertNoContentJS nojs_; +}; + +// Origin: +// https://github.com/v8/v8/blob/84f3877c15bc7f8956d21614da4311337525a3c8/src/objects/string.h#L83-L474 +class String : public HeapObject { + private: + JSString* str() const { return value_.toString(); } + + public: + operator JSString*() const { return str(); } + + // Max char codes. + static const int32_t kMaxOneByteCharCode = unibrow::Latin1::kMaxChar; + static const uint32_t kMaxOneByteCharCodeU = unibrow::Latin1::kMaxChar; + static const int kMaxUtf16CodeUnit = 0xffff; + static const uc32 kMaxCodePoint = 0x10ffff; + + MOZ_ALWAYS_INLINE int length() const { return str()->length(); } + uint16_t Get(uint32_t index); + bool IsFlat() { return str()->isLinear(); }; + + // These are only used in V8 functions that I want to rewrite. + // TODO: Rewrite those functions and delete this + bool IsConsString(); + bool IsExternalString(); + bool IsExternalOneByteString(); + bool IsExternalTwoByteString(); + bool IsSeqString(); + bool IsSeqOneByteString(); + bool IsSeqTwoByteString(); + bool IsSlicedString(); + bool IsThinString(); + + // Origin: + // https://github.com/v8/v8/blob/84f3877c15bc7f8956d21614da4311337525a3c8/src/objects/string.h#L95-L152 + class FlatContent { + public: + FlatContent(JSLinearString* string, const DisallowHeapAllocation& no_gc) + : string_(string), no_gc_(no_gc) {} + inline bool IsOneByte() const { return string_->hasLatin1Chars(); } + inline bool IsTwoByte() const { return !string_->hasLatin1Chars(); } + + Vector ToOneByteVector() const { + MOZ_ASSERT(IsOneByte()); + return Vector(string_->latin1Chars(no_gc_), + string_->length()); + } + Vector ToUC16Vector() const; + // TODO: twoByteChars returns char16_t*, but uc16 is uint16_t, which is + // not compatible :( :( :( + // { + // MOZ_ASSERT(IsTwoByte()); + // return Vector(string_->twoByteChars(no_gc_), + // string_->length()); + // } + private: + const JSLinearString* string_; + const JS::AutoAssertNoGC& no_gc_; + }; + FlatContent GetFlatContent(const DisallowHeapAllocation& no_gc) { + MOZ_ASSERT(IsFlat()); + return FlatContent(&str()->asLinear(), no_gc); + } + + static Handle Flatten(Isolate* isolate, Handle string); + + inline static String cast(Object object) { + String s; + s.value_ = JS::StringValue(JS::Value(object).toString()); + return s; + } + + inline static bool IsOneByteRepresentationUnderneath(String string) { + return string.str()->hasLatin1Chars(); + } + inline bool IsOneByteRepresentation() const { + return str()->hasLatin1Chars(); + } + + std::unique_ptr ToCString(); + + template + Vector GetCharVector(const DisallowHeapAllocation& no_gc); +}; + +// A flat string reader provides random access to the contents of a +// string independent of the character width of the string. The handle +// must be valid as long as the reader is being used. +// Origin: +// https://github.com/v8/v8/blob/84f3877c15bc7f8956d21614da4311337525a3c8/src/objects/string.h#L807-L825 +class MOZ_STACK_CLASS FlatStringReader { + public: + FlatStringReader(JSAtom* string) : string_(string) {} + int length() { return string_->length(); } + + inline char16_t Get(size_t index) { + return string_->latin1OrTwoByteChar(index); + } + + private: + JSAtom* string_; + JS::AutoCheckCannotGC nogc; +}; + +////////////////////////////////////////////////// +// TODO: Refactor NativeRegExpMacroAssembler and delete all of these: +class ConsString : public String { + public: + String first(); + String second(); + + static ConsString cast(Object object); +}; +class ExternalOneByteString : public String { + public: + const uint8_t* GetChars(); + static ExternalOneByteString cast(Object object); +}; +class ExternalTwoByteString : public String { + public: + const uc16* GetChars(); + static ExternalTwoByteString cast(Object object); +}; +class SeqOneByteString : public String { + public: + uint8_t* GetChars(const DisallowHeapAllocation& no_gc); + static SeqOneByteString cast(Object object); +}; +class SeqTwoByteString : public String { + public: + uc16* GetChars(const DisallowHeapAllocation& no_gc); + static SeqTwoByteString cast(Object object); + + static constexpr size_t kMaxCharsSize = JSString::MAX_LENGTH * 2; +}; +class SlicedString : public String { + public: + String parent(); + int offset(); + static SlicedString cast(Object object); +}; +class ThinString : public String { + public: + String actual(); + static ThinString cast(Object object); +}; +class StringShape { + public: + explicit StringShape(const String s); + bool IsCons(); + bool IsSliced(); + bool IsThin(); +}; +// End of "TODO: Delete all of these" +////////////////////////////////////////////////// + +class JSRegExp : public HeapObject { + public: + // ****************************************************** + // Methods that are called from inside the implementation + // ****************************************************** + void TierUpTick(); + bool MarkedForTierUp() const; + + Object Code(bool is_latin1) const; + Object Bytecode(bool is_latin1) const; + + uint32_t BacktrackLimit() const; + + static JSRegExp cast(Object object); + + // ****************************** + // Static constants + // ****************************** + + // Meaning of Type: + // NOT_COMPILED: Initial value. No data has been stored in the JSRegExp yet. + // ATOM: A simple string to match against using an indexOf operation. + // IRREGEXP: Compiled with Irregexp. + enum Type { NOT_COMPILED, ATOM, IRREGEXP }; + + // Maximum number of captures allowed. + static constexpr int kMaxCaptures = 1 << 16; + + // ************************************************** + // JSRegExp::Flags + // ************************************************** + + struct FlagShiftBit { + static constexpr int kGlobal = 0; + static constexpr int kIgnoreCase = 1; + static constexpr int kMultiline = 2; + static constexpr int kSticky = 3; + static constexpr int kUnicode = 4; + static constexpr int kDotAll = 5; + static constexpr int kInvalid = 6; + }; + enum Flag : uint8_t { + kNone = 0, + kGlobal = 1 << FlagShiftBit::kGlobal, + kIgnoreCase = 1 << FlagShiftBit::kIgnoreCase, + kMultiline = 1 << FlagShiftBit::kMultiline, + kSticky = 1 << FlagShiftBit::kSticky, + kUnicode = 1 << FlagShiftBit::kUnicode, + kDotAll = 1 << FlagShiftBit::kDotAll, + kInvalid = 1 << FlagShiftBit::kInvalid, // Not included in FlagCount. + }; + using Flags = base::Flags; + static constexpr int kFlagCount = 6; + + static constexpr int kNoBacktrackLimit = 0; +}; + +class Histogram { + public: + inline void AddSample(int sample) {} +}; + +class Counters { + public: + Histogram* regexp_backtracks() { return ®exp_backtracks_; } + + private: + Histogram regexp_backtracks_; +}; + +#define PROFILE(isolate, call) \ + do { \ + } while (false); + +enum class AllocationType : uint8_t { + kYoung, // Allocate in the nursery + kOld, // Allocate in the tenured heap +}; + +using StackGuard = Isolate; +using Factory = Isolate; + +class Isolate { + public: + //********** Isolate code **********// + RegExpStack* regexp_stack() const { return regexp_stack_; } + bool has_pending_exception() { return cx()->isExceptionPending(); } + void StackOverflow() { js::ReportOverRecursed(cx()); } + + unibrow::Mapping* jsregexp_uncanonicalize(); + unibrow::Mapping* + regexp_macro_assembler_canonicalize(); + unibrow::Mapping* jsregexp_canonrange(); + + // An empty stub for telemetry we don't support + void IncreaseTotalRegexpCodeGenerated(int size) {} + + Counters* counters() { return &counters_; } + + //********** Factory code **********// + inline Factory* factory() { return this; } + + Handle NewByteArray( + int length, AllocationType allocation = AllocationType::kYoung); + MOZ_MUST_USE MaybeHandle NewStringFromOneByte( + const Vector& str, + AllocationType allocation = AllocationType::kYoung); + + // Allocates a fixed array initialized with undefined values. + Handle NewFixedArray( + int length, AllocationType allocation = AllocationType::kYoung); + + template + Handle InternalizeString(const Vector& str); + + //********** Stack guard code **********// + inline StackGuard* stack_guard() { return this; } + Object HandleInterrupts() { + return Object(JS::BooleanValue(cx()->handleInterrupt())); + } + + JSContext* cx() const { return cx_; } + + private: + JSContext* cx_; + RegExpStack* regexp_stack_; + Counters counters_; +}; + +// Origin: +// https://github.com/v8/v8/blob/50dcf2af54ce27801a71c47c1be1d2c5e36b0dd6/src/execution/isolate.h#L1909-L1931 +class StackLimitCheck { + public: + StackLimitCheck(Isolate* isolate) : cx_(isolate->cx()) {} + + // Use this to check for stack-overflows in C++ code. + bool HasOverflowed() { return !CheckRecursionLimitDontReport(cx_); } + + // Use this to check for interrupt request in C++ code. + bool InterruptRequested() { return cx_->hasAnyPendingInterrupt(); } + + // Use this to check for stack-overflow when entering runtime from JS code. + bool JsHasOverflowed() { + return !CheckRecursionLimitConservativeDontReport(cx_); + } + + private: + JSContext* cx_; +}; + +class Code { + public: + bool operator!=(Code& other) const; + + Address raw_instruction_start(); + Address raw_instruction_end(); + Address address(); + + static Code cast(Object object); +}; + +// GeneratedCode provides an interface for calling into jit code. +// It will probably require additional work to hook this up to the +// arm simulator. +// Origin: +// https://github.com/v8/v8/blob/abfbe7687edb5b2dffe0b33b24e0a41bb86a8214/src/execution/simulator.h#L96-L164 +template +class GeneratedCode { + public: + using Signature = Return(Args...); + + static GeneratedCode FromCode(Code code); // TODO: implement + Return Call(Args... args) { return fn_ptr_(args...); } + + private: + friend class GeneratedCode; + Isolate* isolate_; + Signature* fn_ptr_; + GeneratedCode(Isolate* isolate, Signature* fn_ptr) + : isolate_(isolate), fn_ptr_(fn_ptr) {} +}; + +// Allow to use {GeneratedCode} instead of +// {GeneratedCode}. +template +class GeneratedCode : public GeneratedCode { + public: + // Automatically convert from {GeneratedCode} to + // {GeneratedCode}. + GeneratedCode(GeneratedCode other) + : GeneratedCode(other.isolate_, other.fn_ptr_) {} +}; + +enum class MessageTemplate { kStackOverflow }; + +class MessageFormatter { + public: + static const char* TemplateString(MessageTemplate index) { + switch (index) { + case MessageTemplate::kStackOverflow: + return "too much recursion"; + } + } +}; + +// Origin: https://github.com/v8/v8/blob/master/src/codegen/label.h +class Label { + public: + Label() : inner_(js::jit::Label()) {} + + operator js::jit::Label*() { return &inner_; } + + void Unuse() { inner_.reset(); } + + bool is_linked() { return inner_.used(); } + bool is_bound() { return inner_.bound(); } + bool is_unused() { return !inner_.used() && !inner_.bound(); } + + int pos() { return inner_.offset(); } + void link_to(int pos) { inner_.use(pos); } + void bind_to(int pos) { inner_.bind(pos); } + + private: + js::jit::Label inner_; +}; + +// TODO: Map flags to jitoptions +extern bool FLAG_correctness_fuzzer_suppressions; +extern bool FLAG_enable_regexp_unaligned_accesses; +extern bool FLAG_harmony_regexp_sequence; +extern bool FLAG_regexp_interpret_all; +extern bool FLAG_regexp_mode_modifiers; +extern bool FLAG_regexp_optimization; +extern bool FLAG_regexp_peephole_optimization; +extern bool FLAG_regexp_possessive_quantifier; +extern bool FLAG_regexp_tier_up; +extern bool FLAG_trace_regexp_assembler; +extern bool FLAG_trace_regexp_bytecodes; +extern bool FLAG_trace_regexp_parser; +extern bool FLAG_trace_regexp_peephole_optimization; + +} // namespace internal +} // namespace v8 + +#endif // RegexpShim_h diff --git a/js/src/regexp/regexp-stack.h b/js/src/regexp/regexp-stack.h index 2c2cf26304..812195ad12 100644 --- a/js/src/regexp/regexp-stack.h +++ b/js/src/regexp/regexp-stack.h @@ -5,6 +5,7 @@ #ifndef V8_REGEXP_REGEXP_STACK_H_ #define V8_REGEXP_REGEXP_STACK_H_ +#include "regexp/regexp-shim.h" namespace v8 { namespace internal { diff --git a/js/src/regexp/regexp.h b/js/src/regexp/regexp.h index 1ee10da5c4..cce58da384 100644 --- a/js/src/regexp/regexp.h +++ b/js/src/regexp/regexp.h @@ -5,6 +5,7 @@ #ifndef V8_REGEXP_REGEXP_H_ #define V8_REGEXP_REGEXP_H_ +#include "regexp/regexp-shim.h" namespace v8 { namespace internal { diff --git a/js/src/regexp/util/flags.h b/js/src/regexp/util/flags.h new file mode 100644 index 0000000000..1fa421fc0c --- /dev/null +++ b/js/src/regexp/util/flags.h @@ -0,0 +1,93 @@ +// Copyright 2014 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_UTIL_FLAGS_H_ +#define V8_UTIL_FLAGS_H_ + +// Origin: https://github.com/v8/v8/blob/1bafcc6b999b23ea1d394f5d267a08183e3c4e19/src/base/flags.h#L15-L90 + +namespace v8 { +namespace base { + +// The Flags class provides a type-safe way of storing OR-combinations of enum +// values. The Flags class is a template class, where T is an enum type, +// and S is the underlying storage type (usually int). +// +// The traditional C++ approach for storing OR-combinations of enum values is to +// use an int or unsigned int variable. The inconvenience with this approach is +// that there's no type checking at all; any enum value can be OR'd with any +// other enum value and passed on to a function that takes an int or unsigned +// int. +template +class Flags final { + public: + using flag_type = T; + using mask_type = S; + + constexpr Flags() : mask_(0) {} + constexpr Flags(flag_type flag) + : mask_(static_cast(flag)) {} + constexpr explicit Flags(mask_type mask) : mask_(static_cast(mask)) {} + + constexpr bool operator==(flag_type flag) const { + return mask_ == static_cast(flag); + } + constexpr bool operator!=(flag_type flag) const { + return mask_ != static_cast(flag); + } + + Flags& operator&=(const Flags& flags) { + mask_ &= flags.mask_; + return *this; + } + Flags& operator|=(const Flags& flags) { + mask_ |= flags.mask_; + return *this; + } + Flags& operator^=(const Flags& flags) { + mask_ ^= flags.mask_; + return *this; + } + + constexpr Flags operator&(const Flags& flags) const { + return Flags(mask_ & flags.mask_); + } + constexpr Flags operator|(const Flags& flags) const { + return Flags(mask_ | flags.mask_); + } + constexpr Flags operator^(const Flags& flags) const { + return Flags(mask_ ^ flags.mask_); + } + + Flags& operator&=(flag_type flag) { return operator&=(Flags(flag)); } + Flags& operator|=(flag_type flag) { return operator|=(Flags(flag)); } + Flags& operator^=(flag_type flag) { return operator^=(Flags(flag)); } + + constexpr Flags operator&(flag_type flag) const { + return operator&(Flags(flag)); + } + constexpr Flags operator|(flag_type flag) const { + return operator|(Flags(flag)); + } + constexpr Flags operator^(flag_type flag) const { + return operator^(Flags(flag)); + } + + constexpr Flags operator~() const { return Flags(~mask_); } + + constexpr operator mask_type() const { return mask_; } + constexpr bool operator!() const { return !mask_; } + + Flags without(flag_type flag) { return *this & (~Flags(flag)); } + + friend size_t hash_value(const Flags& flags) { return flags.mask_; } + + private: + mask_type mask_; +}; + +} // namespace base +} // namespace v8 + +#endif // V8_UTIL_FLAG_H_ diff --git a/js/src/regexp/util/vector.h b/js/src/regexp/util/vector.h new file mode 100644 index 0000000000..2419447d67 --- /dev/null +++ b/js/src/regexp/util/vector.h @@ -0,0 +1,204 @@ +// Copyright 2014 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_UTIL_VECTOR_H_ +#define V8_UTIL_VECTOR_H_ + +#include +#include +#include +#include + +#include "js/Utility.h" + +namespace v8 { +namespace internal { + +////////////////////////////////////////////////// + +// Adapted from: https://github.com/v8/v8/blob/5f69bbc233c2d1baf149faf869a7901603929914/src/utils/allocation.h#L36-L58 + +template +T* NewArray(size_t size) { + static_assert(std::is_pod::value, ""); + js::AutoEnterOOMUnsafeRegion oomUnsafe; + T* result = static_cast(js_malloc(size * sizeof(T))); + if (!result) { + oomUnsafe.crash("Irregexp NewArray"); + } + return result; +} + +template +void DeleteArray(T* array) { + js_free(array); +} + +////////////////////////////////////////////////// + +// A non-resizable vector containing a pointer and a length. +// The Vector may or may not own the pointer, depending on context. +// Origin: +// https://github.com/v8/v8/blob/5f69bbc233c2d1baf149faf869a7901603929914/src/utils/vector.h#L20-L134 + +template +class Vector { + public: + constexpr Vector() : start_(nullptr), length_(0) {} + + constexpr Vector(T* data, size_t length) : start_(data), length_(length) { + MOZ_ASSERT_IF(length != 0, data != nullptr); + } + + static Vector New(size_t length) { + return Vector(NewArray(length), length); + } + + // Returns a vector using the same backing storage as this one, + // spanning from and including 'from', to but not including 'to'. + Vector SubVector(size_t from, size_t to) const { + MOZ_ASSERT(from < to); + MOZ_ASSERT(to < length_); + return Vector(begin() + from, to - from); + } + + // Returns the length of the vector. Only use this if you really need an + // integer return value. Use {size()} otherwise. + int length() const { + MOZ_ASSERT(length_ <= std::numeric_limits::max()); + return static_cast(length_); + } + + // Returns the length of the vector as a size_t. + constexpr size_t size() const { return length_; } + + // Returns whether or not the vector is empty. + constexpr bool empty() const { return length_ == 0; } + + // Access individual vector elements - checks bounds in debug mode. + T& operator[](size_t index) const { + MOZ_ASSERT(index < length_); + return start_[index]; + } + + const T& at(size_t index) const { return operator[](index); } + + T& first() { return start_[0]; } + + T& last() { + MOZ_ASSERT(length_ > 0); + return start_[length_ - 1]; + } + + // Returns a pointer to the start of the data in the vector. + constexpr T* begin() const { return start_; } + + // Returns a pointer past the end of the data in the vector. + constexpr T* end() const { return start_ + length_; } + + // Returns a clone of this vector with a new backing store. + Vector Clone() const { + T* result = NewArray(length_); + for (size_t i = 0; i < length_; i++) result[i] = start_[i]; + return Vector(result, length_); + } + + void Truncate(size_t length) { + MOZ_ASSERT(length <= length_); + length_ = length; + } + + // Releases the array underlying this vector. Once disposed the + // vector is empty. + void Dispose() { + DeleteArray(start_); + start_ = nullptr; + length_ = 0; + } + + Vector operator+(size_t offset) { + MOZ_ASSERT(offset <= length_); + return Vector(start_ + offset, length_ - offset); + } + + Vector operator+=(size_t offset) { + MOZ_ASSERT(offset <= length_); + start_ += offset; + length_ -= offset; + return *this; + } + + // Implicit conversion from Vector to Vector. + inline operator Vector() const { + return Vector::cast(*this); + } + + template + static constexpr Vector cast(Vector input) { + return Vector(reinterpret_cast(input.begin()), + input.length() * sizeof(S) / sizeof(T)); + } + + bool operator==(const Vector other) const { + if (length_ != other.length_) return false; + if (start_ == other.start_) return true; + for (size_t i = 0; i < length_; ++i) { + if (start_[i] != other.start_[i]) { + return false; + } + } + return true; + } + + private: + T* start_; + size_t length_; +}; + +// The resulting vector does not contain a null-termination byte. If you want +// the null byte, use ArrayVector("foo"). +inline Vector CStrVector(const char* data) { + return Vector(data, strlen(data)); +} + +} // namespace internal + +namespace base { + +// SmallVector uses inline storage first, and reallocates when full. +// It is basically equivalent to js::Vector, and is implemented +// as a thin wrapper. +// V8's implementation: https://github.com/v8/v8/blob/master/src/base/small-vector.h +template +class SmallVector { +public: + inline bool empty() const { return inner_.empty(); } + inline const T& back() const { return inner_.back(); } + inline void pop_back() { inner_.popBack(); }; + template + inline void emplace_back(Args&&... args) { + js::AutoEnterOOMUnsafeRegion oomUnsafe; + if (!inner_.emplaceBack(args...)) { + oomUnsafe.crash("Irregexp SmallVector emplace_back"); + } + }; + inline size_t size() const { return inner_.length(); } + inline const T& at(size_t index) const { return inner_[index]; } + + void resize_no_init(size_t new_size) { + js::AutoEnterOOMUnsafeRegion oomUnsafe; + if (!inner_.resizeUninitialized(new_size)) { + oomUnsafe.crash("Irregexp SmallVector resize"); + } + } +private: + js::Vector inner_; +}; + + +} // namespace base + +} // namespace v8 + +#endif // V8_UTIL_VECTOR_H_ diff --git a/js/src/regexp/util/zone.h b/js/src/regexp/util/zone.h new file mode 100644 index 0000000000..32487f06a6 --- /dev/null +++ b/js/src/regexp/util/zone.h @@ -0,0 +1,356 @@ +// Copyright 2019 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_UTIL_ZONE_H_ +#define V8_UTIL_ZONE_H_ + +#include +#include +#include +#include +#include + +#include "ds/LifoAlloc.h" +#include "regexp/util/vector.h" + +namespace v8 { +namespace internal { + +// V8::Zone ~= LifoAlloc +class Zone { + public: + Zone(size_t defaultChunkSize) : lifoAlloc_(defaultChunkSize) { + lifoAlloc_.setAsInfallibleByDefault(); + } + + void* New(size_t size) { return lifoAlloc_.alloc(size); } + + void DeleteAll() { lifoAlloc_.freeAll(); } + + // Returns true if the total memory allocated exceeds a threshold. + static const size_t kExcessLimit = 256 * 1024 * 1024; + bool excess_allocation() const { + return lifoAlloc_.peakSizeOfExcludingThis() > kExcessLimit; + } +private: + js::LifoAlloc lifoAlloc_; +}; + +// Superclass for classes allocated in a Zone. +// Origin: https://github.com/v8/v8/blob/7b3332844212d78ee87a9426f3a6f7f781a8fbfa/src/zone/zone.h#L138-L155 +class ZoneObject { + public: + // Allocate a new ZoneObject of 'size' bytes in the Zone. + void* operator new(size_t size, Zone* zone) { return zone->New(size); } + + // Ideally, the delete operator should be private instead of + // public, but unfortunately the compiler sometimes synthesizes + // (unused) destructors for classes derived from ZoneObject, which + // require the operator to be visible. MSVC requires the delete + // operator to be public. + + // ZoneObjects should never be deleted individually; use + // Zone::DeleteAll() to delete all zone objects in one go. + void operator delete(void*, size_t) { MOZ_CRASH("unreachable"); } + void operator delete(void* pointer, Zone* zone) { MOZ_CRASH("unreachable"); } +}; + +// ZoneLists are growable lists with constant-time access to the +// elements. The list itself and all its elements are allocated in the +// Zone. ZoneLists cannot be deleted individually; you can delete all +// objects in the Zone by calling Zone::DeleteAll(). +// Used throughout irregexp. +// Origin: https://github.com/v8/v8/blob/5e514a969376dc63517d575b062758efd36cd757/src/zone/zone.h#L173-L318 +// Inlines: https://github.com/v8/v8/blob/5e514a969376dc63517d575b062758efd36cd757/src/zone/zone-list-inl.h#L17-L155 +template +class ZoneList final { + public: + // Construct a new ZoneList with the given capacity; the length is + // always zero. The capacity must be non-negative. + ZoneList(int capacity, Zone* zone) { Initialize(capacity, zone); } + // Construct a new ZoneList from a std::initializer_list + ZoneList(std::initializer_list list, Zone* zone) { + Initialize(static_cast(list.size()), zone); + for (auto& i : list) Add(i, zone); + } + // Construct a new ZoneList by copying the elements of the given ZoneList. + ZoneList(const ZoneList& other, Zone* zone) { + Initialize(other.length(), zone); + AddAll(other, zone); + } + + void* operator new(size_t size, Zone* zone) { return zone->New(size); } + + // Returns a reference to the element at index i. This reference is not safe + // to use after operations that can change the list's backing store + // (e.g. Add). + inline T& operator[](int i) const { + MOZ_ASSERT(0 < i); + MOZ_ASSERT(static_cast(i) < static_cast(length_)); + return data_[i]; + } + inline T& at(int i) const { return operator[](i); } + inline T& last() const { return at(length_ - 1); } + inline T& first() const { return at(0); } + + using iterator = T*; + inline iterator begin() const { return &data_[0]; } + inline iterator end() const { return &data_[length_]; } + + inline bool is_empty() const { return length_ == 0; } + inline int length() const { return length_; } + inline int capacity() const { return capacity_; } + + Vector ToVector() const { return Vector(data_, length_); } + Vector ToVector(int start, int length) const { + return Vector(data_ + start, std::min(length_ - start, length)); + } + + Vector ToConstVector() const { + return Vector(data_, length_); + } + + inline void Initialize(int capacity, Zone* zone) { + MOZ_ASSERT(capacity >= 0); + data_ = (capacity > 0) ? NewData(capacity, zone) : nullptr; + capacity_ = capacity; + length_ = 0; + } + + // Adds a copy of the given 'element' to the end of the list, + // expanding the list if necessary. + void Add(const T& element, Zone* zone) { + if (length_ < capacity_) { + data_[length_++] = element; + } else { + ZoneList::ResizeAdd(element, zone); + } + } + // Add all the elements from the argument list to this list. + void AddAll(const ZoneList& other, Zone* zone) { + AddAll(other.ToVector(), zone); + } + // Add all the elements from the vector to this list. + void AddAll(const Vector& other, Zone* zone) { + int result_length = length_ + other.length(); + if (capacity_ < result_length) { + Resize(result_length, zone); + } + if (std::is_fundamental()) { + memcpy(data_ + length_, other.begin(), sizeof(*data_) * other.length()); + } else { + for (int i = 0; i < other.length(); i++) { + data_[length_ + i] = other.at(i); + } + } + length_ = result_length; + } + + // Overwrites the element at the specific index. + void Set(int index, const T& element) { + MOZ_ASSERT(index >= 0 && index <= length_); + data_[index] = element; + } + + // Removes the i'th element without deleting it even if T is a + // pointer type; moves all elements above i "down". Returns the + // removed element. This function's complexity is linear in the + // size of the list. + T Remove(int i) { + T element = at(i); + length_--; + while (i < length_) { + data_[i] = data_[i + 1]; + i++; + } + return element; + } + + // Removes the last element without deleting it even if T is a + // pointer type. Returns the removed element. + inline T RemoveLast() { return Remove(length_ - 1); } + + // Clears the list by freeing the storage memory. If you want to keep the + // memory, use Rewind(0) instead. Be aware, that even if T is a + // pointer type, clearing the list doesn't delete the entries. + inline void Clear() { + data_ = nullptr; + capacity_ = 0; + length_ = 0; + } + + // Drops all but the first 'pos' elements from the list. + inline void Rewind(int pos) { + MOZ_ASSERT(0 <= pos && pos <= length_); + length_ = pos; + } + + inline bool Contains(const T& elm) const { + for (int i = 0; i < length_; i++) { + if (data_[i] == elm) return true; + } + return false; + } + + template + void StableSort(CompareFunction cmp, size_t s, size_t l) { + std::stable_sort(begin() + s, begin() + s + l, + [cmp](const T& a, const T& b) { return cmp(&a, &b) < 0; }); + } + + void operator delete(void* pointer) { MOZ_CRASH("unreachable"); } + void operator delete(void* pointer, Zone* zone) { MOZ_CRASH("unreachable"); } + + private: + T* data_; + int capacity_; + int length_; + + inline T* NewData(int n, Zone* zone) { + return static_cast(zone->New(n * sizeof(T))); + } + + // Increase the capacity of a full list, and add an element. + // List must be full already. + void ResizeAdd(const T& element, Zone* zone) { + MOZ_ASSERT(length_ >= capacity_); + // Grow the list capacity by 100%, but make sure to let it grow + // even when the capacity is zero (possible initial case). + int new_capacity = 1 + 2 * capacity_; + // Since the element reference could be an element of the list, copy + // it out of the old backing storage before resizing. + T temp = element; + Resize(new_capacity, zone); + data_[length_++] = temp; + } + + // Resize the list. + void Resize(int new_capacity, Zone* zone) { + MOZ_ASSERT(length_ <= new_capacity); + T* new_data = NewData(new_capacity, zone); + if (length_ > 0) { + memcpy(new_data, data_, length_ * sizeof(T)); + } + data_ = new_data; + capacity_ = new_capacity; + } + + ZoneList& operator=(const ZoneList&) = delete; + ZoneList() = delete; + ZoneList(const ZoneList&) = delete; +}; + +// Origin: https://github.com/v8/v8/blob/5e514a969376dc63517d575b062758efd36cd757/src/zone/zone-allocator.h#L14-L77 +template +class ZoneAllocator { +public: + using pointer = T*; + using const_pointer = const T*; + using reference = T&; + using const_reference = const T&; + using value_type = T; + using size_type = size_t; + using difference_type = ptrdiff_t; + template + struct rebind { + using other = ZoneAllocator; + }; + + explicit ZoneAllocator(Zone* zone) : zone_(zone) {} + template + ZoneAllocator(const ZoneAllocator& other) + : ZoneAllocator(other.zone_) {} + template + friend class ZoneAllocator; + + T* allocate(size_t n) { return static_cast(zone_->New(n * sizeof(T))); } + void deallocate(T* p, size_t) {} // noop for zones + + bool operator==(ZoneAllocator const& other) const { + return zone_ == other.zone_; + } + bool operator!=(ZoneAllocator const& other) const { + return zone_ != other.zone_; + } + +private: + Zone* zone_; +}; + +// Zone wrappers for std containers: +// Origin: https://github.com/v8/v8/blob/5e514a969376dc63517d575b062758efd36cd757/src/zone/zone-containers.h#L25-L169 + +// A wrapper subclass for std::vector to make it easy to construct one +// that uses a zone allocator. +// Used throughout irregexp +template +class ZoneVector : public std::vector> { +public: + ZoneVector(Zone* zone) + : std::vector>(ZoneAllocator(zone)) {} + + // Constructs a new vector and fills it with the contents of the range + // [first, last). + template + ZoneVector(Iter first, Iter last, Zone* zone) + : std::vector>(first, last, ZoneAllocator(zone)) {} +}; + +// A wrapper subclass for std::list to make it easy to construct one +// that uses a zone allocator. +// Used in regexp-bytecode-peephole.cc +template +class ZoneLinkedList : public std::list> { + public: + // Constructs an empty list. + explicit ZoneLinkedList(Zone* zone) + : std::list>(ZoneAllocator(zone)) {} +}; + +// A wrapper subclass for std::set to make it easy to construct one that uses +// a zone allocator. +// Used in regexp-parser.cc +template > +class ZoneSet : public std::set> { + public: + // Constructs an empty set. + explicit ZoneSet(Zone* zone) + : std::set>(Compare(), + ZoneAllocator(zone)) {} +}; + +// A wrapper subclass for std::map to make it easy to construct one that uses +// a zone allocator. +// Used in regexp-bytecode-peephole.cc +template > +class ZoneMap + : public std::map>> { + public: + // Constructs an empty map. + explicit ZoneMap(Zone* zone) + : std::map>>( + Compare(), ZoneAllocator>(zone)) {} +}; + +// A wrapper subclass for std::unordered_map to make it easy to construct one +// that uses a zone allocator. +// Used in regexp-bytecode-peephole.cc +template , + typename KeyEqual = std::equal_to> +class ZoneUnorderedMap + : public std::unordered_map>> { + public: + // Constructs an empty map. + explicit ZoneUnorderedMap(Zone* zone, size_t bucket_count = 100) + : std::unordered_map>>( + bucket_count, Hash(), KeyEqual(), + ZoneAllocator>(zone)) {} +}; + +} // namespace internal +} // namespace v8 + +#endif // V8_UTIL_FLAG_H_ From 12c7f5df33ee7f668d927bb217464cdfbe58e11d Mon Sep 17 00:00:00 2001 From: "Matt A. Tobin" Date: Mon, 9 Nov 2020 20:26:59 -0500 Subject: [PATCH 2/9] Issue #1677 - Part 4: Implement shim for regexp --- js/src/regexp/moz.build | 7 +- js/src/regexp/regexp-shim.cc | 147 ++- js/src/regexp/regexp-shim.h | 589 +++++++---- js/src/regexp/special-case.cc | 38 + js/src/regexp/util/unicode.cc | 1865 +++++++++++++++++++++++++++++++++ js/src/regexp/util/zone.h | 29 +- 6 files changed, 2449 insertions(+), 226 deletions(-) create mode 100644 js/src/regexp/special-case.cc create mode 100644 js/src/regexp/util/unicode.cc diff --git a/js/src/regexp/moz.build b/js/src/regexp/moz.build index f8fc99921e..1ef280b7fa 100644 --- a/js/src/regexp/moz.build +++ b/js/src/regexp/moz.build @@ -23,9 +23,14 @@ SOURCES += [ 'regexp-macro-assembler-tracer.cc', 'regexp-macro-assembler.cc', 'regexp-parser.cc', + 'regexp-shim.cc', 'regexp-stack.cc', + 'util/unicode.cc' ] if CONFIG['ENABLE_INTL_API']: CXXFLAGS += ['-DV8_INTL_SUPPORT'] - SOURCES += ['property-sequences.cc'] \ No newline at end of file + SOURCES += [ + 'property-sequences.cc', + 'special-case.cc' + ] \ No newline at end of file diff --git a/js/src/regexp/regexp-shim.cc b/js/src/regexp/regexp-shim.cc index 5e1da7a605..773c17eadd 100644 --- a/js/src/regexp/regexp-shim.cc +++ b/js/src/regexp/regexp-shim.cc @@ -8,6 +8,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include + #include "regexp/regexp-shim.h" namespace v8 { @@ -27,17 +29,24 @@ void PrintF(FILE* out, const char* format, ...) { va_end(arguments); } +StdoutStream::operator std::ostream&() const { return std::cerr; } + +template +std::ostream& StdoutStream::operator<<(T t) { return std::cerr << t; } + +template std::ostream& StdoutStream::operator<<(char const* c); + // Origin: // https://github.com/v8/v8/blob/855591a54d160303349a5f0a32fab15825c708d1/src/utils/ostreams.cc#L120-L169 // (This is a hand-simplified version.) // Writes the given character to the output escaping everything outside // of printable ASCII range. std::ostream& operator<<(std::ostream& os, const AsUC16& c) { - uint16_t v = c.value; + uc16 v = c.value; bool isPrint = 0x20 < v && v <= 0x7e; char buf[10]; const char* format = isPrint ? "%c" : (v <= 0xFF) ? "\\x%02x" : "\\u%04x"; - snprintf(buf, sizeof(buf), format, v); + SprintfLiteral(buf, format, v); return os << buf; } std::ostream& operator<<(std::ostream& os, const AsUC32& c) { @@ -46,12 +55,136 @@ std::ostream& operator<<(std::ostream& os, const AsUC32& c) { return os << AsUC16(v); } char buf[13]; - snprintf(buf, sizeof(buf), "\\u{%06x}", v); + SprintfLiteral(buf, "\\u{%06x}", v); return os << buf; } -DisallowJavascriptExecution::DisallowJavascriptExecution(Isolate* isolate) - : nojs_(isolate->cx()) {} +HandleScope::HandleScope(Isolate* isolate) + : isolate_(isolate) { + isolate->openHandleScope(*this); +} + +HandleScope::~HandleScope() { + isolate_->closeHandleScope(level_, non_gc_level_); +} + +template +Handle::Handle(T object, Isolate* isolate) + : location_(isolate->getHandleLocation(JS::Value(object))) {} + +template Handle::Handle(ByteArray b, Isolate* isolate); +template Handle::Handle(String s, Isolate* isolate); + +template +Handle::Handle(JS::Value value, Isolate* isolate) + : location_(isolate->getHandleLocation(value)) { + T::cast(Object(value)); // Assert that value has the correct type. +} + +JS::Value* Isolate::getHandleLocation(JS::Value value) { + js::AutoEnterOOMUnsafeRegion oomUnsafe; + if (!handleArena_.Append(value)) { + oomUnsafe.crash("Irregexp handle allocation"); + } + return &handleArena_.GetLast(); +} + +void* Isolate::allocatePseudoHandle(size_t bytes) { + PseudoHandle ptr; + ptr.reset(js_malloc(bytes)); + if (!ptr) { + return nullptr; + } + if (!uniquePtrArena_.Append(std::move(ptr))) { + return nullptr; + } + return uniquePtrArena_.GetLast().get(); +} + +template +PseudoHandle Isolate::takeOwnership(void* ptr) { + for (auto iter = uniquePtrArena_.IterFromLast(); !iter.Done(); iter.Prev()) { + auto& entry = iter.Get(); + if (entry.get() == ptr) { + PseudoHandle result; + result.reset(static_cast(entry.release())); + return result; + } + } + MOZ_CRASH("Tried to take ownership of pseudohandle that is not in the arena"); +} + +PseudoHandle ByteArray::takeOwnership(Isolate* isolate) { + PseudoHandle result = + isolate->takeOwnership(value_.toPrivate()); + value_ = JS::PrivateValue(nullptr); + return result; +} + +void Isolate::trace(JSTracer* trc) { + js::gc::AssertRootMarkingPhase(trc); + + for (auto iter = handleArena_.Iter(); !iter.Done(); iter.Next()) { + auto& elem = iter.Get(); + JS::GCPolicy::trace(trc, &elem, "Isolate handle arena"); + } +} + +/*static*/ Handle String::Flatten(Isolate* isolate, + Handle string) { + if (string->IsFlat()) { + return string; + } + js::AutoEnterOOMUnsafeRegion oomUnsafe; + JSLinearString* linear = string->str()->ensureLinear(isolate->cx()); + if (!linear) { + oomUnsafe.crash("Irregexp String::Flatten"); + } + return Handle(JS::StringValue(linear), isolate); +} + +// This is only used for trace messages printing the source of a +// regular expression. To keep things simple, we just return an +// empty string and don't print anything. +std::unique_ptr String::ToCString() { + return std::unique_ptr(); +} + +Handle Isolate::NewByteArray(int length, AllocationType alloc) { + MOZ_RELEASE_ASSERT(length >= 0); + + js::AutoEnterOOMUnsafeRegion oomUnsafe; + + size_t alloc_size = sizeof(uint32_t) + length; + ByteArrayData* data = + static_cast(allocatePseudoHandle(alloc_size)); + if (!data) { + oomUnsafe.crash("Irregexp NewByteArray"); + } + data->length = length; + + return Handle(JS::PrivateValue(data), this); +} + +Handle Isolate::NewFixedArray(int length) { + MOZ_RELEASE_ASSERT(length >= 0); + MOZ_CRASH("TODO"); +} + +template +Handle Isolate::InternalizeString(const Vector& str) { + js::AutoEnterOOMUnsafeRegion oomUnsafe; + JSAtom* atom = js::AtomizeChars(cx(), str.begin(), str.length()); + if (!atom) { + oomUnsafe.crash("Irregexp InternalizeString"); + } + return Handle(JS::StringValue(atom), this); +} + +template Handle +Isolate::InternalizeString(const Vector& str); +template Handle +Isolate::InternalizeString(const Vector& str); // TODO: Map flags to jitoptions bool FLAG_correctness_fuzzer_suppressions = false; @@ -59,8 +192,8 @@ bool FLAG_enable_regexp_unaligned_accesses = false; bool FLAG_harmony_regexp_sequence = false; bool FLAG_regexp_interpret_all = false; bool FLAG_regexp_mode_modifiers = false; -bool FLAG_regexp_optimization = false; -bool FLAG_regexp_peephole_optimization = false; +bool FLAG_regexp_optimization = true; +bool FLAG_regexp_peephole_optimization = true; bool FLAG_regexp_possessive_quantifier = false; bool FLAG_regexp_tier_up = false; bool FLAG_trace_regexp_assembler = false; diff --git a/js/src/regexp/regexp-shim.h b/js/src/regexp/regexp-shim.h index 0497ee552d..38b0357272 100644 --- a/js/src/regexp/regexp-shim.h +++ b/js/src/regexp/regexp-shim.h @@ -15,9 +15,11 @@ #include "mozilla/Attributes.h" #include "mozilla/MathAlgorithms.h" #include "mozilla/Maybe.h" +#include "mozilla/SegmentedVector.h" #include "mozilla/Types.h" #include +#include #include "jit/Label.h" #include "js/Value.h" @@ -127,6 +129,12 @@ using byte = uint8_t; using Address = uintptr_t; static const Address kNullAddress = 0; +// Latin1/UTF-16 constants +// Code-point values in Unicode 4.0 are 21 bits wide. +// Code units in UTF-16 are 16 bits wide. +using uc16 = char16_t; +using uc32 = int32_t; + namespace base { // Origin: @@ -161,12 +169,36 @@ inline uint8_t saturated_cast(int x) { return (x >= 0) ? ((x < 255) ? uint8_t(x) : 255) : 0; } +#define LAZY_INSTANCE_INITIALIZER { mozilla::Nothing() } + +template +struct LazyInstanceImpl { + mozilla::Maybe value_; + T* Pointer() { + if (value_.isNothing()) { + value_.emplace(); + } + return value_.ptr(); + } +}; + +template +class LazyInstance { +public: + using type = LazyInstanceImpl; +}; + + namespace bits { inline uint64_t CountTrailingZeros(uint64_t value) { return mozilla::CountTrailingZeroes64(value); } +inline size_t RoundUpToPowerOfTwo32(size_t value) { + return mozilla::RoundUpPow2(value); +} + } // namespace bits } // namespace base @@ -178,10 +210,10 @@ using uchar = unsigned int; // https://github.com/v8/v8/blob/1f1e4cdb04c75eab77adbecd5f5514ddc3eb56cf/src/strings/unicode.h#L133-L150 class Latin1 { public: - static const uint16_t kMaxChar = 0xff; + static const uc16 kMaxChar = 0xff; // Convert the character to Latin-1 case equivalent if possible. - static inline uint16_t TryConvertToLatin1(uint16_t c) { + static inline uc16 TryConvertToLatin1(uc16 c) { // "GREEK CAPITAL LETTER MU" case maps to "MICRO SIGN". // "GREEK SMALL LETTER MU" case maps to "MICRO SIGN". if (c == 0x039C || c == 0x03BC) { @@ -206,10 +238,10 @@ class Utf16 { static inline bool IsTrailSurrogate(int code) { return js::unicode::IsTrailSurrogate(code); } - static inline uint16_t LeadSurrogate(uint32_t char_code) { + static inline uc16 LeadSurrogate(uint32_t char_code) { return js::unicode::LeadSurrogate(char_code); } - static inline uint16_t TrailSurrogate(uint32_t char_code) { + static inline uc16 TrailSurrogate(uint32_t char_code) { return js::unicode::TrailSurrogate(char_code); } static inline uint32_t CombineSurrogatePair(char16_t lead, char16_t trail) { @@ -218,6 +250,8 @@ class Utf16 { static const uchar kMaxNonSurrogateCharCode = 0xffff; }; +#ifndef V8_INTL_SUPPORT + // A cache used in case conversion. It caches the value for characters // that either have no mapping or map to a single character independent // of context. Characters that map to more than one character or that @@ -228,11 +262,37 @@ template class Mapping { public: inline Mapping() = default; - int get(uchar c, uchar n, uchar* result); + inline int get(uchar c, uchar n, uchar* result) { + CacheEntry entry = entries_[c & kMask]; + if (entry.code_point_ == c) { + if (entry.offset_ == 0) { + return 0; + } else { + result[0] = c + entry.offset_; + return 1; + } + } else { + return CalculateValue(c, n, result); + } + } private: - friend class Test; - int CalculateValue(uchar c, uchar n, uchar* result); + int CalculateValue(uchar c, uchar n, uchar* result) { + bool allow_caching = true; + int length = T::Convert(c, n, result, &allow_caching); + if (allow_caching) { + if (length == 1) { + entries_[c & kMask] = CacheEntry(c, result[0] - c); + return 1; + } else { + entries_[c & kMask] = CacheEntry(c, 0); + return 0; + } + } else { + return length; + } + } + struct CacheEntry { inline CacheEntry() : code_point_(kNoChar), offset_(0) {} inline CacheEntry(uchar code_point, signed offset) @@ -253,7 +313,7 @@ struct Ecma262Canonicalize { static int Convert(uchar c, uchar n, uchar* result, bool* allow_caching_ptr); }; struct Ecma262UnCanonicalize { - static const int kMaxWidth = 1; + static const int kMaxWidth = 4; static int Convert(uchar c, uchar n, uchar* result, bool* allow_caching_ptr); }; struct CanonicalizationRange { @@ -261,6 +321,12 @@ struct CanonicalizationRange { static int Convert(uchar c, uchar n, uchar* result, bool* allow_caching_ptr); }; +#endif // !V8_INTL_SUPPORT + +struct Letter { + static bool Is(uchar c); +}; + } // namespace unibrow namespace internal { @@ -306,12 +372,6 @@ constexpr int kSystemPointerSize = sizeof(void*); // representable as a Number value. ES6 section 20.1.2.6 constexpr double kMaxSafeInteger = 9007199254740991.0; // 2^53-1 -// Latin1/UTF-16 constants -// Code-point values in Unicode 4.0 are 21 bits wide. -// Code units in UTF-16 are 16 bits wide. -using uc16 = uint16_t; -using uc32 = int32_t; - constexpr int kBitsPerByte = 8; constexpr int kBitsPerByteLog2 = 3; constexpr int kUInt32Size = sizeof(uint32_t); @@ -328,10 +388,10 @@ inline bool IsIdentifierPart(uc32 c) { return js::unicode::IsIdentifierPart(uint32_t(c)); } -// Wrappers to disambiguate uint16_t and uc16. +// Wrappers to disambiguate char16_t and uc16. struct AsUC16 { - explicit AsUC16(uint16_t v) : value(v) {} - uint16_t value; + explicit AsUC16(char16_t v) : value(v) {} + char16_t value; }; struct AsUC32 { @@ -339,11 +399,21 @@ struct AsUC32 { int32_t value; }; -class StdoutStream : public std::ostream {}; - std::ostream& operator<<(std::ostream& os, const AsUC16& c); std::ostream& operator<<(std::ostream& os, const AsUC32& c); +// This class is used for the output of trace-regexp-parser. V8 has +// an elaborate implementation to ensure that the output gets to the +// right place, even on Android. We just need something that will +// print output (ideally to stderr, to match the rest of our tracing +// code). This is an empty wrapper that will convert itself to +// std::cerr when used. +class StdoutStream { +public: + operator std::ostream&() const; + template std::ostream& operator<<(T t); +}; + // Reuse existing Maybe implementation using mozilla::Maybe; @@ -357,6 +427,10 @@ mozilla::Nothing Nothing() { return mozilla::Nothing(); } + +template +using PseudoHandle = mozilla::UniquePtr; + // Origin: // https://github.com/v8/v8/blob/855591a54d160303349a5f0a32fab15825c708d1/src/utils/utils.h#L600-L642 // Compare 8bit/16bit chars to 8bit/16bit chars. @@ -388,16 +462,16 @@ inline int CompareChars(const lchar* lhs, const rchar* rhs, size_t chars) { reinterpret_cast(rhs), chars); } else { return CompareCharsUnsigned(reinterpret_cast(lhs), - reinterpret_cast(rhs), + reinterpret_cast(rhs), chars); } } else { if (sizeof(rchar) == 1) { - return CompareCharsUnsigned(reinterpret_cast(lhs), + return CompareCharsUnsigned(reinterpret_cast(lhs), reinterpret_cast(rhs), chars); } else { - return CompareCharsUnsigned(reinterpret_cast(lhs), - reinterpret_cast(rhs), + return CompareCharsUnsigned(reinterpret_cast(lhs), + reinterpret_cast(rhs), chars); } } @@ -434,14 +508,6 @@ class Object { // idiom: return false iff we are throwing an exception. inline bool IsException(Isolate*) const { return !value_.toBoolean(); } - // SpiderMonkey tries to avoid leaking the internal representation of its - // objects. V8 is not so strict. These functions are used when calling / - // being called by native code: objects are converted to Addresses for the - // call, then cast back to objects on the other side. - // We might be able to upstream a patch that eliminates the need for these. - Object(Address bits); - Address ptr() const; - protected: JS::Value value_; }; @@ -460,77 +526,162 @@ class Smi : public Object { // V8::HeapObject ~= JSObject class HeapObject : public Object { -public: - // Only used for bookkeeping of total code generated in regexp-compiler. - // We may be able to refactor this away. - int Size() const; -}; - -// A fixed-size array with Objects (aka Values) as element types -// Implemented as a wrapper around a regular native object with dense elements. -class FixedArray : public HeapObject { public: - inline void set(uint32_t index, Object value) { - JS::Value(*this).toObject().as().setDenseElement(index, - value); + inline static HeapObject cast(Object object) { + HeapObject h; + h.value_ = JS::Value(object); + return h; } }; -// A fixed-size array of bytes. -// TODO: figure out the best implementation for this. Uint8Array might work, -// but it's not currently visible outside of TypedArrayObject.cpp. -class ByteArray : public HeapObject { +// A fixed-size array with Objects (aka Values) as element types +// Only used for named captures. Allocated during parsing, so +// can't be a GC thing. +// TODO: implement. +class FixedArray : public HeapObject { public: - uint8_t get(uint32_t index); - void set(uint32_t index, uint8_t val); - uint32_t length(); - byte* GetDataStartAddress(); - byte* GetDataEndAddress(); + inline void set(uint32_t index, Object value) {} + inline static FixedArray cast(Object object) { MOZ_CRASH("TODO"); } +}; - static ByteArray cast(Object object); +class ByteArrayData { +public: + uint32_t length; + uint8_t* data(); +}; + +/* + * Conceptually, ByteArrayData is a variable-size structure. To + * implement this in a C++-approved way, we allocate a struct + * containing the 32-bit length field, followed by additional memory + * for the data. To access the data, we get a pointer to the next byte + * after the length field and cast it to the correct type. + */ +inline uint8_t* ByteArrayData::data() { + static_assert(alignof(uint8_t) <= alignof(ByteArrayData), + "The trailing data must be aligned to start immediately " + "after the header with no padding."); + ByteArrayData* immediatelyAfter = this + 1; + return reinterpret_cast(immediatelyAfter); +} + +// A fixed-size array of bytes. +class ByteArray : public HeapObject { + ByteArrayData* inner() const { + return static_cast(value_.toPrivate()); + } + PseudoHandle takeOwnership(Isolate* isolate); + + friend class SMRegExpMacroAssembler; +public: + byte get(uint32_t index) { + MOZ_ASSERT(index < length()); + return inner()->data()[index]; + } + void set(uint32_t index, byte val) { + MOZ_ASSERT(index < length()); + inner()->data()[index] = val; + } + uint32_t length() const { return inner()->length; } + byte* GetDataStartAddress() { return inner()->data(); } + + static ByteArray cast(Object object) { + ByteArray b; + b.value_ = JS::Value(object); + return b; + } }; // Like Handles in SM, V8 handles are references to marked pointers. // Unlike SM, where Rooted pointers are created individually on the -// stack, the target of a V8 handle lives in a HandleScope. -// HandleScopes are created on the stack and register themselves with -// the isolate (~= JSContext). Whenever a Handle is created, the -// outermost HandleScope is retrieved from the isolate, and a new root -// is created in that HandleScope. The Handle remains valid for the -// lifetime of the HandleScope. -class HandleScope { - public: +// stack, the target of a V8 handle lives in an arena on the isolate +// (~= JSContext). Whenever a Handle is created, a new "root" is +// created at the end of the arena. +// +// HandleScopes are used to manage the lifetimes of these handles. A +// HandleScope lives on the stack and stores the size of the arena at +// the time of its creation. When the function returns and the +// HandleScope is destroyed, the arena is truncated to its previous +// size, clearing all roots that were created since the creation of +// the HandleScope. +// +// In some cases, objects that are GC-allocated in V8 are not in SM. +// In particular, irregexp allocates ByteArrays during code generation +// to store lookup tables. This does not play nicely with the SM +// macroassembler's requirement that no GC allocations take place +// while it is on the stack. To work around this, this shim layer also +// provides the ability to create pseudo-handles, which are not +// managed by the GC but provide the same API to irregexp. The "root" +// of a pseudohandle is a unique pointer living in a second arena. If +// the allocated object should outlive the HandleScope, it must be +// manually moved out of the arena using takeOwnership. + +class MOZ_STACK_CLASS HandleScope { +public: HandleScope(Isolate* isolate); + ~HandleScope(); + + private: + size_t level_; + size_t non_gc_level_; + Isolate* isolate_; + + friend class Isolate; }; // Origin: // https://github.com/v8/v8/blob/5792f3587116503fc047d2f68c951c72dced08a5/src/handles/handles.h#L88-L171 template -class Handle { +class MOZ_NONHEAP_CLASS Handle { public: - Handle(); + Handle() : location_(nullptr) {} Handle(T object, Isolate* isolate); + Handle(JS::Value value, Isolate* isolate); // Constructor for handling automatic up casting. template ::value>::type> - /*inline*/ Handle(Handle handle); + inline Handle(Handle handle) : location_(handle.location_) {} template - /*inline*/ static const Handle cast(Handle that); + inline static const Handle cast(Handle that) { + return Handle(that.location_); + } - T* operator->() const; - T operator*() const; + inline bool is_null() const { return location_ == nullptr; } - bool is_null() const; + inline T operator*() const { + return T::cast(Object(*location_)); + }; - Address address(); + // {ObjectRef} is returned by {Handle::operator->}. It should never be stored + // anywhere or used in any other code; no one should ever have to spell out + // {ObjectRef} in code. Its only purpose is to be dereferenced immediately by + // "operator-> chaining". Returning the address of the field is valid because + // this object's lifetime only ends at the end of the full statement. + // Origin: + // https://github.com/v8/v8/blob/03aaa4b3bf4cb01eee1f223b252e6869b04ab08c/src/handles/handles.h#L91-L105 + class MOZ_TEMPORARY_CLASS ObjectRef { + public: + T* operator->() { return &object_; } + + private: + friend class Handle; + explicit ObjectRef(T object) : object_(object) {} + + T object_; + }; + inline ObjectRef operator->() const { return ObjectRef{**this}; } private: + Handle(JS::Value* location) : location_(location) {} + template friend class Handle; template friend class MaybeHandle; + + JS::Value* location_; }; // A Handle can be converted into a MaybeHandle. Converting a MaybeHandle @@ -543,21 +694,35 @@ class Handle { // Origin: // https://github.com/v8/v8/blob/5792f3587116503fc047d2f68c951c72dced08a5/src/handles/maybe-handles.h#L15-L78 template -class MaybeHandle final { +class MOZ_NONHEAP_CLASS MaybeHandle final { public: - MaybeHandle() = default; + MaybeHandle() : location_(nullptr) {} // Constructor for handling automatic up casting from Handle. // Ex. Handle can be passed when MaybeHandle is expected. template ::value>::type> - MaybeHandle(Handle handle); + MaybeHandle(Handle handle) : location_(handle.location_) {} - /*inline*/ Handle ToHandleChecked() const; + inline Handle ToHandleChecked() const { + MOZ_RELEASE_ASSERT(location_); + return Handle(location_); + } // Convert to a Handle with a type that can be upcasted to. template - /*inline*/ bool ToHandle(Handle* out) const; + inline bool ToHandle(Handle* out) const { + if (location_) { + *out = Handle(location_); + return true; + } else { + *out = Handle(); + return false; + } + } + +private: + JS::Value* location_; }; // From v8/src/handles/handles-inl.h @@ -590,14 +755,6 @@ class AllowHeapAllocation { AllowHeapAllocation() {} }; -class DisallowJavascriptExecution { - public: - DisallowJavascriptExecution(Isolate* isolate); - - private: - js::AutoAssertNoContentJS nojs_; -}; - // Origin: // https://github.com/v8/v8/blob/84f3877c15bc7f8956d21614da4311337525a3c8/src/objects/string.h#L83-L474 class String : public HeapObject { @@ -605,6 +762,9 @@ class String : public HeapObject { JSString* str() const { return value_.toString(); } public: + String() : HeapObject() {} + String(JSString* str) { value_ = JS::StringValue(str); } + operator JSString*() const { return str(); } // Max char codes. @@ -614,21 +774,8 @@ class String : public HeapObject { static const uc32 kMaxCodePoint = 0x10ffff; MOZ_ALWAYS_INLINE int length() const { return str()->length(); } - uint16_t Get(uint32_t index); bool IsFlat() { return str()->isLinear(); }; - // These are only used in V8 functions that I want to rewrite. - // TODO: Rewrite those functions and delete this - bool IsConsString(); - bool IsExternalString(); - bool IsExternalOneByteString(); - bool IsExternalTwoByteString(); - bool IsSeqString(); - bool IsSeqOneByteString(); - bool IsSeqTwoByteString(); - bool IsSlicedString(); - bool IsThinString(); - // Origin: // https://github.com/v8/v8/blob/84f3877c15bc7f8956d21614da4311337525a3c8/src/objects/string.h#L95-L152 class FlatContent { @@ -643,14 +790,11 @@ class String : public HeapObject { return Vector(string_->latin1Chars(no_gc_), string_->length()); } - Vector ToUC16Vector() const; - // TODO: twoByteChars returns char16_t*, but uc16 is uint16_t, which is - // not compatible :( :( :( - // { - // MOZ_ASSERT(IsTwoByte()); - // return Vector(string_->twoByteChars(no_gc_), - // string_->length()); - // } + Vector ToUC16Vector() const { + MOZ_ASSERT(IsTwoByte()); + return Vector(string_->twoByteChars(no_gc_), + string_->length()); + } private: const JSLinearString* string_; const JS::AutoAssertNoGC& no_gc_; @@ -681,6 +825,22 @@ class String : public HeapObject { Vector GetCharVector(const DisallowHeapAllocation& no_gc); }; +template <> +inline Vector String::GetCharVector( + const DisallowHeapAllocation& no_gc) { + String::FlatContent flat = GetFlatContent(no_gc); + MOZ_ASSERT(flat.IsOneByte()); + return flat.ToOneByteVector(); +} + +template <> +inline Vector String::GetCharVector( + const DisallowHeapAllocation& no_gc) { + String::FlatContent flat = GetFlatContent(no_gc); + MOZ_ASSERT(flat.IsTwoByte()); + return flat.ToUC16Vector(); +} + // A flat string reader provides random access to the contents of a // string independent of the character width of the string. The handle // must be valid as long as the reader is being used. @@ -688,84 +848,66 @@ class String : public HeapObject { // https://github.com/v8/v8/blob/84f3877c15bc7f8956d21614da4311337525a3c8/src/objects/string.h#L807-L825 class MOZ_STACK_CLASS FlatStringReader { public: - FlatStringReader(JSAtom* string) : string_(string) {} - int length() { return string_->length(); } + FlatStringReader(JSLinearString* string) + : length_(string->length()), + is_latin1_(string->hasLatin1Chars()) { + + if (is_latin1_) { + latin1_chars_ = string->latin1Chars(nogc_); + } else { + two_byte_chars_ = string->twoByteChars(nogc_); + } + } + FlatStringReader(const char16_t* chars, size_t length) + : two_byte_chars_(chars), + length_(length), + is_latin1_(false) {} + + int length() { return length_; } inline char16_t Get(size_t index) { - return string_->latin1OrTwoByteChar(index); + MOZ_ASSERT(index < length_); + if (is_latin1_) { + return latin1_chars_[index]; + } else { + return two_byte_chars_[index]; + } } private: - JSAtom* string_; - JS::AutoCheckCannotGC nogc; + union { + const JS::Latin1Char *latin1_chars_; + const char16_t* two_byte_chars_; + }; + size_t length_; + bool is_latin1_; + JS::AutoCheckCannotGC nogc_; }; -////////////////////////////////////////////////// -// TODO: Refactor NativeRegExpMacroAssembler and delete all of these: -class ConsString : public String { - public: - String first(); - String second(); - - static ConsString cast(Object object); -}; -class ExternalOneByteString : public String { - public: - const uint8_t* GetChars(); - static ExternalOneByteString cast(Object object); -}; -class ExternalTwoByteString : public String { - public: - const uc16* GetChars(); - static ExternalTwoByteString cast(Object object); -}; -class SeqOneByteString : public String { - public: - uint8_t* GetChars(const DisallowHeapAllocation& no_gc); - static SeqOneByteString cast(Object object); -}; -class SeqTwoByteString : public String { - public: - uc16* GetChars(const DisallowHeapAllocation& no_gc); - static SeqTwoByteString cast(Object object); - - static constexpr size_t kMaxCharsSize = JSString::MAX_LENGTH * 2; -}; -class SlicedString : public String { - public: - String parent(); - int offset(); - static SlicedString cast(Object object); -}; -class ThinString : public String { - public: - String actual(); - static ThinString cast(Object object); -}; -class StringShape { - public: - explicit StringShape(const String s); - bool IsCons(); - bool IsSliced(); - bool IsThin(); -}; -// End of "TODO: Delete all of these" -////////////////////////////////////////////////// - class JSRegExp : public HeapObject { public: // ****************************************************** // Methods that are called from inside the implementation // ****************************************************** - void TierUpTick(); - bool MarkedForTierUp() const; + void TierUpTick() { /*inner()->tierUpTick();*/ } + bool MarkedForTierUp() const { + return false; /*inner()->markedForTierUp();*/ + } - Object Code(bool is_latin1) const; - Object Bytecode(bool is_latin1) const; + // TODO: hook these up + Object Code(bool is_latin1) const { return Object(JS::UndefinedValue()); } + Object Bytecode(bool is_latin1) const { return Object(JS::UndefinedValue()); } - uint32_t BacktrackLimit() const; + uint32_t BacktrackLimit() const { + return 0; /*inner()->backtrackLimit();*/ + } - static JSRegExp cast(Object object); + static JSRegExp cast(Object object) { + JSRegExp regexp; + MOZ_ASSERT(JS::Value(object).toGCThing()->is()); + regexp.value_ = JS::PrivateGCThingValue(JS::Value(object).toGCThing()); + return regexp; + } // ****************************** // Static constants @@ -807,6 +949,11 @@ class JSRegExp : public HeapObject { static constexpr int kFlagCount = 6; static constexpr int kNoBacktrackLimit = 0; + +private: + js::RegExpShared* inner() { + return value_.toGCThing()->as(); + } }; class Histogram { @@ -841,11 +988,26 @@ class Isolate { bool has_pending_exception() { return cx()->isExceptionPending(); } void StackOverflow() { js::ReportOverRecursed(cx()); } - unibrow::Mapping* jsregexp_uncanonicalize(); +#ifndef V8_INTL_SUPPORT + unibrow::Mapping* jsregexp_uncanonicalize() { + return &jsregexp_uncanonicalize_; + } unibrow::Mapping* - regexp_macro_assembler_canonicalize(); - unibrow::Mapping* jsregexp_canonrange(); + regexp_macro_assembler_canonicalize() { + return ®exp_macro_assembler_canonicalize_; + } + unibrow::Mapping* jsregexp_canonrange() { + return &jsregexp_canonrange_; + } +private: + unibrow::Mapping jsregexp_uncanonicalize_; + unibrow::Mapping + regexp_macro_assembler_canonicalize_; + unibrow::Mapping jsregexp_canonrange_; +#endif // !V8_INTL_SUPPORT + +public: // An empty stub for telemetry we don't support void IncreaseTotalRegexpCodeGenerated(int size) {} @@ -856,13 +1018,9 @@ class Isolate { Handle NewByteArray( int length, AllocationType allocation = AllocationType::kYoung); - MOZ_MUST_USE MaybeHandle NewStringFromOneByte( - const Vector& str, - AllocationType allocation = AllocationType::kYoung); // Allocates a fixed array initialized with undefined values. - Handle NewFixedArray( - int length, AllocationType allocation = AllocationType::kYoung); + Handle NewFixedArray(int length); template Handle InternalizeString(const Vector& str); @@ -875,7 +1033,37 @@ class Isolate { JSContext* cx() const { return cx_; } + void trace(JSTracer* trc); + + //********** Handle code **********// + + JS::Value* getHandleLocation(JS::Value value); + private: + + mozilla::SegmentedVector handleArena_; + mozilla::SegmentedVector> uniquePtrArena_; + + void* allocatePseudoHandle(size_t bytes); + +public: + template + PseudoHandle takeOwnership(void* ptr); + +private: + void openHandleScope(HandleScope& scope) { + scope.level_ = handleArena_.Length(); + scope.non_gc_level_ = uniquePtrArena_.Length(); + } + void closeHandleScope(size_t prevLevel, size_t prevUniqueLevel) { + size_t currLevel = handleArena_.Length(); + handleArena_.PopLastN(currLevel - prevLevel); + + size_t currUniqueLevel = uniquePtrArena_.Length(); + uniquePtrArena_.PopLastN(currUniqueLevel - prevUniqueLevel); + } + friend class HandleScope; + JSContext* cx_; RegExpStack* regexp_stack_; Counters counters_; @@ -902,47 +1090,20 @@ class StackLimitCheck { JSContext* cx_; }; -class Code { +class Code : public HeapObject { public: - bool operator!=(Code& other) const; + uint8_t* raw_instruction_start() { return inner()->raw(); } - Address raw_instruction_start(); - Address raw_instruction_end(); - Address address(); - - static Code cast(Object object); -}; - -// GeneratedCode provides an interface for calling into jit code. -// It will probably require additional work to hook this up to the -// arm simulator. -// Origin: -// https://github.com/v8/v8/blob/abfbe7687edb5b2dffe0b33b24e0a41bb86a8214/src/execution/simulator.h#L96-L164 -template -class GeneratedCode { - public: - using Signature = Return(Args...); - - static GeneratedCode FromCode(Code code); // TODO: implement - Return Call(Args... args) { return fn_ptr_(args...); } - - private: - friend class GeneratedCode; - Isolate* isolate_; - Signature* fn_ptr_; - GeneratedCode(Isolate* isolate, Signature* fn_ptr) - : isolate_(isolate), fn_ptr_(fn_ptr) {} -}; - -// Allow to use {GeneratedCode} instead of -// {GeneratedCode}. -template -class GeneratedCode : public GeneratedCode { - public: - // Automatically convert from {GeneratedCode} to - // {GeneratedCode}. - GeneratedCode(GeneratedCode other) - : GeneratedCode(other.isolate_, other.fn_ptr_) {} + static Code cast(Object object) { + Code c; + MOZ_ASSERT(JS::Value(object).toGCThing()->is()); + c.value_ = JS::PrivateGCThingValue(JS::Value(object).toGCThing()); + return c; + } +private: + js::jit::JitCode* inner() { + return value_.toGCThing()->as(); + } }; enum class MessageTemplate { kStackOverflow }; @@ -993,6 +1154,8 @@ extern bool FLAG_trace_regexp_bytecodes; extern bool FLAG_trace_regexp_parser; extern bool FLAG_trace_regexp_peephole_optimization; +#define V8_USE_COMPUTED_GOTO 1 + } // namespace internal } // namespace v8 diff --git a/js/src/regexp/special-case.cc b/js/src/regexp/special-case.cc new file mode 100644 index 0000000000..d60b987645 --- /dev/null +++ b/js/src/regexp/special-case.cc @@ -0,0 +1,38 @@ +// Copyright 2019 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. + +// Automatically generated by regexp/gen-regexp-special-case.cc +// The following functions are used to build icu::UnicodeSet +// for specical cases different between Unicode and ECMA262. +#ifdef V8_INTL_SUPPORT +#include "regexp/special-case.h" + +#include "unicode/uniset.h" +namespace v8 { +namespace internal { + +icu::UnicodeSet BuildIgnoreSet() { + icu::UnicodeSet set; + set.add(0x3f4); + set.add(0x2126); + set.add(0x212a, 0x212b); + set.freeze(); + return set; +} +icu::UnicodeSet BuildSpecialAddSet() { + icu::UnicodeSet set; + set.add(0xc5); + set.add(0xe5); + set.add(0x398); + set.add(0x3a9); + set.add(0x3b8); + set.add(0x3c9); + set.add(0x3d1); + set.freeze(); + return set; +} + +} // namespace internal +} // namespace v8 +#endif // V8_INTL_SUPPORT diff --git a/js/src/regexp/util/unicode.cc b/js/src/regexp/util/unicode.cc new file mode 100644 index 0000000000..da8cef4445 --- /dev/null +++ b/js/src/regexp/util/unicode.cc @@ -0,0 +1,1865 @@ +// Copyright 2012 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. +// +// This file is a subset of: +// https://github.com/v8/v8/blob/master/src/strings/unicode.cc + +#include "regexp/regexp-shim.h" + +#ifdef V8_INTL_SUPPORT +#include "unicode/uchar.h" +#endif + +namespace v8 { +namespace unibrow { + +#ifndef V8_INTL_SUPPORT +static const int kStartBit = (1 << 30); +static const int kChunkBits = (1 << 13); +#endif // !V8_INTL_SUPPORT + +static const uchar kSentinel = static_cast(-1); + +/** + * \file + * Implementations of functions for working with Unicode. + */ + +using int16_t = signed short; // NOLINT +using uint16_t = unsigned short; // NOLINT +using int32_t = int; // NOLINT + +#ifndef V8_INTL_SUPPORT +// All access to the character table should go through this function. +template +static inline uchar TableGet(const int32_t* table, int index) { + return table[D * index]; +} + +static inline uchar GetEntry(int32_t entry) { return entry & (kStartBit - 1); } + +static inline bool IsStart(int32_t entry) { return (entry & kStartBit) != 0; } + +/** + * Look up a character in the Unicode table using a mix of binary and + * interpolation search. For a uniformly distributed array + * interpolation search beats binary search by a wide margin. However, + * in this case interpolation search degenerates because of some very + * high values in the lower end of the table so this function uses a + * combination. The average number of steps to look up the information + * about a character is around 10, slightly higher if there is no + * information available about the character. + */ +static bool LookupPredicate(const int32_t* table, uint16_t size, uchar chr) { + static const int kEntryDist = 1; + uint16_t value = chr & (kChunkBits - 1); + unsigned int low = 0; + unsigned int high = size - 1; + while (high != low) { + unsigned int mid = low + ((high - low) >> 1); + uchar current_value = GetEntry(TableGet(table, mid)); + // If we've found an entry less than or equal to this one, and the + // next one is not also less than this one, we've arrived. + if ((current_value <= value) && + (mid + 1 == size || + GetEntry(TableGet(table, mid + 1)) > value)) { + low = mid; + break; + } else if (current_value < value) { + low = mid + 1; + } else if (current_value > value) { + // If we've just checked the bottom-most value and it's not + // the one we're looking for, we're done. + if (mid == 0) break; + high = mid - 1; + } + } + int32_t field = TableGet(table, low); + uchar entry = GetEntry(field); + bool is_start = IsStart(field); + return (entry == value) || (entry < value && is_start); +} +#endif // !V8_INTL_SUPPORT + +template +struct MultiCharacterSpecialCase { + static const uchar kEndOfEncoding = kSentinel; + uchar chars[kW]; +}; + +#ifndef V8_INTL_SUPPORT +// Look up the mapping for the given character in the specified table, +// which is of the specified length and uses the specified special case +// mapping for multi-char mappings. The next parameter is the character +// following the one to map. The result will be written in to the result +// buffer and the number of characters written will be returned. Finally, +// if the allow_caching_ptr is non-null then false will be stored in +// it if the result contains multiple characters or depends on the +// context. +// If ranges are linear, a match between a start and end point is +// offset by the distance between the match and the start. Otherwise +// the result is the same as for the start point on the entire range. +template +static int LookupMapping(const int32_t* table, uint16_t size, + const MultiCharacterSpecialCase* multi_chars, + uchar chr, uchar next, uchar* result, + bool* allow_caching_ptr) { + static const int kEntryDist = 2; + uint16_t key = chr & (kChunkBits - 1); + uint16_t chunk_start = chr - key; + unsigned int low = 0; + unsigned int high = size - 1; + while (high != low) { + unsigned int mid = low + ((high - low) >> 1); + uchar current_value = GetEntry(TableGet(table, mid)); + // If we've found an entry less than or equal to this one, and the next one + // is not also less than this one, we've arrived. + if ((current_value <= key) && + (mid + 1 == size || + GetEntry(TableGet(table, mid + 1)) > key)) { + low = mid; + break; + } else if (current_value < key) { + low = mid + 1; + } else if (current_value > key) { + // If we've just checked the bottom-most value and it's not + // the one we're looking for, we're done. + if (mid == 0) break; + high = mid - 1; + } + } + int32_t field = TableGet(table, low); + uchar entry = GetEntry(field); + bool is_start = IsStart(field); + bool found = (entry == key) || (entry < key && is_start); + if (found) { + int32_t value = table[2 * low + 1]; + if (value == 0) { + // 0 means not present + return 0; + } else if ((value & 3) == 0) { + // Low bits 0 means a constant offset from the given character. + if (ranges_are_linear) { + result[0] = chr + (value >> 2); + } else { + result[0] = entry + chunk_start + (value >> 2); + } + return 1; + } else if ((value & 3) == 1) { + // Low bits 1 means a special case mapping + if (allow_caching_ptr) *allow_caching_ptr = false; + const MultiCharacterSpecialCase& mapping = multi_chars[value >> 2]; + int length = 0; + for (length = 0; length < kW; length++) { + uchar mapped = mapping.chars[length]; + if (mapped == MultiCharacterSpecialCase::kEndOfEncoding) break; + if (ranges_are_linear) { + result[length] = mapped + (key - entry); + } else { + result[length] = mapped; + } + } + return length; + } else { + // Low bits 2 means a really really special case + if (allow_caching_ptr) *allow_caching_ptr = false; + // The cases of this switch are defined in unicode.py in the + // really_special_cases mapping. + switch (value >> 2) { + case 1: + // Really special case 1: upper case sigma. This letter + // converts to two different lower case sigmas depending on + // whether or not it occurs at the end of a word. + if (next != 0 && Letter::Is(next)) { + result[0] = 0x03C3; + } else { + result[0] = 0x03C2; + } + return 1; + default: + return 0; + } + return -1; + } + } else { + return 0; + } +} +#endif // !V8_INTL_SUPPORT + +// Letter: point.category in ['Lu', 'Ll', 'Lt', 'Lm', 'Lo', 'Nl'] +#ifdef V8_INTL_SUPPORT +bool Letter::Is(uchar c) { return static_cast(u_isalpha(c)); } +#else +static const uint16_t kLetterTable0Size = 431; +static const int32_t kLetterTable0[431] = { + 1073741889, 90, 1073741921, 122, + 170, 181, 186, 1073742016, // NOLINT + 214, 1073742040, 246, 1073742072, + 705, 1073742534, 721, 1073742560, // NOLINT + 740, 748, 750, 1073742704, + 884, 1073742710, 887, 1073742714, // NOLINT + 893, 895, 902, 1073742728, + 906, 908, 1073742734, 929, // NOLINT + 1073742755, 1013, 1073742839, 1153, + 1073742986, 1327, 1073743153, 1366, // NOLINT + 1369, 1073743201, 1415, 1073743312, + 1514, 1073743344, 1522, 1073743392, // NOLINT + 1610, 1073743470, 1647, 1073743473, + 1747, 1749, 1073743589, 1766, // NOLINT + 1073743598, 1775, 1073743610, 1788, + 1791, 1808, 1073743634, 1839, // NOLINT + 1073743693, 1957, 1969, 1073743818, + 2026, 1073743860, 2037, 2042, // NOLINT + 1073743872, 2069, 2074, 2084, + 2088, 1073743936, 2136, 1073744032, // NOLINT + 2226, 1073744132, 2361, 2365, + 2384, 1073744216, 2401, 1073744241, // NOLINT + 2432, 1073744261, 2444, 1073744271, + 2448, 1073744275, 2472, 1073744298, // NOLINT + 2480, 2482, 1073744310, 2489, + 2493, 2510, 1073744348, 2525, // NOLINT + 1073744351, 2529, 1073744368, 2545, + 1073744389, 2570, 1073744399, 2576, // NOLINT + 1073744403, 2600, 1073744426, 2608, + 1073744434, 2611, 1073744437, 2614, // NOLINT + 1073744440, 2617, 1073744473, 2652, + 2654, 1073744498, 2676, 1073744517, // NOLINT + 2701, 1073744527, 2705, 1073744531, + 2728, 1073744554, 2736, 1073744562, // NOLINT + 2739, 1073744565, 2745, 2749, + 2768, 1073744608, 2785, 1073744645, // NOLINT + 2828, 1073744655, 2832, 1073744659, + 2856, 1073744682, 2864, 1073744690, // NOLINT + 2867, 1073744693, 2873, 2877, + 1073744732, 2909, 1073744735, 2913, // NOLINT + 2929, 2947, 1073744773, 2954, + 1073744782, 2960, 1073744786, 2965, // NOLINT + 1073744793, 2970, 2972, 1073744798, + 2975, 1073744803, 2980, 1073744808, // NOLINT + 2986, 1073744814, 3001, 3024, + 1073744901, 3084, 1073744910, 3088, // NOLINT + 1073744914, 3112, 1073744938, 3129, + 3133, 1073744984, 3161, 1073744992, // NOLINT + 3169, 1073745029, 3212, 1073745038, + 3216, 1073745042, 3240, 1073745066, // NOLINT + 3251, 1073745077, 3257, 3261, + 3294, 1073745120, 3297, 1073745137, // NOLINT + 3314, 1073745157, 3340, 1073745166, + 3344, 1073745170, 3386, 3389, // NOLINT + 3406, 1073745248, 3425, 1073745274, + 3455, 1073745285, 3478, 1073745306, // NOLINT + 3505, 1073745331, 3515, 3517, + 1073745344, 3526, 1073745409, 3632, // NOLINT + 1073745458, 3635, 1073745472, 3654, + 1073745537, 3714, 3716, 1073745543, // NOLINT + 3720, 3722, 3725, 1073745556, + 3735, 1073745561, 3743, 1073745569, // NOLINT + 3747, 3749, 3751, 1073745578, + 3755, 1073745581, 3760, 1073745586, // NOLINT + 3763, 3773, 1073745600, 3780, + 3782, 1073745628, 3807, 3840, // NOLINT + 1073745728, 3911, 1073745737, 3948, + 1073745800, 3980, 1073745920, 4138, // NOLINT + 4159, 1073746000, 4181, 1073746010, + 4189, 4193, 1073746021, 4198, // NOLINT + 1073746030, 4208, 1073746037, 4225, + 4238, 1073746080, 4293, 4295, // NOLINT + 4301, 1073746128, 4346, 1073746172, + 4680, 1073746506, 4685, 1073746512, // NOLINT + 4694, 4696, 1073746522, 4701, + 1073746528, 4744, 1073746570, 4749, // NOLINT + 1073746576, 4784, 1073746610, 4789, + 1073746616, 4798, 4800, 1073746626, // NOLINT + 4805, 1073746632, 4822, 1073746648, + 4880, 1073746706, 4885, 1073746712, // NOLINT + 4954, 1073746816, 5007, 1073746848, + 5108, 1073746945, 5740, 1073747567, // NOLINT + 5759, 1073747585, 5786, 1073747616, + 5866, 1073747694, 5880, 1073747712, // NOLINT + 5900, 1073747726, 5905, 1073747744, + 5937, 1073747776, 5969, 1073747808, // NOLINT + 5996, 1073747822, 6000, 1073747840, + 6067, 6103, 6108, 1073748000, // NOLINT + 6263, 1073748096, 6312, 6314, + 1073748144, 6389, 1073748224, 6430, // NOLINT + 1073748304, 6509, 1073748336, 6516, + 1073748352, 6571, 1073748417, 6599, // NOLINT + 1073748480, 6678, 1073748512, 6740, + 6823, 1073748741, 6963, 1073748805, // NOLINT + 6987, 1073748867, 7072, 1073748910, + 7087, 1073748922, 7141, 1073748992, // NOLINT + 7203, 1073749069, 7247, 1073749082, + 7293, 1073749225, 7404, 1073749230, // NOLINT + 7409, 1073749237, 7414, 1073749248, + 7615, 1073749504, 7957, 1073749784, // NOLINT + 7965, 1073749792, 8005, 1073749832, + 8013, 1073749840, 8023, 8025, // NOLINT + 8027, 8029, 1073749855, 8061, + 1073749888, 8116, 1073749942, 8124, // NOLINT + 8126, 1073749954, 8132, 1073749958, + 8140, 1073749968, 8147, 1073749974, // NOLINT + 8155, 1073749984, 8172, 1073750002, + 8180, 1073750006, 8188}; // NOLINT +static const uint16_t kLetterTable1Size = 87; +static const int32_t kLetterTable1[87] = { + 113, 127, 1073741968, 156, + 258, 263, 1073742090, 275, // NOLINT + 277, 1073742105, 285, 292, + 294, 296, 1073742122, 301, // NOLINT + 1073742127, 313, 1073742140, 319, + 1073742149, 329, 334, 1073742176, // NOLINT + 392, 1073744896, 3118, 1073744944, + 3166, 1073744992, 3300, 1073745131, // NOLINT + 3310, 1073745138, 3315, 1073745152, + 3365, 3367, 3373, 1073745200, // NOLINT + 3431, 3439, 1073745280, 3478, + 1073745312, 3494, 1073745320, 3502, // NOLINT + 1073745328, 3510, 1073745336, 3518, + 1073745344, 3526, 1073745352, 3534, // NOLINT + 1073745360, 3542, 1073745368, 3550, + 3631, 1073745925, 4103, 1073745953, // NOLINT + 4137, 1073745969, 4149, 1073745976, + 4156, 1073745985, 4246, 1073746077, // NOLINT + 4255, 1073746081, 4346, 1073746172, + 4351, 1073746181, 4397, 1073746225, // NOLINT + 4494, 1073746336, 4538, 1073746416, + 4607, 1073746944, 8191}; // NOLINT +static const uint16_t kLetterTable2Size = 4; +static const int32_t kLetterTable2[4] = {1073741824, 3509, 1073745408, + 8191}; // NOLINT +static const uint16_t kLetterTable3Size = 2; +static const int32_t kLetterTable3[2] = {1073741824, 8191}; // NOLINT +static const uint16_t kLetterTable4Size = 2; +static const int32_t kLetterTable4[2] = {1073741824, 8140}; // NOLINT +static const uint16_t kLetterTable5Size = 100; +static const int32_t kLetterTable5[100] = { + 1073741824, 1164, 1073743056, 1277, + 1073743104, 1548, 1073743376, 1567, // NOLINT + 1073743402, 1579, 1073743424, 1646, + 1073743487, 1693, 1073743520, 1775, // NOLINT + 1073743639, 1823, 1073743650, 1928, + 1073743755, 1934, 1073743760, 1965, // NOLINT + 1073743792, 1969, 1073743863, 2049, + 1073743875, 2053, 1073743879, 2058, // NOLINT + 1073743884, 2082, 1073743936, 2163, + 1073744002, 2227, 1073744114, 2295, // NOLINT + 2299, 1073744138, 2341, 1073744176, + 2374, 1073744224, 2428, 1073744260, // NOLINT + 2482, 2511, 1073744352, 2532, + 1073744358, 2543, 1073744378, 2558, // NOLINT + 1073744384, 2600, 1073744448, 2626, + 1073744452, 2635, 1073744480, 2678, // NOLINT + 2682, 1073744510, 2735, 2737, + 1073744565, 2742, 1073744569, 2749, // NOLINT + 2752, 2754, 1073744603, 2781, + 1073744608, 2794, 1073744626, 2804, // NOLINT + 1073744641, 2822, 1073744649, 2830, + 1073744657, 2838, 1073744672, 2854, // NOLINT + 1073744680, 2862, 1073744688, 2906, + 1073744732, 2911, 1073744740, 2917, // NOLINT + 1073744832, 3042, 1073744896, 8191}; // NOLINT +static const uint16_t kLetterTable6Size = 6; +static const int32_t kLetterTable6[6] = {1073741824, 6051, 1073747888, 6086, + 1073747915, 6139}; // NOLINT +static const uint16_t kLetterTable7Size = 48; +static const int32_t kLetterTable7[48] = { + 1073748224, 6765, 1073748592, 6873, + 1073748736, 6918, 1073748755, 6935, // NOLINT + 6941, 1073748767, 6952, 1073748778, + 6966, 1073748792, 6972, 6974, // NOLINT + 1073748800, 6977, 1073748803, 6980, + 1073748806, 7089, 1073748947, 7485, // NOLINT + 1073749328, 7567, 1073749394, 7623, + 1073749488, 7675, 1073749616, 7796, // NOLINT + 1073749622, 7932, 1073749793, 7994, + 1073749825, 8026, 1073749862, 8126, // NOLINT + 1073749954, 8135, 1073749962, 8143, + 1073749970, 8151, 1073749978, 8156}; // NOLINT +bool Letter::Is(uchar c) { + int chunk_index = c >> 13; + switch (chunk_index) { + case 0: + return LookupPredicate(kLetterTable0, kLetterTable0Size, c); + case 1: + return LookupPredicate(kLetterTable1, kLetterTable1Size, c); + case 2: + return LookupPredicate(kLetterTable2, kLetterTable2Size, c); + case 3: + return LookupPredicate(kLetterTable3, kLetterTable3Size, c); + case 4: + return LookupPredicate(kLetterTable4, kLetterTable4Size, c); + case 5: + return LookupPredicate(kLetterTable5, kLetterTable5Size, c); + case 6: + return LookupPredicate(kLetterTable6, kLetterTable6Size, c); + case 7: + return LookupPredicate(kLetterTable7, kLetterTable7Size, c); + default: + return false; + } +} +#endif + +#ifndef V8_INTL_SUPPORT + +static const MultiCharacterSpecialCase<1> kEcma262CanonicalizeMultiStrings0[1] = + { // NOLINT + {{kSentinel}}}; // NOLINT +static const uint16_t kEcma262CanonicalizeTable0Size = 498; // NOLINT +static const int32_t kEcma262CanonicalizeTable0[996] = { + 1073741921, -128, 122, -128, 181, 2972, + 1073742048, -128, 246, -128, 1073742072, -128, + 254, -128, 255, 484, // NOLINT + 257, -4, 259, -4, 261, -4, + 263, -4, 265, -4, 267, -4, + 269, -4, 271, -4, // NOLINT + 273, -4, 275, -4, 277, -4, + 279, -4, 281, -4, 283, -4, + 285, -4, 287, -4, // NOLINT + 289, -4, 291, -4, 293, -4, + 295, -4, 297, -4, 299, -4, + 301, -4, 303, -4, // NOLINT + 307, -4, 309, -4, 311, -4, + 314, -4, 316, -4, 318, -4, + 320, -4, 322, -4, // NOLINT + 324, -4, 326, -4, 328, -4, + 331, -4, 333, -4, 335, -4, + 337, -4, 339, -4, // NOLINT + 341, -4, 343, -4, 345, -4, + 347, -4, 349, -4, 351, -4, + 353, -4, 355, -4, // NOLINT + 357, -4, 359, -4, 361, -4, + 363, -4, 365, -4, 367, -4, + 369, -4, 371, -4, // NOLINT + 373, -4, 375, -4, 378, -4, + 380, -4, 382, -4, 384, 780, + 387, -4, 389, -4, // NOLINT + 392, -4, 396, -4, 402, -4, + 405, 388, 409, -4, 410, 652, + 414, 520, 417, -4, // NOLINT + 419, -4, 421, -4, 424, -4, + 429, -4, 432, -4, 436, -4, + 438, -4, 441, -4, // NOLINT + 445, -4, 447, 224, 453, -4, + 454, -8, 456, -4, 457, -8, + 459, -4, 460, -8, // NOLINT + 462, -4, 464, -4, 466, -4, + 468, -4, 470, -4, 472, -4, + 474, -4, 476, -4, // NOLINT + 477, -316, 479, -4, 481, -4, + 483, -4, 485, -4, 487, -4, + 489, -4, 491, -4, // NOLINT + 493, -4, 495, -4, 498, -4, + 499, -8, 501, -4, 505, -4, + 507, -4, 509, -4, // NOLINT + 511, -4, 513, -4, 515, -4, + 517, -4, 519, -4, 521, -4, + 523, -4, 525, -4, // NOLINT + 527, -4, 529, -4, 531, -4, + 533, -4, 535, -4, 537, -4, + 539, -4, 541, -4, // NOLINT + 543, -4, 547, -4, 549, -4, + 551, -4, 553, -4, 555, -4, + 557, -4, 559, -4, // NOLINT + 561, -4, 563, -4, 572, -4, + 1073742399, 43260, 576, 43260, 578, -4, + 583, -4, 585, -4, // NOLINT + 587, -4, 589, -4, 591, -4, + 592, 43132, 593, 43120, 594, 43128, + 595, -840, 596, -824, // NOLINT + 1073742422, -820, 599, -820, 601, -808, + 603, -812, 604, 169276, 608, -820, + 609, 169260, 611, -828, // NOLINT + 613, 169120, 614, 169232, 616, -836, + 617, -844, 619, 42972, 620, 169220, + 623, -844, 625, 42996, // NOLINT + 626, -852, 629, -856, 637, 42908, + 640, -872, 643, -872, 647, 169128, + 648, -872, 649, -276, // NOLINT + 1073742474, -868, 651, -868, 652, -284, + 658, -876, 670, 169032, 837, 336, + 881, -4, 883, -4, // NOLINT + 887, -4, 1073742715, 520, 893, 520, + 940, -152, 1073742765, -148, 943, -148, + 1073742769, -128, 961, -128, // NOLINT + 962, -124, 1073742787, -128, 971, -128, + 972, -256, 1073742797, -252, 974, -252, + 976, -248, 977, -228, // NOLINT + 981, -188, 982, -216, 983, -32, + 985, -4, 987, -4, 989, -4, + 991, -4, 993, -4, // NOLINT + 995, -4, 997, -4, 999, -4, + 1001, -4, 1003, -4, 1005, -4, + 1007, -4, 1008, -344, // NOLINT + 1009, -320, 1010, 28, 1011, -464, + 1013, -384, 1016, -4, 1019, -4, + 1073742896, -128, 1103, -128, // NOLINT + 1073742928, -320, 1119, -320, 1121, -4, + 1123, -4, 1125, -4, 1127, -4, + 1129, -4, 1131, -4, // NOLINT + 1133, -4, 1135, -4, 1137, -4, + 1139, -4, 1141, -4, 1143, -4, + 1145, -4, 1147, -4, // NOLINT + 1149, -4, 1151, -4, 1153, -4, + 1163, -4, 1165, -4, 1167, -4, + 1169, -4, 1171, -4, // NOLINT + 1173, -4, 1175, -4, 1177, -4, + 1179, -4, 1181, -4, 1183, -4, + 1185, -4, 1187, -4, // NOLINT + 1189, -4, 1191, -4, 1193, -4, + 1195, -4, 1197, -4, 1199, -4, + 1201, -4, 1203, -4, // NOLINT + 1205, -4, 1207, -4, 1209, -4, + 1211, -4, 1213, -4, 1215, -4, + 1218, -4, 1220, -4, // NOLINT + 1222, -4, 1224, -4, 1226, -4, + 1228, -4, 1230, -4, 1231, -60, + 1233, -4, 1235, -4, // NOLINT + 1237, -4, 1239, -4, 1241, -4, + 1243, -4, 1245, -4, 1247, -4, + 1249, -4, 1251, -4, // NOLINT + 1253, -4, 1255, -4, 1257, -4, + 1259, -4, 1261, -4, 1263, -4, + 1265, -4, 1267, -4, // NOLINT + 1269, -4, 1271, -4, 1273, -4, + 1275, -4, 1277, -4, 1279, -4, + 1281, -4, 1283, -4, // NOLINT + 1285, -4, 1287, -4, 1289, -4, + 1291, -4, 1293, -4, 1295, -4, + 1297, -4, 1299, -4, // NOLINT + 1301, -4, 1303, -4, 1305, -4, + 1307, -4, 1309, -4, 1311, -4, + 1313, -4, 1315, -4, // NOLINT + 1317, -4, 1319, -4, 1321, -4, + 1323, -4, 1325, -4, 1327, -4, + 1073743201, -192, 1414, -192, // NOLINT + 7545, 141328, 7549, 15256, 7681, -4, + 7683, -4, 7685, -4, 7687, -4, + 7689, -4, 7691, -4, // NOLINT + 7693, -4, 7695, -4, 7697, -4, + 7699, -4, 7701, -4, 7703, -4, + 7705, -4, 7707, -4, // NOLINT + 7709, -4, 7711, -4, 7713, -4, + 7715, -4, 7717, -4, 7719, -4, + 7721, -4, 7723, -4, // NOLINT + 7725, -4, 7727, -4, 7729, -4, + 7731, -4, 7733, -4, 7735, -4, + 7737, -4, 7739, -4, // NOLINT + 7741, -4, 7743, -4, 7745, -4, + 7747, -4, 7749, -4, 7751, -4, + 7753, -4, 7755, -4, // NOLINT + 7757, -4, 7759, -4, 7761, -4, + 7763, -4, 7765, -4, 7767, -4, + 7769, -4, 7771, -4, // NOLINT + 7773, -4, 7775, -4, 7777, -4, + 7779, -4, 7781, -4, 7783, -4, + 7785, -4, 7787, -4, // NOLINT + 7789, -4, 7791, -4, 7793, -4, + 7795, -4, 7797, -4, 7799, -4, + 7801, -4, 7803, -4, // NOLINT + 7805, -4, 7807, -4, 7809, -4, + 7811, -4, 7813, -4, 7815, -4, + 7817, -4, 7819, -4, // NOLINT + 7821, -4, 7823, -4, 7825, -4, + 7827, -4, 7829, -4, 7835, -236, + 7841, -4, 7843, -4, // NOLINT + 7845, -4, 7847, -4, 7849, -4, + 7851, -4, 7853, -4, 7855, -4, + 7857, -4, 7859, -4, // NOLINT + 7861, -4, 7863, -4, 7865, -4, + 7867, -4, 7869, -4, 7871, -4, + 7873, -4, 7875, -4, // NOLINT + 7877, -4, 7879, -4, 7881, -4, + 7883, -4, 7885, -4, 7887, -4, + 7889, -4, 7891, -4, // NOLINT + 7893, -4, 7895, -4, 7897, -4, + 7899, -4, 7901, -4, 7903, -4, + 7905, -4, 7907, -4, // NOLINT + 7909, -4, 7911, -4, 7913, -4, + 7915, -4, 7917, -4, 7919, -4, + 7921, -4, 7923, -4, // NOLINT + 7925, -4, 7927, -4, 7929, -4, + 7931, -4, 7933, -4, 7935, -4, + 1073749760, 32, 7943, 32, // NOLINT + 1073749776, 32, 7957, 32, 1073749792, 32, + 7975, 32, 1073749808, 32, 7991, 32, + 1073749824, 32, 8005, 32, // NOLINT + 8017, 32, 8019, 32, 8021, 32, + 8023, 32, 1073749856, 32, 8039, 32, + 1073749872, 296, 8049, 296, // NOLINT + 1073749874, 344, 8053, 344, 1073749878, 400, + 8055, 400, 1073749880, 512, 8057, 512, + 1073749882, 448, 8059, 448, // NOLINT + 1073749884, 504, 8061, 504, 1073749936, 32, + 8113, 32, 8126, -28820, 1073749968, 32, + 8145, 32, 1073749984, 32, // NOLINT + 8161, 32, 8165, 28}; // NOLINT +static const MultiCharacterSpecialCase<1> kEcma262CanonicalizeMultiStrings1[1] = + { // NOLINT + {{kSentinel}}}; // NOLINT +static const uint16_t kEcma262CanonicalizeTable1Size = 73; // NOLINT +static const int32_t kEcma262CanonicalizeTable1[146] = { + 334, -112, 1073742192, -64, 383, -64, + 388, -4, 1073743056, -104, 1257, -104, + 1073744944, -192, 3166, -192, // NOLINT + 3169, -4, 3173, -43180, 3174, -43168, + 3176, -4, 3178, -4, 3180, -4, + 3187, -4, 3190, -4, // NOLINT + 3201, -4, 3203, -4, 3205, -4, + 3207, -4, 3209, -4, 3211, -4, + 3213, -4, 3215, -4, // NOLINT + 3217, -4, 3219, -4, 3221, -4, + 3223, -4, 3225, -4, 3227, -4, + 3229, -4, 3231, -4, // NOLINT + 3233, -4, 3235, -4, 3237, -4, + 3239, -4, 3241, -4, 3243, -4, + 3245, -4, 3247, -4, // NOLINT + 3249, -4, 3251, -4, 3253, -4, + 3255, -4, 3257, -4, 3259, -4, + 3261, -4, 3263, -4, // NOLINT + 3265, -4, 3267, -4, 3269, -4, + 3271, -4, 3273, -4, 3275, -4, + 3277, -4, 3279, -4, // NOLINT + 3281, -4, 3283, -4, 3285, -4, + 3287, -4, 3289, -4, 3291, -4, + 3293, -4, 3295, -4, // NOLINT + 3297, -4, 3299, -4, 3308, -4, + 3310, -4, 3315, -4, 1073745152, -29056, + 3365, -29056, 3367, -29056, // NOLINT + 3373, -29056}; // NOLINT +static const MultiCharacterSpecialCase<1> kEcma262CanonicalizeMultiStrings5[1] = + { // NOLINT + {{kSentinel}}}; // NOLINT +static const uint16_t kEcma262CanonicalizeTable5Size = 95; // NOLINT +static const int32_t kEcma262CanonicalizeTable5[190] = { + 1601, -4, 1603, -4, 1605, -4, 1607, -4, + 1609, -4, 1611, -4, 1613, -4, 1615, -4, // NOLINT + 1617, -4, 1619, -4, 1621, -4, 1623, -4, + 1625, -4, 1627, -4, 1629, -4, 1631, -4, // NOLINT + 1633, -4, 1635, -4, 1637, -4, 1639, -4, + 1641, -4, 1643, -4, 1645, -4, 1665, -4, // NOLINT + 1667, -4, 1669, -4, 1671, -4, 1673, -4, + 1675, -4, 1677, -4, 1679, -4, 1681, -4, // NOLINT + 1683, -4, 1685, -4, 1687, -4, 1689, -4, + 1691, -4, 1827, -4, 1829, -4, 1831, -4, // NOLINT + 1833, -4, 1835, -4, 1837, -4, 1839, -4, + 1843, -4, 1845, -4, 1847, -4, 1849, -4, // NOLINT + 1851, -4, 1853, -4, 1855, -4, 1857, -4, + 1859, -4, 1861, -4, 1863, -4, 1865, -4, // NOLINT + 1867, -4, 1869, -4, 1871, -4, 1873, -4, + 1875, -4, 1877, -4, 1879, -4, 1881, -4, // NOLINT + 1883, -4, 1885, -4, 1887, -4, 1889, -4, + 1891, -4, 1893, -4, 1895, -4, 1897, -4, // NOLINT + 1899, -4, 1901, -4, 1903, -4, 1914, -4, + 1916, -4, 1919, -4, 1921, -4, 1923, -4, // NOLINT + 1925, -4, 1927, -4, 1932, -4, 1937, -4, + 1939, -4, 1943, -4, 1945, -4, 1947, -4, // NOLINT + 1949, -4, 1951, -4, 1953, -4, 1955, -4, + 1957, -4, 1959, -4, 1961, -4}; // NOLINT +static const MultiCharacterSpecialCase<1> kEcma262CanonicalizeMultiStrings7[1] = + { // NOLINT + {{kSentinel}}}; // NOLINT +static const uint16_t kEcma262CanonicalizeTable7Size = 2; // NOLINT +static const int32_t kEcma262CanonicalizeTable7[4] = {1073749825, -128, 8026, + -128}; // NOLINT +int Ecma262Canonicalize::Convert(uchar c, uchar n, uchar* result, + bool* allow_caching_ptr) { + int chunk_index = c >> 13; + switch (chunk_index) { + case 0: + return LookupMapping( + kEcma262CanonicalizeTable0, kEcma262CanonicalizeTable0Size, + kEcma262CanonicalizeMultiStrings0, c, n, result, allow_caching_ptr); + case 1: + return LookupMapping( + kEcma262CanonicalizeTable1, kEcma262CanonicalizeTable1Size, + kEcma262CanonicalizeMultiStrings1, c, n, result, allow_caching_ptr); + case 5: + return LookupMapping( + kEcma262CanonicalizeTable5, kEcma262CanonicalizeTable5Size, + kEcma262CanonicalizeMultiStrings5, c, n, result, allow_caching_ptr); + case 7: + return LookupMapping( + kEcma262CanonicalizeTable7, kEcma262CanonicalizeTable7Size, + kEcma262CanonicalizeMultiStrings7, c, n, result, allow_caching_ptr); + default: + return 0; + } +} + +static const MultiCharacterSpecialCase<4> + kEcma262UnCanonicalizeMultiStrings0[507] = { // NOLINT + {{65, 97, kSentinel}}, + {{90, 122, kSentinel}}, + {{181, 924, 956, kSentinel}}, + {{192, 224, kSentinel}}, // NOLINT + {{214, 246, kSentinel}}, + {{216, 248, kSentinel}}, + {{222, 254, kSentinel}}, + {{255, 376, kSentinel}}, // NOLINT + {{256, 257, kSentinel}}, + {{258, 259, kSentinel}}, + {{260, 261, kSentinel}}, + {{262, 263, kSentinel}}, // NOLINT + {{264, 265, kSentinel}}, + {{266, 267, kSentinel}}, + {{268, 269, kSentinel}}, + {{270, 271, kSentinel}}, // NOLINT + {{272, 273, kSentinel}}, + {{274, 275, kSentinel}}, + {{276, 277, kSentinel}}, + {{278, 279, kSentinel}}, // NOLINT + {{280, 281, kSentinel}}, + {{282, 283, kSentinel}}, + {{284, 285, kSentinel}}, + {{286, 287, kSentinel}}, // NOLINT + {{288, 289, kSentinel}}, + {{290, 291, kSentinel}}, + {{292, 293, kSentinel}}, + {{294, 295, kSentinel}}, // NOLINT + {{296, 297, kSentinel}}, + {{298, 299, kSentinel}}, + {{300, 301, kSentinel}}, + {{302, 303, kSentinel}}, // NOLINT + {{306, 307, kSentinel}}, + {{308, 309, kSentinel}}, + {{310, 311, kSentinel}}, + {{313, 314, kSentinel}}, // NOLINT + {{315, 316, kSentinel}}, + {{317, 318, kSentinel}}, + {{319, 320, kSentinel}}, + {{321, 322, kSentinel}}, // NOLINT + {{323, 324, kSentinel}}, + {{325, 326, kSentinel}}, + {{327, 328, kSentinel}}, + {{330, 331, kSentinel}}, // NOLINT + {{332, 333, kSentinel}}, + {{334, 335, kSentinel}}, + {{336, 337, kSentinel}}, + {{338, 339, kSentinel}}, // NOLINT + {{340, 341, kSentinel}}, + {{342, 343, kSentinel}}, + {{344, 345, kSentinel}}, + {{346, 347, kSentinel}}, // NOLINT + {{348, 349, kSentinel}}, + {{350, 351, kSentinel}}, + {{352, 353, kSentinel}}, + {{354, 355, kSentinel}}, // NOLINT + {{356, 357, kSentinel}}, + {{358, 359, kSentinel}}, + {{360, 361, kSentinel}}, + {{362, 363, kSentinel}}, // NOLINT + {{364, 365, kSentinel}}, + {{366, 367, kSentinel}}, + {{368, 369, kSentinel}}, + {{370, 371, kSentinel}}, // NOLINT + {{372, 373, kSentinel}}, + {{374, 375, kSentinel}}, + {{377, 378, kSentinel}}, + {{379, 380, kSentinel}}, // NOLINT + {{381, 382, kSentinel}}, + {{384, 579, kSentinel}}, + {{385, 595, kSentinel}}, + {{386, 387, kSentinel}}, // NOLINT + {{388, 389, kSentinel}}, + {{390, 596, kSentinel}}, + {{391, 392, kSentinel}}, + {{393, 598, kSentinel}}, // NOLINT + {{394, 599, kSentinel}}, + {{395, 396, kSentinel}}, + {{398, 477, kSentinel}}, + {{399, 601, kSentinel}}, // NOLINT + {{400, 603, kSentinel}}, + {{401, 402, kSentinel}}, + {{403, 608, kSentinel}}, + {{404, 611, kSentinel}}, // NOLINT + {{405, 502, kSentinel}}, + {{406, 617, kSentinel}}, + {{407, 616, kSentinel}}, + {{408, 409, kSentinel}}, // NOLINT + {{410, 573, kSentinel}}, + {{412, 623, kSentinel}}, + {{413, 626, kSentinel}}, + {{414, 544, kSentinel}}, // NOLINT + {{415, 629, kSentinel}}, + {{416, 417, kSentinel}}, + {{418, 419, kSentinel}}, + {{420, 421, kSentinel}}, // NOLINT + {{422, 640, kSentinel}}, + {{423, 424, kSentinel}}, + {{425, 643, kSentinel}}, + {{428, 429, kSentinel}}, // NOLINT + {{430, 648, kSentinel}}, + {{431, 432, kSentinel}}, + {{433, 650, kSentinel}}, + {{434, 651, kSentinel}}, // NOLINT + {{435, 436, kSentinel}}, + {{437, 438, kSentinel}}, + {{439, 658, kSentinel}}, + {{440, 441, kSentinel}}, // NOLINT + {{444, 445, kSentinel}}, + {{447, 503, kSentinel}}, + {{452, 453, 454, kSentinel}}, + {{455, 456, 457, kSentinel}}, // NOLINT + {{458, 459, 460, kSentinel}}, + {{461, 462, kSentinel}}, + {{463, 464, kSentinel}}, + {{465, 466, kSentinel}}, // NOLINT + {{467, 468, kSentinel}}, + {{469, 470, kSentinel}}, + {{471, 472, kSentinel}}, + {{473, 474, kSentinel}}, // NOLINT + {{475, 476, kSentinel}}, + {{478, 479, kSentinel}}, + {{480, 481, kSentinel}}, + {{482, 483, kSentinel}}, // NOLINT + {{484, 485, kSentinel}}, + {{486, 487, kSentinel}}, + {{488, 489, kSentinel}}, + {{490, 491, kSentinel}}, // NOLINT + {{492, 493, kSentinel}}, + {{494, 495, kSentinel}}, + {{497, 498, 499, kSentinel}}, + {{500, 501, kSentinel}}, // NOLINT + {{504, 505, kSentinel}}, + {{506, 507, kSentinel}}, + {{508, 509, kSentinel}}, + {{510, 511, kSentinel}}, // NOLINT + {{512, 513, kSentinel}}, + {{514, 515, kSentinel}}, + {{516, 517, kSentinel}}, + {{518, 519, kSentinel}}, // NOLINT + {{520, 521, kSentinel}}, + {{522, 523, kSentinel}}, + {{524, 525, kSentinel}}, + {{526, 527, kSentinel}}, // NOLINT + {{528, 529, kSentinel}}, + {{530, 531, kSentinel}}, + {{532, 533, kSentinel}}, + {{534, 535, kSentinel}}, // NOLINT + {{536, 537, kSentinel}}, + {{538, 539, kSentinel}}, + {{540, 541, kSentinel}}, + {{542, 543, kSentinel}}, // NOLINT + {{546, 547, kSentinel}}, + {{548, 549, kSentinel}}, + {{550, 551, kSentinel}}, + {{552, 553, kSentinel}}, // NOLINT + {{554, 555, kSentinel}}, + {{556, 557, kSentinel}}, + {{558, 559, kSentinel}}, + {{560, 561, kSentinel}}, // NOLINT + {{562, 563, kSentinel}}, + {{570, 11365, kSentinel}}, + {{571, 572, kSentinel}}, + {{574, 11366, kSentinel}}, // NOLINT + {{575, 11390, kSentinel}}, + {{576, 11391, kSentinel}}, + {{577, 578, kSentinel}}, + {{580, 649, kSentinel}}, // NOLINT + {{581, 652, kSentinel}}, + {{582, 583, kSentinel}}, + {{584, 585, kSentinel}}, + {{586, 587, kSentinel}}, // NOLINT + {{588, 589, kSentinel}}, + {{590, 591, kSentinel}}, + {{592, 11375, kSentinel}}, + {{593, 11373, kSentinel}}, // NOLINT + {{594, 11376, kSentinel}}, + {{604, 42923, kSentinel}}, + {{609, 42924, kSentinel}}, + {{613, 42893, kSentinel}}, // NOLINT + {{614, 42922, kSentinel}}, + {{619, 11362, kSentinel}}, + {{620, 42925, kSentinel}}, + {{625, 11374, kSentinel}}, // NOLINT + {{637, 11364, kSentinel}}, + {{647, 42929, kSentinel}}, + {{670, 42928, kSentinel}}, + {{837, 921, 953, 8126}}, // NOLINT + {{880, 881, kSentinel}}, + {{882, 883, kSentinel}}, + {{886, 887, kSentinel}}, + {{891, 1021, kSentinel}}, // NOLINT + {{893, 1023, kSentinel}}, + {{895, 1011, kSentinel}}, + {{902, 940, kSentinel}}, + {{904, 941, kSentinel}}, // NOLINT + {{906, 943, kSentinel}}, + {{908, 972, kSentinel}}, + {{910, 973, kSentinel}}, + {{911, 974, kSentinel}}, // NOLINT + {{913, 945, kSentinel}}, + {{914, 946, 976, kSentinel}}, + {{915, 947, kSentinel}}, + {{916, 948, kSentinel}}, // NOLINT + {{917, 949, 1013, kSentinel}}, + {{918, 950, kSentinel}}, + {{919, 951, kSentinel}}, + {{920, 952, 977, kSentinel}}, // NOLINT + {{922, 954, 1008, kSentinel}}, + {{923, 955, kSentinel}}, + {{925, 957, kSentinel}}, + {{927, 959, kSentinel}}, // NOLINT + {{928, 960, 982, kSentinel}}, + {{929, 961, 1009, kSentinel}}, + {{931, 962, 963, kSentinel}}, + {{932, 964, kSentinel}}, // NOLINT + {{933, 965, kSentinel}}, + {{934, 966, 981, kSentinel}}, + {{935, 967, kSentinel}}, + {{939, 971, kSentinel}}, // NOLINT + {{975, 983, kSentinel}}, + {{984, 985, kSentinel}}, + {{986, 987, kSentinel}}, + {{988, 989, kSentinel}}, // NOLINT + {{990, 991, kSentinel}}, + {{992, 993, kSentinel}}, + {{994, 995, kSentinel}}, + {{996, 997, kSentinel}}, // NOLINT + {{998, 999, kSentinel}}, + {{1000, 1001, kSentinel}}, + {{1002, 1003, kSentinel}}, + {{1004, 1005, kSentinel}}, // NOLINT + {{1006, 1007, kSentinel}}, + {{1010, 1017, kSentinel}}, + {{1015, 1016, kSentinel}}, + {{1018, 1019, kSentinel}}, // NOLINT + {{1024, 1104, kSentinel}}, + {{1039, 1119, kSentinel}}, + {{1040, 1072, kSentinel}}, + {{1071, 1103, kSentinel}}, // NOLINT + {{1120, 1121, kSentinel}}, + {{1122, 1123, kSentinel}}, + {{1124, 1125, kSentinel}}, + {{1126, 1127, kSentinel}}, // NOLINT + {{1128, 1129, kSentinel}}, + {{1130, 1131, kSentinel}}, + {{1132, 1133, kSentinel}}, + {{1134, 1135, kSentinel}}, // NOLINT + {{1136, 1137, kSentinel}}, + {{1138, 1139, kSentinel}}, + {{1140, 1141, kSentinel}}, + {{1142, 1143, kSentinel}}, // NOLINT + {{1144, 1145, kSentinel}}, + {{1146, 1147, kSentinel}}, + {{1148, 1149, kSentinel}}, + {{1150, 1151, kSentinel}}, // NOLINT + {{1152, 1153, kSentinel}}, + {{1162, 1163, kSentinel}}, + {{1164, 1165, kSentinel}}, + {{1166, 1167, kSentinel}}, // NOLINT + {{1168, 1169, kSentinel}}, + {{1170, 1171, kSentinel}}, + {{1172, 1173, kSentinel}}, + {{1174, 1175, kSentinel}}, // NOLINT + {{1176, 1177, kSentinel}}, + {{1178, 1179, kSentinel}}, + {{1180, 1181, kSentinel}}, + {{1182, 1183, kSentinel}}, // NOLINT + {{1184, 1185, kSentinel}}, + {{1186, 1187, kSentinel}}, + {{1188, 1189, kSentinel}}, + {{1190, 1191, kSentinel}}, // NOLINT + {{1192, 1193, kSentinel}}, + {{1194, 1195, kSentinel}}, + {{1196, 1197, kSentinel}}, + {{1198, 1199, kSentinel}}, // NOLINT + {{1200, 1201, kSentinel}}, + {{1202, 1203, kSentinel}}, + {{1204, 1205, kSentinel}}, + {{1206, 1207, kSentinel}}, // NOLINT + {{1208, 1209, kSentinel}}, + {{1210, 1211, kSentinel}}, + {{1212, 1213, kSentinel}}, + {{1214, 1215, kSentinel}}, // NOLINT + {{1216, 1231, kSentinel}}, + {{1217, 1218, kSentinel}}, + {{1219, 1220, kSentinel}}, + {{1221, 1222, kSentinel}}, // NOLINT + {{1223, 1224, kSentinel}}, + {{1225, 1226, kSentinel}}, + {{1227, 1228, kSentinel}}, + {{1229, 1230, kSentinel}}, // NOLINT + {{1232, 1233, kSentinel}}, + {{1234, 1235, kSentinel}}, + {{1236, 1237, kSentinel}}, + {{1238, 1239, kSentinel}}, // NOLINT + {{1240, 1241, kSentinel}}, + {{1242, 1243, kSentinel}}, + {{1244, 1245, kSentinel}}, + {{1246, 1247, kSentinel}}, // NOLINT + {{1248, 1249, kSentinel}}, + {{1250, 1251, kSentinel}}, + {{1252, 1253, kSentinel}}, + {{1254, 1255, kSentinel}}, // NOLINT + {{1256, 1257, kSentinel}}, + {{1258, 1259, kSentinel}}, + {{1260, 1261, kSentinel}}, + {{1262, 1263, kSentinel}}, // NOLINT + {{1264, 1265, kSentinel}}, + {{1266, 1267, kSentinel}}, + {{1268, 1269, kSentinel}}, + {{1270, 1271, kSentinel}}, // NOLINT + {{1272, 1273, kSentinel}}, + {{1274, 1275, kSentinel}}, + {{1276, 1277, kSentinel}}, + {{1278, 1279, kSentinel}}, // NOLINT + {{1280, 1281, kSentinel}}, + {{1282, 1283, kSentinel}}, + {{1284, 1285, kSentinel}}, + {{1286, 1287, kSentinel}}, // NOLINT + {{1288, 1289, kSentinel}}, + {{1290, 1291, kSentinel}}, + {{1292, 1293, kSentinel}}, + {{1294, 1295, kSentinel}}, // NOLINT + {{1296, 1297, kSentinel}}, + {{1298, 1299, kSentinel}}, + {{1300, 1301, kSentinel}}, + {{1302, 1303, kSentinel}}, // NOLINT + {{1304, 1305, kSentinel}}, + {{1306, 1307, kSentinel}}, + {{1308, 1309, kSentinel}}, + {{1310, 1311, kSentinel}}, // NOLINT + {{1312, 1313, kSentinel}}, + {{1314, 1315, kSentinel}}, + {{1316, 1317, kSentinel}}, + {{1318, 1319, kSentinel}}, // NOLINT + {{1320, 1321, kSentinel}}, + {{1322, 1323, kSentinel}}, + {{1324, 1325, kSentinel}}, + {{1326, 1327, kSentinel}}, // NOLINT + {{1329, 1377, kSentinel}}, + {{1366, 1414, kSentinel}}, + {{4256, 11520, kSentinel}}, + {{4293, 11557, kSentinel}}, // NOLINT + {{4295, 11559, kSentinel}}, + {{4301, 11565, kSentinel}}, + {{7545, 42877, kSentinel}}, + {{7549, 11363, kSentinel}}, // NOLINT + {{7680, 7681, kSentinel}}, + {{7682, 7683, kSentinel}}, + {{7684, 7685, kSentinel}}, + {{7686, 7687, kSentinel}}, // NOLINT + {{7688, 7689, kSentinel}}, + {{7690, 7691, kSentinel}}, + {{7692, 7693, kSentinel}}, + {{7694, 7695, kSentinel}}, // NOLINT + {{7696, 7697, kSentinel}}, + {{7698, 7699, kSentinel}}, + {{7700, 7701, kSentinel}}, + {{7702, 7703, kSentinel}}, // NOLINT + {{7704, 7705, kSentinel}}, + {{7706, 7707, kSentinel}}, + {{7708, 7709, kSentinel}}, + {{7710, 7711, kSentinel}}, // NOLINT + {{7712, 7713, kSentinel}}, + {{7714, 7715, kSentinel}}, + {{7716, 7717, kSentinel}}, + {{7718, 7719, kSentinel}}, // NOLINT + {{7720, 7721, kSentinel}}, + {{7722, 7723, kSentinel}}, + {{7724, 7725, kSentinel}}, + {{7726, 7727, kSentinel}}, // NOLINT + {{7728, 7729, kSentinel}}, + {{7730, 7731, kSentinel}}, + {{7732, 7733, kSentinel}}, + {{7734, 7735, kSentinel}}, // NOLINT + {{7736, 7737, kSentinel}}, + {{7738, 7739, kSentinel}}, + {{7740, 7741, kSentinel}}, + {{7742, 7743, kSentinel}}, // NOLINT + {{7744, 7745, kSentinel}}, + {{7746, 7747, kSentinel}}, + {{7748, 7749, kSentinel}}, + {{7750, 7751, kSentinel}}, // NOLINT + {{7752, 7753, kSentinel}}, + {{7754, 7755, kSentinel}}, + {{7756, 7757, kSentinel}}, + {{7758, 7759, kSentinel}}, // NOLINT + {{7760, 7761, kSentinel}}, + {{7762, 7763, kSentinel}}, + {{7764, 7765, kSentinel}}, + {{7766, 7767, kSentinel}}, // NOLINT + {{7768, 7769, kSentinel}}, + {{7770, 7771, kSentinel}}, + {{7772, 7773, kSentinel}}, + {{7774, 7775, kSentinel}}, // NOLINT + {{7776, 7777, 7835, kSentinel}}, + {{7778, 7779, kSentinel}}, + {{7780, 7781, kSentinel}}, + {{7782, 7783, kSentinel}}, // NOLINT + {{7784, 7785, kSentinel}}, + {{7786, 7787, kSentinel}}, + {{7788, 7789, kSentinel}}, + {{7790, 7791, kSentinel}}, // NOLINT + {{7792, 7793, kSentinel}}, + {{7794, 7795, kSentinel}}, + {{7796, 7797, kSentinel}}, + {{7798, 7799, kSentinel}}, // NOLINT + {{7800, 7801, kSentinel}}, + {{7802, 7803, kSentinel}}, + {{7804, 7805, kSentinel}}, + {{7806, 7807, kSentinel}}, // NOLINT + {{7808, 7809, kSentinel}}, + {{7810, 7811, kSentinel}}, + {{7812, 7813, kSentinel}}, + {{7814, 7815, kSentinel}}, // NOLINT + {{7816, 7817, kSentinel}}, + {{7818, 7819, kSentinel}}, + {{7820, 7821, kSentinel}}, + {{7822, 7823, kSentinel}}, // NOLINT + {{7824, 7825, kSentinel}}, + {{7826, 7827, kSentinel}}, + {{7828, 7829, kSentinel}}, + {{7840, 7841, kSentinel}}, // NOLINT + {{7842, 7843, kSentinel}}, + {{7844, 7845, kSentinel}}, + {{7846, 7847, kSentinel}}, + {{7848, 7849, kSentinel}}, // NOLINT + {{7850, 7851, kSentinel}}, + {{7852, 7853, kSentinel}}, + {{7854, 7855, kSentinel}}, + {{7856, 7857, kSentinel}}, // NOLINT + {{7858, 7859, kSentinel}}, + {{7860, 7861, kSentinel}}, + {{7862, 7863, kSentinel}}, + {{7864, 7865, kSentinel}}, // NOLINT + {{7866, 7867, kSentinel}}, + {{7868, 7869, kSentinel}}, + {{7870, 7871, kSentinel}}, + {{7872, 7873, kSentinel}}, // NOLINT + {{7874, 7875, kSentinel}}, + {{7876, 7877, kSentinel}}, + {{7878, 7879, kSentinel}}, + {{7880, 7881, kSentinel}}, // NOLINT + {{7882, 7883, kSentinel}}, + {{7884, 7885, kSentinel}}, + {{7886, 7887, kSentinel}}, + {{7888, 7889, kSentinel}}, // NOLINT + {{7890, 7891, kSentinel}}, + {{7892, 7893, kSentinel}}, + {{7894, 7895, kSentinel}}, + {{7896, 7897, kSentinel}}, // NOLINT + {{7898, 7899, kSentinel}}, + {{7900, 7901, kSentinel}}, + {{7902, 7903, kSentinel}}, + {{7904, 7905, kSentinel}}, // NOLINT + {{7906, 7907, kSentinel}}, + {{7908, 7909, kSentinel}}, + {{7910, 7911, kSentinel}}, + {{7912, 7913, kSentinel}}, // NOLINT + {{7914, 7915, kSentinel}}, + {{7916, 7917, kSentinel}}, + {{7918, 7919, kSentinel}}, + {{7920, 7921, kSentinel}}, // NOLINT + {{7922, 7923, kSentinel}}, + {{7924, 7925, kSentinel}}, + {{7926, 7927, kSentinel}}, + {{7928, 7929, kSentinel}}, // NOLINT + {{7930, 7931, kSentinel}}, + {{7932, 7933, kSentinel}}, + {{7934, 7935, kSentinel}}, + {{7936, 7944, kSentinel}}, // NOLINT + {{7943, 7951, kSentinel}}, + {{7952, 7960, kSentinel}}, + {{7957, 7965, kSentinel}}, + {{7968, 7976, kSentinel}}, // NOLINT + {{7975, 7983, kSentinel}}, + {{7984, 7992, kSentinel}}, + {{7991, 7999, kSentinel}}, + {{8000, 8008, kSentinel}}, // NOLINT + {{8005, 8013, kSentinel}}, + {{8017, 8025, kSentinel}}, + {{8019, 8027, kSentinel}}, + {{8021, 8029, kSentinel}}, // NOLINT + {{8023, 8031, kSentinel}}, + {{8032, 8040, kSentinel}}, + {{8039, 8047, kSentinel}}, + {{8048, 8122, kSentinel}}, // NOLINT + {{8049, 8123, kSentinel}}, + {{8050, 8136, kSentinel}}, + {{8053, 8139, kSentinel}}, + {{8054, 8154, kSentinel}}, // NOLINT + {{8055, 8155, kSentinel}}, + {{8056, 8184, kSentinel}}, + {{8057, 8185, kSentinel}}, + {{8058, 8170, kSentinel}}, // NOLINT + {{8059, 8171, kSentinel}}, + {{8060, 8186, kSentinel}}, + {{8061, 8187, kSentinel}}, + {{8112, 8120, kSentinel}}, // NOLINT + {{8113, 8121, kSentinel}}, + {{8144, 8152, kSentinel}}, + {{8145, 8153, kSentinel}}, + {{8160, 8168, kSentinel}}, // NOLINT + {{8161, 8169, kSentinel}}, + {{8165, 8172, kSentinel}}, + {{kSentinel}}}; // NOLINT +static const uint16_t kEcma262UnCanonicalizeTable0Size = 1005; // NOLINT +static const int32_t kEcma262UnCanonicalizeTable0[2010] = { + 1073741889, 1, 90, 5, 1073741921, 1, + 122, 5, 181, 9, 1073742016, 13, + 214, 17, 1073742040, 21, // NOLINT + 222, 25, 1073742048, 13, 246, 17, + 1073742072, 21, 254, 25, 255, 29, + 256, 33, 257, 33, // NOLINT + 258, 37, 259, 37, 260, 41, + 261, 41, 262, 45, 263, 45, + 264, 49, 265, 49, // NOLINT + 266, 53, 267, 53, 268, 57, + 269, 57, 270, 61, 271, 61, + 272, 65, 273, 65, // NOLINT + 274, 69, 275, 69, 276, 73, + 277, 73, 278, 77, 279, 77, + 280, 81, 281, 81, // NOLINT + 282, 85, 283, 85, 284, 89, + 285, 89, 286, 93, 287, 93, + 288, 97, 289, 97, // NOLINT + 290, 101, 291, 101, 292, 105, + 293, 105, 294, 109, 295, 109, + 296, 113, 297, 113, // NOLINT + 298, 117, 299, 117, 300, 121, + 301, 121, 302, 125, 303, 125, + 306, 129, 307, 129, // NOLINT + 308, 133, 309, 133, 310, 137, + 311, 137, 313, 141, 314, 141, + 315, 145, 316, 145, // NOLINT + 317, 149, 318, 149, 319, 153, + 320, 153, 321, 157, 322, 157, + 323, 161, 324, 161, // NOLINT + 325, 165, 326, 165, 327, 169, + 328, 169, 330, 173, 331, 173, + 332, 177, 333, 177, // NOLINT + 334, 181, 335, 181, 336, 185, + 337, 185, 338, 189, 339, 189, + 340, 193, 341, 193, // NOLINT + 342, 197, 343, 197, 344, 201, + 345, 201, 346, 205, 347, 205, + 348, 209, 349, 209, // NOLINT + 350, 213, 351, 213, 352, 217, + 353, 217, 354, 221, 355, 221, + 356, 225, 357, 225, // NOLINT + 358, 229, 359, 229, 360, 233, + 361, 233, 362, 237, 363, 237, + 364, 241, 365, 241, // NOLINT + 366, 245, 367, 245, 368, 249, + 369, 249, 370, 253, 371, 253, + 372, 257, 373, 257, // NOLINT + 374, 261, 375, 261, 376, 29, + 377, 265, 378, 265, 379, 269, + 380, 269, 381, 273, // NOLINT + 382, 273, 384, 277, 385, 281, + 386, 285, 387, 285, 388, 289, + 389, 289, 390, 293, // NOLINT + 391, 297, 392, 297, 1073742217, 301, + 394, 305, 395, 309, 396, 309, + 398, 313, 399, 317, // NOLINT + 400, 321, 401, 325, 402, 325, + 403, 329, 404, 333, 405, 337, + 406, 341, 407, 345, // NOLINT + 408, 349, 409, 349, 410, 353, + 412, 357, 413, 361, 414, 365, + 415, 369, 416, 373, // NOLINT + 417, 373, 418, 377, 419, 377, + 420, 381, 421, 381, 422, 385, + 423, 389, 424, 389, // NOLINT + 425, 393, 428, 397, 429, 397, + 430, 401, 431, 405, 432, 405, + 1073742257, 409, 434, 413, // NOLINT + 435, 417, 436, 417, 437, 421, + 438, 421, 439, 425, 440, 429, + 441, 429, 444, 433, // NOLINT + 445, 433, 447, 437, 452, 441, + 453, 441, 454, 441, 455, 445, + 456, 445, 457, 445, // NOLINT + 458, 449, 459, 449, 460, 449, + 461, 453, 462, 453, 463, 457, + 464, 457, 465, 461, // NOLINT + 466, 461, 467, 465, 468, 465, + 469, 469, 470, 469, 471, 473, + 472, 473, 473, 477, // NOLINT + 474, 477, 475, 481, 476, 481, + 477, 313, 478, 485, 479, 485, + 480, 489, 481, 489, // NOLINT + 482, 493, 483, 493, 484, 497, + 485, 497, 486, 501, 487, 501, + 488, 505, 489, 505, // NOLINT + 490, 509, 491, 509, 492, 513, + 493, 513, 494, 517, 495, 517, + 497, 521, 498, 521, // NOLINT + 499, 521, 500, 525, 501, 525, + 502, 337, 503, 437, 504, 529, + 505, 529, 506, 533, // NOLINT + 507, 533, 508, 537, 509, 537, + 510, 541, 511, 541, 512, 545, + 513, 545, 514, 549, // NOLINT + 515, 549, 516, 553, 517, 553, + 518, 557, 519, 557, 520, 561, + 521, 561, 522, 565, // NOLINT + 523, 565, 524, 569, 525, 569, + 526, 573, 527, 573, 528, 577, + 529, 577, 530, 581, // NOLINT + 531, 581, 532, 585, 533, 585, + 534, 589, 535, 589, 536, 593, + 537, 593, 538, 597, // NOLINT + 539, 597, 540, 601, 541, 601, + 542, 605, 543, 605, 544, 365, + 546, 609, 547, 609, // NOLINT + 548, 613, 549, 613, 550, 617, + 551, 617, 552, 621, 553, 621, + 554, 625, 555, 625, // NOLINT + 556, 629, 557, 629, 558, 633, + 559, 633, 560, 637, 561, 637, + 562, 641, 563, 641, // NOLINT + 570, 645, 571, 649, 572, 649, + 573, 353, 574, 653, 1073742399, 657, + 576, 661, 577, 665, // NOLINT + 578, 665, 579, 277, 580, 669, + 581, 673, 582, 677, 583, 677, + 584, 681, 585, 681, // NOLINT + 586, 685, 587, 685, 588, 689, + 589, 689, 590, 693, 591, 693, + 592, 697, 593, 701, // NOLINT + 594, 705, 595, 281, 596, 293, + 1073742422, 301, 599, 305, 601, 317, + 603, 321, 604, 709, // NOLINT + 608, 329, 609, 713, 611, 333, + 613, 717, 614, 721, 616, 345, + 617, 341, 619, 725, // NOLINT + 620, 729, 623, 357, 625, 733, + 626, 361, 629, 369, 637, 737, + 640, 385, 643, 393, // NOLINT + 647, 741, 648, 401, 649, 669, + 1073742474, 409, 651, 413, 652, 673, + 658, 425, 670, 745, // NOLINT + 837, 749, 880, 753, 881, 753, + 882, 757, 883, 757, 886, 761, + 887, 761, 1073742715, 765, // NOLINT + 893, 769, 895, 773, 902, 777, + 1073742728, 781, 906, 785, 908, 789, + 1073742734, 793, 911, 797, // NOLINT + 913, 801, 914, 805, 1073742739, 809, + 916, 813, 917, 817, 1073742742, 821, + 919, 825, 920, 829, // NOLINT + 921, 749, 922, 833, 923, 837, + 924, 9, 1073742749, 841, 927, 845, + 928, 849, 929, 853, // NOLINT + 931, 857, 1073742756, 861, 933, 865, + 934, 869, 1073742759, 873, 939, 877, + 940, 777, 1073742765, 781, // NOLINT + 943, 785, 945, 801, 946, 805, + 1073742771, 809, 948, 813, 949, 817, + 1073742774, 821, 951, 825, // NOLINT + 952, 829, 953, 749, 954, 833, + 955, 837, 956, 9, 1073742781, 841, + 959, 845, 960, 849, // NOLINT + 961, 853, 962, 857, 963, 857, + 1073742788, 861, 965, 865, 966, 869, + 1073742791, 873, 971, 877, // NOLINT + 972, 789, 1073742797, 793, 974, 797, + 975, 881, 976, 805, 977, 829, + 981, 869, 982, 849, // NOLINT + 983, 881, 984, 885, 985, 885, + 986, 889, 987, 889, 988, 893, + 989, 893, 990, 897, // NOLINT + 991, 897, 992, 901, 993, 901, + 994, 905, 995, 905, 996, 909, + 997, 909, 998, 913, // NOLINT + 999, 913, 1000, 917, 1001, 917, + 1002, 921, 1003, 921, 1004, 925, + 1005, 925, 1006, 929, // NOLINT + 1007, 929, 1008, 833, 1009, 853, + 1010, 933, 1011, 773, 1013, 817, + 1015, 937, 1016, 937, // NOLINT + 1017, 933, 1018, 941, 1019, 941, + 1073742845, 765, 1023, 769, 1073742848, 945, + 1039, 949, 1073742864, 953, // NOLINT + 1071, 957, 1073742896, 953, 1103, 957, + 1073742928, 945, 1119, 949, 1120, 961, + 1121, 961, 1122, 965, // NOLINT + 1123, 965, 1124, 969, 1125, 969, + 1126, 973, 1127, 973, 1128, 977, + 1129, 977, 1130, 981, // NOLINT + 1131, 981, 1132, 985, 1133, 985, + 1134, 989, 1135, 989, 1136, 993, + 1137, 993, 1138, 997, // NOLINT + 1139, 997, 1140, 1001, 1141, 1001, + 1142, 1005, 1143, 1005, 1144, 1009, + 1145, 1009, 1146, 1013, // NOLINT + 1147, 1013, 1148, 1017, 1149, 1017, + 1150, 1021, 1151, 1021, 1152, 1025, + 1153, 1025, 1162, 1029, // NOLINT + 1163, 1029, 1164, 1033, 1165, 1033, + 1166, 1037, 1167, 1037, 1168, 1041, + 1169, 1041, 1170, 1045, // NOLINT + 1171, 1045, 1172, 1049, 1173, 1049, + 1174, 1053, 1175, 1053, 1176, 1057, + 1177, 1057, 1178, 1061, // NOLINT + 1179, 1061, 1180, 1065, 1181, 1065, + 1182, 1069, 1183, 1069, 1184, 1073, + 1185, 1073, 1186, 1077, // NOLINT + 1187, 1077, 1188, 1081, 1189, 1081, + 1190, 1085, 1191, 1085, 1192, 1089, + 1193, 1089, 1194, 1093, // NOLINT + 1195, 1093, 1196, 1097, 1197, 1097, + 1198, 1101, 1199, 1101, 1200, 1105, + 1201, 1105, 1202, 1109, // NOLINT + 1203, 1109, 1204, 1113, 1205, 1113, + 1206, 1117, 1207, 1117, 1208, 1121, + 1209, 1121, 1210, 1125, // NOLINT + 1211, 1125, 1212, 1129, 1213, 1129, + 1214, 1133, 1215, 1133, 1216, 1137, + 1217, 1141, 1218, 1141, // NOLINT + 1219, 1145, 1220, 1145, 1221, 1149, + 1222, 1149, 1223, 1153, 1224, 1153, + 1225, 1157, 1226, 1157, // NOLINT + 1227, 1161, 1228, 1161, 1229, 1165, + 1230, 1165, 1231, 1137, 1232, 1169, + 1233, 1169, 1234, 1173, // NOLINT + 1235, 1173, 1236, 1177, 1237, 1177, + 1238, 1181, 1239, 1181, 1240, 1185, + 1241, 1185, 1242, 1189, // NOLINT + 1243, 1189, 1244, 1193, 1245, 1193, + 1246, 1197, 1247, 1197, 1248, 1201, + 1249, 1201, 1250, 1205, // NOLINT + 1251, 1205, 1252, 1209, 1253, 1209, + 1254, 1213, 1255, 1213, 1256, 1217, + 1257, 1217, 1258, 1221, // NOLINT + 1259, 1221, 1260, 1225, 1261, 1225, + 1262, 1229, 1263, 1229, 1264, 1233, + 1265, 1233, 1266, 1237, // NOLINT + 1267, 1237, 1268, 1241, 1269, 1241, + 1270, 1245, 1271, 1245, 1272, 1249, + 1273, 1249, 1274, 1253, // NOLINT + 1275, 1253, 1276, 1257, 1277, 1257, + 1278, 1261, 1279, 1261, 1280, 1265, + 1281, 1265, 1282, 1269, // NOLINT + 1283, 1269, 1284, 1273, 1285, 1273, + 1286, 1277, 1287, 1277, 1288, 1281, + 1289, 1281, 1290, 1285, // NOLINT + 1291, 1285, 1292, 1289, 1293, 1289, + 1294, 1293, 1295, 1293, 1296, 1297, + 1297, 1297, 1298, 1301, // NOLINT + 1299, 1301, 1300, 1305, 1301, 1305, + 1302, 1309, 1303, 1309, 1304, 1313, + 1305, 1313, 1306, 1317, // NOLINT + 1307, 1317, 1308, 1321, 1309, 1321, + 1310, 1325, 1311, 1325, 1312, 1329, + 1313, 1329, 1314, 1333, // NOLINT + 1315, 1333, 1316, 1337, 1317, 1337, + 1318, 1341, 1319, 1341, 1320, 1345, + 1321, 1345, 1322, 1349, // NOLINT + 1323, 1349, 1324, 1353, 1325, 1353, + 1326, 1357, 1327, 1357, 1073743153, 1361, + 1366, 1365, 1073743201, 1361, // NOLINT + 1414, 1365, 1073746080, 1369, 4293, 1373, + 4295, 1377, 4301, 1381, 7545, 1385, + 7549, 1389, 7680, 1393, // NOLINT + 7681, 1393, 7682, 1397, 7683, 1397, + 7684, 1401, 7685, 1401, 7686, 1405, + 7687, 1405, 7688, 1409, // NOLINT + 7689, 1409, 7690, 1413, 7691, 1413, + 7692, 1417, 7693, 1417, 7694, 1421, + 7695, 1421, 7696, 1425, // NOLINT + 7697, 1425, 7698, 1429, 7699, 1429, + 7700, 1433, 7701, 1433, 7702, 1437, + 7703, 1437, 7704, 1441, // NOLINT + 7705, 1441, 7706, 1445, 7707, 1445, + 7708, 1449, 7709, 1449, 7710, 1453, + 7711, 1453, 7712, 1457, // NOLINT + 7713, 1457, 7714, 1461, 7715, 1461, + 7716, 1465, 7717, 1465, 7718, 1469, + 7719, 1469, 7720, 1473, // NOLINT + 7721, 1473, 7722, 1477, 7723, 1477, + 7724, 1481, 7725, 1481, 7726, 1485, + 7727, 1485, 7728, 1489, // NOLINT + 7729, 1489, 7730, 1493, 7731, 1493, + 7732, 1497, 7733, 1497, 7734, 1501, + 7735, 1501, 7736, 1505, // NOLINT + 7737, 1505, 7738, 1509, 7739, 1509, + 7740, 1513, 7741, 1513, 7742, 1517, + 7743, 1517, 7744, 1521, // NOLINT + 7745, 1521, 7746, 1525, 7747, 1525, + 7748, 1529, 7749, 1529, 7750, 1533, + 7751, 1533, 7752, 1537, // NOLINT + 7753, 1537, 7754, 1541, 7755, 1541, + 7756, 1545, 7757, 1545, 7758, 1549, + 7759, 1549, 7760, 1553, // NOLINT + 7761, 1553, 7762, 1557, 7763, 1557, + 7764, 1561, 7765, 1561, 7766, 1565, + 7767, 1565, 7768, 1569, // NOLINT + 7769, 1569, 7770, 1573, 7771, 1573, + 7772, 1577, 7773, 1577, 7774, 1581, + 7775, 1581, 7776, 1585, // NOLINT + 7777, 1585, 7778, 1589, 7779, 1589, + 7780, 1593, 7781, 1593, 7782, 1597, + 7783, 1597, 7784, 1601, // NOLINT + 7785, 1601, 7786, 1605, 7787, 1605, + 7788, 1609, 7789, 1609, 7790, 1613, + 7791, 1613, 7792, 1617, // NOLINT + 7793, 1617, 7794, 1621, 7795, 1621, + 7796, 1625, 7797, 1625, 7798, 1629, + 7799, 1629, 7800, 1633, // NOLINT + 7801, 1633, 7802, 1637, 7803, 1637, + 7804, 1641, 7805, 1641, 7806, 1645, + 7807, 1645, 7808, 1649, // NOLINT + 7809, 1649, 7810, 1653, 7811, 1653, + 7812, 1657, 7813, 1657, 7814, 1661, + 7815, 1661, 7816, 1665, // NOLINT + 7817, 1665, 7818, 1669, 7819, 1669, + 7820, 1673, 7821, 1673, 7822, 1677, + 7823, 1677, 7824, 1681, // NOLINT + 7825, 1681, 7826, 1685, 7827, 1685, + 7828, 1689, 7829, 1689, 7835, 1585, + 7840, 1693, 7841, 1693, // NOLINT + 7842, 1697, 7843, 1697, 7844, 1701, + 7845, 1701, 7846, 1705, 7847, 1705, + 7848, 1709, 7849, 1709, // NOLINT + 7850, 1713, 7851, 1713, 7852, 1717, + 7853, 1717, 7854, 1721, 7855, 1721, + 7856, 1725, 7857, 1725, // NOLINT + 7858, 1729, 7859, 1729, 7860, 1733, + 7861, 1733, 7862, 1737, 7863, 1737, + 7864, 1741, 7865, 1741, // NOLINT + 7866, 1745, 7867, 1745, 7868, 1749, + 7869, 1749, 7870, 1753, 7871, 1753, + 7872, 1757, 7873, 1757, // NOLINT + 7874, 1761, 7875, 1761, 7876, 1765, + 7877, 1765, 7878, 1769, 7879, 1769, + 7880, 1773, 7881, 1773, // NOLINT + 7882, 1777, 7883, 1777, 7884, 1781, + 7885, 1781, 7886, 1785, 7887, 1785, + 7888, 1789, 7889, 1789, // NOLINT + 7890, 1793, 7891, 1793, 7892, 1797, + 7893, 1797, 7894, 1801, 7895, 1801, + 7896, 1805, 7897, 1805, // NOLINT + 7898, 1809, 7899, 1809, 7900, 1813, + 7901, 1813, 7902, 1817, 7903, 1817, + 7904, 1821, 7905, 1821, // NOLINT + 7906, 1825, 7907, 1825, 7908, 1829, + 7909, 1829, 7910, 1833, 7911, 1833, + 7912, 1837, 7913, 1837, // NOLINT + 7914, 1841, 7915, 1841, 7916, 1845, + 7917, 1845, 7918, 1849, 7919, 1849, + 7920, 1853, 7921, 1853, // NOLINT + 7922, 1857, 7923, 1857, 7924, 1861, + 7925, 1861, 7926, 1865, 7927, 1865, + 7928, 1869, 7929, 1869, // NOLINT + 7930, 1873, 7931, 1873, 7932, 1877, + 7933, 1877, 7934, 1881, 7935, 1881, + 1073749760, 1885, 7943, 1889, // NOLINT + 1073749768, 1885, 7951, 1889, 1073749776, 1893, + 7957, 1897, 1073749784, 1893, 7965, 1897, + 1073749792, 1901, 7975, 1905, // NOLINT + 1073749800, 1901, 7983, 1905, 1073749808, 1909, + 7991, 1913, 1073749816, 1909, 7999, 1913, + 1073749824, 1917, 8005, 1921, // NOLINT + 1073749832, 1917, 8013, 1921, 8017, 1925, + 8019, 1929, 8021, 1933, 8023, 1937, + 8025, 1925, 8027, 1929, // NOLINT + 8029, 1933, 8031, 1937, 1073749856, 1941, + 8039, 1945, 1073749864, 1941, 8047, 1945, + 1073749872, 1949, 8049, 1953, // NOLINT + 1073749874, 1957, 8053, 1961, 1073749878, 1965, + 8055, 1969, 1073749880, 1973, 8057, 1977, + 1073749882, 1981, 8059, 1985, // NOLINT + 1073749884, 1989, 8061, 1993, 1073749936, 1997, + 8113, 2001, 1073749944, 1997, 8121, 2001, + 1073749946, 1949, 8123, 1953, // NOLINT + 8126, 749, 1073749960, 1957, 8139, 1961, + 1073749968, 2005, 8145, 2009, 1073749976, 2005, + 8153, 2009, 1073749978, 1965, // NOLINT + 8155, 1969, 1073749984, 2013, 8161, 2017, + 8165, 2021, 1073749992, 2013, 8169, 2017, + 1073749994, 1981, 8171, 1985, // NOLINT + 8172, 2021, 1073750008, 1973, 8185, 1977, + 1073750010, 1989, 8187, 1993}; // NOLINT +static const MultiCharacterSpecialCase<2> + kEcma262UnCanonicalizeMultiStrings1[83] = { // NOLINT + {{8498, 8526}}, {{8544, 8560}}, {{8559, 8575}}, + {{8579, 8580}}, // NOLINT + {{9398, 9424}}, {{9423, 9449}}, {{11264, 11312}}, + {{11310, 11358}}, // NOLINT + {{11360, 11361}}, {{619, 11362}}, {{7549, 11363}}, + {{637, 11364}}, // NOLINT + {{570, 11365}}, {{574, 11366}}, {{11367, 11368}}, + {{11369, 11370}}, // NOLINT + {{11371, 11372}}, {{593, 11373}}, {{625, 11374}}, + {{592, 11375}}, // NOLINT + {{594, 11376}}, {{11378, 11379}}, {{11381, 11382}}, + {{575, 11390}}, // NOLINT + {{576, 11391}}, {{11392, 11393}}, {{11394, 11395}}, + {{11396, 11397}}, // NOLINT + {{11398, 11399}}, {{11400, 11401}}, {{11402, 11403}}, + {{11404, 11405}}, // NOLINT + {{11406, 11407}}, {{11408, 11409}}, {{11410, 11411}}, + {{11412, 11413}}, // NOLINT + {{11414, 11415}}, {{11416, 11417}}, {{11418, 11419}}, + {{11420, 11421}}, // NOLINT + {{11422, 11423}}, {{11424, 11425}}, {{11426, 11427}}, + {{11428, 11429}}, // NOLINT + {{11430, 11431}}, {{11432, 11433}}, {{11434, 11435}}, + {{11436, 11437}}, // NOLINT + {{11438, 11439}}, {{11440, 11441}}, {{11442, 11443}}, + {{11444, 11445}}, // NOLINT + {{11446, 11447}}, {{11448, 11449}}, {{11450, 11451}}, + {{11452, 11453}}, // NOLINT + {{11454, 11455}}, {{11456, 11457}}, {{11458, 11459}}, + {{11460, 11461}}, // NOLINT + {{11462, 11463}}, {{11464, 11465}}, {{11466, 11467}}, + {{11468, 11469}}, // NOLINT + {{11470, 11471}}, {{11472, 11473}}, {{11474, 11475}}, + {{11476, 11477}}, // NOLINT + {{11478, 11479}}, {{11480, 11481}}, {{11482, 11483}}, + {{11484, 11485}}, // NOLINT + {{11486, 11487}}, {{11488, 11489}}, {{11490, 11491}}, + {{11499, 11500}}, // NOLINT + {{11501, 11502}}, {{11506, 11507}}, {{4256, 11520}}, + {{4293, 11557}}, // NOLINT + {{4295, 11559}}, {{4301, 11565}}, {{kSentinel}}}; // NOLINT +static const uint16_t kEcma262UnCanonicalizeTable1Size = 149; // NOLINT +static const int32_t kEcma262UnCanonicalizeTable1[298] = { + 306, 1, 334, 1, 1073742176, 5, 367, 9, + 1073742192, 5, 383, 9, 387, 13, 388, 13, // NOLINT + 1073743030, 17, 1231, 21, 1073743056, 17, 1257, 21, + 1073744896, 25, 3118, 29, 1073744944, 25, 3166, 29, // NOLINT + 3168, 33, 3169, 33, 3170, 37, 3171, 41, + 3172, 45, 3173, 49, 3174, 53, 3175, 57, // NOLINT + 3176, 57, 3177, 61, 3178, 61, 3179, 65, + 3180, 65, 3181, 69, 3182, 73, 3183, 77, // NOLINT + 3184, 81, 3186, 85, 3187, 85, 3189, 89, + 3190, 89, 1073745022, 93, 3199, 97, 3200, 101, // NOLINT + 3201, 101, 3202, 105, 3203, 105, 3204, 109, + 3205, 109, 3206, 113, 3207, 113, 3208, 117, // NOLINT + 3209, 117, 3210, 121, 3211, 121, 3212, 125, + 3213, 125, 3214, 129, 3215, 129, 3216, 133, // NOLINT + 3217, 133, 3218, 137, 3219, 137, 3220, 141, + 3221, 141, 3222, 145, 3223, 145, 3224, 149, // NOLINT + 3225, 149, 3226, 153, 3227, 153, 3228, 157, + 3229, 157, 3230, 161, 3231, 161, 3232, 165, // NOLINT + 3233, 165, 3234, 169, 3235, 169, 3236, 173, + 3237, 173, 3238, 177, 3239, 177, 3240, 181, // NOLINT + 3241, 181, 3242, 185, 3243, 185, 3244, 189, + 3245, 189, 3246, 193, 3247, 193, 3248, 197, // NOLINT + 3249, 197, 3250, 201, 3251, 201, 3252, 205, + 3253, 205, 3254, 209, 3255, 209, 3256, 213, // NOLINT + 3257, 213, 3258, 217, 3259, 217, 3260, 221, + 3261, 221, 3262, 225, 3263, 225, 3264, 229, // NOLINT + 3265, 229, 3266, 233, 3267, 233, 3268, 237, + 3269, 237, 3270, 241, 3271, 241, 3272, 245, // NOLINT + 3273, 245, 3274, 249, 3275, 249, 3276, 253, + 3277, 253, 3278, 257, 3279, 257, 3280, 261, // NOLINT + 3281, 261, 3282, 265, 3283, 265, 3284, 269, + 3285, 269, 3286, 273, 3287, 273, 3288, 277, // NOLINT + 3289, 277, 3290, 281, 3291, 281, 3292, 285, + 3293, 285, 3294, 289, 3295, 289, 3296, 293, // NOLINT + 3297, 293, 3298, 297, 3299, 297, 3307, 301, + 3308, 301, 3309, 305, 3310, 305, 3314, 309, // NOLINT + 3315, 309, 1073745152, 313, 3365, 317, 3367, 321, + 3373, 325}; // NOLINT +static const MultiCharacterSpecialCase<2> + kEcma262UnCanonicalizeMultiStrings5[104] = { // NOLINT + {{42560, 42561}}, {{42562, 42563}}, + {{42564, 42565}}, {{42566, 42567}}, // NOLINT + {{42568, 42569}}, {{42570, 42571}}, + {{42572, 42573}}, {{42574, 42575}}, // NOLINT + {{42576, 42577}}, {{42578, 42579}}, + {{42580, 42581}}, {{42582, 42583}}, // NOLINT + {{42584, 42585}}, {{42586, 42587}}, + {{42588, 42589}}, {{42590, 42591}}, // NOLINT + {{42592, 42593}}, {{42594, 42595}}, + {{42596, 42597}}, {{42598, 42599}}, // NOLINT + {{42600, 42601}}, {{42602, 42603}}, + {{42604, 42605}}, {{42624, 42625}}, // NOLINT + {{42626, 42627}}, {{42628, 42629}}, + {{42630, 42631}}, {{42632, 42633}}, // NOLINT + {{42634, 42635}}, {{42636, 42637}}, + {{42638, 42639}}, {{42640, 42641}}, // NOLINT + {{42642, 42643}}, {{42644, 42645}}, + {{42646, 42647}}, {{42648, 42649}}, // NOLINT + {{42650, 42651}}, {{42786, 42787}}, + {{42788, 42789}}, {{42790, 42791}}, // NOLINT + {{42792, 42793}}, {{42794, 42795}}, + {{42796, 42797}}, {{42798, 42799}}, // NOLINT + {{42802, 42803}}, {{42804, 42805}}, + {{42806, 42807}}, {{42808, 42809}}, // NOLINT + {{42810, 42811}}, {{42812, 42813}}, + {{42814, 42815}}, {{42816, 42817}}, // NOLINT + {{42818, 42819}}, {{42820, 42821}}, + {{42822, 42823}}, {{42824, 42825}}, // NOLINT + {{42826, 42827}}, {{42828, 42829}}, + {{42830, 42831}}, {{42832, 42833}}, // NOLINT + {{42834, 42835}}, {{42836, 42837}}, + {{42838, 42839}}, {{42840, 42841}}, // NOLINT + {{42842, 42843}}, {{42844, 42845}}, + {{42846, 42847}}, {{42848, 42849}}, // NOLINT + {{42850, 42851}}, {{42852, 42853}}, + {{42854, 42855}}, {{42856, 42857}}, // NOLINT + {{42858, 42859}}, {{42860, 42861}}, + {{42862, 42863}}, {{42873, 42874}}, // NOLINT + {{42875, 42876}}, {{7545, 42877}}, + {{42878, 42879}}, {{42880, 42881}}, // NOLINT + {{42882, 42883}}, {{42884, 42885}}, + {{42886, 42887}}, {{42891, 42892}}, // NOLINT + {{613, 42893}}, {{42896, 42897}}, + {{42898, 42899}}, {{42902, 42903}}, // NOLINT + {{42904, 42905}}, {{42906, 42907}}, + {{42908, 42909}}, {{42910, 42911}}, // NOLINT + {{42912, 42913}}, {{42914, 42915}}, + {{42916, 42917}}, {{42918, 42919}}, // NOLINT + {{42920, 42921}}, {{614, 42922}}, + {{604, 42923}}, {{609, 42924}}, // NOLINT + {{620, 42925}}, {{670, 42928}}, + {{647, 42929}}, {{kSentinel}}}; // NOLINT +static const uint16_t kEcma262UnCanonicalizeTable5Size = 198; // NOLINT +static const int32_t + kEcma262UnCanonicalizeTable5[396] = + {1600, 1, 1601, 1, 1602, 5, 1603, 5, + 1604, 9, 1605, 9, 1606, 13, 1607, 13, // NOLINT + 1608, 17, 1609, 17, 1610, 21, 1611, 21, + 1612, 25, 1613, 25, 1614, 29, 1615, 29, // NOLINT + 1616, 33, 1617, 33, 1618, 37, 1619, 37, + 1620, 41, 1621, 41, 1622, 45, 1623, 45, // NOLINT + 1624, 49, 1625, 49, 1626, 53, 1627, 53, + 1628, 57, 1629, 57, 1630, 61, 1631, 61, // NOLINT + 1632, 65, 1633, 65, 1634, 69, 1635, 69, + 1636, 73, 1637, 73, 1638, 77, 1639, 77, // NOLINT + 1640, 81, 1641, 81, 1642, 85, 1643, 85, + 1644, 89, 1645, 89, 1664, 93, 1665, 93, // NOLINT + 1666, 97, 1667, 97, 1668, 101, 1669, 101, + 1670, 105, 1671, 105, 1672, 109, 1673, 109, // NOLINT + 1674, 113, 1675, 113, 1676, 117, 1677, 117, + 1678, 121, 1679, 121, 1680, 125, 1681, 125, // NOLINT + 1682, 129, 1683, 129, 1684, 133, 1685, 133, + 1686, 137, 1687, 137, 1688, 141, 1689, 141, // NOLINT + 1690, 145, 1691, 145, 1826, 149, 1827, 149, + 1828, 153, 1829, 153, 1830, 157, 1831, 157, // NOLINT + 1832, 161, 1833, 161, 1834, 165, 1835, 165, + 1836, 169, 1837, 169, 1838, 173, 1839, 173, // NOLINT + 1842, 177, 1843, 177, 1844, 181, 1845, 181, + 1846, 185, 1847, 185, 1848, 189, 1849, 189, // NOLINT + 1850, 193, 1851, 193, 1852, 197, 1853, 197, + 1854, 201, 1855, 201, 1856, 205, 1857, 205, // NOLINT + 1858, 209, 1859, 209, 1860, 213, 1861, 213, + 1862, 217, 1863, 217, 1864, 221, 1865, 221, // NOLINT + 1866, 225, 1867, 225, 1868, 229, 1869, 229, + 1870, 233, 1871, 233, 1872, 237, 1873, 237, // NOLINT + 1874, 241, 1875, 241, 1876, 245, 1877, 245, + 1878, 249, 1879, 249, 1880, 253, 1881, 253, // NOLINT + 1882, 257, 1883, 257, 1884, 261, 1885, 261, + 1886, 265, 1887, 265, 1888, 269, 1889, 269, // NOLINT + 1890, 273, 1891, 273, 1892, 277, 1893, 277, + 1894, 281, 1895, 281, 1896, 285, 1897, 285, // NOLINT + 1898, 289, 1899, 289, 1900, 293, 1901, 293, + 1902, 297, 1903, 297, 1913, 301, 1914, 301, // NOLINT + 1915, 305, 1916, 305, 1917, 309, 1918, 313, + 1919, 313, 1920, 317, 1921, 317, 1922, 321, // NOLINT + 1923, 321, 1924, 325, 1925, 325, 1926, 329, + 1927, 329, 1931, 333, 1932, 333, 1933, 337, // NOLINT + 1936, 341, 1937, 341, 1938, 345, 1939, 345, + 1942, 349, 1943, 349, 1944, 353, 1945, 353, // NOLINT + 1946, 357, 1947, 357, 1948, 361, 1949, 361, + 1950, 365, 1951, 365, 1952, 369, 1953, 369, // NOLINT + 1954, 373, 1955, 373, 1956, 377, 1957, 377, + 1958, 381, 1959, 381, 1960, 385, 1961, 385, // NOLINT + 1962, 389, 1963, 393, 1964, 397, 1965, 401, + 1968, 405, 1969, 409}; // NOLINT +static const MultiCharacterSpecialCase<2> + kEcma262UnCanonicalizeMultiStrings7[3] = { // NOLINT + {{65313, 65345}}, + {{65338, 65370}}, + {{kSentinel}}}; // NOLINT +static const uint16_t kEcma262UnCanonicalizeTable7Size = 4; // NOLINT +static const int32_t kEcma262UnCanonicalizeTable7[8] = { + 1073749793, 1, 7994, 5, 1073749825, 1, 8026, 5}; // NOLINT +int Ecma262UnCanonicalize::Convert(uchar c, uchar n, uchar* result, + bool* allow_caching_ptr) { + int chunk_index = c >> 13; + switch (chunk_index) { + case 0: + return LookupMapping( + kEcma262UnCanonicalizeTable0, kEcma262UnCanonicalizeTable0Size, + kEcma262UnCanonicalizeMultiStrings0, c, n, result, allow_caching_ptr); + case 1: + return LookupMapping( + kEcma262UnCanonicalizeTable1, kEcma262UnCanonicalizeTable1Size, + kEcma262UnCanonicalizeMultiStrings1, c, n, result, allow_caching_ptr); + case 5: + return LookupMapping( + kEcma262UnCanonicalizeTable5, kEcma262UnCanonicalizeTable5Size, + kEcma262UnCanonicalizeMultiStrings5, c, n, result, allow_caching_ptr); + case 7: + return LookupMapping( + kEcma262UnCanonicalizeTable7, kEcma262UnCanonicalizeTable7Size, + kEcma262UnCanonicalizeMultiStrings7, c, n, result, allow_caching_ptr); + default: + return 0; + } +} + +static const MultiCharacterSpecialCase<1> + kCanonicalizationRangeMultiStrings0[1] = { // NOLINT + {{kSentinel}}}; // NOLINT +static const uint16_t kCanonicalizationRangeTable0Size = 70; // NOLINT +static const int32_t kCanonicalizationRangeTable0[140] = { + 1073741889, 100, 90, 0, 1073741921, 100, 122, 0, + 1073742016, 88, 214, 0, 1073742040, 24, 222, 0, // NOLINT + 1073742048, 88, 246, 0, 1073742072, 24, 254, 0, + 1073742715, 8, 893, 0, 1073742728, 8, 906, 0, // NOLINT + 1073742749, 8, 927, 0, 1073742759, 16, 939, 0, + 1073742765, 8, 943, 0, 1073742781, 8, 959, 0, // NOLINT + 1073742791, 16, 971, 0, 1073742845, 8, 1023, 0, + 1073742848, 60, 1039, 0, 1073742864, 124, 1071, 0, // NOLINT + 1073742896, 124, 1103, 0, 1073742928, 60, 1119, 0, + 1073743153, 148, 1366, 0, 1073743201, 148, 1414, 0, // NOLINT + 1073746080, 148, 4293, 0, 1073749760, 28, 7943, 0, + 1073749768, 28, 7951, 0, 1073749776, 20, 7957, 0, // NOLINT + 1073749784, 20, 7965, 0, 1073749792, 28, 7975, 0, + 1073749800, 28, 7983, 0, 1073749808, 28, 7991, 0, // NOLINT + 1073749816, 28, 7999, 0, 1073749824, 20, 8005, 0, + 1073749832, 20, 8013, 0, 1073749856, 28, 8039, 0, // NOLINT + 1073749864, 28, 8047, 0, 1073749874, 12, 8053, 0, + 1073749960, 12, 8139, 0}; // NOLINT +static const MultiCharacterSpecialCase<1> + kCanonicalizationRangeMultiStrings1[1] = { // NOLINT + {{kSentinel}}}; // NOLINT +static const uint16_t kCanonicalizationRangeTable1Size = 14; // NOLINT +static const int32_t kCanonicalizationRangeTable1[28] = { + 1073742176, 60, 367, 0, 1073742192, 60, 383, 0, + 1073743030, 100, 1231, 0, 1073743056, 100, 1257, 0, // NOLINT + 1073744896, 184, 3118, 0, 1073744944, 184, 3166, 0, + 1073745152, 148, 3365, 0}; // NOLINT +static const MultiCharacterSpecialCase<1> + kCanonicalizationRangeMultiStrings7[1] = { // NOLINT + {{kSentinel}}}; // NOLINT +static const uint16_t kCanonicalizationRangeTable7Size = 4; // NOLINT +static const int32_t kCanonicalizationRangeTable7[8] = { + 1073749793, 100, 7994, 0, 1073749825, 100, 8026, 0}; // NOLINT +int CanonicalizationRange::Convert(uchar c, uchar n, uchar* result, + bool* allow_caching_ptr) { + int chunk_index = c >> 13; + switch (chunk_index) { + case 0: + return LookupMapping( + kCanonicalizationRangeTable0, kCanonicalizationRangeTable0Size, + kCanonicalizationRangeMultiStrings0, c, n, result, allow_caching_ptr); + case 1: + return LookupMapping( + kCanonicalizationRangeTable1, kCanonicalizationRangeTable1Size, + kCanonicalizationRangeMultiStrings1, c, n, result, allow_caching_ptr); + case 7: + return LookupMapping( + kCanonicalizationRangeTable7, kCanonicalizationRangeTable7Size, + kCanonicalizationRangeMultiStrings7, c, n, result, allow_caching_ptr); + default: + return 0; + } +} + +#endif // !V8_INTL_SUPPORT + +} // namespace unibrow +} // namespace v8 diff --git a/js/src/regexp/util/zone.h b/js/src/regexp/util/zone.h index 32487f06a6..5a963dd562 100644 --- a/js/src/regexp/util/zone.h +++ b/js/src/regexp/util/zone.h @@ -12,6 +12,7 @@ #include #include "ds/LifoAlloc.h" +#include "ds/Sort.h" #include "regexp/util/vector.h" namespace v8 { @@ -24,14 +25,22 @@ class Zone { lifoAlloc_.setAsInfallibleByDefault(); } - void* New(size_t size) { return lifoAlloc_.alloc(size); } + void* New(size_t size) { + js::LifoAlloc::AutoFallibleScope fallible(&lifoAlloc_); + js::AutoEnterOOMUnsafeRegion oomUnsafe; + void* result = lifoAlloc_.alloc(size); + if (!result) { + oomUnsafe.crash("Irregexp Zone::new"); + } + return result; + } void DeleteAll() { lifoAlloc_.freeAll(); } // Returns true if the total memory allocated exceeds a threshold. static const size_t kExcessLimit = 256 * 1024 * 1024; bool excess_allocation() const { - return lifoAlloc_.peakSizeOfExcludingThis() > kExcessLimit; + return lifoAlloc_.computedSizeOfExcludingThis() > kExcessLimit; } private: js::LifoAlloc lifoAlloc_; @@ -194,9 +203,19 @@ class ZoneList final { } template - void StableSort(CompareFunction cmp, size_t s, size_t l) { - std::stable_sort(begin() + s, begin() + s + l, - [cmp](const T& a, const T& b) { return cmp(&a, &b) < 0; }); + void StableSort(CompareFunction cmp, size_t start, size_t length) { + js::AutoEnterOOMUnsafeRegion oomUnsafe; + T* scratch = static_cast(js_malloc(length * sizeof(T))); + if (!scratch) { + oomUnsafe.crash("Irregexp stable sort scratch space"); + } + auto comparator = [cmp](const T& a, const T& b, bool* lessOrEqual) { + *lessOrEqual = cmp(&a, &b) <= 0; + return true; + }; + MOZ_ALWAYS_TRUE(js::MergeSort(begin() + start, length, scratch, + comparator)); + js_free(scratch); } void operator delete(void* pointer) { MOZ_CRASH("unreachable"); } From e3a3e92316a8e4884190a0067139603573ff17f1 Mon Sep 17 00:00:00 2001 From: "Matt A. Tobin" Date: Mon, 9 Nov 2020 20:37:05 -0500 Subject: [PATCH 3/9] Issue #1677 - Part 5: "Simplify" regexp re-import process (and re-import from later revision) I am going on record to say Mozilla are utter fucking assholes for pulling this as part of their progression. --- js/src/regexp/VERSION | 5 +- js/src/regexp/gen-regexp-special-case.cc | 147 +++++++++++------ js/src/regexp/import-irregexp.py | 143 ++++++++++++++++ js/src/regexp/regexp-ast.h | 29 +++- js/src/regexp/regexp-bytecode-generator.cc | 8 +- js/src/regexp/regexp-bytecode-generator.h | 2 +- js/src/regexp/regexp-bytecode-peephole.cc | 1 - js/src/regexp/regexp-bytecodes.h | 8 +- js/src/regexp/regexp-compiler-tonode.cc | 114 ++----------- js/src/regexp/regexp-compiler.cc | 115 +++++++------ js/src/regexp/regexp-compiler.h | 16 +- js/src/regexp/regexp-error.cc | 22 +++ js/src/regexp/regexp-error.h | 56 +++++++ js/src/regexp/regexp-interpreter.cc | 65 +++----- .../regexp/regexp-macro-assembler-tracer.cc | 10 +- js/src/regexp/regexp-macro-assembler-tracer.h | 1 - js/src/regexp/regexp-macro-assembler.cc | 47 ++---- js/src/regexp/regexp-macro-assembler.h | 10 +- js/src/regexp/regexp-parser.cc | 142 ++++++++-------- js/src/regexp/regexp-parser.h | 20 ++- js/src/regexp/regexp-shim.h | 4 +- js/src/regexp/regexp-stack.h | 6 +- js/src/regexp/regexp.h | 7 +- js/src/regexp/special-case.cc | 60 ++++++- js/src/regexp/special-case.h | 154 +++++++++++------- js/src/regexp/update-headers.py | 38 ----- 26 files changed, 712 insertions(+), 518 deletions(-) create mode 100644 js/src/regexp/import-irregexp.py create mode 100644 js/src/regexp/regexp-error.cc create mode 100644 js/src/regexp/regexp-error.h delete mode 100644 js/src/regexp/update-headers.py diff --git a/js/src/regexp/VERSION b/js/src/regexp/VERSION index 3a0935deac..c7d35a2bb8 100644 --- a/js/src/regexp/VERSION +++ b/js/src/regexp/VERSION @@ -1,3 +1,2 @@ -This code was most recently imported from the following version of V8: - -https://github.com/v8/v8/tree/2599d3cc208a3a4873be517285220abd8416c3d7/src/regexp +Imported using import-irregexp.py from: +https://github.com/v8/v8/tree/560f2d8bb3f3a72d78e1a7d7654235d53fdcc83c/src/regexp diff --git a/js/src/regexp/gen-regexp-special-case.cc b/js/src/regexp/gen-regexp-special-case.cc index 337743f536..b4a8c3da48 100644 --- a/js/src/regexp/gen-regexp-special-case.cc +++ b/js/src/regexp/gen-regexp-special-case.cc @@ -1,4 +1,4 @@ -// Copyright 2019 the V8 project authors. All rights reserved. +// Copyright 2020 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. @@ -7,18 +7,19 @@ #include #include -#include "unicode/uchar.h" -#include "unicode/uniset.h" +#include "regexp/special-case.h" namespace v8 { namespace internal { -// The following code generates BuildSpecialAddSet() and BuildIgnoreSet() -// functions into "src/regexp/special-case.cc". -// See more details in http://shorturl.at/adfO5 -void PrintSet(std::ofstream& out, const char* func_name, +static const uc32 kSurrogateStart = 0xd800; +static const uc32 kSurrogateEnd = 0xdfff; +static const uc32 kNonBmpStart = 0x10000; + +// The following code generates "src/regexp/special-case.cc". +void PrintSet(std::ofstream& out, const char* name, const icu::UnicodeSet& set) { - out << "icu::UnicodeSet " << func_name << "() {\n" + out << "icu::UnicodeSet Build" << name << "() {\n" << " icu::UnicodeSet set;\n"; for (int32_t i = 0; i < set.getRangeCount(); i++) { if (set.getRangeStart(i) == set.getRangeEnd(i)) { @@ -30,73 +31,113 @@ void PrintSet(std::ofstream& out, const char* func_name, } out << " set.freeze();\n" << " return set;\n" - << "}\n"; + << "}\n\n"; + + out << "struct " << name << "Data {\n" + << " " << name << "Data() : set(Build" << name << "()) {}\n" + << " const icu::UnicodeSet set;\n" + << "};\n\n"; + + out << "//static\n" + << "const icu::UnicodeSet& RegExpCaseFolding::" << name << "() {\n" + << " static base::LazyInstance<" << name << "Data>::type set =\n" + << " LAZY_INSTANCE_INITIALIZER;\n" + << " return set.Pointer()->set;\n" + << "}\n\n"; } void PrintSpecial(std::ofstream& out) { icu::UnicodeSet current; - icu::UnicodeSet processed(0xd800, 0xdbff); // Ignore surrogate range. icu::UnicodeSet special_add; icu::UnicodeSet ignore; UErrorCode status = U_ZERO_ERROR; icu::UnicodeSet upper("[\\p{Lu}]", status); CHECK(U_SUCCESS(status)); - // Iterate through all chars in BMP except ASCII and Surrogate. - for (UChar32 i = 0x80; i < 0x010000; i++) { - // Ignore those characters which is already processed. - if (!processed.contains(i)) { - current.set(i, i); - current.closeOver(USET_CASE_INSENSITIVE); - // Remember we already processed current. - processed.addAll(current); + // Iterate through all chars in BMP except surrogates. + for (UChar32 i = 0; i < kNonBmpStart; i++) { + if (i >= kSurrogateStart && i <= kSurrogateEnd) { + continue; // Ignore surrogate range + } + current.set(i, i); + current.closeOver(USET_CASE_INSENSITIVE); - // All uppercase characters in current. - icu::UnicodeSet keep_upper(current); - keep_upper.retainAll(upper); - - // Check if we have more than one uppercase character in current. - // If there are more than one uppercase character, then it is a special - // set which need to be added into either "Special Add" set or "Ignore" - // set. - int32_t number_of_upper = 0; - for (int32_t i = 0; i < keep_upper.getRangeCount() && i <= 1; i++) { - number_of_upper += - keep_upper.getRangeEnd(i) - keep_upper.getRangeStart(i) + 1; + // Check to see if all characters in the case-folding equivalence + // class as defined by UnicodeSet::closeOver all map to the same + // canonical value. + UChar32 canonical = RegExpCaseFolding::Canonicalize(i); + bool class_has_matching_canonical_char = false; + bool class_has_non_matching_canonical_char = false; + for (int32_t j = 0; j < current.getRangeCount(); j++) { + for (UChar32 c = current.getRangeStart(j); c <= current.getRangeEnd(j); + c++) { + if (c == i) { + continue; + } + UChar32 other_canonical = RegExpCaseFolding::Canonicalize(c); + if (canonical == other_canonical) { + class_has_matching_canonical_char = true; + } else { + class_has_non_matching_canonical_char = true; + } } - if (number_of_upper > 1) { - // Add all non uppercase characters (could be Ll or Mn) to special add - // set. - current.removeAll(upper); - special_add.addAll(current); - - // Add the uppercase characters of non uppercase character to - // special add set. - CHECK_GT(current.getRangeCount(), 0); - UChar32 main_upper = u_toupper(current.getRangeStart(0)); - special_add.add(main_upper); - - // Add all uppercase except the main upper to ignore set. - keep_upper.remove(main_upper); - ignore.addAll(keep_upper); + } + // If any other character in i's equivalence class has a + // different canonical value, then i needs special handling. If + // no other character shares a canonical value with i, we can + // ignore i when adding alternatives for case-independent + // comparison. If at least one other character shares a + // canonical value, then i needs special handling. + if (class_has_non_matching_canonical_char) { + if (class_has_matching_canonical_char) { + special_add.add(i); + } else { + ignore.add(i); } } } - // Remove any ASCII - special_add.remove(0x0000, 0x007f); - PrintSet(out, "BuildIgnoreSet", ignore); - PrintSet(out, "BuildSpecialAddSet", special_add); + // Verify that no Unicode equivalence class contains two non-trivial + // JS equivalence classes. Every character in SpecialAddSet has the + // same canonical value as every other non-IgnoreSet character in + // its Unicode equivalence class. Therefore, if we call closeOver on + // a set containing no IgnoreSet characters, the only characters + // that must be removed from the result are in IgnoreSet. This fact + // is used in CharacterRange::AddCaseEquivalents. + for (int32_t i = 0; i < special_add.getRangeCount(); i++) { + for (UChar32 c = special_add.getRangeStart(i); + c <= special_add.getRangeEnd(i); c++) { + UChar32 canonical = RegExpCaseFolding::Canonicalize(c); + current.set(c, c); + current.closeOver(USET_CASE_INSENSITIVE); + current.removeAll(ignore); + for (int32_t j = 0; j < current.getRangeCount(); j++) { + for (UChar32 c2 = current.getRangeStart(j); + c2 <= current.getRangeEnd(j); c2++) { + CHECK_EQ(canonical, RegExpCaseFolding::Canonicalize(c2)); + } + } + } + } + + PrintSet(out, "IgnoreSet", ignore); + PrintSet(out, "SpecialAddSet", special_add); } void WriteHeader(const char* header_filename) { std::ofstream out(header_filename); out << std::hex << std::setfill('0') << std::setw(4); - - out << "// Automatically generated by regexp/gen-regexp-special-case.cc\n" - << "// The following functions are used to build icu::UnicodeSet\n" - << "// for specical cases different between Unicode and ECMA262.\n" + out << "// Copyright 2020 the V8 project authors. All rights reserved.\n" + << "// Use of this source code is governed by a BSD-style license that\n" + << "// can be found in the LICENSE file.\n\n" + << "// Automatically generated by regexp/gen-regexp-special-case.cc\n\n" + << "// The following functions are used to build UnicodeSets\n" + << "// for special cases where the case-folding algorithm used by\n" + << "// UnicodeSet::closeOver(USET_CASE_INSENSITIVE) does not match\n" + << "// the algorithm defined in ECMAScript 2020 21.2.2.8.2 (Runtime\n" + << "// Semantics: Canonicalize) step 3.\n\n" << "#ifdef V8_INTL_SUPPORT\n" + << "#include \"src/base/lazy-instance.h\"\n\n" << "#include \"src/regexp/special-case.h\"\n\n" << "#include \"unicode/uniset.h\"\n" << "namespace v8 {\n" diff --git a/js/src/regexp/import-irregexp.py b/js/src/regexp/import-irregexp.py new file mode 100644 index 0000000000..870387232c --- /dev/null +++ b/js/src/regexp/import-irregexp.py @@ -0,0 +1,143 @@ +#!/usr/bin/env python3 + +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this file, +# You can obtain one at http://mozilla.org/MPL/2.0/. + +# This script handles all the mechanical steps of importing irregexp from v8: +# +# 1. Acquire the source: either from github, or optionally from a local copy of v8. +# 2. Copy the contents of v8/src/regexp into js/src/regexp +# - Exclude files that we have chosen not to import. +# 3. While doing so, update #includes: +# - Change "src/regexp/*" to "regexp/*". +# - Remove other v8-specific headers completely. +# 4. Add '#include "regexp/regexp-shim.h" in the necessary places. +# 5. Update the VERSION file to include the correct git hash. +# +# Usage: +# cd path/to/js/src/regexp +# ./import-irregexp.py --path path/to/v8/src/regexp +# +# Alternatively, without the --path argument, import-irregexp.py will +# clone v8 from github into a temporary directory. +# +# After running this script, changes to the shim code may be necessary +# to account for changes in upstream irregexp. + +import os +import re +import subprocess +import sys +from pathlib import Path + + +def get_hash(path): + # Get the hash for the current git revision + cwd = os.getcwd() + os.chdir(path) + command = ['git', 'rev-parse', 'HEAD'] + result = subprocess.check_output(command, encoding='utf-8') + os.chdir(cwd) + return result.rstrip() + + +def copy_and_update_includes(src_path, dst_path): + # List of header files that need to include the shim header + need_shim = ['property-sequences.h', + 'regexp-ast.h', + 'regexp-bytecode-peephole.h', + 'regexp-bytecodes.h', + 'regexp-dotprinter.h', + 'regexp.h', + 'regexp-macro-assembler.h', + 'regexp-stack.h', + 'special-case.h'] + + src = open(str(src_path), 'r') + dst = open(str(dst_path), 'w') + + # 1. Rewrite includes of V8 regexp headers: + regexp_include = re.compile('#include "src/regexp') + regexp_include_new = '#include "regexp' + + # 2. Remove includes of other V8 headers + other_include = re.compile('#include "src/') + + # 3. If needed, add '#include "regexp/regexp-shim.h"'. + # Note: We get a little fancy to ensure that header files are + # in alphabetic order. `need_to_add_shim` is true if we still + # have to add the shim header in this file. `adding_shim_now` + # is true if we have found a '#include "src/*' and we are just + # waiting to find something alphabetically smaller (or an empty + # line) so that we can insert the shim header in the right place. + need_to_add_shim = src_path.name in need_shim + adding_shim_now = False + + for line in src: + if adding_shim_now: + if (line == '\n' or line > '#include "src/regexp/regexp-shim.h"'): + dst.write('#include "regexp/regexp-shim.h"\n') + need_to_add_shim = False + adding_shim_now = False + + if regexp_include.search(line): + dst.write(re.sub(regexp_include, regexp_include_new, line)) + elif other_include.search(line): + if need_to_add_shim: + adding_shim_now = True + else: + dst.write(line) + + +def import_from(srcdir, dstdir): + excluded = ['OWNERS', + 'regexp.cc', + 'regexp-utils.cc', + 'regexp-utils.h', + 'regexp-macro-assembler-arch.h'] + + for file in srcdir.iterdir(): + if file.is_dir(): + continue + if str(file.name) in excluded: + continue + copy_and_update_includes(file, dstdir / file.name) + + # Update VERSION file + hash = get_hash(srcdir) + version_file = open(str(dstdir / 'VERSION'), 'w') + version_file.write('Imported using import-irregexp.py from:\n') + version_file.write('https://github.com/v8/v8/tree/%s/src/regexp\n' % hash) + + +if __name__ == '__main__': + import argparse + import tempfile + + # This script should be run from js/src/regexp to work correctly. + current_path = Path(os.getcwd()) + expected_path = 'js/src/regexp' + if not current_path.match(expected_path): + raise RuntimeError('%s must be run from %s' % (sys.argv[0], + expected_path)) + + parser = argparse.ArgumentParser(description='Import irregexp from v8') + parser.add_argument('-p', '--path', help='path to v8/src/regexp') + args = parser.parse_args() + + if args.path: + src_path = Path(args.path) + + if not (src_path / 'regexp.h').exists(): + print('Usage:\n import-irregexp.py --path ') + sys.exit(1) + import_from(src_path, current_path) + sys.exit(0) + + with tempfile.TemporaryDirectory() as tempdir: + v8_git = 'https://github.com/v8/v8.git' + clone = 'git clone --depth 1 %s %s' % (v8_git, tempdir) + os.system(clone) + src_path = Path(tempdir) / 'src/regexp' + import_from(src_path, current_path) diff --git a/js/src/regexp/regexp-ast.h b/js/src/regexp/regexp-ast.h index fe6913e1d4..311929d0b9 100644 --- a/js/src/regexp/regexp-ast.h +++ b/js/src/regexp/regexp-ast.h @@ -458,7 +458,11 @@ class RegExpQuantifier final : public RegExpTree { class RegExpCapture final : public RegExpTree { public: explicit RegExpCapture(int index) - : body_(nullptr), index_(index), name_(nullptr) {} + : body_(nullptr), + index_(index), + min_match_(0), + max_match_(0), + name_(nullptr) {} void* Accept(RegExpVisitor* visitor, void* data) override; RegExpNode* ToNode(RegExpCompiler* compiler, RegExpNode* on_success) override; static RegExpNode* ToNode(RegExpTree* body, int index, @@ -468,10 +472,14 @@ class RegExpCapture final : public RegExpTree { bool IsAnchoredAtEnd() override; Interval CaptureRegisters() override; bool IsCapture() override; - int min_match() override { return body_->min_match(); } - int max_match() override { return body_->max_match(); } + int min_match() override { return min_match_; } + int max_match() override { return max_match_; } RegExpTree* body() { return body_; } - void set_body(RegExpTree* body) { body_ = body; } + void set_body(RegExpTree* body) { + body_ = body; + min_match_ = body->min_match(); + max_match_ = body->max_match(); + } int index() const { return index_; } const ZoneVector* name() const { return name_; } void set_name(const ZoneVector* name) { name_ = name; } @@ -481,12 +489,17 @@ class RegExpCapture final : public RegExpTree { private: RegExpTree* body_; int index_; + int min_match_; + int max_match_; const ZoneVector* name_; }; class RegExpGroup final : public RegExpTree { public: - explicit RegExpGroup(RegExpTree* body) : body_(body) {} + explicit RegExpGroup(RegExpTree* body) + : body_(body), + min_match_(body->min_match()), + max_match_(body->max_match()) {} void* Accept(RegExpVisitor* visitor, void* data) override; RegExpNode* ToNode(RegExpCompiler* compiler, RegExpNode* on_success) override { @@ -496,13 +509,15 @@ class RegExpGroup final : public RegExpTree { bool IsAnchoredAtStart() override { return body_->IsAnchoredAtStart(); } bool IsAnchoredAtEnd() override { return body_->IsAnchoredAtEnd(); } bool IsGroup() override; - int min_match() override { return body_->min_match(); } - int max_match() override { return body_->max_match(); } + int min_match() override { return min_match_; } + int max_match() override { return max_match_; } Interval CaptureRegisters() override { return body_->CaptureRegisters(); } RegExpTree* body() { return body_; } private: RegExpTree* body_; + int min_match_; + int max_match_; }; class RegExpLookaround final : public RegExpTree { diff --git a/js/src/regexp/regexp-bytecode-generator.cc b/js/src/regexp/regexp-bytecode-generator.cc index 239b27605f..db151de851 100644 --- a/js/src/regexp/regexp-bytecode-generator.cc +++ b/js/src/regexp/regexp-bytecode-generator.cc @@ -327,13 +327,11 @@ void RegExpBytecodeGenerator::CheckNotBackReference(int start_reg, } void RegExpBytecodeGenerator::CheckNotBackReferenceIgnoreCase( - int start_reg, bool read_backward, bool unicode, Label* on_not_equal) { + int start_reg, bool read_backward, Label* on_not_equal) { DCHECK_LE(0, start_reg); DCHECK_GE(kMaxRegister, start_reg); - Emit(read_backward ? (unicode ? BC_CHECK_NOT_BACK_REF_NO_CASE_UNICODE_BACKWARD - : BC_CHECK_NOT_BACK_REF_NO_CASE_BACKWARD) - : (unicode ? BC_CHECK_NOT_BACK_REF_NO_CASE_UNICODE - : BC_CHECK_NOT_BACK_REF_NO_CASE), + Emit(read_backward ? BC_CHECK_NOT_BACK_REF_NO_CASE_BACKWARD + : BC_CHECK_NOT_BACK_REF_NO_CASE, start_reg); EmitOrLink(on_not_equal); } diff --git a/js/src/regexp/regexp-bytecode-generator.h b/js/src/regexp/regexp-bytecode-generator.h index 15fbda8ecb..f5502464d4 100644 --- a/js/src/regexp/regexp-bytecode-generator.h +++ b/js/src/regexp/regexp-bytecode-generator.h @@ -69,7 +69,7 @@ class V8_EXPORT_PRIVATE RegExpBytecodeGenerator : public RegExpMacroAssembler { virtual void CheckNotBackReference(int start_reg, bool read_backward, Label* on_no_match); virtual void CheckNotBackReferenceIgnoreCase(int start_reg, - bool read_backward, bool unicode, + bool read_backward, Label* on_no_match); virtual void IfRegisterLT(int register_index, int comparand, Label* if_lt); virtual void IfRegisterGE(int register_index, int comparand, Label* if_ge); diff --git a/js/src/regexp/regexp-bytecode-peephole.cc b/js/src/regexp/regexp-bytecode-peephole.cc index 2bc1b5aa26..4266b4a807 100644 --- a/js/src/regexp/regexp-bytecode-peephole.cc +++ b/js/src/regexp/regexp-bytecode-peephole.cc @@ -428,7 +428,6 @@ BytecodeArgumentMapping BytecodeSequenceNode::ArgumentMapping( size_t index) const { DCHECK(IsSequence()); DCHECK(argument_mapping_ != nullptr); - DCHECK_GE(index, 0); DCHECK_LT(index, argument_mapping_->size()); return argument_mapping_->at(index); diff --git a/js/src/regexp/regexp-bytecodes.h b/js/src/regexp/regexp-bytecodes.h index 24d6925db9..1cfef1b2d4 100644 --- a/js/src/regexp/regexp-bytecodes.h +++ b/js/src/regexp/regexp-bytecodes.h @@ -100,12 +100,12 @@ STATIC_ASSERT(1 << BYTECODE_SHIFT > BYTECODE_MASK); V(CHECK_BIT_IN_TABLE, 34, 24) /* bc8 pad24 addr32 bits128 */ \ V(CHECK_LT, 35, 8) /* bc8 pad8 uc16 addr32 */ \ V(CHECK_GT, 36, 8) /* bc8 pad8 uc16 addr32 */ \ - V(CHECK_NOT_BACK_REF, 37, 8) /* bc8 reg_idx24 addr32 */ \ - V(CHECK_NOT_BACK_REF_NO_CASE, 38, 8) /* bc8 reg_idx24 addr32 */ \ - V(CHECK_NOT_BACK_REF_NO_CASE_UNICODE, 39, 8) \ + V(CHECK_NOT_BACK_REF, 37, 8) /* bc8 reg_idx24 addr32 */ \ + V(CHECK_NOT_BACK_REF_NO_CASE, 38, 8) /* bc8 reg_idx24 addr32 */ \ + V(CHECK_NOT_BACK_REF_NO_CASE_UNICODE, 39, 8) /* UNUSED */ \ V(CHECK_NOT_BACK_REF_BACKWARD, 40, 8) /* bc8 reg_idx24 addr32 */ \ V(CHECK_NOT_BACK_REF_NO_CASE_BACKWARD, 41, 8) /* bc8 reg_idx24 addr32 */ \ - V(CHECK_NOT_BACK_REF_NO_CASE_UNICODE_BACKWARD, 42, 8) \ + V(CHECK_NOT_BACK_REF_NO_CASE_UNICODE_BACKWARD, 42, 8) /* UNUSED */ \ V(CHECK_NOT_REGS_EQUAL, 43, 12) /* bc8 regidx24 reg_idx32 addr32 */ \ V(CHECK_REGISTER_LT, 44, 12) /* bc8 reg_idx24 value32 addr32 */ \ V(CHECK_REGISTER_GE, 45, 12) /* bc8 reg_idx24 value32 addr32 */ \ diff --git a/js/src/regexp/regexp-compiler-tonode.cc b/js/src/regexp/regexp-compiler-tonode.cc index fc734ac7c1..257030589d 100644 --- a/js/src/regexp/regexp-compiler-tonode.cc +++ b/js/src/regexp/regexp-compiler-tonode.cc @@ -1137,39 +1137,6 @@ Vector CharacterRange::GetWordBounds() { return Vector(kWordRanges, kWordRangeCount - 1); } -#ifdef V8_INTL_SUPPORT -struct IgnoreSet { - IgnoreSet() : set(BuildIgnoreSet()) {} - const icu::UnicodeSet set; -}; - -struct SpecialAddSet { - SpecialAddSet() : set(BuildSpecialAddSet()) {} - const icu::UnicodeSet set; -}; - -icu::UnicodeSet BuildAsciiAToZSet() { - icu::UnicodeSet set('a', 'z'); - set.add('A', 'Z'); - set.freeze(); - return set; -} - -struct AsciiAToZSet { - AsciiAToZSet() : set(BuildAsciiAToZSet()) {} - const icu::UnicodeSet set; -}; - -static base::LazyInstance::type ignore_set = - LAZY_INSTANCE_INITIALIZER; - -static base::LazyInstance::type special_add_set = - LAZY_INSTANCE_INITIALIZER; - -static base::LazyInstance::type ascii_a_to_z_set = - LAZY_INSTANCE_INITIALIZER; -#endif // V8_INTL_SUPPORT - // static void CharacterRange::AddCaseEquivalents(Isolate* isolate, Zone* zone, ZoneList* ranges, @@ -1192,75 +1159,22 @@ void CharacterRange::AddCaseEquivalents(Isolate* isolate, Zone* zone, others.add(from, to); } - // Set of characters already added to ranges that do not need to be added - // again. + // Compute the set of additional characters that should be added, + // using UnicodeSet::closeOver. ECMA 262 defines slightly different + // case-folding rules than Unicode, so some characters that are + // added by closeOver do not match anything other than themselves in + // JS. For example, 'Å¿' (U+017F LATIN SMALL LETTER LONG S) is the + // same case-insensitive character as 's' or 'S' according to + // Unicode, but does not match any other character in JS. To handle + // this case, we add such characters to the IgnoreSet and filter + // them out. We filter twice: once before calling closeOver (to + // prevent 'Å¿' from adding 's'), and once after calling closeOver + // (to prevent 's' from adding 'Å¿'). See regexp/special-case.h for + // more information. icu::UnicodeSet already_added(others); - - // Set of characters in ranges that are in the 52 ASCII characters [a-zA-Z]. - icu::UnicodeSet in_ascii_a_to_z(others); - in_ascii_a_to_z.retainAll(ascii_a_to_z_set.Pointer()->set); - - // Remove all chars in [a-zA-Z] from others. - others.removeAll(in_ascii_a_to_z); - - // Set of characters in ranges that are overlapping with special add set. - icu::UnicodeSet in_special_add(others); - in_special_add.retainAll(special_add_set.Pointer()->set); - - others.removeAll(in_special_add); - - // Ignore all chars in ignore set. - others.removeAll(ignore_set.Pointer()->set); - - // For most of the chars in ranges that is still in others, find the case - // equivlant set by calling closeOver(USET_CASE_INSENSITIVE). + others.removeAll(RegExpCaseFolding::IgnoreSet()); others.closeOver(USET_CASE_INSENSITIVE); - - // Because closeOver(USET_CASE_INSENSITIVE) may add ASCII [a-zA-Z] to others, - // but ECMA262 "i" mode won't consider that, remove them from others. - // Ex: U+017F add 'S' and 's' to others. - others.removeAll(ascii_a_to_z_set.Pointer()->set); - - // Special handling for in_ascii_a_to_z. - for (int32_t i = 0; i < in_ascii_a_to_z.getRangeCount(); i++) { - UChar32 start = in_ascii_a_to_z.getRangeStart(i); - UChar32 end = in_ascii_a_to_z.getRangeEnd(i); - // Check if it is uppercase A-Z by checking bit 6. - if (start & 0x0020) { - // Add the lowercases - others.add(start & 0x005F, end & 0x005F); - } else { - // Add the uppercases - others.add(start | 0x0020, end | 0x0020); - } - } - - // Special handling for chars in "Special Add" set. - for (int32_t i = 0; i < in_special_add.getRangeCount(); i++) { - UChar32 end = in_special_add.getRangeEnd(i); - for (UChar32 ch = in_special_add.getRangeStart(i); ch <= end; ch++) { - // Add the uppercase of this character if itself is not an uppercase - // character. - // Note: The if condiction cannot be u_islower(ch) because ch could be - // neither uppercase nor lowercase but Mn. - if (!u_isupper(ch)) { - others.add(u_toupper(ch)); - } - icu::UnicodeSet candidates(ch, ch); - candidates.closeOver(USET_CASE_INSENSITIVE); - for (int32_t j = 0; j < candidates.getRangeCount(); j++) { - UChar32 end2 = candidates.getRangeEnd(j); - for (UChar32 ch2 = candidates.getRangeStart(j); ch2 <= end2; ch2++) { - // Add character that is not uppercase to others. - if (!u_isupper(ch2)) { - others.add(ch2); - } - } - } - } - } - - // Remove all characters which already in the ranges. + others.removeAll(RegExpCaseFolding::IgnoreSet()); others.removeAll(already_added); // Add others to the ranges diff --git a/js/src/regexp/regexp-compiler.cc b/js/src/regexp/regexp-compiler.cc index 9a2aa30dcc..c0070061f8 100644 --- a/js/src/regexp/regexp-compiler.cc +++ b/js/src/regexp/regexp-compiler.cc @@ -5,7 +5,9 @@ #include "regexp/regexp-compiler.h" #include "regexp/regexp-macro-assembler-arch.h" -#include "regexp/regexp-macro-assembler-tracer.h" +#ifdef V8_INTL_SUPPORT +#include "regexp/special-case.h" +#endif // V8_INTL_SUPPORT #ifdef V8_INTL_SUPPORT #include "unicode/locid.h" @@ -237,20 +239,15 @@ RegExpCompiler::RegExpCompiler(Isolate* isolate, Zone* zone, int capture_count, RegExpCompiler::CompilationResult RegExpCompiler::Assemble( Isolate* isolate, RegExpMacroAssembler* macro_assembler, RegExpNode* start, int capture_count, Handle pattern) { -#ifdef DEBUG - if (FLAG_trace_regexp_assembler) - macro_assembler_ = new RegExpMacroAssemblerTracer(isolate, macro_assembler); - else -#endif - macro_assembler_ = macro_assembler; + macro_assembler_ = macro_assembler; - std::vector work_list; + ZoneVector work_list(zone()); work_list_ = &work_list; Label fail; macro_assembler_->PushBacktrack(&fail); Trace new_trace; start->Emit(this, &new_trace); - macro_assembler_->Bind(&fail); + macro_assembler_->BindJumpTarget(&fail); macro_assembler_->Fail(); while (!work_list.empty()) { RegExpNode* node = work_list.back(); @@ -264,14 +261,9 @@ RegExpCompiler::CompilationResult RegExpCompiler::Assemble( } Handle code = macro_assembler_->GetCode(pattern); - isolate->IncreaseTotalRegexpCodeGenerated(code->Size()); + isolate->IncreaseTotalRegexpCodeGenerated(code); work_list_ = nullptr; -#ifdef DEBUG - if (FLAG_trace_regexp_assembler) { - delete macro_assembler_; - } -#endif return {*code, next_register_}; } @@ -557,7 +549,7 @@ void Trace::Flush(RegExpCompiler* compiler, RegExpNode* successor) { } // On backtrack we need to restore state. - assembler->Bind(&undo); + assembler->BindJumpTarget(&undo); RestoreAffectedRegisters(assembler, max_register, registers_to_pop, registers_to_clear); if (backtrack() == nullptr) { @@ -720,32 +712,34 @@ static int GetCaseIndependentLetters(Isolate* isolate, uc16 character, unibrow::uchar* letters, int letter_length) { #ifdef V8_INTL_SUPPORT - // Special case for U+017F which has upper case in ASCII range. - if (character == 0x017f) { + if (RegExpCaseFolding::IgnoreSet().contains(character)) { letters[0] = character; return 1; } + bool in_special_add_set = + RegExpCaseFolding::SpecialAddSet().contains(character); + icu::UnicodeSet set; set.add(character); set = set.closeOver(USET_CASE_INSENSITIVE); + + UChar32 canon = 0; + if (in_special_add_set) { + canon = RegExpCaseFolding::Canonicalize(character); + } + int32_t range_count = set.getRangeCount(); int items = 0; for (int32_t i = 0; i < range_count; i++) { UChar32 start = set.getRangeStart(i); UChar32 end = set.getRangeEnd(i); CHECK(end - start + items <= letter_length); - // Only add to the output if character is not in ASCII range - // or the case equivalent character is in ASCII range. - // #sec-runtime-semantics-canonicalize-ch - // 3.g If the numeric value of ch ≥ 128 and the numeric value of cu < 128, - // return ch. - if (!((start >= 128) && (character < 128))) { - // No range have start and end span across code point 128. - DCHECK((start >= 128) == (end >= 128)); - for (UChar32 cu = start; cu <= end; cu++) { - if (one_byte_subject && cu > String::kMaxOneByteCharCode) break; - letters[items++] = (unibrow::uchar)(cu); + for (UChar32 cu = start; cu <= end; cu++) { + if (one_byte_subject && cu > String::kMaxOneByteCharCode) break; + if (in_special_add_set && RegExpCaseFolding::Canonicalize(cu) != canon) { + continue; } + letters[items++] = (unibrow::uchar)(cu); } } return items; @@ -852,10 +846,6 @@ static bool ShortCutEmitCharacterPair(RegExpMacroAssembler* macro_assembler, return false; } -using EmitCharacterFunction = bool(Isolate* isolate, RegExpCompiler* compiler, - uc16 c, Label* on_failure, int cp_offset, - bool check, bool preloaded); - // Only emits letters (things that have case). Only used for case independent // matches. static inline bool EmitAtomLetter(Isolate* isolate, RegExpCompiler* compiler, @@ -1843,13 +1833,13 @@ RegExpNode* TextNode::FilterOneByte(int depth) { if (elm.text_type() == TextElement::ATOM) { Vector quarks = elm.atom()->data(); for (int j = 0; j < quarks.length(); j++) { - uint16_t c = quarks[j]; + uc16 c = quarks[j]; if (elm.atom()->ignore_case()) { c = unibrow::Latin1::TryConvertToLatin1(c); } if (c > unibrow::Latin1::kMaxChar) return set_replacement(nullptr); // Replace quark in case we converted to Latin-1. - uint16_t* writable_quarks = const_cast(quarks.begin()); + uc16* writable_quarks = const_cast(quarks.begin()); writable_quarks[j] = c; } } else { @@ -2304,7 +2294,6 @@ void TextNode::TextEmitPass(RegExpCompiler* compiler, TextEmitPassType pass, for (int j = preloaded ? 0 : quarks.length() - 1; j >= 0; j--) { if (first_element_checked && i == 0 && j == 0) continue; if (DeterminedAlready(quick_check, elm.cp_offset() + j)) continue; - EmitCharacterFunction* emit_function = nullptr; uc16 quark = quarks[j]; if (elm.atom()->ignore_case()) { // Everywhere else we assume that a non-Latin-1 character cannot match @@ -2312,6 +2301,9 @@ void TextNode::TextEmitPass(RegExpCompiler* compiler, TextEmitPassType pass, // invalid by using the Latin1 equivalent instead. quark = unibrow::Latin1::TryConvertToLatin1(quark); } + bool needs_bounds_check = + *checked_up_to < cp_offset + j || read_backward(); + bool bounds_checked = false; switch (pass) { case NON_LATIN1_MATCH: DCHECK(one_byte); @@ -2321,24 +2313,24 @@ void TextNode::TextEmitPass(RegExpCompiler* compiler, TextEmitPassType pass, } break; case NON_LETTER_CHARACTER_MATCH: - emit_function = &EmitAtomNonLetter; + bounds_checked = + EmitAtomNonLetter(isolate, compiler, quark, backtrack, + cp_offset + j, needs_bounds_check, preloaded); break; case SIMPLE_CHARACTER_MATCH: - emit_function = &EmitSimpleCharacter; + bounds_checked = EmitSimpleCharacter(isolate, compiler, quark, + backtrack, cp_offset + j, + needs_bounds_check, preloaded); break; case CASE_CHARACTER_MATCH: - emit_function = &EmitAtomLetter; + bounds_checked = + EmitAtomLetter(isolate, compiler, quark, backtrack, + cp_offset + j, needs_bounds_check, preloaded); break; default: break; } - if (emit_function != nullptr) { - bool bounds_check = *checked_up_to < cp_offset + j || read_backward(); - bool bound_checked = - emit_function(isolate, compiler, quark, backtrack, cp_offset + j, - bounds_check, preloaded); - if (bound_checked) UpdateBoundsCheck(cp_offset + j, checked_up_to); - } + if (bounds_checked) UpdateBoundsCheck(cp_offset + j, checked_up_to); } } else { DCHECK_EQ(TextElement::CHAR_CLASS, elm.text_type()); @@ -3424,8 +3416,8 @@ void BackReferenceNode::Emit(RegExpCompiler* compiler, Trace* trace) { DCHECK_EQ(start_reg_ + 1, end_reg_); if (IgnoreCase(flags_)) { - assembler->CheckNotBackReferenceIgnoreCase( - start_reg_, read_backward(), IsUnicode(flags_), trace->backtrack()); + assembler->CheckNotBackReferenceIgnoreCase(start_reg_, read_backward(), + trace->backtrack()); } else { assembler->CheckNotBackReference(start_reg_, read_backward(), trace->backtrack()); @@ -3597,12 +3589,17 @@ template class Analysis : public NodeVisitor { public: Analysis(Isolate* isolate, bool is_one_byte) - : isolate_(isolate), is_one_byte_(is_one_byte), error_message_(nullptr) {} + : isolate_(isolate), + is_one_byte_(is_one_byte), + error_(RegExpError::kNone) {} void EnsureAnalyzed(RegExpNode* that) { StackLimitCheck check(isolate()); if (check.HasOverflowed()) { - fail("Stack overflow"); + if (FLAG_correctness_fuzzer_suppressions) { + FATAL("Analysis: Aborting on stack overflow"); + } + fail(RegExpError::kAnalysisStackOverflow); return; } if (that->info()->been_analyzed || that->info()->being_analyzed) return; @@ -3612,12 +3609,12 @@ class Analysis : public NodeVisitor { that->info()->been_analyzed = true; } - bool has_failed() { return error_message_ != nullptr; } - const char* error_message() { - DCHECK(error_message_ != nullptr); - return error_message_; + bool has_failed() { return error_ != RegExpError::kNone; } + RegExpError error() { + DCHECK(error_ != RegExpError::kNone); + return error_; } - void fail(const char* error_message) { error_message_ = error_message; } + void fail(RegExpError error) { error_ = error; } Isolate* isolate() const { return isolate_; } @@ -3702,19 +3699,19 @@ class Analysis : public NodeVisitor { private: Isolate* isolate_; bool is_one_byte_; - const char* error_message_; + RegExpError error_; DISALLOW_IMPLICIT_CONSTRUCTORS(Analysis); }; -const char* AnalyzeRegExp(Isolate* isolate, bool is_one_byte, +RegExpError AnalyzeRegExp(Isolate* isolate, bool is_one_byte, RegExpNode* node) { Analysis analysis(isolate, is_one_byte); DCHECK_EQ(node->info()->been_analyzed, false); analysis.EnsureAnalyzed(node); - DCHECK_IMPLIES(analysis.has_failed(), analysis.error_message() != nullptr); - return analysis.has_failed() ? analysis.error_message() : nullptr; + DCHECK_IMPLIES(analysis.has_failed(), analysis.error() != RegExpError::kNone); + return analysis.has_failed() ? analysis.error() : RegExpError::kNone; } void BackReferenceNode::FillInBMInfo(Isolate* isolate, int offset, int budget, diff --git a/js/src/regexp/regexp-compiler.h b/js/src/regexp/regexp-compiler.h index 192b3284d6..1954f1a4c4 100644 --- a/js/src/regexp/regexp-compiler.h +++ b/js/src/regexp/regexp-compiler.h @@ -422,10 +422,7 @@ struct PreloadState { // Analysis performs assertion propagation and computes eats_at_least_ values. // See the comments on AssertionPropagator and EatsAtLeastPropagator for more // details. -// -// This method returns nullptr on success or a null-terminated failure message -// on failure. -const char* AnalyzeRegExp(Isolate* isolate, bool is_one_byte, RegExpNode* node); +RegExpError AnalyzeRegExp(Isolate* isolate, bool is_one_byte, RegExpNode* node); class FrequencyCollator { public: @@ -502,18 +499,17 @@ class RegExpCompiler { } struct CompilationResult final { - explicit CompilationResult(const char* error_message) - : error_message(error_message) {} + explicit CompilationResult(RegExpError err) : error(err) {} CompilationResult(Object code, int registers) : code(code), num_registers(registers) {} static CompilationResult RegExpTooBig() { - return CompilationResult("RegExp too big"); + return CompilationResult(RegExpError::kTooLarge); } - bool Succeeded() const { return error_message == nullptr; } + bool Succeeded() const { return error == RegExpError::kNone; } - const char* const error_message = nullptr; + const RegExpError error = RegExpError::kNone; Object code; int num_registers = 0; }; @@ -575,7 +571,7 @@ class RegExpCompiler { int next_register_; int unicode_lookaround_stack_register_; int unicode_lookaround_position_register_; - std::vector* work_list_; + ZoneVector* work_list_; int recursion_depth_; RegExpMacroAssembler* macro_assembler_; bool one_byte_; diff --git a/js/src/regexp/regexp-error.cc b/js/src/regexp/regexp-error.cc new file mode 100644 index 0000000000..3906f9d9ff --- /dev/null +++ b/js/src/regexp/regexp-error.cc @@ -0,0 +1,22 @@ +// Copyright 2020 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. + +#include "regexp/regexp-error.h" + +namespace v8 { +namespace internal { + +const char* kRegExpErrorStrings[] = { +#define TEMPLATE(NAME, STRING) STRING, + REGEXP_ERROR_MESSAGES(TEMPLATE) +#undef TEMPLATE +}; + +const char* RegExpErrorString(RegExpError error) { + DCHECK_LT(error, RegExpError::NumErrors); + return kRegExpErrorStrings[static_cast(error)]; +} + +} // namespace internal +} // namespace v8 diff --git a/js/src/regexp/regexp-error.h b/js/src/regexp/regexp-error.h new file mode 100644 index 0000000000..ef9d037dd3 --- /dev/null +++ b/js/src/regexp/regexp-error.h @@ -0,0 +1,56 @@ +// Copyright 2020 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_REGEXP_REGEXP_ERROR_H_ +#define V8_REGEXP_REGEXP_ERROR_H_ + + +namespace v8 { +namespace internal { + +#define REGEXP_ERROR_MESSAGES(T) \ + T(None, "") \ + T(StackOverflow, "Maximum call stack size exceeded") \ + T(AnalysisStackOverflow, "Stack overflow") \ + T(TooLarge, "Regular expression too large") \ + T(UnterminatedGroup, "Unterminated group") \ + T(UnmatchedParen, "Unmatched ')'") \ + T(EscapeAtEndOfPattern, "\\ at end of pattern") \ + T(InvalidPropertyName, "Invalid property name") \ + T(InvalidEscape, "Invalid escape") \ + T(InvalidDecimalEscape, "Invalid decimal escape") \ + T(InvalidUnicodeEscape, "Invalid Unicode escape") \ + T(NothingToRepeat, "Nothing to repeat") \ + T(LoneQuantifierBrackets, "Lone quantifier brackets") \ + T(RangeOutOfOrder, "numbers out of order in {} quantifier") \ + T(IncompleteQuantifier, "Incomplete quantifier") \ + T(InvalidQuantifier, "Invalid quantifier") \ + T(InvalidGroup, "Invalid group") \ + T(MultipleFlagDashes, "Multiple dashes in flag group") \ + T(RepeatedFlag, "Repeated flag in flag group") \ + T(InvalidFlagGroup, "Invalid flag group") \ + T(TooManyCaptures, "Too many captures") \ + T(InvalidCaptureGroupName, "Invalid capture group name") \ + T(DuplicateCaptureGroupName, "Duplicate capture group name") \ + T(InvalidNamedReference, "Invalid named reference") \ + T(InvalidNamedCaptureReference, "Invalid named capture referenced") \ + T(InvalidClassEscape, "Invalid class escape") \ + T(InvalidClassPropertyName, "Invalid property name in character class") \ + T(InvalidCharacterClass, "Invalid character class") \ + T(UnterminatedCharacterClass, "Unterminated character class") \ + T(OutOfOrderCharacterClass, "Range out of order in character class") + +enum class RegExpError : uint32_t { +#define TEMPLATE(NAME, STRING) k##NAME, + REGEXP_ERROR_MESSAGES(TEMPLATE) +#undef TEMPLATE + NumErrors +}; + +V8_EXPORT_PRIVATE const char* RegExpErrorString(RegExpError error); + +} // namespace internal +} // namespace v8 + +#endif // V8_REGEXP_REGEXP_ERROR_H_ diff --git a/js/src/regexp/regexp-interpreter.cc b/js/src/regexp/regexp-interpreter.cc index 6632cd7296..7735d68855 100644 --- a/js/src/regexp/regexp-interpreter.cc +++ b/js/src/regexp/regexp-interpreter.cc @@ -28,18 +28,18 @@ namespace internal { namespace { bool BackRefMatchesNoCase(Isolate* isolate, int from, int current, int len, - Vector subject, bool unicode) { + Vector subject) { Address offset_a = reinterpret_cast
(const_cast(&subject.at(from))); Address offset_b = reinterpret_cast
(const_cast(&subject.at(current))); size_t length = len * kUC16Size; - return RegExpMacroAssembler::CaseInsensitiveCompareUC16( - offset_a, offset_b, length, unicode ? nullptr : isolate) == 1; + return RegExpMacroAssembler::CaseInsensitiveCompareUC16(offset_a, offset_b, + length, isolate) == 1; } bool BackRefMatchesNoCase(Isolate* isolate, int from, int current, int len, - Vector subject, bool unicode) { + Vector subject) { // For Latin1 characters the unicode flag makes no difference. for (int i = 0; i < len; i++) { unsigned int old_char = subject[from++]; @@ -82,11 +82,17 @@ int32_t Load32Aligned(const byte* pc) { return *reinterpret_cast(pc); } -int32_t Load16Aligned(const byte* pc) { +// TODO(jgruber): Rename to Load16AlignedUnsigned. +uint32_t Load16Aligned(const byte* pc) { DCHECK_EQ(0, reinterpret_cast(pc) & 1); return *reinterpret_cast(pc); } +int32_t Load16AlignedSigned(const byte* pc) { + DCHECK_EQ(0, reinterpret_cast(pc) & 1); + return *reinterpret_cast(pc); +} + // A simple abstraction over the backtracking stack used by the interpreter. // // Despite the name 'backtracking' stack, it's actually used as a generic stack @@ -734,26 +740,14 @@ IrregexpInterpreter::Result RawMatch(Isolate* isolate, ByteArray code_array, DISPATCH(); } BYTECODE(CHECK_NOT_BACK_REF_NO_CASE_UNICODE) { - int from = registers[insn >> BYTECODE_SHIFT]; - int len = registers[(insn >> BYTECODE_SHIFT) + 1] - from; - if (from >= 0 && len > 0) { - if (current + len > subject.length() || - !BackRefMatchesNoCase(isolate, from, current, len, subject, true)) { - SET_PC_FROM_OFFSET(Load32Aligned(pc + 4)); - DISPATCH(); - } - current += len; - } - ADVANCE(CHECK_NOT_BACK_REF_NO_CASE_UNICODE); - DISPATCH(); + UNREACHABLE(); // TODO(jgruber): Remove this unused bytecode. } BYTECODE(CHECK_NOT_BACK_REF_NO_CASE) { int from = registers[insn >> BYTECODE_SHIFT]; int len = registers[(insn >> BYTECODE_SHIFT) + 1] - from; if (from >= 0 && len > 0) { if (current + len > subject.length() || - !BackRefMatchesNoCase(isolate, from, current, len, subject, - false)) { + !BackRefMatchesNoCase(isolate, from, current, len, subject)) { SET_PC_FROM_OFFSET(Load32Aligned(pc + 4)); DISPATCH(); } @@ -763,27 +757,14 @@ IrregexpInterpreter::Result RawMatch(Isolate* isolate, ByteArray code_array, DISPATCH(); } BYTECODE(CHECK_NOT_BACK_REF_NO_CASE_UNICODE_BACKWARD) { - int from = registers[insn >> BYTECODE_SHIFT]; - int len = registers[(insn >> BYTECODE_SHIFT) + 1] - from; - if (from >= 0 && len > 0) { - if (current - len < 0 || - !BackRefMatchesNoCase(isolate, from, current - len, len, subject, - true)) { - SET_PC_FROM_OFFSET(Load32Aligned(pc + 4)); - DISPATCH(); - } - current -= len; - } - ADVANCE(CHECK_NOT_BACK_REF_NO_CASE_UNICODE_BACKWARD); - DISPATCH(); + UNREACHABLE(); // TODO(jgruber): Remove this unused bytecode. } BYTECODE(CHECK_NOT_BACK_REF_NO_CASE_BACKWARD) { int from = registers[insn >> BYTECODE_SHIFT]; int len = registers[(insn >> BYTECODE_SHIFT) + 1] - from; if (from >= 0 && len > 0) { if (current - len < 0 || - !BackRefMatchesNoCase(isolate, from, current - len, len, subject, - false)) { + !BackRefMatchesNoCase(isolate, from, current - len, len, subject)) { SET_PC_FROM_OFFSET(Load32Aligned(pc + 4)); DISPATCH(); } @@ -828,7 +809,7 @@ IrregexpInterpreter::Result RawMatch(Isolate* isolate, ByteArray code_array, } BYTECODE(SKIP_UNTIL_CHAR) { int load_offset = (insn >> BYTECODE_SHIFT); - uint32_t advance = Load16Aligned(pc + 4); + int32_t advance = Load16AlignedSigned(pc + 4); uint32_t c = Load16Aligned(pc + 6); while (static_cast(current + load_offset) < static_cast(subject.length())) { @@ -844,7 +825,7 @@ IrregexpInterpreter::Result RawMatch(Isolate* isolate, ByteArray code_array, } BYTECODE(SKIP_UNTIL_CHAR_AND) { int load_offset = (insn >> BYTECODE_SHIFT); - uint16_t advance = Load16Aligned(pc + 4); + int32_t advance = Load16AlignedSigned(pc + 4); uint16_t c = Load16Aligned(pc + 6); uint32_t mask = Load32Aligned(pc + 8); int32_t maximum_offset = Load32Aligned(pc + 12); @@ -862,7 +843,7 @@ IrregexpInterpreter::Result RawMatch(Isolate* isolate, ByteArray code_array, } BYTECODE(SKIP_UNTIL_CHAR_POS_CHECKED) { int load_offset = (insn >> BYTECODE_SHIFT); - uint16_t advance = Load16Aligned(pc + 4); + int32_t advance = Load16AlignedSigned(pc + 4); uint16_t c = Load16Aligned(pc + 6); int32_t maximum_offset = Load32Aligned(pc + 8); while (static_cast(current + maximum_offset) <= @@ -879,7 +860,7 @@ IrregexpInterpreter::Result RawMatch(Isolate* isolate, ByteArray code_array, } BYTECODE(SKIP_UNTIL_BIT_IN_TABLE) { int load_offset = (insn >> BYTECODE_SHIFT); - uint32_t advance = Load16Aligned(pc + 4); + int32_t advance = Load16AlignedSigned(pc + 4); const byte* table = pc + 8; while (static_cast(current + load_offset) < static_cast(subject.length())) { @@ -895,7 +876,7 @@ IrregexpInterpreter::Result RawMatch(Isolate* isolate, ByteArray code_array, } BYTECODE(SKIP_UNTIL_GT_OR_NOT_BIT_IN_TABLE) { int load_offset = (insn >> BYTECODE_SHIFT); - uint16_t advance = Load16Aligned(pc + 4); + int32_t advance = Load16AlignedSigned(pc + 4); uint16_t limit = Load16Aligned(pc + 6); const byte* table = pc + 8; while (static_cast(current + load_offset) < @@ -916,7 +897,7 @@ IrregexpInterpreter::Result RawMatch(Isolate* isolate, ByteArray code_array, } BYTECODE(SKIP_UNTIL_CHAR_OR_CHAR) { int load_offset = (insn >> BYTECODE_SHIFT); - uint32_t advance = Load32Aligned(pc + 4); + int32_t advance = Load32Aligned(pc + 4); uint16_t c = Load16Aligned(pc + 8); uint16_t c2 = Load16Aligned(pc + 10); while (static_cast(current + load_offset) < @@ -1016,6 +997,8 @@ IrregexpInterpreter::Result IrregexpInterpreter::MatchInternal( } } +#ifndef COMPILING_IRREGEXP_FOR_EXTERNAL_EMBEDDER + // This method is called through an external reference from RegExpExecInternal // builtin. IrregexpInterpreter::Result IrregexpInterpreter::MatchForCallFromJs( @@ -1042,6 +1025,8 @@ IrregexpInterpreter::Result IrregexpInterpreter::MatchForCallFromJs( start_position, call_origin); } +#endif // !COMPILING_IRREGEXP_FOR_EXTERNAL_EMBEDDER + IrregexpInterpreter::Result IrregexpInterpreter::MatchForCallFromRuntime( Isolate* isolate, Handle regexp, Handle subject_string, int* registers, int registers_length, int start_position) { diff --git a/js/src/regexp/regexp-macro-assembler-tracer.cc b/js/src/regexp/regexp-macro-assembler-tracer.cc index 331c57d1ae..b71a0f48e9 100644 --- a/js/src/regexp/regexp-macro-assembler-tracer.cc +++ b/js/src/regexp/regexp-macro-assembler-tracer.cc @@ -349,17 +349,15 @@ void RegExpMacroAssemblerTracer::CheckNotBackReference(int start_reg, assembler_->CheckNotBackReference(start_reg, read_backward, on_no_match); } - void RegExpMacroAssemblerTracer::CheckNotBackReferenceIgnoreCase( - int start_reg, bool read_backward, bool unicode, Label* on_no_match) { - PrintF(" CheckNotBackReferenceIgnoreCase(register=%d, %s %s, label[%08x]);\n", + int start_reg, bool read_backward, Label* on_no_match) { + PrintF(" CheckNotBackReferenceIgnoreCase(register=%d, %s, label[%08x]);\n", start_reg, read_backward ? "backward" : "forward", - unicode ? "unicode" : "non-unicode", LabelToInt(on_no_match)); - assembler_->CheckNotBackReferenceIgnoreCase(start_reg, read_backward, unicode, + LabelToInt(on_no_match)); + assembler_->CheckNotBackReferenceIgnoreCase(start_reg, read_backward, on_no_match); } - void RegExpMacroAssemblerTracer::CheckPosition(int cp_offset, Label* on_outside_input) { PrintF(" CheckPosition(cp_offset=%d, label[%08x]);\n", cp_offset, diff --git a/js/src/regexp/regexp-macro-assembler-tracer.h b/js/src/regexp/regexp-macro-assembler-tracer.h index 938f84796d..5332e59b89 100644 --- a/js/src/regexp/regexp-macro-assembler-tracer.h +++ b/js/src/regexp/regexp-macro-assembler-tracer.h @@ -33,7 +33,6 @@ class RegExpMacroAssemblerTracer: public RegExpMacroAssembler { void CheckNotBackReference(int start_reg, bool read_backward, Label* on_no_match) override; void CheckNotBackReferenceIgnoreCase(int start_reg, bool read_backward, - bool unicode, Label* on_no_match) override; void CheckNotCharacter(unsigned c, Label* on_not_equal) override; void CheckNotCharacterAfterAnd(unsigned c, unsigned and_with, diff --git a/js/src/regexp/regexp-macro-assembler.cc b/js/src/regexp/regexp-macro-assembler.cc index 4a8dcd3ce8..7f8de25437 100644 --- a/js/src/regexp/regexp-macro-assembler.cc +++ b/js/src/regexp/regexp-macro-assembler.cc @@ -110,34 +110,7 @@ bool NativeRegExpMacroAssembler::CanReadUnaligned() { return FLAG_enable_regexp_unaligned_accesses && !slow_safe(); } -const byte* NativeRegExpMacroAssembler::StringCharacterPosition( - String subject, int start_index, const DisallowHeapAllocation& no_gc) { - if (subject.IsConsString()) { - subject = ConsString::cast(subject).first(); - } else if (subject.IsSlicedString()) { - start_index += SlicedString::cast(subject).offset(); - subject = SlicedString::cast(subject).parent(); - } - if (subject.IsThinString()) { - subject = ThinString::cast(subject).actual(); - } - DCHECK_LE(0, start_index); - DCHECK_LE(start_index, subject.length()); - if (subject.IsSeqOneByteString()) { - return reinterpret_cast( - SeqOneByteString::cast(subject).GetChars(no_gc) + start_index); - } else if (subject.IsSeqTwoByteString()) { - return reinterpret_cast( - SeqTwoByteString::cast(subject).GetChars(no_gc) + start_index); - } else if (subject.IsExternalOneByteString()) { - return reinterpret_cast( - ExternalOneByteString::cast(subject).GetChars() + start_index); - } else { - DCHECK(subject.IsExternalTwoByteString()); - return reinterpret_cast( - ExternalTwoByteString::cast(subject).GetChars() + start_index); - } -} +#ifndef COMPILING_IRREGEXP_FOR_EXTERNAL_EMBEDDER // This method may only be called after an interrupt. int NativeRegExpMacroAssembler::CheckStackGuardState( @@ -145,9 +118,10 @@ int NativeRegExpMacroAssembler::CheckStackGuardState( Address* return_address, Code re_code, Address* subject, const byte** input_start, const byte** input_end) { DisallowHeapAllocation no_gc; + Address old_pc = PointerAuthentication::AuthenticatePC(return_address, 0); + DCHECK_LE(re_code.raw_instruction_start(), old_pc); + DCHECK_LE(old_pc, re_code.raw_instruction_end()); - DCHECK(re_code.raw_instruction_start() <= *return_address); - DCHECK(*return_address <= re_code.raw_instruction_end()); StackLimitCheck check(isolate); bool js_has_overflowed = check.JsHasOverflowed(); @@ -189,9 +163,11 @@ int NativeRegExpMacroAssembler::CheckStackGuardState( } if (*code_handle != re_code) { // Return address no longer valid - intptr_t delta = code_handle->address() - re_code.address(); // Overwrite the return address on the stack. - *return_address += delta; + intptr_t delta = code_handle->address() - re_code.address(); + Address new_pc = old_pc + delta; + // TODO(v8:10026): avoid replacing a signed pointer. + PointerAuthentication::ReplacePC(return_address, new_pc, 0); } // If we continue, we need to update the subject string addresses. @@ -206,8 +182,7 @@ int NativeRegExpMacroAssembler::CheckStackGuardState( } else { *subject = subject_handle->ptr(); intptr_t byte_length = *input_end - *input_start; - *input_start = - StringCharacterPosition(*subject_handle, start_index, no_gc); + *input_start = subject_handle->AddressOfCharacterAt(start_index, no_gc); *input_end = *input_start + byte_length; } } @@ -255,7 +230,7 @@ int NativeRegExpMacroAssembler::Match(Handle regexp, DisallowHeapAllocation no_gc; const byte* input_start = - StringCharacterPosition(subject_ptr, start_offset + slice_offset, no_gc); + subject_ptr.AddressOfCharacterAt(start_offset + slice_offset, no_gc); int byte_length = char_length << char_size_shift; const byte* input_end = input_start + byte_length; return Execute(*subject, start_offset, input_start, input_end, offsets_vector, @@ -301,6 +276,8 @@ int NativeRegExpMacroAssembler::Execute( return result; } +#endif // !COMPILING_IRREGEXP_FOR_EXTERNAL_EMBEDDER + // clang-format off const byte NativeRegExpMacroAssembler::word_character_map[] = { 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, diff --git a/js/src/regexp/regexp-macro-assembler.h b/js/src/regexp/regexp-macro-assembler.h index dd059a43d0..ef3961a70a 100644 --- a/js/src/regexp/regexp-macro-assembler.h +++ b/js/src/regexp/regexp-macro-assembler.h @@ -87,7 +87,7 @@ class RegExpMacroAssembler { virtual void CheckNotBackReference(int start_reg, bool read_backward, Label* on_no_match) = 0; virtual void CheckNotBackReferenceIgnoreCase(int start_reg, - bool read_backward, bool unicode, + bool read_backward, Label* on_no_match) = 0; // Check the current character for a match with a literal character. If we // fail to match then goto the on_failure label. End of input always @@ -122,6 +122,11 @@ class RegExpMacroAssembler { // not have custom support. // May clobber the current loaded character. virtual bool CheckSpecialCharacterClass(uc16 type, Label* on_no_match); + + // Control-flow integrity: + // Define a jump target and bind a label. + virtual void BindJumpTarget(Label* label) { Bind(label); } + virtual void Fail() = 0; virtual Handle GetCode(Handle source) = 0; virtual void GoTo(Label* label) = 0; @@ -246,9 +251,6 @@ class NativeRegExpMacroAssembler: public RegExpMacroAssembler { static Address GrowStack(Address stack_pointer, Address* stack_top, Isolate* isolate); - static const byte* StringCharacterPosition( - String subject, int start_index, const DisallowHeapAllocation& no_gc); - static int CheckStackGuardState(Isolate* isolate, int start_index, RegExp::CallOrigin call_origin, Address* return_address, Code re_code, diff --git a/js/src/regexp/regexp-parser.cc b/js/src/regexp/regexp-parser.cc index 377b942477..e2bbb6ed03 100644 --- a/js/src/regexp/regexp-parser.cc +++ b/js/src/regexp/regexp-parser.cc @@ -17,11 +17,10 @@ namespace v8 { namespace internal { -RegExpParser::RegExpParser(FlatStringReader* in, Handle* error, - JSRegExp::Flags flags, Isolate* isolate, Zone* zone) +RegExpParser::RegExpParser(FlatStringReader* in, JSRegExp::Flags flags, + Isolate* isolate, Zone* zone) : isolate_(isolate), zone_(zone), - error_(error), captures_(nullptr), named_captures_(nullptr), named_back_references_(nullptr), @@ -74,13 +73,12 @@ void RegExpParser::Advance() { if (FLAG_correctness_fuzzer_suppressions) { FATAL("Aborting on stack overflow"); } - ReportError(CStrVector( - MessageFormatter::TemplateString(MessageTemplate::kStackOverflow))); + ReportError(RegExpError::kStackOverflow); } else if (zone()->excess_allocation()) { if (FLAG_correctness_fuzzer_suppressions) { FATAL("Aborting on excess zone allocation"); } - ReportError(CStrVector("Regular expression too large")); + ReportError(RegExpError::kTooLarge); } else { current_ = ReadNext(); } @@ -132,15 +130,12 @@ bool RegExpParser::IsSyntaxCharacterOrSlash(uc32 c) { return false; } - -RegExpTree* RegExpParser::ReportError(Vector message) { +RegExpTree* RegExpParser::ReportError(RegExpError error) { if (failed_) return nullptr; // Do not overwrite any existing error. failed_ = true; - *error_ = isolate() - ->factory() - ->NewStringFromOneByte(Vector::cast(message)) - .ToHandleChecked(); - // Zip to the end to make sure the no more input is read. + error_ = error; + error_pos_ = position(); + // Zip to the end to make sure no more input is read. current_ = kEndMarker; next_pos_ = in()->length(); return nullptr; @@ -187,14 +182,14 @@ RegExpTree* RegExpParser::ParseDisjunction() { case kEndMarker: if (state->IsSubexpression()) { // Inside a parenthesized group when hitting end of input. - return ReportError(CStrVector("Unterminated group")); + return ReportError(RegExpError::kUnterminatedGroup); } DCHECK_EQ(INITIAL, state->group_type()); // Parsing completed successfully. return builder->ToRegExp(); case ')': { if (!state->IsSubexpression()) { - return ReportError(CStrVector("Unmatched ')'")); + return ReportError(RegExpError::kUnmatchedParen); } DCHECK_NE(INITIAL, state->group_type()); @@ -245,7 +240,7 @@ RegExpTree* RegExpParser::ParseDisjunction() { case '*': case '+': case '?': - return ReportError(CStrVector("Nothing to repeat")); + return ReportError(RegExpError::kNothingToRepeat); case '^': { Advance(); if (builder->multiline()) { @@ -300,7 +295,7 @@ RegExpTree* RegExpParser::ParseDisjunction() { case '\\': switch (Next()) { case kEndMarker: - return ReportError(CStrVector("\\ at end of pattern")); + return ReportError(RegExpError::kEscapeAtEndOfPattern); case 'b': Advance(2); builder->AddAssertion(new (zone()) RegExpAssertion( @@ -340,7 +335,8 @@ RegExpTree* RegExpParser::ParseDisjunction() { if (unicode()) { ZoneList* ranges = new (zone()) ZoneList(2, zone()); - std::vector name_1, name_2; + ZoneVector name_1(zone()); + ZoneVector name_2(zone()); if (ParsePropertyClassName(&name_1, &name_2)) { if (AddPropertyClassRange(ranges, p == 'P', name_1, name_2)) { RegExpCharacterClass* cc = new (zone()) @@ -356,7 +352,7 @@ RegExpTree* RegExpParser::ParseDisjunction() { } } } - return ReportError(CStrVector("Invalid property name")); + return ReportError(RegExpError::kInvalidPropertyName); } else { builder->AddCharacter(p); } @@ -392,7 +388,7 @@ RegExpTree* RegExpParser::ParseDisjunction() { // With /u, no identity escapes except for syntax characters // are allowed. Otherwise, all identity escapes are allowed. if (unicode()) { - return ReportError(CStrVector("Invalid escape")); + return ReportError(RegExpError::kInvalidEscape); } uc32 first_digit = Next(); if (first_digit == '8' || first_digit == '9') { @@ -406,7 +402,7 @@ RegExpTree* RegExpParser::ParseDisjunction() { Advance(); if (unicode() && Next() >= '0' && Next() <= '9') { // With /u, decimal escape with leading 0 are not parsed as octal. - return ReportError(CStrVector("Invalid decimal escape")); + return ReportError(RegExpError::kInvalidDecimalEscape); } uc32 octal = ParseOctalLiteral(); builder->AddCharacter(octal); @@ -447,7 +443,7 @@ RegExpTree* RegExpParser::ParseDisjunction() { // ES#prod-annexB-ExtendedPatternCharacter if (unicode()) { // With /u, invalid escapes are not treated as identity escapes. - return ReportError(CStrVector("Invalid unicode escape")); + return ReportError(RegExpError::kInvalidUnicodeEscape); } builder->AddCharacter('\\'); } else { @@ -465,7 +461,7 @@ RegExpTree* RegExpParser::ParseDisjunction() { builder->AddCharacter('x'); } else { // With /u, invalid escapes are not treated as identity escapes. - return ReportError(CStrVector("Invalid escape")); + return ReportError(RegExpError::kInvalidEscape); } break; } @@ -478,7 +474,7 @@ RegExpTree* RegExpParser::ParseDisjunction() { builder->AddCharacter('u'); } else { // With /u, invalid escapes are not treated as identity escapes. - return ReportError(CStrVector("Invalid Unicode escape")); + return ReportError(RegExpError::kInvalidUnicodeEscape); } break; } @@ -502,7 +498,7 @@ RegExpTree* RegExpParser::ParseDisjunction() { builder->AddCharacter(current()); Advance(); } else { - return ReportError(CStrVector("Invalid escape")); + return ReportError(RegExpError::kInvalidEscape); } break; } @@ -510,13 +506,13 @@ RegExpTree* RegExpParser::ParseDisjunction() { case '{': { int dummy; bool parsed = ParseIntervalQuantifier(&dummy, &dummy CHECK_FAILED); - if (parsed) return ReportError(CStrVector("Nothing to repeat")); + if (parsed) return ReportError(RegExpError::kNothingToRepeat); V8_FALLTHROUGH; } case '}': case ']': if (unicode()) { - return ReportError(CStrVector("Lone quantifier brackets")); + return ReportError(RegExpError::kLoneQuantifierBrackets); } V8_FALLTHROUGH; default: @@ -551,13 +547,12 @@ RegExpTree* RegExpParser::ParseDisjunction() { case '{': if (ParseIntervalQuantifier(&min, &max)) { if (max < min) { - return ReportError( - CStrVector("numbers out of order in {} quantifier")); + return ReportError(RegExpError::kRangeOutOfOrder); } break; } else if (unicode()) { // With /u, incomplete quantifiers are not allowed. - return ReportError(CStrVector("Incomplete quantifier")); + return ReportError(RegExpError::kIncompleteQuantifier); } continue; default: @@ -573,7 +568,7 @@ RegExpTree* RegExpParser::ParseDisjunction() { Advance(); } if (!builder->AddQuantifierToAtom(min, max, quantifier_type)) { - return ReportError(CStrVector("Invalid quantifier")); + return ReportError(RegExpError::kInvalidQuantifier); } } } @@ -608,7 +603,7 @@ RegExpParser::RegExpParserState* RegExpParser::ParseOpenParenthesis( case 's': case 'm': { if (!FLAG_regexp_mode_modifiers) { - ReportError(CStrVector("Invalid group")); + ReportError(RegExpError::kInvalidGroup); return nullptr; } Advance(); @@ -617,7 +612,7 @@ RegExpParser::RegExpParserState* RegExpParser::ParseOpenParenthesis( switch (current()) { case '-': if (!flags_sense) { - ReportError(CStrVector("Multiple dashes in flag group")); + ReportError(RegExpError::kMultipleFlagDashes); return nullptr; } flags_sense = false; @@ -631,7 +626,7 @@ RegExpParser::RegExpParserState* RegExpParser::ParseOpenParenthesis( if (current() == 'm') bit = JSRegExp::kMultiline; if (current() == 's') bit = JSRegExp::kDotAll; if (((switch_on | switch_off) & bit) != 0) { - ReportError(CStrVector("Repeated flag in flag group")); + ReportError(RegExpError::kRepeatedFlag); return nullptr; } if (flags_sense) { @@ -659,7 +654,7 @@ RegExpParser::RegExpParserState* RegExpParser::ParseOpenParenthesis( subexpr_type = GROUPING; // Will break us out of the outer loop. continue; default: - ReportError(CStrVector("Invalid flag group")); + ReportError(RegExpError::kInvalidFlagGroup); return nullptr; } } @@ -683,13 +678,13 @@ RegExpParser::RegExpParserState* RegExpParser::ParseOpenParenthesis( Advance(); break; default: - ReportError(CStrVector("Invalid group")); + ReportError(RegExpError::kInvalidGroup); return nullptr; } } if (subexpr_type == CAPTURE) { if (captures_started_ >= JSRegExp::kMaxCaptures) { - ReportError(CStrVector("Too many captures")); + ReportError(RegExpError::kTooManyCaptures); return nullptr; } captures_started_++; @@ -838,20 +833,20 @@ const ZoneVector* RegExpParser::ParseCaptureGroupName() { if (c == '\\' && current() == 'u') { Advance(); if (!ParseUnicodeEscape(&c)) { - ReportError(CStrVector("Invalid Unicode escape sequence")); + ReportError(RegExpError::kInvalidUnicodeEscape); return nullptr; } } // The backslash char is misclassified as both ID_Start and ID_Continue. if (c == '\\') { - ReportError(CStrVector("Invalid capture group name")); + ReportError(RegExpError::kInvalidCaptureGroupName); return nullptr; } if (at_start) { if (!IsIdentifierStart(c)) { - ReportError(CStrVector("Invalid capture group name")); + ReportError(RegExpError::kInvalidCaptureGroupName); return nullptr; } push_code_unit(name, c); @@ -862,7 +857,7 @@ const ZoneVector* RegExpParser::ParseCaptureGroupName() { } else if (IsIdentifierPart(c)) { push_code_unit(name, c); } else { - ReportError(CStrVector("Invalid capture group name")); + ReportError(RegExpError::kInvalidCaptureGroupName); return nullptr; } } @@ -889,7 +884,7 @@ bool RegExpParser::CreateNamedCaptureAtIndex(const ZoneVector* name, const auto& named_capture_it = named_captures_->find(capture); if (named_capture_it != named_captures_->end()) { - ReportError(CStrVector("Duplicate capture group name")); + ReportError(RegExpError::kDuplicateCaptureGroupName); return false; } } @@ -903,7 +898,7 @@ bool RegExpParser::ParseNamedBackReference(RegExpBuilder* builder, RegExpParserState* state) { // The parser is assumed to be on the '<' in \k. if (current() != '<') { - ReportError(CStrVector("Invalid named reference")); + ReportError(RegExpError::kInvalidNamedReference); return false; } @@ -936,7 +931,7 @@ void RegExpParser::PatchNamedBackReferences() { if (named_back_references_ == nullptr) return; if (named_captures_ == nullptr) { - ReportError(CStrVector("Invalid named capture referenced")); + ReportError(RegExpError::kInvalidNamedCaptureReference); return; } @@ -957,7 +952,7 @@ void RegExpParser::PatchNamedBackReferences() { if (capture_it != named_captures_->end()) { index = (*capture_it)->index(); } else { - ReportError(CStrVector("Invalid named capture referenced")); + ReportError(RegExpError::kInvalidNamedCaptureReference); return; } @@ -1378,8 +1373,8 @@ bool IsUnicodePropertyValueCharacter(char c) { } // anonymous namespace -bool RegExpParser::ParsePropertyClassName(std::vector* name_1, - std::vector* name_2) { +bool RegExpParser::ParsePropertyClassName(ZoneVector* name_1, + ZoneVector* name_2) { DCHECK(name_1->empty()); DCHECK(name_2->empty()); // Parse the property class as follows: @@ -1418,8 +1413,8 @@ bool RegExpParser::ParsePropertyClassName(std::vector* name_1, bool RegExpParser::AddPropertyClassRange(ZoneList* add_to, bool negate, - const std::vector& name_1, - const std::vector& name_2) { + const ZoneVector& name_1, + const ZoneVector& name_2) { if (name_2.empty()) { // First attempt to interpret as general category property value name. const char* name = name_1.data(); @@ -1456,7 +1451,7 @@ bool RegExpParser::AddPropertyClassRange(ZoneList* add_to, } } -RegExpTree* RegExpParser::GetPropertySequence(const std::vector& name_1) { +RegExpTree* RegExpParser::GetPropertySequence(const ZoneVector& name_1) { if (!FLAG_harmony_regexp_sequence) return nullptr; const char* name = name_1.data(); const uc32* sequence_list = nullptr; @@ -1522,19 +1517,19 @@ RegExpTree* RegExpParser::GetPropertySequence(const std::vector& name_1) { #else // V8_INTL_SUPPORT -bool RegExpParser::ParsePropertyClassName(std::vector* name_1, - std::vector* name_2) { +bool RegExpParser::ParsePropertyClassName(ZoneVector* name_1, + ZoneVector* name_2) { return false; } bool RegExpParser::AddPropertyClassRange(ZoneList* add_to, bool negate, - const std::vector& name_1, - const std::vector& name_2) { + const ZoneVector& name_1, + const ZoneVector& name_2) { return false; } -RegExpTree* RegExpParser::GetPropertySequence(const std::vector& name) { +RegExpTree* RegExpParser::GetPropertySequence(const ZoneVector& name) { return nullptr; } @@ -1598,7 +1593,7 @@ uc32 RegExpParser::ParseClassCharacterEscape() { } if (unicode()) { // With /u, invalid escapes are not treated as identity escapes. - ReportError(CStrVector("Invalid class escape")); + ReportError(RegExpError::kInvalidClassEscape); return 0; } if ((controlLetter >= '0' && controlLetter <= '9') || @@ -1631,7 +1626,7 @@ uc32 RegExpParser::ParseClassCharacterEscape() { // ES#prod-annexB-LegacyOctalEscapeSequence if (unicode()) { // With /u, decimal escape is not interpreted as octal character code. - ReportError(CStrVector("Invalid class escape")); + ReportError(RegExpError::kInvalidClassEscape); return 0; } return ParseOctalLiteral(); @@ -1641,7 +1636,7 @@ uc32 RegExpParser::ParseClassCharacterEscape() { if (ParseHexEscape(2, &value)) return value; if (unicode()) { // With /u, invalid escapes are not treated as identity escapes. - ReportError(CStrVector("Invalid escape")); + ReportError(RegExpError::kInvalidEscape); return 0; } // If \x is not followed by a two-digit hexadecimal, treat it @@ -1654,7 +1649,7 @@ uc32 RegExpParser::ParseClassCharacterEscape() { if (ParseUnicodeEscape(&value)) return value; if (unicode()) { // With /u, invalid escapes are not treated as identity escapes. - ReportError(CStrVector("Invalid unicode escape")); + ReportError(RegExpError::kInvalidUnicodeEscape); return 0; } // If \u is not followed by a two-digit hexadecimal, treat it @@ -1669,11 +1664,11 @@ uc32 RegExpParser::ParseClassCharacterEscape() { Advance(); return result; } - ReportError(CStrVector("Invalid escape")); + ReportError(RegExpError::kInvalidEscape); return 0; } } - return 0; + UNREACHABLE(); } void RegExpParser::ParseClassEscape(ZoneList* ranges, @@ -1696,17 +1691,18 @@ void RegExpParser::ParseClassEscape(ZoneList* ranges, return; } case kEndMarker: - ReportError(CStrVector("\\ at end of pattern")); + ReportError(RegExpError::kEscapeAtEndOfPattern); return; case 'p': case 'P': if (unicode()) { bool negate = Next() == 'P'; Advance(2); - std::vector name_1, name_2; + ZoneVector name_1(zone); + ZoneVector name_2(zone); if (!ParsePropertyClassName(&name_1, &name_2) || !AddPropertyClassRange(ranges, negate, name_1, name_2)) { - ReportError(CStrVector("Invalid property name in character class")); + ReportError(RegExpError::kInvalidClassPropertyName); } *is_class_escape = true; return; @@ -1725,10 +1721,6 @@ void RegExpParser::ParseClassEscape(ZoneList* ranges, } RegExpTree* RegExpParser::ParseCharacterClass(const RegExpBuilder* builder) { - static const char* kUnterminated = "Unterminated character class"; - static const char* kRangeInvalid = "Invalid character class"; - static const char* kRangeOutOfOrder = "Range out of order in character class"; - DCHECK_EQ(current(), '['); Advance(); bool is_negated = false; @@ -1761,7 +1753,7 @@ RegExpTree* RegExpParser::ParseCharacterClass(const RegExpBuilder* builder) { // Either end is an escaped character class. Treat the '-' verbatim. if (unicode()) { // ES2015 21.2.2.15.1 step 1. - return ReportError(CStrVector(kRangeInvalid)); + return ReportError(RegExpError::kInvalidCharacterClass); } if (!is_class_1) ranges->Add(CharacterRange::Singleton(char_1), zone()); ranges->Add(CharacterRange::Singleton('-'), zone()); @@ -1770,7 +1762,7 @@ RegExpTree* RegExpParser::ParseCharacterClass(const RegExpBuilder* builder) { } // ES2015 21.2.2.15.1 step 6. if (char_1 > char_2) { - return ReportError(CStrVector(kRangeOutOfOrder)); + return ReportError(RegExpError::kOutOfOrderCharacterClass); } ranges->Add(CharacterRange::Range(char_1, char_2), zone()); } else { @@ -1778,7 +1770,7 @@ RegExpTree* RegExpParser::ParseCharacterClass(const RegExpBuilder* builder) { } } if (!has_more()) { - return ReportError(CStrVector(kUnterminated)); + return ReportError(RegExpError::kUnterminatedCharacterClass); } Advance(); RegExpCharacterClass::CharacterClassFlags character_class_flags; @@ -1795,14 +1787,16 @@ bool RegExpParser::ParseRegExp(Isolate* isolate, Zone* zone, FlatStringReader* input, JSRegExp::Flags flags, RegExpCompileData* result) { DCHECK(result != nullptr); - RegExpParser parser(input, &result->error, flags, isolate, zone); + RegExpParser parser(input, flags, isolate, zone); RegExpTree* tree = parser.ParsePattern(); if (parser.failed()) { DCHECK(tree == nullptr); - DCHECK(!result->error.is_null()); + DCHECK(parser.error_ != RegExpError::kNone); + result->error = parser.error_; + result->error_pos = parser.error_pos_; } else { DCHECK(tree != nullptr); - DCHECK(result->error.is_null()); + DCHECK(parser.error_ == RegExpError::kNone); if (FLAG_trace_regexp_parser) { StdoutStream os; tree->Print(os, zone); diff --git a/js/src/regexp/regexp-parser.h b/js/src/regexp/regexp-parser.h index 91677d6c35..131d12161f 100644 --- a/js/src/regexp/regexp-parser.h +++ b/js/src/regexp/regexp-parser.h @@ -6,6 +6,7 @@ #define V8_REGEXP_REGEXP_PARSER_H_ #include "regexp/regexp-ast.h" +#include "regexp/regexp-error.h" namespace v8 { namespace internal { @@ -150,8 +151,8 @@ class RegExpBuilder : public ZoneObject { class V8_EXPORT_PRIVATE RegExpParser { public: - RegExpParser(FlatStringReader* in, Handle* error, - JSRegExp::Flags flags, Isolate* isolate, Zone* zone); + RegExpParser(FlatStringReader* in, JSRegExp::Flags flags, Isolate* isolate, + Zone* zone); static bool ParseRegExp(Isolate* isolate, Zone* zone, FlatStringReader* input, JSRegExp::Flags flags, RegExpCompileData* result); @@ -174,13 +175,13 @@ class V8_EXPORT_PRIVATE RegExpParser { bool ParseUnicodeEscape(uc32* value); bool ParseUnlimitedLengthHexNumber(int max_value, uc32* value); - bool ParsePropertyClassName(std::vector* name_1, - std::vector* name_2); + bool ParsePropertyClassName(ZoneVector* name_1, + ZoneVector* name_2); bool AddPropertyClassRange(ZoneList* add_to, bool negate, - const std::vector& name_1, - const std::vector& name_2); + const ZoneVector& name_1, + const ZoneVector& name_2); - RegExpTree* GetPropertySequence(const std::vector& name_1); + RegExpTree* GetPropertySequence(const ZoneVector& name_1); RegExpTree* ParseCharacterClass(const RegExpBuilder* state); uc32 ParseOctalLiteral(); @@ -199,7 +200,7 @@ class V8_EXPORT_PRIVATE RegExpParser { char ParseClassEscape(); - RegExpTree* ReportError(Vector message); + RegExpTree* ReportError(RegExpError error); void Advance(); void Advance(int dist); void Reset(int pos); @@ -332,7 +333,8 @@ class V8_EXPORT_PRIVATE RegExpParser { Isolate* isolate_; Zone* zone_; - Handle* error_; + RegExpError error_ = RegExpError::kNone; + int error_pos_ = 0; ZoneList* captures_; ZoneSet* named_captures_; ZoneList* named_back_references_; diff --git a/js/src/regexp/regexp-shim.h b/js/src/regexp/regexp-shim.h index 38b0357272..462e396f40 100644 --- a/js/src/regexp/regexp-shim.h +++ b/js/src/regexp/regexp-shim.h @@ -60,6 +60,7 @@ class RegExpStack; #define DCHECK_NOT_NULL(val) MOZ_ASSERT((val) != nullptr) #define DCHECK_IMPLIES(lhs, rhs) MOZ_ASSERT_IF(lhs, rhs) #define CHECK MOZ_RELEASE_ASSERT +#define CHECK_LE(lhs, rhs) MOZ_RELEASE_ASSERT((lhs) <= (rhs)) template static constexpr inline T Min(T t1, T t2) { @@ -1009,7 +1010,7 @@ private: public: // An empty stub for telemetry we don't support - void IncreaseTotalRegexpCodeGenerated(int size) {} + void IncreaseTotalRegexpCodeGenerated(Handle code) {} Counters* counters() { return &counters_; } @@ -1155,6 +1156,7 @@ extern bool FLAG_trace_regexp_parser; extern bool FLAG_trace_regexp_peephole_optimization; #define V8_USE_COMPUTED_GOTO 1 +#define COMPILING_IRREGEXP_FOR_EXTERNAL_EMBEDDER } // namespace internal } // namespace v8 diff --git a/js/src/regexp/regexp-stack.h b/js/src/regexp/regexp-stack.h index 812195ad12..0b452c0059 100644 --- a/js/src/regexp/regexp-stack.h +++ b/js/src/regexp/regexp-stack.h @@ -36,6 +36,9 @@ class RegExpStackScope { class RegExpStack { public: + RegExpStack(); + ~RegExpStack(); + // Number of allocated locations on the stack below the limit. // No sequence of pushes must be longer that this without doing a stack-limit // check. @@ -75,9 +78,6 @@ class RegExpStack { static constexpr size_t kMaximumStackSize = 64 * MB; private: - RegExpStack(); - ~RegExpStack(); - // Artificial limit used when the thread-local state has been destroyed. static const Address kMemoryTop = static_cast
(static_cast(-1)); diff --git a/js/src/regexp/regexp.h b/js/src/regexp/regexp.h index cce58da384..a36662b78a 100644 --- a/js/src/regexp/regexp.h +++ b/js/src/regexp/regexp.h @@ -5,6 +5,7 @@ #ifndef V8_REGEXP_REGEXP_H_ #define V8_REGEXP_REGEXP_H_ +#include "regexp/regexp-error.h" #include "regexp/regexp-shim.h" namespace v8 { @@ -42,7 +43,11 @@ struct RegExpCompileData { // The error message. Only used if an error occurred during parsing or // compilation. - Handle error; + RegExpError error = RegExpError::kNone; + + // The position at which the error was detected. Only used if an + // error occurred. + int error_pos = 0; // The number of capture groups, without the global capture \0. int capture_count = 0; diff --git a/js/src/regexp/special-case.cc b/js/src/regexp/special-case.cc index d60b987645..6b12d28d7d 100644 --- a/js/src/regexp/special-case.cc +++ b/js/src/regexp/special-case.cc @@ -1,10 +1,15 @@ -// Copyright 2019 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. +// Copyright 2020 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. // Automatically generated by regexp/gen-regexp-special-case.cc -// The following functions are used to build icu::UnicodeSet -// for specical cases different between Unicode and ECMA262. + +// The following functions are used to build UnicodeSets +// for special cases where the case-folding algorithm used by +// UnicodeSet::closeOver(USET_CASE_INSENSITIVE) does not match +// the algorithm defined in ECMAScript 2020 21.2.2.8.2 (Runtime +// Semantics: Canonicalize) step 3. + #ifdef V8_INTL_SUPPORT #include "regexp/special-case.h" @@ -14,14 +19,46 @@ namespace internal { icu::UnicodeSet BuildIgnoreSet() { icu::UnicodeSet set; + set.add(0xdf); + set.add(0x17f); + set.add(0x390); + set.add(0x3b0); set.add(0x3f4); + set.add(0x1e9e); + set.add(0x1f80, 0x1faf); + set.add(0x1fb3); + set.add(0x1fbc); + set.add(0x1fc3); + set.add(0x1fcc); + set.add(0x1fd3); + set.add(0x1fe3); + set.add(0x1ff3); + set.add(0x1ffc); set.add(0x2126); set.add(0x212a, 0x212b); + set.add(0xfb05, 0xfb06); set.freeze(); return set; } + +struct IgnoreSetData { + IgnoreSetData() : set(BuildIgnoreSet()) {} + const icu::UnicodeSet set; +}; + +//static +const icu::UnicodeSet& RegExpCaseFolding::IgnoreSet() { + static base::LazyInstance::type set = + LAZY_INSTANCE_INITIALIZER; + return set.Pointer()->set; +} + icu::UnicodeSet BuildSpecialAddSet() { icu::UnicodeSet set; + set.add(0x4b); + set.add(0x53); + set.add(0x6b); + set.add(0x73); set.add(0xc5); set.add(0xe5); set.add(0x398); @@ -33,6 +70,19 @@ icu::UnicodeSet BuildSpecialAddSet() { return set; } +struct SpecialAddSetData { + SpecialAddSetData() : set(BuildSpecialAddSet()) {} + const icu::UnicodeSet set; +}; + +//static +const icu::UnicodeSet& RegExpCaseFolding::SpecialAddSet() { + static base::LazyInstance::type set = + LAZY_INSTANCE_INITIALIZER; + return set.Pointer()->set; +} + + } // namespace internal } // namespace v8 #endif // V8_INTL_SUPPORT diff --git a/js/src/regexp/special-case.h b/js/src/regexp/special-case.h index 1ccec5d31a..3aca983028 100644 --- a/js/src/regexp/special-case.h +++ b/js/src/regexp/special-case.h @@ -6,70 +6,108 @@ #define V8_REGEXP_SPECIAL_CASE_H_ #ifdef V8_INTL_SUPPORT -#include "unicode/uversion.h" -namespace U_ICU_NAMESPACE { -class UnicodeSet; -} // namespace U_ICU_NAMESPACE +#include "regexp/regexp-shim.h" + +#include "unicode/uchar.h" +#include "unicode/uniset.h" +#include "unicode/unistr.h" namespace v8 { namespace internal { -// Functions to build special sets of Unicode characters that need special -// handling under "i" mode that cannot use closeOver(USET_CASE_INSENSITIVE). -// -// For the characters in the "ignore set", the process should not treat other -// characters in the result of closeOver(USET_CASE_INSENSITIVE) as case -// equivlant under the ECMA262 RegExp "i" mode because these characters are -// uppercase themselves that no other characters in the set uppercase to. -// -// For the characters in the "special add set", the proecess should add only -// those characters in the result of closeOver(USET_CASE_INSENSITIVE) which is -// not uppercase characters as case equivlant under the ECMA262 RegExp "i" mode -// and also that ONE uppercase character that other non uppercase character -// uppercase into to the set. Other uppercase characters in the result of -// closeOver(USET_CASE_INSENSITIVE) should not be considered because ECMA262 -// RegExp "i" mode consider two characters as "case equivlant" if both -// characters uppercase to the same character. -// -// For example, consider the following case equivalent set defined by Unicode -// standard. Notice there are more than one uppercase characters in this set: -// U+212B Å Angstrom Sign - an uppercase character. -// U+00C5 Å Latin Capital Letter A with Ring Above - an uppercase character. -// U+00E5 å Latin Small Letter A with Ring Above - a lowercase character which -// uppercase to U+00C5. -// In this case equivlant set is a special set and need special handling while -// considering "case equivlant" under the ECMA262 RegExp "i" mode which is -// different than Unicode Standard: -// * U+212B should be included into the "ignore" set because there are no other -// characters, under the ECMA262 "i" mode, are considered as "case equivlant" -// to it because U+212B is itself an uppercase but neither U+00C5 nor U+00E5 -// uppercase to U+212B. -// * U+00C5 and U+00E5 will both be included into the "special add" set. While -// calculate the "equivlant set" under ECMA262 "i" mode, the process will -// add U+00E5, because it is not an uppercase character in the set. The -// process will also add U+00C5, because it is the uppercase character which -// other non uppercase character, U+00C5, uppercase into. -// -// For characters not included in "ignore set" and "special add set", the -// process will just use closeOver(USET_CASE_INSENSITIVE) to calcualte, which is -// much faster. -// -// Under Unicode 12.0, there are only 7 characters in the "special add set" and -// 4 characters in "ignore set" so even the special add process is slower, it is -// limited to a small set of cases only. -// -// The implementation of these two function will be generated by calling ICU -// icu::UnicodeSet during the build time into gen/src/regexp/special-case.cc by -// the code in src/regexp/gen-regexp-special-case.cc. -// -// These two function will be used with LazyInstance<> template to generate -// global sharable set to reduce memory usage and speed up performance. +// Sets of Unicode characters that need special handling under "i" mode -// Function to build and return the Ignore set. -icu::UnicodeSet BuildIgnoreSet(); +// For non-unicode ignoreCase matches (aka "i", not "iu"), ECMA 262 +// defines slightly different case-folding rules than Unicode. An +// input character should match a pattern character if the result of +// the Canonicalize algorithm is the same for both characters. +// +// Roughly speaking, for "i" regexps, Canonicalize(c) is the same as +// c.toUpperCase(), unless a) c.toUpperCase() is a multi-character +// string, or b) c is non-ASCII, and c.toUpperCase() is ASCII. See +// https://tc39.es/ecma262/#sec-runtime-semantics-canonicalize-ch for +// the precise definition. +// +// While compiling such regular expressions, we need to compute the +// set of characters that should match a given input character. (See +// GetCaseIndependentLetters and CharacterRange::AddCaseEquivalents.) +// For almost all characters, this can be efficiently computed using +// UnicodeSet::closeOver(USET_CASE_INSENSITIVE). These sets represent +// the remaining special cases. +// +// For a character c, the rules are as follows: +// +// 1. If c is in neither IgnoreSet nor SpecialAddSet, then calling +// UnicodeSet::closeOver(USET_CASE_INSENSITIVE) on a UnicodeSet +// containing c will produce the set of characters that should +// match /c/i (or /[c]/i), and only those characters. +// +// 2. If c is in IgnoreSet, then the only character it should match is +// itself. However, closeOver will add additional incorrect +// matches. For example, consider SHARP S: 'ß' (U+00DF) and 'ẞ' +// (U+1E9E). Although closeOver('ß') = "ßẞ", uppercase('ß') is +// "SS". Step 3.e therefore requires that 'ß' canonicalizes to +// itself, and should not match 'ẞ'. In these cases, we can skip +// the closeOver entirely, because it will never add an equivalent +// character. +// +// 3. If c is in SpecialAddSet, then it should match at least one +// character other than itself. However, closeOver will add at +// least one additional incorrect match. For example, consider the +// letter 'k'. Closing over 'k' gives "kKK" (lowercase k, uppercase +// K, U+212A KELVIN SIGN). However, because of step 3.g, KELVIN +// SIGN should not match either of the other two characters. As a +// result, "k" and "K" are in SpecialAddSet (and KELVIN SIGN is in +// IgnoreSet). To find the correct matches for characters in +// SpecialAddSet, we closeOver the original character, but filter +// out the results that do not have the same canonical value. +// +// The contents of these sets are calculated at build time by +// src/regexp/gen-regexp-special-case.cc, which generates +// gen/src/regexp/special-case.cc. This is done by iterating over the +// result of closeOver for each BMP character, and finding sets for +// which at least one character has a different canonical value than +// another character. Characters that match no other characters in +// their equivalence class are added to IgnoreSet. Characters that +// match at least one other character are added to SpecialAddSet. -// Function to build and return the Special Add set. -icu::UnicodeSet BuildSpecialAddSet(); +class RegExpCaseFolding final : public AllStatic { + public: + static const icu::UnicodeSet& IgnoreSet(); + static const icu::UnicodeSet& SpecialAddSet(); + + // This implements ECMAScript 2020 21.2.2.8.2 (Runtime Semantics: + // Canonicalize) step 3, which is used to determine whether + // characters match when ignoreCase is true and unicode is false. + static UChar32 Canonicalize(UChar32 ch) { + // a. Assert: ch is a UTF-16 code unit. + CHECK_LE(ch, 0xffff); + + // b. Let s be the String value consisting of the single code unit ch. + icu::UnicodeString s(ch); + + // c. Let u be the same result produced as if by performing the algorithm + // for String.prototype.toUpperCase using s as the this value. + // d. Assert: Type(u) is String. + icu::UnicodeString& u = s.toUpper(); + + // e. If u does not consist of a single code unit, return ch. + if (u.length() != 1) { + return ch; + } + + // f. Let cu be u's single code unit element. + UChar32 cu = u.char32At(0); + + // g. If the value of ch >= 128 and the value of cu < 128, return ch. + if (ch >= 128 && cu < 128) { + return ch; + } + + // h. Return cu. + return cu; + } +}; } // namespace internal } // namespace v8 diff --git a/js/src/regexp/update-headers.py b/js/src/regexp/update-headers.py deleted file mode 100644 index 0cff9d6aee..0000000000 --- a/js/src/regexp/update-headers.py +++ /dev/null @@ -1,38 +0,0 @@ -#!/usr/bin/env python - -# This Source Code Form is subject to the terms of the Mozilla Public -# License, v. 2.0. If a copy of the MPL was not distributed with this file, -# You can obtain one at http://mozilla.org/MPL/2.0/. - -# -# This script modifies V8 regexp source files to make them suitable for -# inclusion in SpiderMonkey. Specifically, it: -# -# 1. Rewrites all #includes of V8 regexp headers to point to their location in -# the SM tree: src/regexp/* --> regexp/* -# 2. Removes all #includes of other V8 src/* headers. The required definitions -# will be provided by regexp-shim.h. -# -# Usage: -# cd js/src/regexp -# find . -name "*.h" -o -name "*.cc" | xargs ./update_headers.py -# - -import fileinput -import re -import sys - -# 1. Rewrite includes of V8 regexp headers -regexp_include = re.compile('#include "src/regexp') -regexp_include_new = '#include "regexp' - -# 2. Remove includes of other V8 headers -other_include = re.compile('#include "src/') - -for line in fileinput.input(inplace=1): - if regexp_include.search(line): - sys.stdout.write(re.sub(regexp_include, regexp_include_new, line)) - elif other_include.search(line): - pass - else: - sys.stdout.write(line) From 0789f637d1034d743ade73fd944241b5e638f8bc Mon Sep 17 00:00:00 2001 From: "Matt A. Tobin" Date: Mon, 9 Nov 2020 20:44:26 -0500 Subject: [PATCH 4/9] Issue #1677 - Part 6: Implement NativeRegExpMacroAssembler for new regexp import based on irregexp/NativeRegExpMacroAssembler.cpp --- js/src/regexp/RegExpTypes.h | 51 + js/src/regexp/moz.build | 1 + js/src/regexp/regexp-macro-assembler-arch.h | 279 +++- .../regexp/regexp-native-macro-assembler.cc | 1215 +++++++++++++++++ js/src/regexp/regexp-shim.cc | 7 + js/src/regexp/regexp-shim.h | 32 +- 6 files changed, 1572 insertions(+), 13 deletions(-) create mode 100644 js/src/regexp/RegExpTypes.h create mode 100644 js/src/regexp/regexp-native-macro-assembler.cc diff --git a/js/src/regexp/RegExpTypes.h b/js/src/regexp/RegExpTypes.h new file mode 100644 index 0000000000..e260b5bb6d --- /dev/null +++ b/js/src/regexp/RegExpTypes.h @@ -0,0 +1,51 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * vim: set ts=8 sts=2 et sw=2 tw=80: + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +// This file forward-defines Irregexp classes that need to be visible +// to the rest of Spidermonkey and re-exports them into js::irregexp. + +#ifndef regexp_RegExpTypes_h +#define regexp_RegExpTypes_h + +namespace js { +class MatchPairs; +} + +namespace v8 { +namespace internal { + +struct InputOutputData { + const void* inputStart; + const void* inputEnd; + + // Index into inputStart (in chars) at which to begin matching. + size_t startIndex; + + js::MatchPairs* matches; + + template + InputOutputData(const CharT* inputStart, const CharT* inputEnd, + size_t startIndex, js::MatchPairs* matches) + : inputStart(inputStart), + inputEnd(inputEnd), + startIndex(startIndex), + matches(matches) + {} +}; + +} // namespace internal +} // namespace v8 + + +namespace js { +namespace irregexp { + +using InputOutputData = v8::internal::InputOutputData; + +} // namespace irregexp +} // namespace js + +#endif // regexp_RegExpTypes_h diff --git a/js/src/regexp/moz.build b/js/src/regexp/moz.build index 1ef280b7fa..4caa4589c0 100644 --- a/js/src/regexp/moz.build +++ b/js/src/regexp/moz.build @@ -22,6 +22,7 @@ SOURCES += [ 'regexp-interpreter.cc', 'regexp-macro-assembler-tracer.cc', 'regexp-macro-assembler.cc', + 'regexp-native-macro-assembler.cc', 'regexp-parser.cc', 'regexp-shim.cc', 'regexp-stack.cc', diff --git a/js/src/regexp/regexp-macro-assembler-arch.h b/js/src/regexp/regexp-macro-assembler-arch.h index 60b5c94de4..1baa5ddd52 100644 --- a/js/src/regexp/regexp-macro-assembler-arch.h +++ b/js/src/regexp/regexp-macro-assembler-arch.h @@ -4,6 +4,10 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +// Copyright 2020 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. + // This file implements the NativeRegExpMacroAssembler interface for // SpiderMonkey. It provides the same interface as each of V8's // architecture-specific implementations. @@ -11,6 +15,277 @@ #ifndef RegexpMacroAssemblerArch_h #define RegexpMacroAssemblerArch_h -#include "regexp/regexp-shim.h" +#include "jit/MacroAssembler.h" +#include "regexp/regexp-macro-assembler.h" -#endif // RegexpMacroAssemblerArch_h +namespace v8 { +namespace internal { + +struct FrameData { + // Character position at the start of the input, stored as a + // negative offset from the end of the string (input_end_pointer_). + size_t inputStart; + + // The backtrack_stack_pointer_ register points to the top of the stack. + // This points to the bottom of the backtrack stack. + void* backtrackStackBase; + + // Copy of the input MatchPairs. + int32_t* matches; // pointer to capture array + int32_t numMatches; // size of capture array +}; + +class SMRegExpMacroAssembler final : public NativeRegExpMacroAssembler { + public: + SMRegExpMacroAssembler(JSContext* cx, Isolate* isolate, + js::jit::StackMacroAssembler& masm, Zone* zone, + Mode mode, uint32_t num_capture_registers); + virtual ~SMRegExpMacroAssembler() {} // Nothing to do here + + virtual int stack_limit_slack(); + virtual IrregexpImplementation Implementation(); + + virtual bool Succeed(); + virtual void Fail(); + + virtual void AdvanceCurrentPosition(int by); + virtual void PopCurrentPosition(); + virtual void PushCurrentPosition(); + virtual void SetCurrentPositionFromEnd(int by); + + virtual void Backtrack(); + virtual void Bind(Label* label); + virtual void GoTo(Label* label); + virtual void PushBacktrack(Label* label); + + virtual void CheckCharacter(uint32_t c, Label* on_equal); + virtual void CheckNotCharacter(uint32_t c, Label* on_not_equal); + virtual void CheckCharacterGT(uc16 limit, Label* on_greater); + virtual void CheckCharacterLT(uc16 limit, Label* on_less); + virtual void CheckCharacterAfterAnd(uint32_t c, uint32_t mask, + Label* on_equal); + virtual void CheckNotCharacterAfterAnd(uint32_t c, uint32_t mask, + Label* on_not_equal); + virtual void CheckNotCharacterAfterMinusAnd(uc16 c, uc16 minus, uc16 mask, + Label* on_not_equal); + virtual void CheckGreedyLoop(Label* on_tos_equals_current_position); + virtual void CheckCharacterInRange(uc16 from, uc16 to, Label* on_in_range); + virtual void CheckCharacterNotInRange(uc16 from, uc16 to, + Label* on_not_in_range); + virtual void CheckAtStart(int cp_offset, Label* on_at_start); + virtual void CheckNotAtStart(int cp_offset, Label* on_not_at_start); + virtual void CheckPosition(int cp_offset, Label* on_outside_input); + virtual void CheckBitInTable(Handle table, Label* on_bit_set); + virtual bool CheckSpecialCharacterClass(uc16 type, Label* on_no_match); + virtual void CheckNotBackReference(int start_reg, bool read_backward, + Label* on_no_match); + virtual void CheckNotBackReferenceIgnoreCase(int start_reg, + bool read_backward, + Label* on_no_match); + + virtual void LoadCurrentCharacterImpl(int cp_offset, Label* on_end_of_input, + bool check_bounds, int characters, + int eats_at_least); + + virtual void AdvanceRegister(int reg, int by); + virtual void IfRegisterGE(int reg, int comparand, Label* if_ge); + virtual void IfRegisterLT(int reg, int comparand, Label* if_lt); + virtual void IfRegisterEqPos(int reg, Label* if_eq); + virtual void PopRegister(int register_index); + virtual void PushRegister(int register_index, + StackCheckFlag check_stack_limit); + virtual void ReadCurrentPositionFromRegister(int reg); + virtual void WriteCurrentPositionToRegister(int reg, int cp_offset); + virtual void ReadStackPointerFromRegister(int reg); + virtual void WriteStackPointerToRegister(int reg); + virtual void SetRegister(int register_index, int to); + virtual void ClearRegisters(int reg_from, int reg_to); + + virtual Handle GetCode(Handle source); + + private: + size_t frameSize_ = 0; + + void createStackFrame(); + void initFrameAndRegs(); + void successHandler(); + void exitHandler(); + void backtrackHandler(); + void stackOverflowHandler(); + + // Push a register on the backtrack stack. + void Push(js::jit::Register value); + + // Pop a value from the backtrack stack. + void Pop(js::jit::Register target); + + void CheckAtStartImpl(int cp_offset, Label* on_cond, + js::jit::Assembler::Condition cond); + void CheckCharacterImpl(js::jit::Imm32 c, Label* on_cond, + js::jit::Assembler::Condition cond); + void CheckCharacterAfterAndImpl(uint32_t c, uint32_t and_with, Label* on_cond, + bool negate); + void CheckCharacterInRangeImpl(uc16 from, uc16 to, Label* on_cond, + js::jit::Assembler::Condition cond); + void CheckNotBackReferenceImpl(int start_reg, bool read_backward, + Label* on_no_match, bool ignore_case); + + void LoadCurrentCharacterUnchecked(int cp_offset, int characters); + + void JumpOrBacktrack(Label* to); + + // MacroAssembler methods that take a Label can be called with a + // null label, which means that we should backtrack if we would jump + // to that label. This is a helper to avoid writing out the same + // logic a dozen times. + inline js::jit::Label* LabelOrBacktrack(Label* to) { + return to ? to->inner() : &backtrack_label_; + } + + void CheckBacktrackStackLimit(); + + static bool GrowBacktrackStack(RegExpStack* regexp_stack); + + static uint32_t CaseInsensitiveCompareStrings(const char16_t* substring1, + const char16_t* substring2, + size_t byteLength); + static uint32_t CaseInsensitiveCompareUCStrings(const char16_t* substring1, + const char16_t* substring2, + size_t byteLength); + + inline int char_size() { return static_cast(mode_); } + inline js::jit::Scale factor() { + return mode_ == UC16 ? js::jit::TimesTwo : js::jit::TimesOne; + } + + js::jit::Address inputStart() { + return js::jit::Address(masm_.getStackPointer(), + offsetof(FrameData, inputStart)); + } + js::jit::Address backtrackStackBase() { + return js::jit::Address(masm_.getStackPointer(), + offsetof(FrameData, backtrackStackBase)); + } + js::jit::Address matches() { + return js::jit::Address(masm_.getStackPointer(), + offsetof(FrameData, matches)); + } + js::jit::Address numMatches() { + return js::jit::Address(masm_.getStackPointer(), + offsetof(FrameData, numMatches)); + } + + // The stack-pointer-relative location of a regexp register. + js::jit::Address register_location(int register_index) { + return js::jit::Address(masm_.getStackPointer(), + register_offset(register_index)); + } + + int32_t register_offset(int register_index) { + MOZ_ASSERT(register_index >= 0 && register_index <= kMaxRegister); + if (num_registers_ <= register_index) { + num_registers_ = register_index + 1; + } + static_assert(alignof(uintptr_t) <= alignof(FrameData)); + return sizeof(FrameData) + register_index * sizeof(uintptr_t*); + } + + JSContext* cx_; + js::jit::StackMacroAssembler& masm_; + + /* + * This assembler uses the following registers: + * + * - current_character_: + * Contains the character (or characters) currently being examined. + * Must be loaded using LoadCurrentCharacter before using any of the + * dispatch methods. After a matching pass for a global regexp, + * temporarily stores the index of capture start. + * - current_position_: + * Current position in input *as negative byte offset from end of string*. + * - input_end_pointer_: + * Points to byte after last character in the input. current_position_ is + * relative to this. + * - backtrack_stack_pointer_: + * Points to tip of the (heap-allocated) backtrack stack. The stack grows + * downward (like the native stack). + * - temp0_, temp1_, temp2_: + * Scratch registers. + * + * The native stack pointer is used to access arguments (InputOutputData), + * local variables (FrameData), and irregexp's internal virtual registers + * (see register_location). + */ + + js::jit::Register current_character_; + js::jit::Register current_position_; + js::jit::Register input_end_pointer_; + js::jit::Register backtrack_stack_pointer_; + js::jit::Register temp0_, temp1_, temp2_; + + js::jit::Label entry_label_; + js::jit::Label start_label_; + js::jit::Label backtrack_label_; + js::jit::Label success_label_; + js::jit::Label exit_label_; + js::jit::Label stack_overflow_label_; + js::jit::Label exit_with_exception_label_; + + // When we generate the code to push a backtrack label's address + // onto the backtrack stack, we don't know its final address. We + // have to patch it after linking. This is slightly delicate, as the + // Label itself (which is allocated on the stack) may not exist by + // the time we link. The approach is as follows: + // + // 1. When we push a label on the backtrack stack (PushBacktrack), + // we bind the label's patchOffset_ field to the offset within + // the code that should be overwritten. This works because each + // label is only pushed by a single instruction. + // + // 2. When we bind a label (Bind), we check to see if it has a + // bound patchOffset_. If it does, we create a LabelPatch mapping + // its patch offset to the offset of the label itself. + // + // 3. While linking the code, we walk the list of label patches + // and patch the code accordingly. + class LabelPatch { + public: + LabelPatch(js::jit::CodeOffset patchOffset, size_t labelOffset) + : patchOffset_(patchOffset), labelOffset_(labelOffset) {} + + js::jit::CodeOffset patchOffset_; + size_t labelOffset_ = 0; + }; + + js::Vector labelPatches_; + void AddLabelPatch(js::jit::CodeOffset patchOffset, size_t labelOffset) { + js::AutoEnterOOMUnsafeRegion oomUnsafe; + if (!labelPatches_.emplaceBack(patchOffset, labelOffset)) { + oomUnsafe.crash("Irregexp label patch"); + } + } + + Mode mode_; + int num_registers_; + int num_capture_registers_; + js::jit::LiveGeneralRegisterSet savedRegisters_; + + public: + using TableVector = + js::Vector, 4, js::SystemAllocPolicy>; + TableVector& tables() { return tables_; } + + private: + TableVector tables_; + void AddTable(PseudoHandle table) { + js::AutoEnterOOMUnsafeRegion oomUnsafe; + if (!tables_.append(std::move(table))) { + oomUnsafe.crash("Irregexp table append"); + } + } +}; + +} // namespace internal +} // namespace v8 + +#endif // RegexpMacroAssemblerArch_h diff --git a/js/src/regexp/regexp-native-macro-assembler.cc b/js/src/regexp/regexp-native-macro-assembler.cc new file mode 100644 index 0000000000..15182ad713 --- /dev/null +++ b/js/src/regexp/regexp-native-macro-assembler.cc @@ -0,0 +1,1215 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * vim: set ts=8 sts=2 et sw=2 tw=80: + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +// Copyright 2020 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. + +#include "jit/Linker.h" +#include "regexp/regexp-macro-assembler-arch.h" +#include "regexp/regexp-stack.h" +#include "vm/MatchPairs.h" + +#include "jit/MacroAssembler-inl.h" + +namespace v8 { +namespace internal { + +using js::MatchPairs; +using js::jit::AbsoluteAddress; +using js::jit::Address; +using js::jit::AllocatableGeneralRegisterSet; +using js::jit::Assembler; +using js::jit::BaseIndex; +using js::jit::CodeLocationLabel; +using js::jit::GeneralRegisterBackwardIterator; +using js::jit::GeneralRegisterForwardIterator; +using js::jit::GeneralRegisterSet; +using js::jit::Imm32; +using js::jit::ImmPtr; +using js::jit::ImmWord; +using js::jit::JitCode; +using js::jit::Linker; +using js::jit::LiveGeneralRegisterSet; +using js::jit::Register; +using js::jit::Registers; +using js::jit::StackMacroAssembler; + +SMRegExpMacroAssembler::SMRegExpMacroAssembler(JSContext* cx, Isolate* isolate, + StackMacroAssembler& masm, + Zone* zone, Mode mode, + uint32_t num_capture_registers) + : NativeRegExpMacroAssembler(isolate, zone), + cx_(cx), + masm_(masm), + mode_(mode), + num_registers_(num_capture_registers), + num_capture_registers_(num_capture_registers) { + // Each capture has a start and an end register + MOZ_ASSERT(num_capture_registers_ % 2 == 0); + + AllocatableGeneralRegisterSet regs(GeneralRegisterSet::All()); + + temp0_ = regs.takeAny(); + temp1_ = regs.takeAny(); + temp2_ = regs.takeAny(); + input_end_pointer_ = regs.takeAny(); + current_character_ = regs.takeAny(); + current_position_ = regs.takeAny(); + backtrack_stack_pointer_ = regs.takeAny(); + savedRegisters_ = js::jit::SavedNonVolatileRegisters(regs); + + masm_.jump(&entry_label_); // We'll generate the entry code later + masm_.bind(&start_label_); // and continue from here. +} + +int SMRegExpMacroAssembler::stack_limit_slack() { + return RegExpStack::kStackLimitSlack; +} + +void SMRegExpMacroAssembler::AdvanceCurrentPosition(int by) { + if (by != 0) { + masm_.addPtr(Imm32(by * char_size()), current_position_); + } +} + +void SMRegExpMacroAssembler::AdvanceRegister(int reg, int by) { + MOZ_ASSERT(reg >= 0 && reg < num_registers_); + if (by != 0) { + masm_.addPtr(Imm32(by), register_location(reg)); + } +} + +void SMRegExpMacroAssembler::Backtrack() { + // Check for an interrupt. We have to restart from the beginning if we + // are interrupted, so we only check for urgent interrupts. + js::jit::Label noInterrupt; + masm_.branchTest32( + Assembler::Zero, AbsoluteAddress(cx_->addressOfInterruptBits()), + Imm32(uint32_t(js::InterruptReason::CallbackUrgent)), &noInterrupt); + masm_.movePtr(ImmWord(js::RegExpRunStatus_Error), temp0_); + masm_.jump(&exit_label_); + masm_.bind(&noInterrupt); + + // Pop code location from backtrack stack and jump to location. + Pop(temp0_); + masm_.jump(temp0_); +} + +void SMRegExpMacroAssembler::Bind(Label* label) { + masm_.bind(label->inner()); + if (label->patchOffset_.bound()) { + AddLabelPatch(label->patchOffset_, label->pos()); + } +} + +// Check if current_position + cp_offset is the input start +void SMRegExpMacroAssembler::CheckAtStartImpl(int cp_offset, Label* on_cond, + Assembler::Condition cond) { + Address addr(current_position_, cp_offset * char_size()); + masm_.computeEffectiveAddress(addr, temp0_); + + masm_.branchPtr(cond, inputStart(), temp0_, + LabelOrBacktrack(on_cond)); +} + +void SMRegExpMacroAssembler::CheckAtStart(int cp_offset, Label* on_at_start) { + CheckAtStartImpl(cp_offset, on_at_start, Assembler::Equal); +} + +void SMRegExpMacroAssembler::CheckNotAtStart(int cp_offset, + Label* on_not_at_start) { + CheckAtStartImpl(cp_offset, on_not_at_start, Assembler::NotEqual); +} + +void SMRegExpMacroAssembler::CheckCharacterImpl(Imm32 c, Label* on_cond, + Assembler::Condition cond) { + masm_.branch32(cond, current_character_, c, LabelOrBacktrack(on_cond)); +} + +void SMRegExpMacroAssembler::CheckCharacter(uint32_t c, Label* on_equal) { + CheckCharacterImpl(Imm32(c), on_equal, Assembler::Equal); +} + +void SMRegExpMacroAssembler::CheckNotCharacter(uint32_t c, + Label* on_not_equal) { + CheckCharacterImpl(Imm32(c), on_not_equal, Assembler::NotEqual); +} + +void SMRegExpMacroAssembler::CheckCharacterGT(uc16 c, Label* on_greater) { + CheckCharacterImpl(Imm32(c), on_greater, Assembler::GreaterThan); +} + +void SMRegExpMacroAssembler::CheckCharacterLT(uc16 c, Label* on_less) { + CheckCharacterImpl(Imm32(c), on_less, Assembler::LessThan); +} + +// Bitwise-and the current character with mask and then check for a +// match with c. +void SMRegExpMacroAssembler::CheckCharacterAfterAndImpl(uint32_t c, + uint32_t mask, + Label* on_cond, + bool is_not) { + if (c == 0) { + Assembler::Condition cond = is_not ? Assembler::NonZero : Assembler::Zero; + masm_.branchTest32(cond, current_character_, Imm32(mask), + LabelOrBacktrack(on_cond)); + } else { + Assembler::Condition cond = is_not ? Assembler::NotEqual : Assembler::Equal; + masm_.move32(Imm32(mask), temp0_); + masm_.and32(current_character_, temp0_); + masm_.branch32(cond, temp0_, Imm32(c), LabelOrBacktrack(on_cond)); + } +} + +void SMRegExpMacroAssembler::CheckCharacterAfterAnd(uint32_t c, + uint32_t mask, + Label* on_equal) { + CheckCharacterAfterAndImpl(c, mask, on_equal, /*is_not =*/false); +} + +void SMRegExpMacroAssembler::CheckNotCharacterAfterAnd(uint32_t c, + uint32_t mask, + Label* on_not_equal) { + CheckCharacterAfterAndImpl(c, mask, on_not_equal, /*is_not =*/true); +} + + +// Subtract minus from the current character, then bitwise-and the +// result with mask, then check for a match with c. +void SMRegExpMacroAssembler::CheckNotCharacterAfterMinusAnd( + uc16 c, uc16 minus, uc16 mask, Label* on_not_equal) { + masm_.computeEffectiveAddress(Address(current_character_, -minus), temp0_); + if (c == 0) { + masm_.branchTest32(Assembler::NonZero, temp0_, Imm32(mask), + LabelOrBacktrack(on_not_equal)); + } else { + masm_.and32(Imm32(mask), temp0_); + masm_.branch32(Assembler::NotEqual, temp0_, Imm32(c), + LabelOrBacktrack(on_not_equal)); + } +} + +// If the current position matches the position stored on top of the backtrack +// stack, pops the backtrack stack and branches to the given label. +void SMRegExpMacroAssembler::CheckGreedyLoop(Label* on_equal) { + js::jit::Label fallthrough; + masm_.branchPtr(Assembler::NotEqual, Address(backtrack_stack_pointer_, 0), + current_position_, &fallthrough); + masm_.addPtr(Imm32(sizeof(void*)), backtrack_stack_pointer_); // Pop. + JumpOrBacktrack(on_equal); + masm_.bind(&fallthrough); +} + +void SMRegExpMacroAssembler::CheckCharacterInRangeImpl( + uc16 from, uc16 to, Label* on_cond, Assembler::Condition cond) { + // x is in [from,to] if unsigned(x - from) <= to - from + masm_.computeEffectiveAddress(Address(current_character_, -from), temp0_); + masm_.branch32(cond, temp0_, Imm32(to - from), LabelOrBacktrack(on_cond)); +} + +void SMRegExpMacroAssembler::CheckCharacterInRange(uc16 from, uc16 to, + Label* on_in_range) { + CheckCharacterInRangeImpl(from, to, on_in_range, Assembler::BelowOrEqual); +} + +void SMRegExpMacroAssembler::CheckCharacterNotInRange(uc16 from, uc16 to, + Label* on_not_in_range) { + CheckCharacterInRangeImpl(from, to, on_not_in_range, Assembler::Above); +} + +void SMRegExpMacroAssembler::CheckBitInTable(Handle table, + Label* on_bit_set) { + // Claim ownership of the ByteArray from the current HandleScope. + // ByteArrays are allocated on the C++ heap and are (eventually) + // owned by the RegExpShared. + PseudoHandle rawTable = table->takeOwnership(isolate()); + + masm_.movePtr(ImmPtr(rawTable->data()), temp0_); + + masm_.move32(Imm32(kTableMask), temp1_); + masm_.and32(current_character_, temp1_); + + masm_.load8ZeroExtend(BaseIndex(temp0_, temp1_, js::jit::TimesOne), temp0_); + masm_.branchTest32(Assembler::NonZero, temp0_, temp0_, + LabelOrBacktrack(on_bit_set)); + + // Transfer ownership of |rawTable| to the |tables_| vector. + AddTable(std::move(rawTable)); +} + +void SMRegExpMacroAssembler::CheckNotBackReferenceImpl(int start_reg, + bool read_backward, + Label* on_no_match, + bool ignore_case) { + js::jit::Label fallthrough; + + // Captures are stored as a sequential pair of registers. + // Find the length of the back-referenced capture and load the + // capture's start index into current_character_. + masm_.loadPtr(register_location(start_reg), // index of start + current_character_); + masm_.loadPtr(register_location(start_reg + 1), temp0_); // index of end + masm_.subPtr(current_character_, temp0_); // length of capture + + // Capture registers are either both set or both cleared. + // If the capture length is zero, then the capture is either empty or cleared. + // Fall through in both cases. + masm_.branchPtr(Assembler::Equal, temp0_, ImmWord(0), &fallthrough); + + // Check that there are sufficient characters left in the input. + if (read_backward) { + // If start + len > current, there isn't enough room for a + // lookbehind backreference. + masm_.loadPtr(inputStart(), temp1_); + masm_.addPtr(temp0_, temp1_); + masm_.branchPtr(Assembler::GreaterThan, temp1_, current_position_, + LabelOrBacktrack(on_no_match)); + } else { + // current_position_ is the negative offset from the end. + // If current + len > 0, there isn't enough room for a backreference. + masm_.movePtr(current_position_, temp1_); + masm_.addPtr(temp0_, temp1_); + masm_.branchPtr(Assembler::GreaterThan, temp1_, ImmWord(0), + LabelOrBacktrack(on_no_match)); + } + + if (mode_ == UC16 && ignore_case) { + // We call a helper function for case-insensitive non-latin1 strings. + + // Save volatile regs. temp1_ and temp2_ don't need to be saved. + LiveGeneralRegisterSet volatileRegs(GeneralRegisterSet::Volatile()); + volatileRegs.takeUnchecked(temp1_); + volatileRegs.takeUnchecked(temp2_); + masm_.PushRegsInMask(volatileRegs); + + // Parameters are + // Address captured - Address of captured substring's start. + // Address current - Address of current character position. + // size_t byte_length - length of capture (in bytes) + + // Compute |captured| + masm_.addPtr(input_end_pointer_, current_character_); + + // Compute |current| + masm_.addPtr(input_end_pointer_, current_position_); + if (read_backward) { + // Offset by length when matching backwards. + masm_.subPtr(temp0_, current_position_); + } + + masm_.setupUnalignedABICall(temp1_); + masm_.passABIArg(current_character_); + masm_.passABIArg(current_position_); + masm_.passABIArg(temp0_); + + bool unicode = true; // TODO: Fix V8 bug + if (unicode) { + uint32_t (*fun)(const char16_t*, const char16_t*, size_t) = + CaseInsensitiveCompareUCStrings; + masm_.callWithABI(JS_FUNC_TO_DATA_PTR(void*, fun)); + } else { + uint32_t (*fun)(const char16_t*, const char16_t*, size_t) = + CaseInsensitiveCompareStrings; + masm_.callWithABI(JS_FUNC_TO_DATA_PTR(void*, fun)); + } + masm_.storeCallInt32Result(temp1_); + masm_.PopRegsInMask(volatileRegs); + masm_.branchTest32(Assembler::Zero, temp1_, temp1_, + LabelOrBacktrack(on_no_match)); + + // On success, advance position by length of capture + if (read_backward) { + masm_.subPtr(temp0_, current_position_); + } else { + masm_.addPtr(temp0_, current_position_); + } + + masm_.bind(&fallthrough); + return; + } + + // We will be modifying current_position_. Save it in case the match fails. + masm_.push(current_position_); + + // Compute start of capture string + masm_.addPtr(input_end_pointer_, current_character_); + + // Compute start of match string + masm_.addPtr(input_end_pointer_, current_position_); + if (read_backward) { + // Offset by length when matching backwards. + masm_.subPtr(temp0_, current_position_); + } + + // Compute end of match string + masm_.addPtr(current_position_, temp0_); + + js::jit::Label success; + js::jit::Label fail; + js::jit::Label loop; + masm_.bind(&loop); + + // Load next character from each string. + if (mode_ == LATIN1) { + masm_.load8ZeroExtend(Address(current_character_, 0), temp1_); + masm_.load8ZeroExtend(Address(current_position_, 0), temp2_); + } else { + masm_.load16ZeroExtend(Address(current_character_, 0), temp1_); + masm_.load16ZeroExtend(Address(current_position_, 0), temp2_); + } + + if (ignore_case) { + MOZ_ASSERT(mode_ == LATIN1); + // Try exact match. + js::jit::Label loop_increment; + masm_.branch32(Assembler::Equal, temp1_, temp2_, &loop_increment); + + // Mismatch. Try case-insensitive match. + // Force the match character to lower case (by setting bit 0x20) + // then check to see if it is a letter. + js::jit::Label convert_capture; + masm_.or32(Imm32(0x20), temp1_); + + // Check if it is in [a,z]. + masm_.computeEffectiveAddress(Address(temp1_, -'a'), temp2_); + masm_.branch32(Assembler::BelowOrEqual, temp2_, Imm32('z' - 'a'), + &convert_capture); + // Check for values in range [224,254]. + // Exclude 247 (U+00F7 DIVISION SIGN). + masm_.sub32(Imm32(224 - 'a'), temp2_); + masm_.branch32(Assembler::Above, temp2_, Imm32(254 - 224), &fail); + masm_.branch32(Assembler::Equal, temp2_, Imm32(247 - 224), &fail); + + // Match character is lower case. Convert capture character + // to lower case and compare. + masm_.bind(&convert_capture); + masm_.load8ZeroExtend(Address(current_character_, 0), temp2_); + masm_.or32(Imm32(0x20), temp2_); + masm_.branch32(Assembler::NotEqual, temp1_, temp2_, &fail); + + masm_.bind(&loop_increment); + } else { + // Fail if characters do not match. + masm_.branch32(Assembler::NotEqual, temp1_, temp2_, &fail); + } + + // Increment pointers into match and capture strings. + masm_.addPtr(Imm32(char_size()), current_character_); + masm_.addPtr(Imm32(char_size()), current_position_); + + // Loop if we have not reached the end of the match string. + masm_.branchPtr(Assembler::Below, current_position_, temp0_, &loop); + masm_.jump(&success); + + // If we fail, restore current_position_ and branch. + masm_.bind(&fail); + masm_.pop(current_position_); + JumpOrBacktrack(on_no_match); + + masm_.bind(&success); + + // current_position_ is a pointer. Convert it back to an offset. + masm_.subPtr(input_end_pointer_, current_position_); + if (read_backward) { + // Subtract match length if we matched backward + masm_.addPtr(register_location(start_reg), current_position_); + masm_.subPtr(register_location(start_reg + 1), current_position_); + } + + // Drop saved value of current_position_ + masm_.addToStackPtr(Imm32(sizeof(uintptr_t))); + + masm_.bind(&fallthrough); +} + +// Branch if a back-reference does not match a previous capture. +void SMRegExpMacroAssembler::CheckNotBackReference(int start_reg, + bool read_backward, + Label* on_no_match) { + CheckNotBackReferenceImpl(start_reg, read_backward, on_no_match, + /*ignore_case = */ false); +} + +void SMRegExpMacroAssembler::CheckNotBackReferenceIgnoreCase( + int start_reg, bool read_backward, Label* on_no_match) { + CheckNotBackReferenceImpl(start_reg, read_backward, on_no_match, + /*ignore_case = */ true); +} + +// Checks whether the given offset from the current position is +// inside the input string. +void SMRegExpMacroAssembler::CheckPosition(int cp_offset, + Label* on_outside_input) { + // Note: current_position_ is a (negative) byte offset relative to + // the end of the input string. + if (cp_offset >= 0) { + // end + current + offset >= end + // <=> current + offset >= 0 + // <=> current >= -offset + masm_.branchPtr(Assembler::GreaterThanOrEqual, current_position_, + ImmWord(-cp_offset * char_size()), + LabelOrBacktrack(on_outside_input)); + } else { + // Compute offset position + masm_.computeEffectiveAddress( + Address(current_position_, cp_offset * char_size()), temp0_); + + // Compare to start of input. + masm_.branchPtr(Assembler::GreaterThanOrEqual, inputStart(), temp0_, + LabelOrBacktrack(on_outside_input)); + } +} + +// This function attempts to generate special case code for character classes. +// Returns true if a special case is generated. +// Otherwise returns false and generates no code. +bool SMRegExpMacroAssembler::CheckSpecialCharacterClass(uc16 type, + Label* on_no_match) { + js::jit::Label* no_match = LabelOrBacktrack(on_no_match); + + // Note: throughout this function, range checks (c in [min, max]) + // are implemented by an unsigned (c - min) <= (max - min) check. + switch (type) { + case 's': { + // Match space-characters + if (mode_ != LATIN1) { + return false; + } + js::jit::Label success; + // One byte space characters are ' ', '\t'..'\r', and '\u00a0' (NBSP). + + // Check ' ' + masm_.branch32(Assembler::Equal, current_character_, Imm32(' '), + &success); + + // Check '\t'..'\r' + masm_.computeEffectiveAddress(Address(current_character_, -'\t'), + temp0_); + masm_.branch32(Assembler::BelowOrEqual, temp0_, Imm32('\r' - '\t'), + &success); + + // Check \u00a0. + masm_.branch32(Assembler::NotEqual, temp0_, Imm32(0x00a0 - '\t'), + no_match); + + masm_.bind(&success); + return true; + } + case 'S': + // The emitted code for generic character classes is good enough. + return false; + case 'd': + // Match latin1 digits ('0'-'9') + masm_.computeEffectiveAddress(Address(current_character_, -'0'), temp0_); + masm_.branch32(Assembler::Above, temp0_, Imm32('9' - '0'), no_match); + return true; + case 'D': + // Match anything except latin1 digits ('0'-'9') + masm_.computeEffectiveAddress(Address(current_character_, -'0'), temp0_); + masm_.branch32(Assembler::BelowOrEqual, temp0_, Imm32('9' - '0'), + no_match); + return true; + case '.': + // Match non-newlines. This excludes '\n' (0x0a), '\r' (0x0d), + // U+2028 LINE SEPARATOR, and U+2029 PARAGRAPH SEPARATOR. + // See https://tc39.es/ecma262/#prod-LineTerminator + + // To test for 0x0a and 0x0d efficiently, we XOR the input with 1. + // This converts 0x0a to 0x0b, and 0x0d to 0x0c, allowing us to + // test for the contiguous range 0x0b..0x0c. + masm_.move32(current_character_, temp0_); + masm_.xor32(Imm32(0x01), temp0_); + masm_.sub32(Imm32(0x0b), temp0_); + masm_.branch32(Assembler::BelowOrEqual, temp0_, Imm32(0x0c - 0x0b), + no_match); + + if (mode_ == UC16) { + // Compare original value to 0x2028 and 0x2029, using the already + // computed (current_char ^ 0x01 - 0x0b). I.e., check for + // 0x201d (0x2028 - 0x0b) or 0x201e. + masm_.sub32(Imm32(0x2028 - 0x0b), temp0_); + masm_.branch32(Assembler::BelowOrEqual, temp0_, Imm32(0x2029 - 0x2028), + no_match); + } + return true; + case 'w': + // \w matches the set of 63 characters defined in Runtime Semantics: + // WordCharacters. We use a static lookup table, which is defined in + // regexp-macro-assembler.cc. + // Note: if both Unicode and IgnoreCase are true, \w matches a + // larger set of characters. That case is handled elsewhere. + if (mode_ != LATIN1) { + masm_.branch32(Assembler::Above, current_character_, Imm32('z'), + no_match); + } + static_assert(arraysize(word_character_map) > unibrow::Latin1::kMaxChar); + masm_.movePtr(ImmPtr(word_character_map), temp0_); + masm_.load8ZeroExtend( + BaseIndex(temp0_, current_character_, js::jit::TimesOne), temp0_); + masm_.branchTest32(Assembler::Zero, temp0_, temp0_, no_match); + return true; + case 'W': { + // See 'w' above. + js::jit::Label done; + if (mode_ != LATIN1) { + masm_.branch32(Assembler::Above, current_character_, Imm32('z'), &done); + } + static_assert(arraysize(word_character_map) > unibrow::Latin1::kMaxChar); + masm_.movePtr(ImmPtr(word_character_map), temp0_); + masm_.load8ZeroExtend( + BaseIndex(temp0_, current_character_, js::jit::TimesOne), temp0_); + masm_.branchTest32(Assembler::NonZero, temp0_, temp0_, no_match); + if (mode_ != LATIN1) { + masm_.bind(&done); + } + return true; + } + //////////////////////////////////////////////////////////////////////// + // Non-standard classes (with no syntactic shorthand) used internally // + //////////////////////////////////////////////////////////////////////// + case '*': + // Match any character + return true; + case 'n': + // Match newlines. The opposite of '.'. See '.' above. + masm_.move32(current_character_, temp0_); + masm_.xor32(Imm32(0x01), temp0_); + masm_.sub32(Imm32(0x0b), temp0_); + if (mode_ == LATIN1) { + masm_.branch32(Assembler::Above, temp0_, Imm32(0x0c - 0x0b), no_match); + } else { + MOZ_ASSERT(mode_ == UC16); + js::jit::Label done; + masm_.branch32(Assembler::BelowOrEqual, temp0_, Imm32(0x0c - 0x0b), + &done); + + // Compare original value to 0x2028 and 0x2029, using the already + // computed (current_char ^ 0x01 - 0x0b). I.e., check for + // 0x201d (0x2028 - 0x0b) or 0x201e. + masm_.sub32(Imm32(0x2028 - 0x0b), temp0_); + masm_.branch32(Assembler::Above, temp0_, Imm32(0x2029 - 0x2028), + no_match); + masm_.bind(&done); + } + return true; + + // No custom implementation + default: + return false; + } +} + +void SMRegExpMacroAssembler::Fail() { + masm_.movePtr(ImmWord(js::RegExpRunStatus_Success_NotFound), temp0_); + masm_.jump(&exit_label_); +} + +void SMRegExpMacroAssembler::GoTo(Label* to) { + masm_.jump(LabelOrBacktrack(to)); +} + +void SMRegExpMacroAssembler::IfRegisterGE(int reg, int comparand, + Label* if_ge) { + masm_.branchPtr(Assembler::GreaterThanOrEqual, register_location(reg), + ImmWord(comparand), LabelOrBacktrack(if_ge)); +} + +void SMRegExpMacroAssembler::IfRegisterLT(int reg, int comparand, + Label* if_lt) { + masm_.branchPtr(Assembler::LessThan, register_location(reg), + ImmWord(comparand), LabelOrBacktrack(if_lt)); +} + +void SMRegExpMacroAssembler::IfRegisterEqPos(int reg, Label* if_eq) { + masm_.branchPtr(Assembler::Equal, register_location(reg), current_position_, + LabelOrBacktrack(if_eq)); +} + +// This is a word-for-word identical copy of the V8 code, which is +// duplicated in at least nine different places in V8 (one per +// supported architecture) with no differences outside of comments and +// formatting. It should be hoisted into the superclass. Once that is +// done upstream, this version can be deleted. +void SMRegExpMacroAssembler::LoadCurrentCharacterImpl(int cp_offset, + Label* on_end_of_input, + bool check_bounds, + int characters, + int eats_at_least) { + // It's possible to preload a small number of characters when each success + // path requires a large number of characters, but not the reverse. + MOZ_ASSERT(eats_at_least >= characters); + MOZ_ASSERT(cp_offset < (1 << 30)); // Be sane! (And ensure negation works) + + if (check_bounds) { + if (cp_offset >= 0) { + CheckPosition(cp_offset + eats_at_least - 1, on_end_of_input); + } else { + CheckPosition(cp_offset, on_end_of_input); + } + } + LoadCurrentCharacterUnchecked(cp_offset, characters); +} + +// Load the character (or characters) at the specified offset from the +// current position. Zero-extend to 32 bits. +void SMRegExpMacroAssembler::LoadCurrentCharacterUnchecked(int cp_offset, + int characters) { + BaseIndex address(input_end_pointer_, current_position_, js::jit::TimesOne, + cp_offset * char_size()); + if (mode_ == LATIN1) { + if (characters == 4) { + masm_.load32(address, current_character_); + } else if (characters == 2) { + masm_.load16ZeroExtend(address, current_character_); + } else { + MOZ_ASSERT(characters == 1); + masm_.load8ZeroExtend(address, current_character_); + } + } else { + MOZ_ASSERT(mode_ == UC16); + if (characters == 2) { + masm_.load32(address, current_character_); + } else { + MOZ_ASSERT(characters == 1); + masm_.load16ZeroExtend(address, current_character_); + } + } +} + +void SMRegExpMacroAssembler::PopCurrentPosition() { Pop(current_position_); } + +void SMRegExpMacroAssembler::PopRegister(int register_index) { + Pop(temp0_); + masm_.storePtr(temp0_, register_location(register_index)); +} + +void SMRegExpMacroAssembler::PushBacktrack(Label* label) { + MOZ_ASSERT(!label->is_bound()); + MOZ_ASSERT(!label->patchOffset_.bound()); + label->patchOffset_ = masm_.movWithPatch(ImmPtr(nullptr), temp0_); + MOZ_ASSERT(label->patchOffset_.bound()); + + Push(temp0_); + + CheckBacktrackStackLimit(); +} + +void SMRegExpMacroAssembler::PushCurrentPosition() { Push(current_position_); } + +void SMRegExpMacroAssembler::PushRegister(int register_index, + StackCheckFlag check_stack_limit) { + masm_.loadPtr(register_location(register_index), temp0_); + Push(temp0_); + if (check_stack_limit) { + CheckBacktrackStackLimit(); + } +} + +void SMRegExpMacroAssembler::ReadCurrentPositionFromRegister(int reg) { + masm_.loadPtr(register_location(reg), current_position_); +} + +void SMRegExpMacroAssembler::WriteCurrentPositionToRegister(int reg, + int cp_offset) { + if (cp_offset == 0) { + masm_.storePtr(current_position_, register_location(reg)); + } else { + Address addr(current_position_, cp_offset * char_size()); + masm_.computeEffectiveAddress(addr, temp0_); + masm_.storePtr(temp0_, register_location(reg)); + } +} + +// Note: The backtrack stack pointer is stored in a register as an +// offset from the stack top, not as a bare pointer, so that it is not +// corrupted if the backtrack stack grows (and therefore moves). +void SMRegExpMacroAssembler::ReadStackPointerFromRegister(int reg) { + masm_.loadPtr(register_location(reg), backtrack_stack_pointer_); + masm_.addPtr(backtrackStackBase(), backtrack_stack_pointer_); +} +void SMRegExpMacroAssembler::WriteStackPointerToRegister(int reg) { + masm_.movePtr(backtrack_stack_pointer_, temp0_); + masm_.subPtr(backtrackStackBase(), temp0_); + masm_.storePtr(temp0_, register_location(reg)); +} + +// When matching a regexp that is anchored at the end, this operation +// is used to try skipping the beginning of long strings. If the +// maximum length of a match is less than the length of the string, we +// can skip the initial len - max_len bytes. +void SMRegExpMacroAssembler::SetCurrentPositionFromEnd(int by) { + js::jit::Label after_position; + masm_.branchPtr(Assembler::GreaterThanOrEqual, current_position_, + ImmWord(-by * char_size()), &after_position); + masm_.movePtr(ImmWord(-by * char_size()), current_position_); + + // On RegExp code entry (where this operation is used), the character before + // the current position is expected to be already loaded. + // We have advanced the position, so it's safe to read backwards. + LoadCurrentCharacterUnchecked(-1, 1); + masm_.bind(&after_position); +} + +void SMRegExpMacroAssembler::SetRegister(int register_index, int to) { + MOZ_ASSERT(register_index >= num_capture_registers_); + masm_.storePtr(ImmWord(to), register_location(register_index)); +} + +// Returns true if a regexp match can be restarted (aka the regexp is global). +// The return value is not used anywhere, but we implement it to be safe. +bool SMRegExpMacroAssembler::Succeed() { + masm_.jump(&success_label_); + return global(); +} + +// Capture registers are initialized to input[-1] +void SMRegExpMacroAssembler::ClearRegisters(int reg_from, int reg_to) { + MOZ_ASSERT(reg_from <= reg_to); + masm_.loadPtr(inputStart(), temp0_); + masm_.subPtr(Imm32(char_size()), temp0_); + for (int reg = reg_from; reg <= reg_to; reg++) { + masm_.storePtr(temp0_, register_location(reg)); + } +} + +void SMRegExpMacroAssembler::Push(Register source) { + MOZ_ASSERT(source != backtrack_stack_pointer_); + + masm_.subPtr(Imm32(sizeof(void*)), backtrack_stack_pointer_); + masm_.storePtr(source, Address(backtrack_stack_pointer_, 0)); +} + +void SMRegExpMacroAssembler::Pop(Register target) { + MOZ_ASSERT(target != backtrack_stack_pointer_); + + masm_.loadPtr(Address(backtrack_stack_pointer_, 0), target); + masm_.addPtr(Imm32(sizeof(void*)), backtrack_stack_pointer_); +} + +void SMRegExpMacroAssembler::JumpOrBacktrack(Label* to) { + if (to) { + masm_.jump(to->inner()); + } else { + Backtrack(); + } +} + +// Generate a quick inline test for backtrack stack overflow. +// If the test fails, call an OOL handler to try growing the stack. +void SMRegExpMacroAssembler::CheckBacktrackStackLimit() { + js::jit::Label no_stack_overflow; + masm_.branchPtr( + Assembler::BelowOrEqual, + AbsoluteAddress(isolate()->regexp_stack()->limit_address_address()), + backtrack_stack_pointer_, &no_stack_overflow); + + masm_.call(&stack_overflow_label_); + + // Exit with an exception if the call failed + masm_.branchTest32(Assembler::Zero, temp0_, temp0_, + &exit_with_exception_label_); + + masm_.bind(&no_stack_overflow); +} + +// This is used to sneak an OOM through the V8 layer. +static Handle DummyCode() { + return Handle::fromHandleValue(JS::UndefinedHandleValue); +} + +// Finalize code. This is called last, so that we know how many +// registers we need. +Handle SMRegExpMacroAssembler::GetCode(Handle source) { + if (!cx_->realm()->ensureJitRealmExists(cx_)) { + return DummyCode(); + } + + masm_.bind(&entry_label_); + + createStackFrame(); + initFrameAndRegs(); + + masm_.jump(&start_label_); + + successHandler(); + exitHandler(); + backtrackHandler(); + stackOverflowHandler(); + + Linker linker(masm_); + JitCode* code = linker.newCode(cx_, js::jit::CodeKind::RegExp); + if (!code) { + return DummyCode(); + } + + for (LabelPatch& lp : labelPatches_) { + Assembler::PatchDataWithValueCheck(CodeLocationLabel(code, lp.patchOffset_), + ImmPtr(code->raw() + lp.labelOffset_), + ImmPtr(nullptr)); + } + + return Handle(JS::PrivateGCThingValue(code), isolate()); +} + +/* + * The stack will have the following structure: + * sp-> - FrameData + * - inputStart + * - backtrack stack base + * - matches + * - numMatches + * - Registers + * - Capture positions + * - Scratch registers + * --- frame alignment --- + * - Saved register area + * - Return address + */ +void SMRegExpMacroAssembler::createStackFrame() { +#ifdef JS_CODEGEN_ARM64 + // ARM64 communicates stack address via SP, but uses a pseudo-sp (PSP) for + // addressing. The register we use for PSP may however also be used by + // calling code, and it is nonvolatile, so save it. Do this as a special + // case first because the generic save/restore code needs the PSP to be + // initialized already. + MOZ_ASSERT(js::jit::PseudoStackPointer64.Is(masm_.GetStackPointer64())); + masm_.Str(js::jit::PseudoStackPointer64, + vixl::MemOperand(js::jit::sp, -16, vixl::PreIndex)); + + // Initialize the PSP from the SP. + masm_.initPseudoStackPtr(); +#endif + + // Push non-volatile registers which might be modified by jitcode. + size_t pushedNonVolatileRegisters = 0; + for (GeneralRegisterForwardIterator iter(savedRegisters_); iter.more(); + ++iter) { + masm_.Push(*iter); + pushedNonVolatileRegisters++; + } + + // The pointer to InputOutputData is passed as the first argument. + // On x86 we have to load it off the stack into temp0_. + // On other platforms it is already in a register. +#ifdef JS_CODEGEN_X86 + Address ioDataAddr(masm_.getStackPointer(), + (pushedNonVolatileRegisters + 1) * sizeof(void*)); + masm_.loadPtr(ioDataAddr, temp0_); +#else + if (js::jit::IntArgReg0 != temp0_) { + masm_.movePtr(js::jit::IntArgReg0, temp0_); + } +#endif + + // Start a new stack frame. + size_t frameBytes = sizeof(FrameData) + num_registers_ * sizeof(void*); + frameSize_ = js::jit::StackDecrementForCall(js::jit::ABIStackAlignment, + masm_.framePushed(), frameBytes); + masm_.reserveStack(frameSize_); + masm_.checkStackAlignment(); + + // Check if we have space on the stack. Use the *NoInterrupt stack limit to + // avoid failing repeatedly when the regex code is called from Ion JIT code. + // (See bug 1208819) + js::jit::Label stack_ok; + AbsoluteAddress limit_addr(cx_->addressOfJitStackLimitNoInterrupt()); + masm_.branchStackPtrRhs(Assembler::Below, limit_addr, &stack_ok); + + // There is not enough space on the stack. Exit with an exception. + masm_.movePtr(ImmWord(js::RegExpRunStatus_Error), temp0_); + masm_.jump(&exit_label_); + + masm_.bind(&stack_ok); +} + +void SMRegExpMacroAssembler::initFrameAndRegs() { + // At this point, an uninitialized stack frame has been created, + // and the address of the InputOutputData is in temp0_. + Register ioDataReg = temp0_; + + Register matchesReg = temp1_; + masm_.loadPtr(Address(ioDataReg, offsetof(InputOutputData, matches)), + matchesReg); + + // Initialize output registers + masm_.loadPtr(Address(matchesReg, MatchPairs::offsetOfPairs()), temp2_); + masm_.storePtr(temp2_, matches()); + masm_.load32(Address(matchesReg, MatchPairs::offsetOfPairCount()), temp2_); + masm_.store32(temp2_, numMatches()); + +#ifdef DEBUG + // Bounds-check numMatches. + js::jit::Label enoughRegisters; + masm_.branchPtr(Assembler::GreaterThanOrEqual, temp2_, + ImmWord(num_capture_registers_ / 2), &enoughRegisters); + masm_.assumeUnreachable("Not enough output pairs for RegExp"); + masm_.bind(&enoughRegisters); +#endif + + // Load input start pointer. + masm_.loadPtr(Address(ioDataReg, offsetof(InputOutputData, inputStart)), + current_position_); + + // Load input end pointer + masm_.loadPtr(Address(ioDataReg, offsetof(InputOutputData, inputEnd)), + input_end_pointer_); + + // Set up input position to be negative offset from string end. + masm_.subPtr(input_end_pointer_, current_position_); + + // Store inputStart + masm_.storePtr(current_position_, inputStart()); + + // Load start index + Register startIndexReg = temp1_; + masm_.loadPtr(Address(ioDataReg, offsetof(InputOutputData, startIndex)), + startIndexReg); + masm_.computeEffectiveAddress( + BaseIndex(current_position_, startIndexReg, factor()), current_position_); + + // Initialize current_character_. + // Load newline if index is at start, or previous character otherwise. + js::jit::Label start_regexp; + js::jit::Label load_previous_character; + masm_.branchPtr(Assembler::NotEqual, startIndexReg, ImmWord(0), + &load_previous_character); + masm_.movePtr(ImmWord('\n'), current_character_); + masm_.jump(&start_regexp); + + masm_.bind(&load_previous_character); + LoadCurrentCharacterUnchecked(-1, 1); + masm_.bind(&start_regexp); + + // Initialize captured registers with inputStart - 1 + MOZ_ASSERT(num_capture_registers_ > 0); + Register inputStartMinusOneReg = temp2_; + masm_.loadPtr(inputStart(), inputStartMinusOneReg); + masm_.subPtr(Imm32(char_size()), inputStartMinusOneReg); + if (num_capture_registers_ > 8) { + masm_.movePtr(ImmWord(register_offset(0)), temp1_); + js::jit::Label init_loop; + masm_.bind(&init_loop); + masm_.storePtr(inputStartMinusOneReg, BaseIndex(masm_.getStackPointer(), + temp1_, js::jit::TimesOne)); + masm_.addPtr(ImmWord(sizeof(void*)), temp1_); + masm_.branchPtr(Assembler::LessThan, temp1_, + ImmWord(register_offset(num_capture_registers_)), + &init_loop); + } else { + // Unroll the loop + for (int i = 0; i < num_capture_registers_; i++) { + masm_.storePtr(inputStartMinusOneReg, register_location(i)); + } + } + + // Initialize backtrack stack pointer + masm_.loadPtr(AbsoluteAddress(isolate()->top_of_regexp_stack()), + backtrack_stack_pointer_); + masm_.storePtr(backtrack_stack_pointer_, backtrackStackBase()); +} + +void SMRegExpMacroAssembler::successHandler() { + MOZ_ASSERT(success_label_.used()); + masm_.bind(&success_label_); + + // Copy captures to the MatchPairs pointed to by the InputOutputData. + // Captures are stored as positions, which are negative byte offsets + // from the end of the string. We must convert them to actual + // indices. + // + // Index: [ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ][END] + // Pos (1-byte): [-6 ][-5 ][-4 ][-3 ][-2 ][-1 ][ 0 ] // IS = -6 + // Pos (2-byte): [-12][-10][-8 ][-6 ][-4 ][-2 ][ 0 ] // IS = -12 + // + // To convert a position to an index, we subtract InputStart, and + // divide the result by char_size. + Register matchesReg = temp1_; + masm_.loadPtr(matches(), matchesReg); + + Register inputStartReg = temp2_; + masm_.loadPtr(inputStart(), inputStartReg); + + for (int i = 0; i < num_capture_registers_; i++) { + masm_.loadPtr(register_location(i), temp0_); + masm_.subPtr(inputStartReg, temp0_); + if (mode_ == UC16) { + masm_.rshiftPtrArithmetic(Imm32(1), temp0_); + } + masm_.store32(temp0_, Address(matchesReg, i * sizeof(int32_t))); + } + + masm_.movePtr(ImmWord(js::RegExpRunStatus_Success), temp0_); + // This falls through to the exit handler. +} + +void SMRegExpMacroAssembler::exitHandler() { + masm_.bind(&exit_label_); + + if (temp0_ != js::jit::ReturnReg) { + masm_.movePtr(temp0_, js::jit::ReturnReg); + } + + masm_.freeStack(frameSize_); + + // Restore registers which were saved on entry + for (GeneralRegisterBackwardIterator iter(savedRegisters_); iter.more(); + ++iter) { + masm_.Pop(*iter); + } + +#ifdef JS_CODEGEN_ARM64 + // Now restore the value that was in the PSP register on entry, and return. + + // Obtain the correct SP from the PSP. + masm_.Mov(js::jit::sp, js::jit::PseudoStackPointer64); + + // Restore the saved value of the PSP register, this value is whatever the + // caller had saved in it, not any actual SP value, and it must not be + // overwritten subsequently. + masm_.Ldr(js::jit::PseudoStackPointer64, + vixl::MemOperand(js::jit::sp, 16, vixl::PostIndex)); + + // Perform a plain Ret(), as abiret() will move SP <- PSP and that is wrong. + masm_.Ret(vixl::lr); +#else + masm_.abiret(); +#endif + + if (exit_with_exception_label_.used()) { + masm_.bind(&exit_with_exception_label_); + + // Exit with an error result to signal thrown exception + masm_.movePtr(ImmWord(js::RegExpRunStatus_Error), temp0_); + masm_.jump(&exit_label_); + } +} + +void SMRegExpMacroAssembler::backtrackHandler() { + if (!backtrack_label_.used()) { + return; + } + masm_.bind(&backtrack_label_); + Backtrack(); +} + +void SMRegExpMacroAssembler::stackOverflowHandler() { + if (!stack_overflow_label_.used()) { + return; + } + + // Called if the backtrack-stack limit has been hit. + // NOTE: depending on architecture, the call may have + // changed the stack pointer. We adjust for that below. + masm_.bind(&stack_overflow_label_); + + // Load argument + masm_.movePtr(ImmPtr(isolate()->regexp_stack()), temp1_); + + // Save registers before calling C function + LiveGeneralRegisterSet volatileRegs(GeneralRegisterSet::Volatile()); + +#ifdef JS_USE_LINK_REGISTER + masm.pushReturnAddress(); +#endif + + // Adjust for the return address on the stack. + size_t frameOffset = sizeof(void*); + + volatileRegs.takeUnchecked(temp0_); + volatileRegs.takeUnchecked(temp1_); + masm_.PushRegsInMask(volatileRegs); + + masm_.setupUnalignedABICall(temp0_); + masm_.passABIArg(temp1_); + masm_.callWithABI(JS_FUNC_TO_DATA_PTR(void*, GrowBacktrackStack)); + masm_.storeCallBoolResult(temp0_); + + masm_.PopRegsInMask(volatileRegs); + + // If GrowBacktrackStack returned false, we have failed to grow the + // stack, and must exit with a stack-overflow exception. Do this in + // the caller so that the stack is adjusted by our return instruction. + js::jit::Label overflow_return; + masm_.branchTest32(Assembler::Zero, temp0_, temp0_, &overflow_return); + + // Otherwise, store the new backtrack stack base and recompute the new + // top of the stack. + Address bsbAddress(masm_.getStackPointer(), + offsetof(FrameData, backtrackStackBase) + frameOffset); + masm_.subPtr(bsbAddress, backtrack_stack_pointer_); + + masm_.loadPtr(AbsoluteAddress(isolate()->top_of_regexp_stack()), temp1_); + masm_.storePtr(temp1_, bsbAddress); + masm_.addPtr(temp1_, backtrack_stack_pointer_); + + // Resume execution in calling code. + masm_.bind(&overflow_return); + masm_.ret(); +} + +// This is only used by tracing code. +// The return value doesn't matter. +RegExpMacroAssembler::IrregexpImplementation +SMRegExpMacroAssembler::Implementation() { + return kBytecodeImplementation; +} + +/*static */ +uint32_t SMRegExpMacroAssembler::CaseInsensitiveCompareStrings( + const char16_t* substring1, const char16_t* substring2, size_t byteLength) { + js::AutoUnsafeCallWithABI unsafe; + + MOZ_ASSERT(byteLength % sizeof(char16_t) == 0); + size_t length = byteLength / sizeof(char16_t); + + for (size_t i = 0; i < length; i++) { + char16_t c1 = substring1[i]; + char16_t c2 = substring2[i]; + if (c1 != c2) { + c1 = js::unicode::ToUpperCase(c1); + c2 = js::unicode::ToUpperCase(c2); + if (c1 != c2) { + return 0; + } + } + } + + return 1; +} + +/*static */ +uint32_t SMRegExpMacroAssembler::CaseInsensitiveCompareUCStrings( + const char16_t* substring1, const char16_t* substring2, size_t byteLength) { + js::AutoUnsafeCallWithABI unsafe; + + MOZ_ASSERT(byteLength % sizeof(char16_t) == 0); + size_t length = byteLength / sizeof(char16_t); + + for (size_t i = 0; i < length; i++) { + char16_t c1 = substring1[i]; + char16_t c2 = substring2[i]; + if (c1 != c2) { + c1 = js::unicode::FoldCase(c1); + c2 = js::unicode::FoldCase(c2); + if (c1 != c2) { + return 0; + } + } + } + + return 1; +} + +/* static */ +bool SMRegExpMacroAssembler::GrowBacktrackStack(RegExpStack* regexp_stack) { + js::AutoUnsafeCallWithABI unsafe; + size_t size = regexp_stack->stack_capacity(); + return !!regexp_stack->EnsureCapacity(size * 2); +} + +} // namespace internal +} // namespace v8 diff --git a/js/src/regexp/regexp-shim.cc b/js/src/regexp/regexp-shim.cc index 773c17eadd..3f3fa40eb0 100644 --- a/js/src/regexp/regexp-shim.cc +++ b/js/src/regexp/regexp-shim.cc @@ -11,6 +11,7 @@ #include #include "regexp/regexp-shim.h" +#include "regexp/regexp-stack.h" namespace v8 { namespace internal { @@ -73,6 +74,8 @@ Handle::Handle(T object, Isolate* isolate) : location_(isolate->getHandleLocation(JS::Value(object))) {} template Handle::Handle(ByteArray b, Isolate* isolate); +template Handle::Handle(JS::Value v, Isolate* isolate); +template Handle::Handle(JSRegExp re, Isolate* isolate); template Handle::Handle(String s, Isolate* isolate); template @@ -150,6 +153,10 @@ std::unique_ptr String::ToCString() { return std::unique_ptr(); } +byte* Isolate::top_of_regexp_stack() const { + return reinterpret_cast(regexpStack_->memory_top_address_address()); +} + Handle Isolate::NewByteArray(int length, AllocationType alloc) { MOZ_RELEASE_ASSERT(length >= 0); diff --git a/js/src/regexp/regexp-shim.h b/js/src/regexp/regexp-shim.h index 462e396f40..942fcf733f 100644 --- a/js/src/regexp/regexp-shim.h +++ b/js/src/regexp/regexp-shim.h @@ -22,7 +22,9 @@ #include #include "jit/Label.h" +#include "jit/shared/Assembler-shared.h" #include "js/Value.h" +#include "regexp/RegExpTypes.h" #include "regexp/util/flags.h" #include "regexp/util/vector.h" #include "regexp/util/zone.h" @@ -571,10 +573,8 @@ class ByteArray : public HeapObject { ByteArrayData* inner() const { return static_cast(value_.toPrivate()); } - PseudoHandle takeOwnership(Isolate* isolate); - - friend class SMRegExpMacroAssembler; public: + PseudoHandle takeOwnership(Isolate* isolate); byte get(uint32_t index) { MOZ_ASSERT(index < length()); return inner()->data()[index]; @@ -674,15 +674,19 @@ class MOZ_NONHEAP_CLASS Handle { }; inline ObjectRef operator->() const { return ObjectRef{**this}; } + static Handle fromHandleValue(JS::HandleValue handle) { + return Handle(handle.address()); + } + private: - Handle(JS::Value* location) : location_(location) {} + Handle(const JS::Value* location) : location_(location) {} template friend class Handle; template friend class MaybeHandle; - JS::Value* location_; + const JS::Value* location_; }; // A Handle can be converted into a MaybeHandle. Converting a MaybeHandle @@ -985,9 +989,13 @@ using Factory = Isolate; class Isolate { public: //********** Isolate code **********// - RegExpStack* regexp_stack() const { return regexp_stack_; } - bool has_pending_exception() { return cx()->isExceptionPending(); } - void StackOverflow() { js::ReportOverRecursed(cx()); } + RegExpStack* regexp_stack() const { return regexpStack_; } + byte* top_of_regexp_stack() const; + + // This is called from inside no-GC code. Instead of suppressing GC + // to allocate the error, we return false from Execute and call + // ReportOverRecursed in the caller. + void StackOverflow() {} #ifndef V8_INTL_SUPPORT unibrow::Mapping* jsregexp_uncanonicalize() { @@ -1066,7 +1074,7 @@ private: friend class HandleScope; JSContext* cx_; - RegExpStack* regexp_stack_; + RegExpStack* regexpStack_; Counters counters_; }; @@ -1101,7 +1109,6 @@ class Code : public HeapObject { c.value_ = JS::PrivateGCThingValue(JS::Value(object).toGCThing()); return c; } -private: js::jit::JitCode* inner() { return value_.toGCThing()->as(); } @@ -1124,7 +1131,7 @@ class Label { public: Label() : inner_(js::jit::Label()) {} - operator js::jit::Label*() { return &inner_; } + js::jit::Label* inner() { return &inner_; } void Unuse() { inner_.reset(); } @@ -1138,6 +1145,9 @@ class Label { private: js::jit::Label inner_; + js::jit::CodeOffset patchOffset_; + + friend class SMRegExpMacroAssembler; }; // TODO: Map flags to jitoptions From c44d7d218912dae58e60c9bfcf944b6716361cd8 Mon Sep 17 00:00:00 2001 From: "Matt A. Tobin" Date: Mon, 9 Nov 2020 20:48:11 -0500 Subject: [PATCH 5/9] Issue #1677 - Part 7: Add unicode/uniset.h to check_spidermonkey_style.py for some reason --- config/check_spidermonkey_style.py | 1 + 1 file changed, 1 insertion(+) diff --git a/config/check_spidermonkey_style.py b/config/check_spidermonkey_style.py index a3d1b61aee..cc8695cee7 100644 --- a/config/check_spidermonkey_style.py +++ b/config/check_spidermonkey_style.py @@ -86,6 +86,7 @@ included_inclnames_to_ignore = set([ 'unicode/udat.h', # ICU 'unicode/udatpg.h', # ICU 'unicode/uenum.h', # ICU + 'unicode/uniset.h', # ICU 'unicode/unorm.h', # ICU 'unicode/unum.h', # ICU 'unicode/unumsys.h', # ICU From d646d4403f2257c46e88339ebbbf8527324bec37 Mon Sep 17 00:00:00 2001 From: "Matt A. Tobin" Date: Mon, 9 Nov 2020 21:10:53 -0500 Subject: [PATCH 6/9] Bug 1328948 - Add is(), as() to Cell. Tag #1679 --- js/src/gc/Heap.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/js/src/gc/Heap.h b/js/src/gc/Heap.h index 60e7ef729c..2089415326 100644 --- a/js/src/gc/Heap.h +++ b/js/src/gc/Heap.h @@ -28,6 +28,7 @@ #include "js/HeapAPI.h" #include "js/RootingAPI.h" #include "js/TracingAPI.h" +#include "js/TraceKind.h" struct JSRuntime; @@ -272,6 +273,23 @@ struct Cell static MOZ_ALWAYS_INLINE bool needWriteBarrierPre(JS::Zone* zone); + template + inline bool is() const { + return getTraceKind() == JS::MapTypeToTraceKind::kind; + } + + template + inline T* as() { + MOZ_ASSERT(is()); + return static_cast(this); + } + + template + inline const T* as() const { + MOZ_ASSERT(is()); + return static_cast(this); + } + #ifdef DEBUG inline bool isAligned() const; void dump(FILE* fp) const; From 86402bf7e9fd17fc6fe998ece77974ba38578253 Mon Sep 17 00:00:00 2001 From: "Matt A. Tobin" Date: Mon, 9 Nov 2020 21:12:46 -0500 Subject: [PATCH 7/9] Bug 1466909 - Use AddLvalueReference for UniquePtr's operator*(). This is required for UniquePtr to accept , which is required for PseudoHandle = mozilla::UniquePtr; in turn for mozilla::SegmentedVector> uniquePtrArena_; Tag #1679 --- mfbt/UniquePtr.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mfbt/UniquePtr.h b/mfbt/UniquePtr.h index 5679d4e03f..97fb8d27ae 100644 --- a/mfbt/UniquePtr.h +++ b/mfbt/UniquePtr.h @@ -313,7 +313,7 @@ public: return *this; } - T& operator*() const { return *get(); } + typename AddLvalueReference::Type operator*() const { return *get(); } Pointer operator->() const { MOZ_ASSERT(get(), "dereferencing a UniquePtr containing nullptr"); From dbeab6d3af44550592095c53149b2f3884a573d9 Mon Sep 17 00:00:00 2001 From: "Matt A. Tobin" Date: Mon, 9 Nov 2020 21:18:57 -0500 Subject: [PATCH 8/9] Issue #1679 - Part 1: First pass account for some of the refactoring differences in regexp-shim.h This is the patch Moonchild committed on the aborted branch before the plan was revised. --- js/src/regexp/regexp-shim.h | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/js/src/regexp/regexp-shim.h b/js/src/regexp/regexp-shim.h index 942fcf733f..6b6a8c6394 100644 --- a/js/src/regexp/regexp-shim.h +++ b/js/src/regexp/regexp-shim.h @@ -662,7 +662,7 @@ class MOZ_NONHEAP_CLASS Handle { // this object's lifetime only ends at the end of the full statement. // Origin: // https://github.com/v8/v8/blob/03aaa4b3bf4cb01eee1f223b252e6869b04ab08c/src/handles/handles.h#L91-L105 - class MOZ_TEMPORARY_CLASS ObjectRef { + class ObjectRef { public: T* operator->() { return &object_; } @@ -742,10 +742,10 @@ inline Handle handle(T object, Isolate* isolate) { class DisallowHeapAllocation { public: DisallowHeapAllocation() {} - operator const JS::AutoAssertNoGC&() const { return no_gc_; } + operator const JS::AutoCheckCannotGC&() const { return no_gc_; } private: - const JS::AutoAssertNoGC no_gc_; + const JS::AutoCheckCannotGC no_gc_; }; // This is used inside DisallowHeapAllocation regions to enable @@ -802,7 +802,7 @@ class String : public HeapObject { } private: const JSLinearString* string_; - const JS::AutoAssertNoGC& no_gc_; + const JS::AutoCheckCannotGC& no_gc_; }; FlatContent GetFlatContent(const DisallowHeapAllocation& no_gc) { MOZ_ASSERT(IsFlat()); @@ -1037,7 +1037,7 @@ public: //********** Stack guard code **********// inline StackGuard* stack_guard() { return this; } Object HandleInterrupts() { - return Object(JS::BooleanValue(cx()->handleInterrupt())); + return Object(JS::BooleanValue(cx()->handleInterrupt(cx()))); } JSContext* cx() const { return cx_; } @@ -1085,14 +1085,21 @@ class StackLimitCheck { StackLimitCheck(Isolate* isolate) : cx_(isolate->cx()) {} // Use this to check for stack-overflows in C++ code. - bool HasOverflowed() { return !CheckRecursionLimitDontReport(cx_); } + bool HasOverflowed() { + JS_CHECK_RECURSION_DONT_REPORT(cx_, return true); + return false; + } // Use this to check for interrupt request in C++ code. - bool InterruptRequested() { return cx_->hasAnyPendingInterrupt(); } + bool InterruptRequested() { + JSRuntime* rt = cx_->runtime(); + return rt->hasPendingInterrupt(); + } // Use this to check for stack-overflow when entering runtime from JS code. bool JsHasOverflowed() { - return !CheckRecursionLimitConservativeDontReport(cx_); + JS_CHECK_RECURSION_CONSERVATIVE_DONT_REPORT(cx_, return true); + return false; } private: From 87982cb409d8d604c0ff5107de0fdd4c47748259 Mon Sep 17 00:00:00 2001 From: Moonchild Date: Thu, 12 Nov 2020 11:58:39 +0000 Subject: [PATCH 9/9] Issue #1679 - Part 2: Some small issues to address buildability. - Explicitly |this| the Asserts in Cell.as<> - Unified deprot in regexp-error.h - Convert RegExpShared* inner() to a reinterpret_cast because for SOME reason .as<> no longer works after the reimport (which is a static cast) --- js/src/gc/Heap.h | 4 ++-- js/src/regexp/regexp-error.h | 1 + js/src/regexp/regexp-shim.h | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/js/src/gc/Heap.h b/js/src/gc/Heap.h index 2089415326..2f27702601 100644 --- a/js/src/gc/Heap.h +++ b/js/src/gc/Heap.h @@ -280,13 +280,13 @@ struct Cell template inline T* as() { - MOZ_ASSERT(is()); + MOZ_ASSERT(this->is()); return static_cast(this); } template inline const T* as() const { - MOZ_ASSERT(is()); + MOZ_ASSERT(this->is()); return static_cast(this); } diff --git a/js/src/regexp/regexp-error.h b/js/src/regexp/regexp-error.h index ef9d037dd3..4b495f07d1 100644 --- a/js/src/regexp/regexp-error.h +++ b/js/src/regexp/regexp-error.h @@ -5,6 +5,7 @@ #ifndef V8_REGEXP_REGEXP_ERROR_H_ #define V8_REGEXP_REGEXP_ERROR_H_ +#include "regexp-shim.h" namespace v8 { namespace internal { diff --git a/js/src/regexp/regexp-shim.h b/js/src/regexp/regexp-shim.h index 6b6a8c6394..7677da084b 100644 --- a/js/src/regexp/regexp-shim.h +++ b/js/src/regexp/regexp-shim.h @@ -957,7 +957,7 @@ class JSRegExp : public HeapObject { private: js::RegExpShared* inner() { - return value_.toGCThing()->as(); + return reinterpret_cast(value_.toGCThing()); } };