regexp-macro-assembler.h 15.8 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5 6
#ifndef V8_REGEXP_REGEXP_MACRO_ASSEMBLER_H_
#define V8_REGEXP_REGEXP_MACRO_ASSEMBLER_H_
7

8
#include "src/base/strings.h"
9
#include "src/regexp/regexp-ast.h"
10
#include "src/regexp/regexp.h"
11

12 13
namespace v8 {
namespace internal {
14

15
class ByteArray;
16
class JSRegExp;
17
class Label;
18
class String;
19

20 21 22 23 24 25
static const base::uc32 kLeadSurrogateStart = 0xd800;
static const base::uc32 kLeadSurrogateEnd = 0xdbff;
static const base::uc32 kTrailSurrogateStart = 0xdc00;
static const base::uc32 kTrailSurrogateEnd = 0xdfff;
static const base::uc32 kNonBmpStart = 0x10000;
static const base::uc32 kNonBmpEnd = 0x10ffff;
26

27 28
class RegExpMacroAssembler {
 public:
29
  // The implementation must be able to handle at least:
30 31 32 33 34 35 36 37
  static constexpr int kMaxRegisterCount = (1 << 16);
  static constexpr int kMaxRegister = kMaxRegisterCount - 1;
  static constexpr int kMaxCPOffset = (1 << 15) - 1;
  static constexpr int kMinCPOffset = -(1 << 15);

  static constexpr int kTableSizeBits = 7;
  static constexpr int kTableSize = 1 << kTableSizeBits;
  static constexpr int kTableMask = kTableSize - 1;
38

39 40
  static constexpr int kUseCharactersValue = -1;

41 42
  RegExpMacroAssembler(Isolate* isolate, Zone* zone);
  virtual ~RegExpMacroAssembler() = default;
43

44
  virtual Handle<HeapObject> GetCode(Handle<String> source) = 0;
45

46 47 48
  // This function is called when code generation is aborted, so that
  // the assembler could clean up internal data structures.
  virtual void AbortedCodeGeneration() {}
49 50 51 52
  // The maximal number of pushes between stack checks. Users must supply
  // kCheckStackLimit flag to push operations (instead of kNoStackLimitCheck)
  // at least once for every stack_limit() pushes that are executed.
  virtual int stack_limit_slack() = 0;
53 54
  virtual bool CanReadUnaligned() const = 0;

55 56
  virtual void AdvanceCurrentPosition(int by) = 0;  // Signed cp change.
  virtual void AdvanceRegister(int reg, int by) = 0;  // r[reg] += by.
57 58
  // Continues execution from the position pushed on the top of the backtrack
  // stack by an earlier PushBacktrack(Label*).
59 60 61 62
  virtual void Backtrack() = 0;
  virtual void Bind(Label* label) = 0;
  // Dispatch after looking the current character up in a 2-bits-per-entry
  // map.  The destinations vector has up to 4 labels.
63
  virtual void CheckCharacter(unsigned c, Label* on_equal) = 0;
64 65
  // Bitwise and the current character with the given constant and then
  // check for a match with c.
66 67
  virtual void CheckCharacterAfterAnd(unsigned c,
                                      unsigned and_with,
68
                                      Label* on_equal) = 0;
69 70
  virtual void CheckCharacterGT(base::uc16 limit, Label* on_greater) = 0;
  virtual void CheckCharacterLT(base::uc16 limit, Label* on_less) = 0;
erik.corry@gmail.com's avatar
erik.corry@gmail.com committed
71
  virtual void CheckGreedyLoop(Label* on_tos_equals_current_position) = 0;
72
  virtual void CheckAtStart(int cp_offset, Label* on_at_start) = 0;
73 74 75
  virtual void CheckNotAtStart(int cp_offset, Label* on_not_at_start) = 0;
  virtual void CheckNotBackReference(int start_reg, bool read_backward,
                                     Label* on_no_match) = 0;
76
  virtual void CheckNotBackReferenceIgnoreCase(int start_reg,
77
                                               bool read_backward, bool unicode,
78
                                               Label* on_no_match) = 0;
79 80
  // 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
81 82
  // matches.  If the label is nullptr then we should pop a backtrack address
  // off the stack and go to that.
83 84 85
  virtual void CheckNotCharacter(unsigned c, Label* on_not_equal) = 0;
  virtual void CheckNotCharacterAfterAnd(unsigned c,
                                         unsigned and_with,
86
                                         Label* on_not_equal) = 0;
87
  // Subtract a constant from the current character, then and with the given
88
  // constant and then check for a match with c.
89 90
  virtual void CheckNotCharacterAfterMinusAnd(base::uc16 c, base::uc16 minus,
                                              base::uc16 and_with,
91
                                              Label* on_not_equal) = 0;
92 93
  virtual void CheckCharacterInRange(base::uc16 from,
                                     base::uc16 to,  // Both inclusive.
94
                                     Label* on_in_range) = 0;
95 96
  virtual void CheckCharacterNotInRange(base::uc16 from,
                                        base::uc16 to,  // Both inclusive.
97
                                        Label* on_not_in_range) = 0;
98 99 100 101 102
  // Returns true if the check was emitted, false otherwise.
  virtual bool CheckCharacterInRangeArray(
      const ZoneList<CharacterRange>* ranges, Label* on_in_range) = 0;
  virtual bool CheckCharacterNotInRangeArray(
      const ZoneList<CharacterRange>* ranges, Label* on_not_in_range) = 0;
103 104 105 106 107

  // The current character (modulus the kTableSize) is looked up in the byte
  // array, and if the found byte is non-zero, we jump to the on_bit_set label.
  virtual void CheckBitInTable(Handle<ByteArray> table, Label* on_bit_set) = 0;

108 109
  // Checks whether the given offset from the current position is before
  // the end of the string.  May overwrite the current character.
110
  virtual void CheckPosition(int cp_offset, Label* on_outside_input);
111 112 113 114
  // Check whether a standard/default character class matches the current
  // character. Returns false if the type of special character class does
  // not have custom support.
  // May clobber the current loaded character.
115 116
  virtual bool CheckSpecialCharacterClass(StandardCharacterSet type,
                                          Label* on_no_match) {
117 118
    return false;
  }
119 120 121 122 123

  // Control-flow integrity:
  // Define a jump target and bind a label.
  virtual void BindJumpTarget(Label* label) { Bind(label); }

124 125 126
  virtual void Fail() = 0;
  virtual void GoTo(Label* label) = 0;
  // Check whether a register is >= a given constant and go to a label if it
127
  // is.  Backtracks instead if the label is nullptr.
128 129
  virtual void IfRegisterGE(int reg, int comparand, Label* if_ge) = 0;
  // Check whether a register is < a given constant and go to a label if it is.
130
  // Backtracks instead if the label is nullptr.
131
  virtual void IfRegisterLT(int reg, int comparand, Label* if_lt) = 0;
132 133 134
  // Check whether a register is == to the current position and go to a
  // label if it is.
  virtual void IfRegisterEqPos(int reg, Label* if_eq) = 0;
135 136 137 138 139 140
  V8_EXPORT_PRIVATE void LoadCurrentCharacter(
      int cp_offset, Label* on_end_of_input, bool check_bounds = true,
      int characters = 1, int eats_at_least = kUseCharactersValue);
  virtual void LoadCurrentCharacterImpl(int cp_offset, Label* on_end_of_input,
                                        bool check_bounds, int characters,
                                        int eats_at_least) = 0;
141 142
  virtual void PopCurrentPosition() = 0;
  virtual void PopRegister(int register_index) = 0;
143 144
  // Pushes the label on the backtrack stack, so that a following Backtrack
  // will go to this label. Always checks the backtrack stack limit.
145 146
  virtual void PushBacktrack(Label* label) = 0;
  virtual void PushCurrentPosition() = 0;
147
  enum StackCheckFlag { kNoStackLimitCheck = false, kCheckStackLimit = true };
148 149
  virtual void PushRegister(int register_index,
                            StackCheckFlag check_stack_limit) = 0;
150 151
  virtual void ReadCurrentPositionFromRegister(int reg) = 0;
  virtual void ReadStackPointerFromRegister(int reg) = 0;
152
  virtual void SetCurrentPositionFromEnd(int by) = 0;
153
  virtual void SetRegister(int register_index, int to) = 0;
154 155
  // Return whether the matching (with a global regexp) will be restarted.
  virtual bool Succeed() = 0;
erik.corry@gmail.com's avatar
erik.corry@gmail.com committed
156
  virtual void WriteCurrentPositionToRegister(int reg, int cp_offset) = 0;
157
  virtual void ClearRegisters(int reg_from, int reg_to) = 0;
158
  virtual void WriteStackPointerToRegister(int reg) = 0;
159

160 161 162 163 164 165 166 167 168 169
  // Check that we are not in the middle of a surrogate pair.
  void CheckNotInSurrogatePair(int cp_offset, Label* on_failure);

#define IMPLEMENTATIONS_LIST(V) \
  V(IA32)                       \
  V(ARM)                        \
  V(ARM64)                      \
  V(MIPS)                       \
  V(LOONG64)                    \
  V(RISCV)                      \
170
  V(RISCV32)                    \
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
  V(S390)                       \
  V(PPC)                        \
  V(X64)                        \
  V(Bytecode)

  enum IrregexpImplementation {
#define V(Name) k##Name##Implementation,
    IMPLEMENTATIONS_LIST(V)
#undef V
  };

  inline const char* ImplementationToString(IrregexpImplementation impl) {
    static const char* const kNames[] = {
#define V(Name) #Name,
        IMPLEMENTATIONS_LIST(V)
#undef V
    };
    return kNames[impl];
  }
#undef IMPLEMENTATIONS_LIST
  virtual IrregexpImplementation Implementation() = 0;

193
  // Compare two-byte strings case insensitively.
194 195
  //
  // Called from generated code.
196 197 198 199 200 201 202 203
  static int CaseInsensitiveCompareNonUnicode(Address byte_offset1,
                                              Address byte_offset2,
                                              size_t byte_length,
                                              Isolate* isolate);
  static int CaseInsensitiveCompareUnicode(Address byte_offset1,
                                           Address byte_offset2,
                                           size_t byte_length,
                                           Isolate* isolate);
204

205 206 207 208 209 210 211 212 213 214 215 216 217
  // `raw_byte_array` is a ByteArray containing a set of character ranges,
  // where ranges are encoded as uint16_t elements:
  //
  //  [from0, to0, from1, to1, ..., fromN, toN], or
  //  [from0, to0, from1, to1, ..., fromN]  (open-ended last interval).
  //
  // fromN is inclusive, toN is exclusive. Returns zero if not in a range,
  // non-zero otherwise.
  //
  // Called from generated code.
  static uint32_t IsCharacterInRangeArray(uint32_t current_char,
                                          Address raw_byte_array,
                                          Isolate* isolate);
218

219 220
  // Controls the generation of large inlined constants in the code.
  void set_slow_safe(bool ssc) { slow_safe_compiler_ = ssc; }
221
  bool slow_safe() const { return slow_safe_compiler_; }
222

223 224 225 226
  // Controls after how many backtracks irregexp should abort execution.  If it
  // can fall back to the experimental engine (see `set_can_fallback`), it will
  // return the appropriate error code, otherwise it will return the number of
  // matches found so far (perhaps none).
227 228 229 230
  void set_backtrack_limit(uint32_t backtrack_limit) {
    backtrack_limit_ = backtrack_limit;
  }

231 232 233 234 235
  // Set whether or not irregexp can fall back to the experimental engine on
  // excessive backtracking.  The number of backtracks considered excessive can
  // be controlled with set_backtrack_limit.
  void set_can_fallback(bool val) { can_fallback_ = val; }

236 237 238 239 240 241
  enum GlobalMode {
    NOT_GLOBAL,
    GLOBAL_NO_ZERO_LENGTH_CHECK,
    GLOBAL,
    GLOBAL_UNICODE
  };
242 243
  // Set whether the regular expression has the global flag.  Exiting due to
  // a failure in a global regexp may still mean success overall.
244
  inline void set_global_mode(GlobalMode mode) { global_mode_ = mode; }
245 246
  inline bool global() const { return global_mode_ != NOT_GLOBAL; }
  inline bool global_with_zero_length_check() const {
247
    return global_mode_ == GLOBAL || global_mode_ == GLOBAL_UNICODE;
248
  }
249
  inline bool global_unicode() const { return global_mode_ == GLOBAL_UNICODE; }
250

251
  Isolate* isolate() const { return isolate_; }
252 253
  Zone* zone() const { return zone_; }

254
 protected:
255
  bool has_backtrack_limit() const;
256 257
  uint32_t backtrack_limit() const { return backtrack_limit_; }

258 259
  bool can_fallback() const { return can_fallback_; }

260 261
 private:
  bool slow_safe_compiler_;
262
  uint32_t backtrack_limit_;
263
  bool can_fallback_ = false;
264
  GlobalMode global_mode_;
265 266
  Isolate* const isolate_;
  Zone* const zone_;
267 268
};

269
class NativeRegExpMacroAssembler: public RegExpMacroAssembler {
270
 public:
271
  // Type of input string to generate code for.
272
  enum Mode { LATIN1 = 1, UC16 = 2 };
273

274 275 276 277
  // Result of calling generated native RegExp code.
  // RETRY: Something significant changed during execution, and the matching
  //        should be retried from scratch.
  // EXCEPTION: Something failed during execution. If no exception has been
278 279
  //            thrown, it's an internal out-of-memory, and the caller should
  //            throw the exception.
280 281
  // FAILURE: Matching failed.
  // SUCCESS: Matching succeeded, and the output array has been filled with
282 283 284
  //          capture positions.
  // FALLBACK_TO_EXPERIMENTAL: Execute the regexp on this subject using the
  //                           experimental engine instead.
285 286 287 288 289
  enum Result {
    FAILURE = RegExp::kInternalRegExpFailure,
    SUCCESS = RegExp::kInternalRegExpSuccess,
    EXCEPTION = RegExp::kInternalRegExpException,
    RETRY = RegExp::kInternalRegExpRetry,
290 291
    FALLBACK_TO_EXPERIMENTAL = RegExp::kInternalRegExpFallbackToExperimental,
    SMALLEST_REGEXP_RESULT = RegExp::kInternalRegExpSmallestResult,
292
  };
293

294
  NativeRegExpMacroAssembler(Isolate* isolate, Zone* zone)
295
      : RegExpMacroAssembler(isolate, zone), range_array_cache_(zone) {}
296
  ~NativeRegExpMacroAssembler() override = default;
297

298
  // Returns a {Result} sentinel, or the number of successful matches.
299
  static int Match(Handle<JSRegExp> regexp, Handle<String> subject,
300 301
                   int* offsets_vector, int offsets_vector_length,
                   int previous_index, Isolate* isolate);
302

303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
  V8_EXPORT_PRIVATE static int ExecuteForTesting(String input, int start_offset,
                                                 const byte* input_start,
                                                 const byte* input_end,
                                                 int* output, int output_size,
                                                 Isolate* isolate,
                                                 JSRegExp regexp);

  bool CanReadUnaligned() const override;

  void LoadCurrentCharacterImpl(int cp_offset, Label* on_end_of_input,
                                bool check_bounds, int characters,
                                int eats_at_least) override;
  // Load a number of characters at the given offset from the
  // current position, into the current-character register.
  virtual void LoadCurrentCharacterUnchecked(int cp_offset,
                                             int character_count) = 0;

320 321 322
  // Called from RegExp if the backtrack stack limit is hit. Tries to expand
  // the stack. Returns the new stack-pointer if successful, or returns 0 if
  // unable to grow the stack.
lrn@chromium.org's avatar
lrn@chromium.org committed
323
  // This function must not trigger a garbage collection.
324 325
  //
  // Called from generated code.
326
  static Address GrowStack(Isolate* isolate);
lrn@chromium.org's avatar
lrn@chromium.org committed
327

328
  // Called from generated code.
329
  static int CheckStackGuardState(Isolate* isolate, int start_index,
330 331 332
                                  RegExp::CallOrigin call_origin,
                                  Address* return_address, Code re_code,
                                  Address* subject, const byte** input_start,
333 334
                                  const byte** input_end);

335 336 337 338 339
  static Address word_character_map_address() {
    return reinterpret_cast<Address>(&word_character_map[0]);
  }

 protected:
340
  // Byte map of one byte characters with a 0xff if the character is a word
341 342
  // character (digit, letter or underscore) and 0x00 otherwise.
  // Used by generated RegExp code.
343
  static const byte word_character_map[256];
344

345 346
  Handle<ByteArray> GetOrAddRangeArray(const ZoneList<CharacterRange>* ranges);

347
 private:
348
  // Returns a {Result} sentinel, or the number of successful matches.
349 350 351
  static int Execute(String input, int start_offset, const byte* input_start,
                     const byte* input_end, int* output, int output_size,
                     Isolate* isolate, JSRegExp regexp);
352

353
  ZoneUnorderedMap<uint32_t, Handle<ByteArray>> range_array_cache_;
354
};
lrn@chromium.org's avatar
lrn@chromium.org committed
355

356 357
}  // namespace internal
}  // namespace v8
358

359
#endif  // V8_REGEXP_REGEXP_MACRO_ASSEMBLER_H_