Debian Codegraph @

js/src/irregexp/imported/regexp-interpreter.cc

raw
1 // Copyright 2011 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 // A simple interpreter for the Irregexp byte code.
6
7 #include "irregexp/imported/regexp-interpreter.h"
8
9 #include "irregexp/imported/regexp-bytecodes.h"
10 #include "irregexp/imported/regexp-macro-assembler.h"
11 #include "irregexp/imported/regexp-stack.h" // For kMaximumStackSize.
12 #include "irregexp/imported/regexp.h"
13
14 #ifdef V8_INTL_SUPPORT
15 #include "unicode/uchar.h"
16 #endif // V8_INTL_SUPPORT
17
18 // Use token threaded dispatch iff the compiler supports computed gotos and the
19 // build argument v8_enable_regexp_interpreter_threaded_dispatch was set.
20 #if V8_HAS_COMPUTED_GOTO && \
21 defined(V8_ENABLE_REGEXP_INTERPRETER_THREADED_DISPATCH)
22 #define V8_USE_COMPUTED_GOTO 1
23 #endif // V8_HAS_COMPUTED_GOTO
24
25 namespace v8 {
26 namespace internal {
27
28 namespace {
29
30 bool BackRefMatchesNoCase(Isolate* isolate, int from, int current, int len,
31 base::Vector<const base::uc16> subject,
32 bool unicode) {
33 Address offset_a =
34 reinterpret_cast<Address>(const_cast<base::uc16*>(&subject.at(from)));
35 Address offset_b =
36 reinterpret_cast<Address>(const_cast<base::uc16*>(&subject.at(current)));
37 size_t length = len * base::kUC16Size;
38
39 bool result = unicode
40 ? RegExpMacroAssembler::CaseInsensitiveCompareUnicode(
41 offset_a, offset_b, length, isolate)
42 : RegExpMacroAssembler::CaseInsensitiveCompareNonUnicode(
43 offset_a, offset_b, length, isolate);
44 return result == 1;
45 }
46
47 bool BackRefMatchesNoCase(Isolate* isolate, int from, int current, int len,
48 base::Vector<const uint8_t> subject, bool unicode) {
49 // For Latin1 characters the unicode flag makes no difference.
50 for (int i = 0; i < len; i++) {
51 unsigned int old_char = subject[from++];
52 unsigned int new_char = subject[current++];
53 if (old_char == new_char) continue;
54 // Convert both characters to lower case.
55 old_char |= 0x20;
56 new_char |= 0x20;
57 if (old_char != new_char) return false;
58 // Not letters in the ASCII range and Latin-1 range.
59 if (!(old_char - 'a' <= 'z' - 'a') &&
60 !(old_char - 224 <= 254 - 224 && old_char != 247)) {
61 return false;
62 }
63 }
64 return true;
65 }
66
67 #ifdef DEBUG
68 void MaybeTraceInterpreter(const uint8_t* code_base, const uint8_t* pc,
69 int stack_depth, int current_position,
70 uint32_t current_char, int bytecode_length,
71 const char* bytecode_name) {
72 if (v8_flags.trace_regexp_bytecodes) {
73 const bool printable = std::isprint(current_char);
74 const char* format =
75 printable
76 ? "pc = %02x, sp = %d, curpos = %d, curchar = %08x (%c), bc = "
77 : "pc = %02x, sp = %d, curpos = %d, curchar = %08x .%c., bc = ";
78 PrintF(format, pc - code_base, stack_depth, current_position, current_char,
79 printable ? current_char : '.');
80
81 RegExpBytecodeDisassembleSingle(code_base, pc);
82 }
83 }
84 #endif // DEBUG
85
86 int32_t Load32Aligned(const uint8_t* pc) {
87 DCHECK_EQ(0, reinterpret_cast<intptr_t>(pc) & 3);
88 return *reinterpret_cast<const int32_t*>(pc);
89 }
90
91 uint32_t Load16AlignedUnsigned(const uint8_t* pc) {
92 DCHECK_EQ(0, reinterpret_cast<intptr_t>(pc) & 1);
93 return *reinterpret_cast<const uint16_t*>(pc);
94 }
95
96 int32_t Load16AlignedSigned(const uint8_t* pc) {
97 DCHECK_EQ(0, reinterpret_cast<intptr_t>(pc) & 1);
98 return *reinterpret_cast<const int16_t*>(pc);
99 }
100
101 // Helpers to access the packed argument. Takes the 32 bits containing the
102 // current bytecode, where the 8 LSB contain the bytecode and the rest contains
103 // a packed 24-bit argument.
104 // TODO(jgruber): Specify signed-ness in bytecode signature declarations, and
105 // police restrictions during bytecode generation.
106 int32_t LoadPacked24Signed(int32_t bytecode_and_packed_arg) {
107 return bytecode_and_packed_arg >> BYTECODE_SHIFT;
108 }
109 uint32_t LoadPacked24Unsigned(int32_t bytecode_and_packed_arg) {
110 return static_cast<uint32_t>(bytecode_and_packed_arg) >> BYTECODE_SHIFT;
111 }
112
113 // A simple abstraction over the backtracking stack used by the interpreter.
114 //
115 // Despite the name 'backtracking' stack, it's actually used as a generic stack
116 // that stores both program counters (= offsets into the bytecode) and generic
117 // integer values.
118 class BacktrackStack {
119 public:
120 BacktrackStack() = default;
121 BacktrackStack(const BacktrackStack&) = delete;
122 BacktrackStack& operator=(const BacktrackStack&) = delete;
123
124 V8_WARN_UNUSED_RESULT bool push(int v) {
125 data_.emplace_back(v);
126 return (static_cast<int>(data_.size()) <= kMaxSize);
127 }
128 int peek() const {
129 SBXCHECK(!data_.empty());
130 return data_.back();
131 }
132 int pop() {
133 int v = peek();
134 data_.pop_back();
135 return v;
136 }
137
138 // The 'sp' is the index of the first empty element in the stack.
139 int sp() const { return static_cast<int>(data_.size()); }
140 void set_sp(uint32_t new_sp) {
141 DCHECK_LE(new_sp, sp());
142 data_.resize_no_init(new_sp);
143 }
144
145 private:
146 // Semi-arbitrary. Should be large enough for common cases to remain in the
147 // static stack-allocated backing store, but small enough not to waste space.
148 static constexpr int kStaticCapacity = 64;
149
150 using ValueT = int;
151 base::SmallVector<ValueT, kStaticCapacity> data_;
152
153 static constexpr int kMaxSize =
154 RegExpStack::kMaximumStackSize / sizeof(ValueT);
155 };
156
157 // Registers used during interpreter execution. These consist of output
158 // registers in indices [0, output_register_count[ which will contain matcher
159 // results as a {start,end} index tuple for each capture (where the whole match
160 // counts as implicit capture 0); and internal registers in indices
161 // [output_register_count, total_register_count[.
162 class InterpreterRegisters {
163 public:
164 using RegisterT = int;
165
166 InterpreterRegisters(int total_register_count, RegisterT* output_registers,
167 int output_register_count)
168 : registers_(total_register_count),
169 output_registers_(output_registers),
170 total_register_count_(total_register_count),
171 output_register_count_(output_register_count) {
172 // TODO(jgruber): Use int32_t consistently for registers. Currently, CSA
173 // uses int32_t while runtime uses int.
174 static_assert(sizeof(int) == sizeof(int32_t));
175 SBXCHECK_GE(output_register_count, 2); // At least 2 for the match itself.
176 SBXCHECK_GE(total_register_count, output_register_count);
177 SBXCHECK_LE(total_register_count, RegExpMacroAssembler::kMaxRegisterCount);
178 DCHECK_NOT_NULL(output_registers);
179
180 // Initialize the output register region to -1 signifying 'no match'.
181 std::memset(registers_.data(), -1,
182 output_register_count * sizeof(RegisterT));
183 USE(total_register_count_);
184 }
185
186 const RegisterT& operator[](size_t index) const {
187 SBXCHECK_LT(index, total_register_count_);
188 return registers_[index];
189 }
190 RegisterT& operator[](size_t index) {
191 SBXCHECK_LT(index, total_register_count_);
192 return registers_[index];
193 }
194
195 void CopyToOutputRegisters() {
196 MemCopy(output_registers_, registers_.data(),
197 output_register_count_ * sizeof(RegisterT));
198 }
199
200 private:
201 static constexpr int kStaticCapacity = 64; // Arbitrary.
202 base::SmallVector<RegisterT, kStaticCapacity> registers_;
203 RegisterT* const output_registers_;
204 const int total_register_count_;
205 const int output_register_count_;
206 };
207
208 IrregexpInterpreter::Result ThrowStackOverflow(Isolate* isolate,
209 RegExp::CallOrigin call_origin) {
210 CHECK(call_origin == RegExp::CallOrigin::kFromRuntime);
211 // We abort interpreter execution after the stack overflow is thrown, and thus
212 // allow allocation here despite the outer DisallowGarbageCollectionScope.
213 AllowGarbageCollection yes_gc;
214 isolate->StackOverflow();
215 return IrregexpInterpreter::EXCEPTION;
216 }
217
218 // Only throws if called from the runtime, otherwise just returns the EXCEPTION
219 // status code.
220 IrregexpInterpreter::Result MaybeThrowStackOverflow(
221 Isolate* isolate, RegExp::CallOrigin call_origin) {
222 if (call_origin == RegExp::CallOrigin::kFromRuntime) {
223 return ThrowStackOverflow(isolate, call_origin);
224 } else {
225 return IrregexpInterpreter::EXCEPTION;
226 }
227 }
228
229 template <typename Char>
230 void UpdateCodeAndSubjectReferences(
231 Isolate* isolate, DirectHandle<TrustedByteArray> code_array,
232 DirectHandle<String> subject_string,
233 Tagged<TrustedByteArray>* code_array_out, const uint8_t** code_base_out,
234 const uint8_t** pc_out, Tagged<String>* subject_string_out,
235 base::Vector<const Char>* subject_string_vector_out) {
236 DisallowGarbageCollection no_gc;
237
238 if (*code_base_out != code_array->begin()) {
239 *code_array_out = *code_array;
240 const intptr_t pc_offset = *pc_out - *code_base_out;
241 DCHECK_GT(pc_offset, 0);
242 *code_base_out = code_array->begin();
243 *pc_out = *code_base_out + pc_offset;
244 }
245
246 DCHECK(subject_string->IsFlat());
247 *subject_string_out = *subject_string;
248 *subject_string_vector_out = subject_string->GetCharVector<Char>(no_gc);
249 }
250
251 // Runs all pending interrupts and updates unhandlified object references if
252 // necessary.
253 template <typename Char>
254 IrregexpInterpreter::Result HandleInterrupts(
255 Isolate* isolate, RegExp::CallOrigin call_origin,
256 Tagged<TrustedByteArray>* code_array_out,
257 Tagged<String>* subject_string_out, const uint8_t** code_base_out,
258 base::Vector<const Char>* subject_string_vector_out,
259 const uint8_t** pc_out) {
260 DisallowGarbageCollection no_gc;
261
262 StackLimitCheck check(isolate);
263 bool js_has_overflowed = check.JsHasOverflowed();
264
265 if (call_origin == RegExp::CallOrigin::kFromJs) {
266 // Direct calls from JavaScript can be interrupted in two ways:
267 // 1. A real stack overflow, in which case we let the caller throw the
268 // exception.
269 // 2. The stack guard was used to interrupt execution for another purpose,
270 // forcing the call through the runtime system.
271 if (js_has_overflowed) {
272 return IrregexpInterpreter::EXCEPTION;
273 } else if (check.InterruptRequested()) {
274 return IrregexpInterpreter::RETRY;
275 }
276 } else {
277 DCHECK(call_origin == RegExp::CallOrigin::kFromRuntime);
278 // Prepare for possible GC.
279 HandleScope handles(isolate);
280 DirectHandle<TrustedByteArray> code_handle(*code_array_out, isolate);
281 DirectHandle<String> subject_handle(*subject_string_out, isolate);
282
283 if (js_has_overflowed) {
284 return ThrowStackOverflow(isolate, call_origin);
285 } else if (check.InterruptRequested()) {
286 const bool was_one_byte =
287 (*subject_string_out)->IsOneByteRepresentation();
288 Tagged<Object> result;
289 {
290 AllowGarbageCollection yes_gc;
291 result = isolate->stack_guard()->HandleInterrupts();
292 }
293 if (IsException(result, isolate)) {
294 return IrregexpInterpreter::EXCEPTION;
295 }
296
297 // If we changed between a LATIN1 and a UC16 string, we need to
298 // restart regexp matching with the appropriate template instantiation of
299 // RawMatch.
300 if (subject_handle->IsOneByteRepresentation() != was_one_byte) {
301 return IrregexpInterpreter::RETRY;
302 }
303
304 UpdateCodeAndSubjectReferences(
305 isolate, code_handle, subject_handle, code_array_out, code_base_out,
306 pc_out, subject_string_out, subject_string_vector_out);
307 }
308 }
309
310 return IrregexpInterpreter::SUCCESS;
311 }
312
313 bool CheckBitInTable(const uint32_t current_char, const uint8_t* const table) {
314 int mask = RegExpMacroAssembler::kTableMask;
315 int b = table[(current_char & mask) >> kBitsPerByteLog2];
316 int bit = (current_char & (kBitsPerByte - 1));
317 return (b & (1 << bit)) != 0;
318 }
319
320 // Returns true iff 0 <= index < length.
321 bool IndexIsInBounds(int index, int length) {
322 DCHECK_GE(length, 0);
323 return static_cast<uintptr_t>(index) < static_cast<uintptr_t>(length);
324 }
325
326 // If computed gotos are supported by the compiler, we can get addresses to
327 // labels directly in C/C++. Every bytecode handler has its own label and we
328 // store the addresses in a dispatch table indexed by bytecode. To execute the
329 // next handler we simply jump (goto) directly to its address.
330 #if V8_USE_COMPUTED_GOTO
331 #define BC_LABEL(name) BC_##name:
332 #define DECODE() \
333 do { \
334 next_insn = Load32Aligned(next_pc); \
335 next_handler_addr = dispatch_table[next_insn & BYTECODE_MASK]; \
336 } while (false)
337 #define DISPATCH() \
338 pc = next_pc; \
339 insn = next_insn; \
340 goto* next_handler_addr
341 // Without computed goto support, we fall back to a simple switch-based
342 // dispatch (A large switch statement inside a loop with a case for every
343 // bytecode).
344 #else // V8_USE_COMPUTED_GOTO
345 #define BC_LABEL(name) case BC_##name:
346 #define DECODE() next_insn = Load32Aligned(next_pc)
347 #define DISPATCH() \
348 pc = next_pc; \
349 insn = next_insn; \
350 goto switch_dispatch_continuation
351 #endif // V8_USE_COMPUTED_GOTO
352
353 // ADVANCE/SET_PC_FROM_OFFSET are separated from DISPATCH, because ideally some
354 // instructions can be executed between ADVANCE/SET_PC_FROM_OFFSET and DISPATCH.
355 // We want those two macros as far apart as possible, because the goto in
356 // DISPATCH is dependent on a memory load in ADVANCE/SET_PC_FROM_OFFSET. If we
357 // don't hit the cache and have to fetch the next handler address from physical
358 // memory, instructions between ADVANCE/SET_PC_FROM_OFFSET and DISPATCH can
359 // potentially be executed unconditionally, reducing memory stall.
360 #define ADVANCE(name) \
361 next_pc = pc + RegExpBytecodeLength(BC_##name); \
362 DECODE()
363 #define SET_PC_FROM_OFFSET(offset) \
364 next_pc = code_base + offset; \
365 DECODE()
366
367 // Current position mutations.
368 #define SET_CURRENT_POSITION(value) \
369 do { \
370 current = (value); \
371 DCHECK(base::IsInRange(current, 0, subject.length())); \
372 } while (false)
373 #define ADVANCE_CURRENT_POSITION(by) SET_CURRENT_POSITION(current + (by))
374
375 #ifdef DEBUG
376 #define BYTECODE(name) \
377 BC_LABEL(name) \
378 MaybeTraceInterpreter(code_base, pc, backtrack_stack.sp(), current, \
379 current_char, RegExpBytecodeLength(BC_##name), #name);
380 #else
381 #define BYTECODE(name) BC_LABEL(name)
382 #endif // DEBUG
383
384 template <typename Char>
385 IrregexpInterpreter::Result RawMatch(
386 Isolate* isolate, Tagged<TrustedByteArray>* code_array,
387 Tagged<String>* subject_string, base::Vector<const Char> subject,
388 int* output_registers, int output_register_count, int total_register_count,
389 int current, uint32_t current_char, RegExp::CallOrigin call_origin,
390 const uint32_t backtrack_limit) {
391 DisallowGarbageCollection no_gc;
392
393 #if V8_USE_COMPUTED_GOTO
394
395 // We have to make sure that no OOB access to the dispatch table is possible and
396 // all values are valid label addresses.
397 // Otherwise jumps to arbitrary addresses could potentially happen.
398 // This is ensured as follows:
399 // Every index to the dispatch table gets masked using BYTECODE_MASK in
400 // DECODE(). This way we can only get values between 0 (only the least
401 // significant byte of an integer is used) and kRegExpPaddedBytecodeCount - 1
402 // (BYTECODE_MASK is defined to be exactly this value).
403 // All entries from kRegExpBytecodeCount to kRegExpPaddedBytecodeCount have to
404 // be filled with BREAKs (invalid operation).
405
406 // Fill dispatch table from last defined bytecode up to the next power of two
407 // with BREAK (invalid operation).
408 // TODO(pthier): Find a way to fill up automatically (at compile time)
409 // 59 real bytecodes -> 5 fillers
410 #define BYTECODE_FILLER_ITERATOR(V) \
411 V(BREAK) /* 1 */ \
412 V(BREAK) /* 2 */ \
413 V(BREAK) /* 3 */ \
414 V(BREAK) /* 4 */ \
415 V(BREAK) /* 5 */
416
417 #define COUNT(...) +1
418 static constexpr int kRegExpBytecodeFillerCount =
419 BYTECODE_FILLER_ITERATOR(COUNT);
420 #undef COUNT
421
422 // Make sure kRegExpPaddedBytecodeCount is actually the closest possible power
423 // of two.
424 DCHECK_EQ(kRegExpPaddedBytecodeCount,
425 base::bits::RoundUpToPowerOfTwo32(kRegExpBytecodeCount));
426
427 // Make sure every bytecode we get by using BYTECODE_MASK is well defined.
428 static_assert(kRegExpBytecodeCount <= kRegExpPaddedBytecodeCount);
429 static_assert(kRegExpBytecodeCount + kRegExpBytecodeFillerCount ==
430 kRegExpPaddedBytecodeCount);
431
432 #define DECLARE_DISPATCH_TABLE_ENTRY(name, ...) &&BC_##name,
433 static const void* const dispatch_table[kRegExpPaddedBytecodeCount] = {
434 BYTECODE_ITERATOR(DECLARE_DISPATCH_TABLE_ENTRY)
435 BYTECODE_FILLER_ITERATOR(DECLARE_DISPATCH_TABLE_ENTRY)};
436 #undef DECLARE_DISPATCH_TABLE_ENTRY
437 #undef BYTECODE_FILLER_ITERATOR
438
439 #endif // V8_USE_COMPUTED_GOTO
440
441 const uint8_t* pc = (*code_array)->begin();
442 const uint8_t* code_base = pc;
443
444 InterpreterRegisters registers(total_register_count, output_registers,
445 output_register_count);
446 BacktrackStack backtrack_stack;
447
448 uint32_t backtrack_count = 0;
449
450 #ifdef DEBUG
451 if (v8_flags.trace_regexp_bytecodes) {
452 PrintF("\n\nStart bytecode interpreter\n\n");
453 }
454 #endif
455
456 while (true) {
457 const uint8_t* next_pc = pc;
458 int32_t insn;
459 int32_t next_insn;
460 #if V8_USE_COMPUTED_GOTO
461 const void* next_handler_addr;
462 DECODE();
463 DISPATCH();
464 #else
465 insn = Load32Aligned(pc);
466 switch (insn & BYTECODE_MASK) {
467 #endif // V8_USE_COMPUTED_GOTO
468 BYTECODE(BREAK) { UNREACHABLE(); }
469 BYTECODE(PUSH_CP) {
470 ADVANCE(PUSH_CP);
471 if (!backtrack_stack.push(current)) {
472 return MaybeThrowStackOverflow(isolate, call_origin);
473 }
474 DISPATCH();
475 }
476 BYTECODE(PUSH_BT) {
477 ADVANCE(PUSH_BT);
478 if (!backtrack_stack.push(Load32Aligned(pc + 4))) {
479 return MaybeThrowStackOverflow(isolate, call_origin);
480 }
481 DISPATCH();
482 }
483 BYTECODE(PUSH_REGISTER) {
484 ADVANCE(PUSH_REGISTER);
485 if (!backtrack_stack.push(registers[LoadPacked24Unsigned(insn)])) {
486 return MaybeThrowStackOverflow(isolate, call_origin);
487 }
488 DISPATCH();
489 }
490 BYTECODE(SET_REGISTER) {
491 ADVANCE(SET_REGISTER);
492 registers[LoadPacked24Unsigned(insn)] = Load32Aligned(pc + 4);
493 DISPATCH();
494 }
495 BYTECODE(ADVANCE_REGISTER) {
496 ADVANCE(ADVANCE_REGISTER);
497 registers[LoadPacked24Unsigned(insn)] += Load32Aligned(pc + 4);
498 DISPATCH();
499 }
500 BYTECODE(SET_REGISTER_TO_CP) {
501 ADVANCE(SET_REGISTER_TO_CP);
502 registers[LoadPacked24Unsigned(insn)] = current + Load32Aligned(pc + 4);
503 DISPATCH();
504 }
505 BYTECODE(SET_CP_TO_REGISTER) {
506 ADVANCE(SET_CP_TO_REGISTER);
507 SET_CURRENT_POSITION(registers[LoadPacked24Unsigned(insn)]);
508 DISPATCH();
509 }
510 BYTECODE(SET_REGISTER_TO_SP) {
511 ADVANCE(SET_REGISTER_TO_SP);
512 registers[LoadPacked24Unsigned(insn)] = backtrack_stack.sp();
513 DISPATCH();
514 }
515 BYTECODE(SET_SP_TO_REGISTER) {
516 ADVANCE(SET_SP_TO_REGISTER);
517 backtrack_stack.set_sp(registers[LoadPacked24Unsigned(insn)]);
518 DISPATCH();
519 }
520 BYTECODE(POP_CP) {
521 ADVANCE(POP_CP);
522 SET_CURRENT_POSITION(backtrack_stack.pop());
523 DISPATCH();
524 }
525 BYTECODE(POP_BT) {
526 static_assert(JSRegExp::kNoBacktrackLimit == 0);
527 if (++backtrack_count == backtrack_limit) {
528 int return_code = LoadPacked24Signed(insn);
529 return static_cast<IrregexpInterpreter::Result>(return_code);
530 }
531
532 IrregexpInterpreter::Result return_code =
533 HandleInterrupts(isolate, call_origin, code_array, subject_string,
534 &code_base, &subject, &pc);
535 if (return_code != IrregexpInterpreter::SUCCESS) return return_code;
536
537 SET_PC_FROM_OFFSET(backtrack_stack.pop());
538 DISPATCH();
539 }
540 BYTECODE(POP_REGISTER) {
541 ADVANCE(POP_REGISTER);
542 registers[LoadPacked24Unsigned(insn)] = backtrack_stack.pop();
543 DISPATCH();
544 }
545 BYTECODE(FAIL) {
546 isolate->counters()->regexp_backtracks()->AddSample(
547 static_cast<int>(backtrack_count));
548 return IrregexpInterpreter::FAILURE;
549 }
550 BYTECODE(SUCCEED) {
551 isolate->counters()->regexp_backtracks()->AddSample(
552 static_cast<int>(backtrack_count));
553 registers.CopyToOutputRegisters();
554 return IrregexpInterpreter::SUCCESS;
555 }
556 BYTECODE(ADVANCE_CP) {
557 ADVANCE(ADVANCE_CP);
558 ADVANCE_CURRENT_POSITION(LoadPacked24Signed(insn));
559 DISPATCH();
560 }
561 BYTECODE(GOTO) {
562 SET_PC_FROM_OFFSET(Load32Aligned(pc + 4));
563 DISPATCH();
564 }
565 BYTECODE(ADVANCE_CP_AND_GOTO) {
566 SET_PC_FROM_OFFSET(Load32Aligned(pc + 4));
567 ADVANCE_CURRENT_POSITION(LoadPacked24Signed(insn));
568 DISPATCH();
569 }
570 BYTECODE(CHECK_GREEDY) {
571 if (current == backtrack_stack.peek()) {
572 SET_PC_FROM_OFFSET(Load32Aligned(pc + 4));
573 backtrack_stack.pop();
574 } else {
575 ADVANCE(CHECK_GREEDY);
576 }
577 DISPATCH();
578 }
579 BYTECODE(LOAD_CURRENT_CHAR) {
580 int pos = current + LoadPacked24Signed(insn);
581 if (pos >= subject.length() || pos < 0) {
582 SET_PC_FROM_OFFSET(Load32Aligned(pc + 4));
583 } else {
584 ADVANCE(LOAD_CURRENT_CHAR);
585 current_char = subject[pos];
586 }
587 DISPATCH();
588 }
589 BYTECODE(LOAD_CURRENT_CHAR_UNCHECKED) {
590 ADVANCE(LOAD_CURRENT_CHAR_UNCHECKED);
591 int pos = current + LoadPacked24Signed(insn);
592 current_char = subject[pos];
593 DISPATCH();
594 }
595 BYTECODE(LOAD_2_CURRENT_CHARS) {
596 int pos = current + LoadPacked24Signed(insn);
597 if (pos + 2 > subject.length() || pos < 0) {
598 SET_PC_FROM_OFFSET(Load32Aligned(pc + 4));
599 } else {
600 ADVANCE(LOAD_2_CURRENT_CHARS);
601 Char next = subject[pos + 1];
602 current_char = (subject[pos] | (next << (kBitsPerByte * sizeof(Char))));
603 }
604 DISPATCH();
605 }
606 BYTECODE(LOAD_2_CURRENT_CHARS_UNCHECKED) {
607 ADVANCE(LOAD_2_CURRENT_CHARS_UNCHECKED);
608 int pos = current + LoadPacked24Signed(insn);
609 Char next = subject[pos + 1];
610 current_char = (subject[pos] | (next << (kBitsPerByte * sizeof(Char))));
611 DISPATCH();
612 }
613 BYTECODE(LOAD_4_CURRENT_CHARS) {
614 DCHECK_EQ(1, sizeof(Char));
615 int pos = current + LoadPacked24Signed(insn);
616 if (pos + 4 > subject.length() || pos < 0) {
617 SET_PC_FROM_OFFSET(Load32Aligned(pc + 4));
618 } else {
619 ADVANCE(LOAD_4_CURRENT_CHARS);
620 Char next1 = subject[pos + 1];
621 Char next2 = subject[pos + 2];
622 Char next3 = subject[pos + 3];
623 current_char =
624 (subject[pos] | (next1 << 8) | (next2 << 16) | (next3 << 24));
625 }
626 DISPATCH();
627 }
628 BYTECODE(LOAD_4_CURRENT_CHARS_UNCHECKED) {
629 ADVANCE(LOAD_4_CURRENT_CHARS_UNCHECKED);
630 DCHECK_EQ(1, sizeof(Char));
631 int pos = current + LoadPacked24Signed(insn);
632 Char next1 = subject[pos + 1];
633 Char next2 = subject[pos + 2];
634 Char next3 = subject[pos + 3];
635 current_char =
636 (subject[pos] | (next1 << 8) | (next2 << 16) | (next3 << 24));
637 DISPATCH();
638 }
639 BYTECODE(CHECK_4_CHARS) {
640 uint32_t c = Load32Aligned(pc + 4);
641 if (c == current_char) {
642 SET_PC_FROM_OFFSET(Load32Aligned(pc + 8));
643 } else {
644 ADVANCE(CHECK_4_CHARS);
645 }
646 DISPATCH();
647 }
648 BYTECODE(CHECK_CHAR) {
649 uint32_t c = LoadPacked24Unsigned(insn);
650 if (c == current_char) {
651 SET_PC_FROM_OFFSET(Load32Aligned(pc + 4));
652 } else {
653 ADVANCE(CHECK_CHAR);
654 }
655 DISPATCH();
656 }
657 BYTECODE(CHECK_NOT_4_CHARS) {
658 uint32_t c = Load32Aligned(pc + 4);
659 if (c != current_char) {
660 SET_PC_FROM_OFFSET(Load32Aligned(pc + 8));
661 } else {
662 ADVANCE(CHECK_NOT_4_CHARS);
663 }
664 DISPATCH();
665 }
666 BYTECODE(CHECK_NOT_CHAR) {
667 uint32_t c = LoadPacked24Unsigned(insn);
668 if (c != current_char) {
669 SET_PC_FROM_OFFSET(Load32Aligned(pc + 4));
670 } else {
671 ADVANCE(CHECK_NOT_CHAR);
672 }
673 DISPATCH();
674 }
675 BYTECODE(AND_CHECK_4_CHARS) {
676 uint32_t c = Load32Aligned(pc + 4);
677 if (c == (current_char & Load32Aligned(pc + 8))) {
678 SET_PC_FROM_OFFSET(Load32Aligned(pc + 12));
679 } else {
680 ADVANCE(AND_CHECK_4_CHARS);
681 }
682 DISPATCH();
683 }
684 BYTECODE(AND_CHECK_CHAR) {
685 uint32_t c = LoadPacked24Unsigned(insn);
686 if (c == (current_char & Load32Aligned(pc + 4))) {
687 SET_PC_FROM_OFFSET(Load32Aligned(pc + 8));
688 } else {
689 ADVANCE(AND_CHECK_CHAR);
690 }
691 DISPATCH();
692 }
693 BYTECODE(AND_CHECK_NOT_4_CHARS) {
694 uint32_t c = Load32Aligned(pc + 4);
695 if (c != (current_char & Load32Aligned(pc + 8))) {
696 SET_PC_FROM_OFFSET(Load32Aligned(pc + 12));
697 } else {
698 ADVANCE(AND_CHECK_NOT_4_CHARS);
699 }
700 DISPATCH();
701 }
702 BYTECODE(AND_CHECK_NOT_CHAR) {
703 uint32_t c = LoadPacked24Unsigned(insn);
704 if (c != (current_char & Load32Aligned(pc + 4))) {
705 SET_PC_FROM_OFFSET(Load32Aligned(pc + 8));
706 } else {
707 ADVANCE(AND_CHECK_NOT_CHAR);
708 }
709 DISPATCH();
710 }
711 BYTECODE(MINUS_AND_CHECK_NOT_CHAR) {
712 uint32_t c = LoadPacked24Unsigned(insn);
713 uint32_t minus = Load16AlignedUnsigned(pc + 4);
714 uint32_t mask = Load16AlignedUnsigned(pc + 6);
715 if (c != ((current_char - minus) & mask)) {
716 SET_PC_FROM_OFFSET(Load32Aligned(pc + 8));
717 } else {
718 ADVANCE(MINUS_AND_CHECK_NOT_CHAR);
719 }
720 DISPATCH();
721 }
722 BYTECODE(CHECK_CHAR_IN_RANGE) {
723 uint32_t from = Load16AlignedUnsigned(pc + 4);
724 uint32_t to = Load16AlignedUnsigned(pc + 6);
725 if (from <= current_char && current_char <= to) {
726 SET_PC_FROM_OFFSET(Load32Aligned(pc + 8));
727 } else {
728 ADVANCE(CHECK_CHAR_IN_RANGE);
729 }
730 DISPATCH();
731 }
732 BYTECODE(CHECK_CHAR_NOT_IN_RANGE) {
733 uint32_t from = Load16AlignedUnsigned(pc + 4);
734 uint32_t to = Load16AlignedUnsigned(pc + 6);
735 if (from > current_char || current_char > to) {
736 SET_PC_FROM_OFFSET(Load32Aligned(pc + 8));
737 } else {
738 ADVANCE(CHECK_CHAR_NOT_IN_RANGE);
739 }
740 DISPATCH();
741 }
742 BYTECODE(CHECK_BIT_IN_TABLE) {
743 if (CheckBitInTable(current_char, pc + 8)) {
744 SET_PC_FROM_OFFSET(Load32Aligned(pc + 4));
745 } else {
746 ADVANCE(CHECK_BIT_IN_TABLE);
747 }
748 DISPATCH();
749 }
750 BYTECODE(CHECK_LT) {
751 uint32_t limit = LoadPacked24Unsigned(insn);
752 if (current_char < limit) {
753 SET_PC_FROM_OFFSET(Load32Aligned(pc + 4));
754 } else {
755 ADVANCE(CHECK_LT);
756 }
757 DISPATCH();
758 }
759 BYTECODE(CHECK_GT) {
760 uint32_t limit = LoadPacked24Unsigned(insn);
761 if (current_char > limit) {
762 SET_PC_FROM_OFFSET(Load32Aligned(pc + 4));
763 } else {
764 ADVANCE(CHECK_GT);
765 }
766 DISPATCH();
767 }
768 BYTECODE(CHECK_REGISTER_LT) {
769 if (registers[LoadPacked24Unsigned(insn)] < Load32Aligned(pc + 4)) {
770 SET_PC_FROM_OFFSET(Load32Aligned(pc + 8));
771 } else {
772 ADVANCE(CHECK_REGISTER_LT);
773 }
774 DISPATCH();
775 }
776 BYTECODE(CHECK_REGISTER_GE) {
777 if (registers[LoadPacked24Unsigned(insn)] >= Load32Aligned(pc + 4)) {
778 SET_PC_FROM_OFFSET(Load32Aligned(pc + 8));
779 } else {
780 ADVANCE(CHECK_REGISTER_GE);
781 }
782 DISPATCH();
783 }
784 BYTECODE(CHECK_REGISTER_EQ_POS) {
785 if (registers[LoadPacked24Unsigned(insn)] == current) {
786 SET_PC_FROM_OFFSET(Load32Aligned(pc + 4));
787 } else {
788 ADVANCE(CHECK_REGISTER_EQ_POS);
789 }
790 DISPATCH();
791 }
792 BYTECODE(CHECK_NOT_REGS_EQUAL) {
793 if (registers[LoadPacked24Unsigned(insn)] ==
794 registers[Load32Aligned(pc + 4)]) {
795 ADVANCE(CHECK_NOT_REGS_EQUAL);
796 } else {
797 SET_PC_FROM_OFFSET(Load32Aligned(pc + 8));
798 }
799 DISPATCH();
800 }
801 BYTECODE(CHECK_NOT_BACK_REF) {
802 int from = registers[LoadPacked24Unsigned(insn)];
803 int len = registers[LoadPacked24Unsigned(insn) + 1] - from;
804 if (from >= 0 && len > 0) {
805 if (current + len > subject.length() ||
806 !CompareCharsEqual(&subject[from], &subject[current], len)) {
807 SET_PC_FROM_OFFSET(Load32Aligned(pc + 4));
808 DISPATCH();
809 }
810 ADVANCE_CURRENT_POSITION(len);
811 }
812 ADVANCE(CHECK_NOT_BACK_REF);
813 DISPATCH();
814 }
815 BYTECODE(CHECK_NOT_BACK_REF_BACKWARD) {
816 int from = registers[LoadPacked24Unsigned(insn)];
817 int len = registers[LoadPacked24Unsigned(insn) + 1] - from;
818 if (from >= 0 && len > 0) {
819 if (current - len < 0 ||
820 !CompareCharsEqual(&subject[from], &subject[current - len], len)) {
821 SET_PC_FROM_OFFSET(Load32Aligned(pc + 4));
822 DISPATCH();
823 }
824 SET_CURRENT_POSITION(current - len);
825 }
826 ADVANCE(CHECK_NOT_BACK_REF_BACKWARD);
827 DISPATCH();
828 }
829 BYTECODE(CHECK_NOT_BACK_REF_NO_CASE_UNICODE) {
830 int from = registers[LoadPacked24Unsigned(insn)];
831 int len = registers[LoadPacked24Unsigned(insn) + 1] - from;
832 if (from >= 0 && len > 0) {
833 if (current + len > subject.length() ||
834 !BackRefMatchesNoCase(isolate, from, current, len, subject, true)) {
835 SET_PC_FROM_OFFSET(Load32Aligned(pc + 4));
836 DISPATCH();
837 }
838 ADVANCE_CURRENT_POSITION(len);
839 }
840 ADVANCE(CHECK_NOT_BACK_REF_NO_CASE_UNICODE);
841 DISPATCH();
842 }
843 BYTECODE(CHECK_NOT_BACK_REF_NO_CASE) {
844 int from = registers[LoadPacked24Unsigned(insn)];
845 int len = registers[LoadPacked24Unsigned(insn) + 1] - from;
846 if (from >= 0 && len > 0) {
847 if (current + len > subject.length() ||
848 !BackRefMatchesNoCase(isolate, from, current, len, subject,
849 false)) {
850 SET_PC_FROM_OFFSET(Load32Aligned(pc + 4));
851 DISPATCH();
852 }
853 ADVANCE_CURRENT_POSITION(len);
854 }
855 ADVANCE(CHECK_NOT_BACK_REF_NO_CASE);
856 DISPATCH();
857 }
858 BYTECODE(CHECK_NOT_BACK_REF_NO_CASE_UNICODE_BACKWARD) {
859 int from = registers[LoadPacked24Unsigned(insn)];
860 int len = registers[LoadPacked24Unsigned(insn) + 1] - from;
861 if (from >= 0 && len > 0) {
862 if (current - len < 0 ||
863 !BackRefMatchesNoCase(isolate, from, current - len, len, subject,
864 true)) {
865 SET_PC_FROM_OFFSET(Load32Aligned(pc + 4));
866 DISPATCH();
867 }
868 SET_CURRENT_POSITION(current - len);
869 }
870 ADVANCE(CHECK_NOT_BACK_REF_NO_CASE_UNICODE_BACKWARD);
871 DISPATCH();
872 }
873 BYTECODE(CHECK_NOT_BACK_REF_NO_CASE_BACKWARD) {
874 int from = registers[LoadPacked24Unsigned(insn)];
875 int len = registers[LoadPacked24Unsigned(insn) + 1] - from;
876 if (from >= 0 && len > 0) {
877 if (current - len < 0 ||
878 !BackRefMatchesNoCase(isolate, from, current - len, len, subject,
879 false)) {
880 SET_PC_FROM_OFFSET(Load32Aligned(pc + 4));
881 DISPATCH();
882 }
883 SET_CURRENT_POSITION(current - len);
884 }
885 ADVANCE(CHECK_NOT_BACK_REF_NO_CASE_BACKWARD);
886 DISPATCH();
887 }
888 BYTECODE(CHECK_AT_START) {
889 if (current + LoadPacked24Signed(insn) == 0) {
890 SET_PC_FROM_OFFSET(Load32Aligned(pc + 4));
891 } else {
892 ADVANCE(CHECK_AT_START);
893 }
894 DISPATCH();
895 }
896 BYTECODE(CHECK_NOT_AT_START) {
897 if (current + LoadPacked24Signed(insn) == 0) {
898 ADVANCE(CHECK_NOT_AT_START);
899 } else {
900 SET_PC_FROM_OFFSET(Load32Aligned(pc + 4));
901 }
902 DISPATCH();
903 }
904 BYTECODE(SET_CURRENT_POSITION_FROM_END) {
905 ADVANCE(SET_CURRENT_POSITION_FROM_END);
906 int by = LoadPacked24Unsigned(insn);
907 if (subject.length() - current > by) {
908 SET_CURRENT_POSITION(subject.length() - by);
909 current_char = subject[current - 1];
910 }
911 DISPATCH();
912 }
913 BYTECODE(CHECK_CURRENT_POSITION) {
914 int pos = current + LoadPacked24Signed(insn);
915 if (pos > subject.length() || pos < 0) {
916 SET_PC_FROM_OFFSET(Load32Aligned(pc + 4));
917 } else {
918 ADVANCE(CHECK_CURRENT_POSITION);
919 }
920 DISPATCH();
921 }
922 BYTECODE(SKIP_UNTIL_CHAR) {
923 int32_t load_offset = LoadPacked24Signed(insn);
924 int32_t advance = Load16AlignedSigned(pc + 4);
925 uint32_t c = Load16AlignedUnsigned(pc + 6);
926 while (IndexIsInBounds(current + load_offset, subject.length())) {
927 current_char = subject[current + load_offset];
928 if (c == current_char) {
929 SET_PC_FROM_OFFSET(Load32Aligned(pc + 8));
930 DISPATCH();
931 }
932 ADVANCE_CURRENT_POSITION(advance);
933 }
934 SET_PC_FROM_OFFSET(Load32Aligned(pc + 12));
935 DISPATCH();
936 }
937 BYTECODE(SKIP_UNTIL_CHAR_AND) {
938 int32_t load_offset = LoadPacked24Signed(insn);
939 int32_t advance = Load16AlignedSigned(pc + 4);
940 uint16_t c = Load16AlignedUnsigned(pc + 6);
941 uint32_t mask = Load32Aligned(pc + 8);
942 int32_t maximum_offset = Load32Aligned(pc + 12);
943 while (static_cast<uintptr_t>(current + maximum_offset) <=
944 static_cast<uintptr_t>(subject.length())) {
945 current_char = subject[current + load_offset];
946 if (c == (current_char & mask)) {
947 SET_PC_FROM_OFFSET(Load32Aligned(pc + 16));
948 DISPATCH();
949 }
950 ADVANCE_CURRENT_POSITION(advance);
951 }
952 SET_PC_FROM_OFFSET(Load32Aligned(pc + 20));
953 DISPATCH();
954 }
955 BYTECODE(SKIP_UNTIL_CHAR_POS_CHECKED) {
956 int32_t load_offset = LoadPacked24Signed(insn);
957 int32_t advance = Load16AlignedSigned(pc + 4);
958 uint16_t c = Load16AlignedUnsigned(pc + 6);
959 int32_t maximum_offset = Load32Aligned(pc + 8);
960 while (static_cast<uintptr_t>(current + maximum_offset) <=
961 static_cast<uintptr_t>(subject.length())) {
962 current_char = subject[current + load_offset];
963 if (c == current_char) {
964 SET_PC_FROM_OFFSET(Load32Aligned(pc + 12));
965 DISPATCH();
966 }
967 ADVANCE_CURRENT_POSITION(advance);
968 }
969 SET_PC_FROM_OFFSET(Load32Aligned(pc + 16));
970 DISPATCH();
971 }
972 BYTECODE(SKIP_UNTIL_BIT_IN_TABLE) {
973 int32_t load_offset = LoadPacked24Signed(insn);
974 int32_t advance = Load32Aligned(pc + 4);
975 const uint8_t* table = pc + 8;
976 while (IndexIsInBounds(current + load_offset, subject.length())) {
977 current_char = subject[current + load_offset];
978 if (CheckBitInTable(current_char, table)) {
979 SET_PC_FROM_OFFSET(Load32Aligned(pc + 24));
980 DISPATCH();
981 }
982 ADVANCE_CURRENT_POSITION(advance);
983 }
984 SET_PC_FROM_OFFSET(Load32Aligned(pc + 28));
985 DISPATCH();
986 }
987 BYTECODE(SKIP_UNTIL_GT_OR_NOT_BIT_IN_TABLE) {
988 int32_t load_offset = LoadPacked24Signed(insn);
989 int32_t advance = Load16AlignedSigned(pc + 4);
990 uint16_t limit = Load16AlignedUnsigned(pc + 6);
991 const uint8_t* table = pc + 8;
992 while (IndexIsInBounds(current + load_offset, subject.length())) {
993 current_char = subject[current + load_offset];
994 if (current_char > limit) {
995 SET_PC_FROM_OFFSET(Load32Aligned(pc + 24));
996 DISPATCH();
997 }
998 if (!CheckBitInTable(current_char, table)) {
999 SET_PC_FROM_OFFSET(Load32Aligned(pc + 24));
1000 DISPATCH();
1001 }
1002 ADVANCE_CURRENT_POSITION(advance);
1003 }
1004 SET_PC_FROM_OFFSET(Load32Aligned(pc + 28));
1005 DISPATCH();
1006 }
1007 BYTECODE(SKIP_UNTIL_CHAR_OR_CHAR) {
1008 int32_t load_offset = LoadPacked24Signed(insn);
1009 int32_t advance = Load32Aligned(pc + 4);
1010 uint16_t c = Load16AlignedUnsigned(pc + 8);
1011 uint16_t c2 = Load16AlignedUnsigned(pc + 10);
1012 while (IndexIsInBounds(current + load_offset, subject.length())) {
1013 current_char = subject[current + load_offset];
1014 // The two if-statements below are split up intentionally, as combining
1015 // them seems to result in register allocation behaving quite
1016 // differently and slowing down the resulting code.
1017 if (c == current_char) {
1018 SET_PC_FROM_OFFSET(Load32Aligned(pc + 12));
1019 DISPATCH();
1020 }
1021 if (c2 == current_char) {
1022 SET_PC_FROM_OFFSET(Load32Aligned(pc + 12));
1023 DISPATCH();
1024 }
1025 ADVANCE_CURRENT_POSITION(advance);
1026 }
1027 SET_PC_FROM_OFFSET(Load32Aligned(pc + 16));
1028 DISPATCH();
1029 }
1030 #if V8_USE_COMPUTED_GOTO
1031 // Lint gets confused a lot if we just use !V8_USE_COMPUTED_GOTO or ifndef
1032 // V8_USE_COMPUTED_GOTO here.
1033 #else
1034 default:
1035 UNREACHABLE();
1036 }
1037 // Label we jump to in DISPATCH(). There must be no instructions between the
1038 // end of the switch, this label and the end of the loop.
1039 switch_dispatch_continuation : {}
1040 #endif // V8_USE_COMPUTED_GOTO
1041 }
1042 }
1043
1044 #undef BYTECODE
1045 #undef ADVANCE_CURRENT_POSITION
1046 #undef SET_CURRENT_POSITION
1047 #undef DISPATCH
1048 #undef DECODE
1049 #undef SET_PC_FROM_OFFSET
1050 #undef ADVANCE
1051 #undef BC_LABEL
1052 #undef V8_USE_COMPUTED_GOTO
1053
1054 } // namespace
1055
1056 // static
1057 int IrregexpInterpreter::Match(Isolate* isolate,
1058 Tagged<IrRegExpData> regexp_data,
1059 Tagged<String> subject_string,
1060 int* output_registers, int output_register_count,
1061 int start_position,
1062 RegExp::CallOrigin call_origin) {
1063 if (v8_flags.regexp_tier_up) regexp_data->TierUpTick();
1064
1065 bool is_any_unicode =
1066 IsEitherUnicode(JSRegExp::AsRegExpFlags(regexp_data->flags()));
1067 bool is_one_byte = subject_string->IsOneByteRepresentation();
1068 Tagged<TrustedByteArray> code_array = regexp_data->bytecode(is_one_byte);
1069 int total_register_count = regexp_data->max_register_count();
1070
1071 // MatchInternal only supports returning a single match per call. In global
1072 // mode, i.e. when output_registers has space for more than one match, we
1073 // need to keep running until all matches are filled in.
1074 int registers_per_match =
1075 JSRegExp::RegistersForCaptureCount(regexp_data->capture_count());
1076 DCHECK_LE(registers_per_match, output_register_count);
1077 int number_of_matches_in_output_registers =
1078 output_register_count / registers_per_match;
1079
1080 int backtrack_limit = regexp_data->backtrack_limit();
1081
1082 int num_matches = 0;
1083 int* current_output_registers = output_registers;
1084 for (int i = 0; i < number_of_matches_in_output_registers; i++) {
1085 auto current_result = MatchInternal(
1086 isolate, &code_array, &subject_string, current_output_registers,
1087 registers_per_match, total_register_count, start_position, call_origin,
1088 backtrack_limit);
1089
1090 if (current_result == SUCCESS) {
1091 // Fall through.
1092 } else if (current_result == FAILURE) {
1093 break;
1094 } else {
1095 DCHECK(current_result == EXCEPTION ||
1096 current_result == FALLBACK_TO_EXPERIMENTAL ||
1097 current_result == RETRY);
1098 return current_result;
1099 }
1100
1101 // Found a match. Advance the index.
1102
1103 num_matches++;
1104
1105 int next_start_position = current_output_registers[1];
1106 if (next_start_position == current_output_registers[0]) {
1107 // Zero-length matches.
1108 // TODO(jgruber): Use AdvanceStringIndex based on flat contents instead.
1109 next_start_position = static_cast<int>(RegExpUtils::AdvanceStringIndex(
1110 subject_string, next_start_position, is_any_unicode));
1111 if (next_start_position > static_cast<int>(subject_string->length())) {
1112 break;
1113 }
1114 }
1115
1116 start_position = next_start_position;
1117 current_output_registers += registers_per_match;
1118 }
1119
1120 return num_matches;
1121 }
1122
1123 IrregexpInterpreter::Result IrregexpInterpreter::MatchInternal(
1124 Isolate* isolate, Tagged<TrustedByteArray>* code_array,
1125 Tagged<String>* subject_string, int* output_registers,
1126 int output_register_count, int total_register_count, int start_position,
1127 RegExp::CallOrigin call_origin, uint32_t backtrack_limit) {
1128 DCHECK((*subject_string)->IsFlat());
1129
1130 // Note: Heap allocation *is* allowed in two situations if calling from
1131 // Runtime:
1132 // 1. When creating & throwing a stack overflow exception. The interpreter
1133 // aborts afterwards, and thus possible-moved objects are never used.
1134 // 2. When handling interrupts. We manually relocate unhandlified references
1135 // after interrupts have run.
1136 DisallowGarbageCollection no_gc;
1137
1138 base::uc16 previous_char = '\n';
1139 String::FlatContent subject_content =
1140 (*subject_string)->GetFlatContent(no_gc);
1141 // Because interrupts can result in GC and string content relocation, the
1142 // checksum verification in FlatContent may fail even though this code is
1143 // safe. See (2) above.
1144 subject_content.UnsafeDisableChecksumVerification();
1145 if (subject_content.IsOneByte()) {
1146 base::Vector<const uint8_t> subject_vector =
1147 subject_content.ToOneByteVector();
1148 if (start_position != 0) previous_char = subject_vector[start_position - 1];
1149 return RawMatch(isolate, code_array, subject_string, subject_vector,
1150 output_registers, output_register_count,
1151 total_register_count, start_position, previous_char,
1152 call_origin, backtrack_limit);
1153 } else {
1154 DCHECK(subject_content.IsTwoByte());
1155 base::Vector<const base::uc16> subject_vector =
1156 subject_content.ToUC16Vector();
1157 if (start_position != 0) previous_char = subject_vector[start_position - 1];
1158 return RawMatch(isolate, code_array, subject_string, subject_vector,
1159 output_registers, output_register_count,
1160 total_register_count, start_position, previous_char,
1161 call_origin, backtrack_limit);
1162 }
1163 }
1164
1165 #ifndef COMPILING_IRREGEXP_FOR_EXTERNAL_EMBEDDER
1166
1167 // This method is called through an external reference from RegExpExecInternal
1168 // builtin.
1169 int IrregexpInterpreter::MatchForCallFromJs(
1170 Address subject, int32_t start_position, Address, Address,
1171 int* output_registers, int32_t output_register_count,
1172 RegExp::CallOrigin call_origin, Isolate* isolate, Address regexp_data) {
1173 DCHECK_NOT_NULL(isolate);
1174 DCHECK_NOT_NULL(output_registers);
1175 DCHECK(call_origin == RegExp::CallOrigin::kFromJs);
1176
1177 DisallowGarbageCollection no_gc;
1178 DisallowJavascriptExecution no_js(isolate);
1179 DisallowHandleAllocation no_handles;
1180 DisallowHandleDereference no_deref;
1181
1182 Tagged<String> subject_string = Cast<String>(Tagged<Object>(subject));
1183 Tagged<IrRegExpData> regexp_data_obj =
1184 Cast<IrRegExpData>(Tagged<Object>(regexp_data));
1185
1186 if (regexp_data_obj->MarkedForTierUp()) {
1187 // Returning RETRY will re-enter through runtime, where actual recompilation
1188 // for tier-up takes place.
1189 return IrregexpInterpreter::RETRY;
1190 }
1191
1192 return Match(isolate, regexp_data_obj, subject_string, output_registers,
1193 output_register_count, start_position, call_origin);
1194 }
1195
1196 #endif // !COMPILING_IRREGEXP_FOR_EXTERNAL_EMBEDDER
1197
1198 int IrregexpInterpreter::MatchForCallFromRuntime(
1199 Isolate* isolate, DirectHandle<IrRegExpData> regexp_data,
1200 DirectHandle<String> subject_string, int* output_registers,
1201 int output_register_count, int start_position) {
1202 return Match(isolate, *regexp_data, *subject_string, output_registers,
1203 output_register_count, start_position,
1204 RegExp::CallOrigin::kFromRuntime);
1205 }
1206
1207 } // namespace internal
1208 } // namespace v8