| 1 |
// Copyright 2012 the V8 project authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style license that can be |
| 3 |
// found in the LICENSE file. |
| 4 |
|
| 5 |
#ifndef V8_REGEXP_REGEXP_MACRO_ASSEMBLER_H_ |
| 6 |
#define V8_REGEXP_REGEXP_MACRO_ASSEMBLER_H_ |
| 7 |
|
| 8 |
#include "irregexp/imported/regexp-ast.h" |
| 9 |
#include "irregexp/imported/regexp.h" |
| 10 |
#include "irregexp/RegExpShim.h" |
| 11 |
|
| 12 |
namespace v8 { |
| 13 |
namespace internal { |
| 14 |
|
| 15 |
class ByteArray; |
| 16 |
class JSRegExp; |
| 17 |
class Label; |
| 18 |
class String; |
| 19 |
|
| 20 |
static const base::uc32 kLeadSurrogateStart = 0xd800; |
| 21 |
static const base::uc32 kLeadSurrogateEnd = 0xdbff; |
| 22 |
static const base::uc32 kTrailSurrogateStart = 0xdc00; |
| 23 |
static const base::uc32 kTrailSurrogateEnd = 0xdfff; |
| 24 |
static const base::uc32 kNonBmpStart = 0x10000; |
| 25 |
static const base::uc32 kNonBmpEnd = 0x10ffff; |
| 26 |
|
| 27 |
class RegExpMacroAssembler { |
| 28 |
public: |
| 29 |
// The implementation must be able to handle at least: |
| 30 |
static constexpr int kMaxRegisterCount = (1 << 16); |
| 31 |
static constexpr int kMaxRegister = kMaxRegisterCount - 1; |
| 32 |
static constexpr int kMaxCaptures = (kMaxRegister - 1) / 2; |
| 33 |
static constexpr int kMaxCPOffset = (1 << 15) - 1; |
| 34 |
static constexpr int kMinCPOffset = -(1 << 15); |
| 35 |
|
| 36 |
static constexpr int kTableSizeBits = 7; |
| 37 |
static constexpr int kTableSize = 1 << kTableSizeBits; |
| 38 |
static constexpr int kTableMask = kTableSize - 1; |
| 39 |
|
| 40 |
static constexpr int kUseCharactersValue = -1; |
| 41 |
|
| 42 |
RegExpMacroAssembler(Isolate* isolate, Zone* zone); |
| 43 |
virtual ~RegExpMacroAssembler() = default; |
| 44 |
|
| 45 |
virtual DirectHandle<HeapObject> GetCode(DirectHandle<String> source, |
| 46 |
RegExpFlags flags) = 0; |
| 47 |
|
| 48 |
// This function is called when code generation is aborted, so that |
| 49 |
// the assembler could clean up internal data structures. |
| 50 |
virtual void AbortedCodeGeneration() {} |
| 51 |
// The maximal number of pushes between stack checks. Users must supply |
| 52 |
// kCheckStackLimit flag to push operations (instead of kNoStackLimitCheck) |
| 53 |
// at least once for every stack_limit() pushes that are executed. |
| 54 |
virtual int stack_limit_slack_slot_count() = 0; |
| 55 |
virtual bool CanReadUnaligned() const = 0; |
| 56 |
|
| 57 |
virtual void AdvanceCurrentPosition(int by) = 0; // Signed cp change. |
| 58 |
virtual void AdvanceRegister(int reg, int by) = 0; // r[reg] += by. |
| 59 |
// Continues execution from the position pushed on the top of the backtrack |
| 60 |
// stack by an earlier PushBacktrack(Label*). |
| 61 |
virtual void Backtrack() = 0; |
| 62 |
virtual void Bind(Label* label) = 0; |
| 63 |
// Dispatch after looking the current character up in a 2-bits-per-entry |
| 64 |
// map. The destinations vector has up to 4 labels. |
| 65 |
virtual void CheckCharacter(unsigned c, Label* on_equal) = 0; |
| 66 |
// Bitwise and the current character with the given constant and then |
| 67 |
// check for a match with c. |
| 68 |
virtual void CheckCharacterAfterAnd(unsigned c, |
| 69 |
unsigned and_with, |
| 70 |
Label* on_equal) = 0; |
| 71 |
virtual void CheckCharacterGT(base::uc16 limit, Label* on_greater) = 0; |
| 72 |
virtual void CheckCharacterLT(base::uc16 limit, Label* on_less) = 0; |
| 73 |
virtual void CheckGreedyLoop(Label* on_tos_equals_current_position) = 0; |
| 74 |
virtual void CheckAtStart(int cp_offset, Label* on_at_start) = 0; |
| 75 |
virtual void CheckNotAtStart(int cp_offset, Label* on_not_at_start) = 0; |
| 76 |
virtual void CheckNotBackReference(int start_reg, bool read_backward, |
| 77 |
Label* on_no_match) = 0; |
| 78 |
virtual void CheckNotBackReferenceIgnoreCase(int start_reg, |
| 79 |
bool read_backward, bool unicode, |
| 80 |
Label* on_no_match) = 0; |
| 81 |
// Check the current character for a match with a literal character. If we |
| 82 |
// fail to match then goto the on_failure label. End of input always |
| 83 |
// matches. If the label is nullptr then we should pop a backtrack address |
| 84 |
// off the stack and go to that. |
| 85 |
virtual void CheckNotCharacter(unsigned c, Label* on_not_equal) = 0; |
| 86 |
virtual void CheckNotCharacterAfterAnd(unsigned c, |
| 87 |
unsigned and_with, |
| 88 |
Label* on_not_equal) = 0; |
| 89 |
// Subtract a constant from the current character, then and with the given |
| 90 |
// constant and then check for a match with c. |
| 91 |
virtual void CheckNotCharacterAfterMinusAnd(base::uc16 c, base::uc16 minus, |
| 92 |
base::uc16 and_with, |
| 93 |
Label* on_not_equal) = 0; |
| 94 |
virtual void CheckCharacterInRange(base::uc16 from, |
| 95 |
base::uc16 to, // Both inclusive. |
| 96 |
Label* on_in_range) = 0; |
| 97 |
virtual void CheckCharacterNotInRange(base::uc16 from, |
| 98 |
base::uc16 to, // Both inclusive. |
| 99 |
Label* on_not_in_range) = 0; |
| 100 |
// Returns true if the check was emitted, false otherwise. |
| 101 |
virtual bool CheckCharacterInRangeArray( |
| 102 |
const ZoneList<CharacterRange>* ranges, Label* on_in_range) = 0; |
| 103 |
virtual bool CheckCharacterNotInRangeArray( |
| 104 |
const ZoneList<CharacterRange>* ranges, Label* on_not_in_range) = 0; |
| 105 |
|
| 106 |
// The current character (modulus the kTableSize) is looked up in the byte |
| 107 |
// array, and if the found byte is non-zero, we jump to the on_bit_set label. |
| 108 |
virtual void CheckBitInTable(Handle<ByteArray> table, Label* on_bit_set) = 0; |
| 109 |
|
| 110 |
virtual void SkipUntilBitInTable(int cp_offset, Handle<ByteArray> table, |
| 111 |
Handle<ByteArray> nibble_table, |
| 112 |
int advance_by) = 0; |
| 113 |
virtual bool SkipUntilBitInTableUseSimd(int advance_by) { return false; } |
| 114 |
|
| 115 |
// Checks whether the given offset from the current position is before |
| 116 |
// the end of the string. May overwrite the current character. |
| 117 |
virtual void CheckPosition(int cp_offset, Label* on_outside_input); |
| 118 |
// Check whether a standard/default character class matches the current |
| 119 |
// character. Returns false if the type of special character class does |
| 120 |
// not have custom support. |
| 121 |
// May clobber the current loaded character. |
| 122 |
virtual bool CheckSpecialClassRanges(StandardCharacterSet type, |
| 123 |
Label* on_no_match) { |
| 124 |
return false; |
| 125 |
} |
| 126 |
|
| 127 |
// Control-flow integrity: |
| 128 |
// Define a jump target and bind a label. |
| 129 |
virtual void BindJumpTarget(Label* label) { Bind(label); } |
| 130 |
|
| 131 |
virtual void Fail() = 0; |
| 132 |
virtual void GoTo(Label* label) = 0; |
| 133 |
// Check whether a register is >= a given constant and go to a label if it |
| 134 |
// is. Backtracks instead if the label is nullptr. |
| 135 |
virtual void IfRegisterGE(int reg, int comparand, Label* if_ge) = 0; |
| 136 |
// Check whether a register is < a given constant and go to a label if it is. |
| 137 |
// Backtracks instead if the label is nullptr. |
| 138 |
virtual void IfRegisterLT(int reg, int comparand, Label* if_lt) = 0; |
| 139 |
// Check whether a register is == to the current position and go to a |
| 140 |
// label if it is. |
| 141 |
virtual void IfRegisterEqPos(int reg, Label* if_eq) = 0; |
| 142 |
V8_EXPORT_PRIVATE void LoadCurrentCharacter( |
| 143 |
int cp_offset, Label* on_end_of_input, bool check_bounds = true, |
| 144 |
int characters = 1, int eats_at_least = kUseCharactersValue); |
| 145 |
virtual void LoadCurrentCharacterImpl(int cp_offset, Label* on_end_of_input, |
| 146 |
bool check_bounds, int characters, |
| 147 |
int eats_at_least) = 0; |
| 148 |
virtual void PopCurrentPosition() = 0; |
| 149 |
virtual void PopRegister(int register_index) = 0; |
| 150 |
// Pushes the label on the backtrack stack, so that a following Backtrack |
| 151 |
// will go to this label. Always checks the backtrack stack limit. |
| 152 |
virtual void PushBacktrack(Label* label) = 0; |
| 153 |
virtual void PushCurrentPosition() = 0; |
| 154 |
enum StackCheckFlag { kNoStackLimitCheck = false, kCheckStackLimit = true }; |
| 155 |
virtual void PushRegister(int register_index, |
| 156 |
StackCheckFlag check_stack_limit) = 0; |
| 157 |
virtual void ReadCurrentPositionFromRegister(int reg) = 0; |
| 158 |
virtual void ReadStackPointerFromRegister(int reg) = 0; |
| 159 |
virtual void SetCurrentPositionFromEnd(int by) = 0; |
| 160 |
virtual void SetRegister(int register_index, int to) = 0; |
| 161 |
// Return whether the matching (with a global regexp) will be restarted. |
| 162 |
virtual bool Succeed() = 0; |
| 163 |
virtual void WriteCurrentPositionToRegister(int reg, int cp_offset) = 0; |
| 164 |
virtual void ClearRegisters(int reg_from, int reg_to) = 0; |
| 165 |
virtual void WriteStackPointerToRegister(int reg) = 0; |
| 166 |
|
| 167 |
// Check that we are not in the middle of a surrogate pair. |
| 168 |
void CheckNotInSurrogatePair(int cp_offset, Label* on_failure); |
| 169 |
|
| 170 |
#define IMPLEMENTATIONS_LIST(V) \ |
| 171 |
V(IA32) \ |
| 172 |
V(ARM) \ |
| 173 |
V(ARM64) \ |
| 174 |
V(MIPS) \ |
| 175 |
V(LOONG64) \ |
| 176 |
V(RISCV) \ |
| 177 |
V(RISCV32) \ |
| 178 |
V(S390) \ |
| 179 |
V(PPC) \ |
| 180 |
V(X64) \ |
| 181 |
V(Bytecode) |
| 182 |
|
| 183 |
enum IrregexpImplementation { |
| 184 |
#define V(Name) k##Name##Implementation, |
| 185 |
IMPLEMENTATIONS_LIST(V) |
| 186 |
#undef V |
| 187 |
}; |
| 188 |
|
| 189 |
inline const char* ImplementationToString(IrregexpImplementation impl) { |
| 190 |
static const char* const kNames[] = { |
| 191 |
#define V(Name) #Name, |
| 192 |
IMPLEMENTATIONS_LIST(V) |
| 193 |
#undef V |
| 194 |
}; |
| 195 |
return kNames[impl]; |
| 196 |
} |
| 197 |
#undef IMPLEMENTATIONS_LIST |
| 198 |
virtual IrregexpImplementation Implementation() = 0; |
| 199 |
|
| 200 |
// Compare two-byte strings case insensitively. |
| 201 |
// |
| 202 |
// Called from generated code. |
| 203 |
static int CaseInsensitiveCompareNonUnicode(Address byte_offset1, |
| 204 |
Address byte_offset2, |
| 205 |
size_t byte_length, |
| 206 |
Isolate* isolate); |
| 207 |
static int CaseInsensitiveCompareUnicode(Address byte_offset1, |
| 208 |
Address byte_offset2, |
| 209 |
size_t byte_length, |
| 210 |
Isolate* isolate); |
| 211 |
|
| 212 |
// `raw_byte_array` is a ByteArray containing a set of character ranges, |
| 213 |
// where ranges are encoded as uint16_t elements: |
| 214 |
// |
| 215 |
// [from0, to0, from1, to1, ..., fromN, toN], or |
| 216 |
// [from0, to0, from1, to1, ..., fromN] (open-ended last interval). |
| 217 |
// |
| 218 |
// fromN is inclusive, toN is exclusive. Returns zero if not in a range, |
| 219 |
// non-zero otherwise. |
| 220 |
// |
| 221 |
// Called from generated code. |
| 222 |
static uint32_t IsCharacterInRangeArray(uint32_t current_char, |
| 223 |
Address raw_byte_array); |
| 224 |
|
| 225 |
// Controls the generation of large inlined constants in the code. |
| 226 |
void set_slow_safe(bool ssc) { slow_safe_compiler_ = ssc; } |
| 227 |
bool slow_safe() const { return slow_safe_compiler_; } |
| 228 |
|
| 229 |
// Controls after how many backtracks irregexp should abort execution. If it |
| 230 |
// can fall back to the experimental engine (see `set_can_fallback`), it will |
| 231 |
// return the appropriate error code, otherwise it will return the number of |
| 232 |
// matches found so far (perhaps none). |
| 233 |
void set_backtrack_limit(uint32_t backtrack_limit) { |
| 234 |
backtrack_limit_ = backtrack_limit; |
| 235 |
} |
| 236 |
|
| 237 |
// Set whether or not irregexp can fall back to the experimental engine on |
| 238 |
// excessive backtracking. The number of backtracks considered excessive can |
| 239 |
// be controlled with set_backtrack_limit. |
| 240 |
void set_can_fallback(bool val) { can_fallback_ = val; } |
| 241 |
|
| 242 |
enum GlobalMode { |
| 243 |
NOT_GLOBAL, |
| 244 |
GLOBAL_NO_ZERO_LENGTH_CHECK, |
| 245 |
GLOBAL, |
| 246 |
GLOBAL_UNICODE |
| 247 |
}; |
| 248 |
// Set whether the regular expression has the global flag. Exiting due to |
| 249 |
// a failure in a global regexp may still mean success overall. |
| 250 |
inline void set_global_mode(GlobalMode mode) { global_mode_ = mode; } |
| 251 |
inline bool global() const { return global_mode_ != NOT_GLOBAL; } |
| 252 |
inline bool global_with_zero_length_check() const { |
| 253 |
return global_mode_ == GLOBAL || global_mode_ == GLOBAL_UNICODE; |
| 254 |
} |
| 255 |
inline bool global_unicode() const { return global_mode_ == GLOBAL_UNICODE; } |
| 256 |
|
| 257 |
Isolate* isolate() const { return isolate_; } |
| 258 |
Zone* zone() const { return zone_; } |
| 259 |
|
| 260 |
protected: |
| 261 |
bool has_backtrack_limit() const; |
| 262 |
uint32_t backtrack_limit() const { return backtrack_limit_; } |
| 263 |
|
| 264 |
bool can_fallback() const { return can_fallback_; } |
| 265 |
|
| 266 |
private: |
| 267 |
bool slow_safe_compiler_; |
| 268 |
uint32_t backtrack_limit_; |
| 269 |
bool can_fallback_ = false; |
| 270 |
GlobalMode global_mode_; |
| 271 |
Isolate* const isolate_; |
| 272 |
Zone* const zone_; |
| 273 |
}; |
| 274 |
|
| 275 |
class NativeRegExpMacroAssembler: public RegExpMacroAssembler { |
| 276 |
public: |
| 277 |
// Type of input string to generate code for. |
| 278 |
enum Mode { LATIN1 = 1, UC16 = 2 }; |
| 279 |
|
| 280 |
// Result of calling generated native RegExp code. |
| 281 |
// RETRY: Something significant changed during execution, and the matching |
| 282 |
// should be retried from scratch. |
| 283 |
// EXCEPTION: Something failed during execution. If no exception has been |
| 284 |
// thrown, it's an internal out-of-memory, and the caller should |
| 285 |
// throw the exception. |
| 286 |
// FAILURE: Matching failed. |
| 287 |
// SUCCESS: Matching succeeded, and the output array has been filled with |
| 288 |
// capture positions. |
| 289 |
// FALLBACK_TO_EXPERIMENTAL: Execute the regexp on this subject using the |
| 290 |
// experimental engine instead. |
| 291 |
enum Result { |
| 292 |
FAILURE = RegExp::kInternalRegExpFailure, |
| 293 |
SUCCESS = RegExp::kInternalRegExpSuccess, |
| 294 |
EXCEPTION = RegExp::kInternalRegExpException, |
| 295 |
RETRY = RegExp::kInternalRegExpRetry, |
| 296 |
FALLBACK_TO_EXPERIMENTAL = RegExp::kInternalRegExpFallbackToExperimental, |
| 297 |
SMALLEST_REGEXP_RESULT = RegExp::kInternalRegExpSmallestResult, |
| 298 |
}; |
| 299 |
|
| 300 |
NativeRegExpMacroAssembler(Isolate* isolate, Zone* zone) |
| 301 |
: RegExpMacroAssembler(isolate, zone), range_array_cache_(zone) {} |
| 302 |
~NativeRegExpMacroAssembler() override = default; |
| 303 |
|
| 304 |
// Returns a {Result} sentinel, or the number of successful matches. |
| 305 |
static int Match(DirectHandle<IrRegExpData> regexp_data, |
| 306 |
DirectHandle<String> subject, int* offsets_vector, |
| 307 |
int offsets_vector_length, int previous_index, |
| 308 |
Isolate* isolate); |
| 309 |
|
| 310 |
V8_EXPORT_PRIVATE static int ExecuteForTesting( |
| 311 |
Tagged<String> input, int start_offset, const uint8_t* input_start, |
| 312 |
const uint8_t* input_end, int* output, int output_size, Isolate* isolate, |
| 313 |
Tagged<JSRegExp> regexp); |
| 314 |
|
| 315 |
bool CanReadUnaligned() const override; |
| 316 |
|
| 317 |
void LoadCurrentCharacterImpl(int cp_offset, Label* on_end_of_input, |
| 318 |
bool check_bounds, int characters, |
| 319 |
int eats_at_least) override; |
| 320 |
// Load a number of characters at the given offset from the |
| 321 |
// current position, into the current-character register. |
| 322 |
virtual void LoadCurrentCharacterUnchecked(int cp_offset, |
| 323 |
int character_count) = 0; |
| 324 |
|
| 325 |
// Called from RegExp if the backtrack stack limit is hit. Tries to expand |
| 326 |
// the stack. Returns the new stack-pointer if successful, or returns 0 if |
| 327 |
// unable to grow the stack. |
| 328 |
// This function must not trigger a garbage collection. |
| 329 |
// |
| 330 |
// Called from generated code. |
| 331 |
static Address GrowStack(Isolate* isolate); |
| 332 |
|
| 333 |
// Called from generated code. |
| 334 |
static int CheckStackGuardState(Isolate* isolate, int start_index, |
| 335 |
RegExp::CallOrigin call_origin, |
| 336 |
Address* return_address, |
| 337 |
Tagged<InstructionStream> re_code, |
| 338 |
Address* subject, const uint8_t** input_start, |
| 339 |
const uint8_t** input_end, uintptr_t gap); |
| 340 |
|
| 341 |
static Address word_character_map_address() { |
| 342 |
return reinterpret_cast<Address>(&word_character_map[0]); |
| 343 |
} |
| 344 |
|
| 345 |
protected: |
| 346 |
// Byte map of one byte characters with a 0xff if the character is a word |
| 347 |
// character (digit, letter or underscore) and 0x00 otherwise. |
| 348 |
// Used by generated RegExp code. |
| 349 |
static const uint8_t word_character_map[256]; |
| 350 |
|
| 351 |
Handle<ByteArray> GetOrAddRangeArray(const ZoneList<CharacterRange>* ranges); |
| 352 |
|
| 353 |
private: |
| 354 |
// Returns a {Result} sentinel, or the number of successful matches. |
| 355 |
static int Execute(Tagged<String> input, int start_offset, |
| 356 |
const uint8_t* input_start, const uint8_t* input_end, |
| 357 |
int* output, int output_size, Isolate* isolate, |
| 358 |
Tagged<IrRegExpData> regexp_data); |
| 359 |
|
| 360 |
ZoneUnorderedMap<uint32_t, IndirectHandle<FixedUInt16Array>> |
| 361 |
range_array_cache_; |
| 362 |
}; |
| 363 |
|
| 364 |
} // namespace internal |
| 365 |
} // namespace v8 |
| 366 |
|
| 367 |
#endif // V8_REGEXP_REGEXP_MACRO_ASSEMBLER_H_ |