1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
|
/* -*- 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/. */
#include "vm/RegExpObject.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/PodOperations.h"
#include <type_traits>
#include "builtin/RegExp.h"
#include "builtin/SelfHostingDefines.h" // REGEXP_*_FLAG
#include "frontend/FrontendContext.h" // AutoReportFrontendContext
#include "frontend/TokenStream.h"
#include "gc/HashUtil.h"
#include "irregexp/RegExpAPI.h"
#include "js/friend/ErrorMessages.h" // js::GetErrorMessage, JSMSG_*
#include "js/friend/StackLimits.h" // js::ReportOverRecursed
#include "js/Object.h" // JS::GetBuiltinClass
#include "js/Printer.h" // js::GenericPrinter
#include "js/RegExp.h"
#include "js/RegExpFlags.h" // JS::RegExpFlags
#include "util/StringBuffer.h"
#include "util/Unicode.h"
#include "vm/JSONPrinter.h" // js::JSONPrinter
#include "vm/MatchPairs.h"
#include "vm/PlainObject.h"
#include "vm/RegExpStatics.h"
#include "vm/StringType.h"
#include "vm/JSContext-inl.h"
#include "vm/JSObject-inl.h"
#include "vm/NativeObject-inl.h"
#include "vm/Shape-inl.h"
using namespace js;
using JS::AutoStableStringChars;
using JS::CompileOptions;
using JS::RegExpFlag;
using JS::RegExpFlags;
using mozilla::DebugOnly;
using mozilla::PodCopy;
using JS::AutoCheckCannotGC;
static_assert(RegExpFlag::HasIndices == REGEXP_HASINDICES_FLAG,
"self-hosted JS and /d flag bits must agree");
static_assert(RegExpFlag::Global == REGEXP_GLOBAL_FLAG,
"self-hosted JS and /g flag bits must agree");
static_assert(RegExpFlag::IgnoreCase == REGEXP_IGNORECASE_FLAG,
"self-hosted JS and /i flag bits must agree");
static_assert(RegExpFlag::Multiline == REGEXP_MULTILINE_FLAG,
"self-hosted JS and /m flag bits must agree");
static_assert(RegExpFlag::DotAll == REGEXP_DOTALL_FLAG,
"self-hosted JS and /s flag bits must agree");
static_assert(RegExpFlag::Unicode == REGEXP_UNICODE_FLAG,
"self-hosted JS and /u flag bits must agree");
static_assert(RegExpFlag::UnicodeSets == REGEXP_UNICODESETS_FLAG,
"self-hosted JS and /v flag bits must agree");
static_assert(RegExpFlag::Sticky == REGEXP_STICKY_FLAG,
"self-hosted JS and /y flag bits must agree");
RegExpObject* js::RegExpAlloc(JSContext* cx, NewObjectKind newKind,
HandleObject proto /* = nullptr */) {
Rooted<RegExpObject*> regexp(
cx, NewObjectWithClassProtoAndKind<RegExpObject>(cx, proto, newKind));
if (!regexp) {
return nullptr;
}
if (!SharedShape::ensureInitialCustomShape<RegExpObject>(cx, regexp)) {
return nullptr;
}
MOZ_ASSERT(regexp->lookupPure(cx->names().lastIndex)->slot() ==
RegExpObject::lastIndexSlot());
return regexp;
}
/* MatchPairs */
bool VectorMatchPairs::initArrayFrom(VectorMatchPairs& copyFrom) {
MOZ_ASSERT(copyFrom.pairCount() > 0);
if (!allocOrExpandArray(copyFrom.pairCount())) {
return false;
}
PodCopy(pairs_, copyFrom.pairs_, pairCount_);
return true;
}
bool VectorMatchPairs::allocOrExpandArray(size_t pairCount) {
if (!vec_.resizeUninitialized(pairCount)) {
return false;
}
pairs_ = &vec_[0];
pairCount_ = pairCount;
return true;
}
/* RegExpObject */
/* static */
RegExpShared* RegExpObject::getShared(JSContext* cx,
Handle<RegExpObject*> regexp) {
if (regexp->hasShared()) {
return regexp->getShared();
}
return createShared(cx, regexp);
}
/* static */
bool RegExpObject::isOriginalFlagGetter(JSNative native, RegExpFlags* mask) {
if (native == regexp_hasIndices) {
*mask = RegExpFlag::HasIndices;
return true;
}
if (native == regexp_global) {
*mask = RegExpFlag::Global;
return true;
}
if (native == regexp_ignoreCase) {
*mask = RegExpFlag::IgnoreCase;
return true;
}
if (native == regexp_multiline) {
*mask = RegExpFlag::Multiline;
return true;
}
if (native == regexp_dotAll) {
*mask = RegExpFlag::DotAll;
return true;
}
if (native == regexp_sticky) {
*mask = RegExpFlag::Sticky;
return true;
}
if (native == regexp_unicode) {
*mask = RegExpFlag::Unicode;
return true;
}
if (native == regexp_unicodeSets) {
*mask = RegExpFlag::UnicodeSets;
return true;
}
return false;
}
static bool FinishRegExpClassInit(JSContext* cx, JS::HandleObject ctor,
JS::HandleObject proto) {
#ifdef DEBUG
// Assert RegExp.prototype.exec is usually stored in a dynamic slot. The
// optimization in InlinableNativeIRGenerator::tryAttachIntrinsicRegExpExec
// depends on this.
Handle<NativeObject*> nproto = proto.as<NativeObject>();
auto prop = nproto->lookupPure(cx->names().exec);
MOZ_ASSERT(prop->isDataProperty());
MOZ_ASSERT(!nproto->isFixedSlot(prop->slot()));
#endif
return true;
}
static const ClassSpec RegExpObjectClassSpec = {
GenericCreateConstructor<js::regexp_construct, 2, gc::AllocKind::FUNCTION>,
GenericCreatePrototype<RegExpObject>,
nullptr,
js::regexp_static_props,
js::regexp_methods,
js::regexp_properties,
FinishRegExpClassInit};
const JSClass RegExpObject::class_ = {
"RegExp",
JSCLASS_HAS_RESERVED_SLOTS(RegExpObject::RESERVED_SLOTS) |
JSCLASS_HAS_CACHED_PROTO(JSProto_RegExp),
JS_NULL_CLASS_OPS, &RegExpObjectClassSpec};
const JSClass RegExpObject::protoClass_ = {
"RegExp.prototype", JSCLASS_HAS_CACHED_PROTO(JSProto_RegExp),
JS_NULL_CLASS_OPS, &RegExpObjectClassSpec};
template <typename CharT>
RegExpObject* RegExpObject::create(JSContext* cx, const CharT* chars,
size_t length, RegExpFlags flags,
NewObjectKind newKind) {
static_assert(std::is_same_v<CharT, char16_t>,
"this code may need updating if/when CharT encodes UTF-8");
Rooted<JSAtom*> source(cx, AtomizeChars(cx, chars, length));
if (!source) {
return nullptr;
}
return create(cx, source, flags, newKind);
}
template RegExpObject* RegExpObject::create(JSContext* cx,
const char16_t* chars,
size_t length, RegExpFlags flags,
NewObjectKind newKind);
RegExpObject* RegExpObject::createSyntaxChecked(JSContext* cx,
Handle<JSAtom*> source,
RegExpFlags flags,
NewObjectKind newKind) {
Rooted<RegExpObject*> regexp(cx, RegExpAlloc(cx, newKind));
if (!regexp) {
return nullptr;
}
regexp->initAndZeroLastIndex(source, flags, cx);
return regexp;
}
RegExpObject* RegExpObject::create(JSContext* cx, Handle<JSAtom*> source,
RegExpFlags flags, NewObjectKind newKind) {
Rooted<RegExpObject*> regexp(cx);
{
AutoReportFrontendContext fc(cx);
CompileOptions dummyOptions(cx);
frontend::DummyTokenStream dummyTokenStream(&fc, dummyOptions);
LifoAllocScope allocScope(&cx->tempLifoAlloc());
if (!irregexp::CheckPatternSyntax(cx, cx->stackLimitForCurrentPrincipal(),
dummyTokenStream, source, flags)) {
return nullptr;
}
regexp = RegExpAlloc(cx, newKind);
if (!regexp) {
return nullptr;
}
regexp->initAndZeroLastIndex(source, flags, cx);
MOZ_ASSERT(!regexp->hasShared());
}
return regexp;
}
/* static */
RegExpShared* RegExpObject::createShared(JSContext* cx,
Handle<RegExpObject*> regexp) {
MOZ_ASSERT(!regexp->hasShared());
Rooted<JSAtom*> source(cx, regexp->getSource());
RegExpShared* shared =
cx->zone()->regExps().get(cx, source, regexp->getFlags());
if (!shared) {
return nullptr;
}
regexp->setShared(shared);
MOZ_ASSERT(regexp->hasShared());
return shared;
}
SharedShape* RegExpObject::assignInitialShape(JSContext* cx,
Handle<RegExpObject*> self) {
MOZ_ASSERT(self->empty());
static_assert(LAST_INDEX_SLOT == 0);
/* The lastIndex property alone is writable but non-configurable. */
if (!NativeObject::addPropertyInReservedSlot(cx, self, cx->names().lastIndex,
LAST_INDEX_SLOT,
{PropertyFlag::Writable})) {
return nullptr;
}
return self->sharedShape();
}
void RegExpObject::initIgnoringLastIndex(JSAtom* source, RegExpFlags flags) {
// If this is a re-initialization with an existing RegExpShared, 'flags'
// may not match getShared()->flags, so forget the RegExpShared.
clearShared();
setSource(source);
setFlags(flags);
}
void RegExpObject::initAndZeroLastIndex(JSAtom* source, RegExpFlags flags,
JSContext* cx) {
initIgnoringLastIndex(source, flags);
zeroLastIndex(cx);
}
template <typename KnownF, typename UnknownF>
void ForEachRegExpFlag(JS::RegExpFlags flags, KnownF known, UnknownF unknown) {
uint8_t raw = flags.value();
for (uint8_t i = 1; i; i = i << 1) {
if (!(raw & i)) {
continue;
}
switch (raw & i) {
case RegExpFlag::HasIndices:
known("HasIndices", "d");
break;
case RegExpFlag::Global:
known("Global", "g");
break;
case RegExpFlag::IgnoreCase:
known("IgnoreCase", "i");
break;
case RegExpFlag::Multiline:
known("Multiline", "m");
break;
case RegExpFlag::DotAll:
known("DotAll", "s");
break;
case RegExpFlag::Unicode:
known("Unicode", "u");
break;
case RegExpFlag::Sticky:
known("Sticky", "y");
break;
default:
unknown(i);
break;
}
}
}
std::ostream& JS::operator<<(std::ostream& os, RegExpFlags flags) {
ForEachRegExpFlag(
flags, [&](const char* name, const char* c) { os << c; },
[&](uint8_t value) { os << '?'; });
return os;
}
#if defined(DEBUG) || defined(JS_JITSPEW)
void RegExpObject::dumpOwnFields(js::JSONPrinter& json) const {
{
js::GenericPrinter& out = json.beginStringProperty("source");
getSource()->dumpPropertyName(out);
json.endStringProperty();
}
json.beginInlineListProperty("flags");
ForEachRegExpFlag(
getFlags(),
[&](const char* name, const char* c) { json.value("%s", name); },
[&](uint8_t value) { json.value("Unknown(%02x)", value); });
json.endInlineList();
{
js::GenericPrinter& out = json.beginStringProperty("lastIndex");
getLastIndex().dumpStringContent(out);
json.endStringProperty();
}
}
void RegExpObject::dumpOwnStringContent(js::GenericPrinter& out) const {
out.put("/");
getSource()->dumpCharsNoQuote(out);
out.put("/");
ForEachRegExpFlag(
getFlags(), [&](const char* name, const char* c) { out.put(c); },
[&](uint8_t value) {});
}
#endif /* defined(DEBUG) || defined(JS_JITSPEW) */
static MOZ_ALWAYS_INLINE bool IsRegExpLineTerminator(const JS::Latin1Char c) {
return c == '\n' || c == '\r';
}
static MOZ_ALWAYS_INLINE bool IsRegExpLineTerminator(const char16_t c) {
return c == '\n' || c == '\r' || c == 0x2028 || c == 0x2029;
}
static MOZ_ALWAYS_INLINE bool AppendEscapedLineTerminator(
StringBuffer& sb, const JS::Latin1Char c) {
switch (c) {
case '\n':
if (!sb.append('n')) {
return false;
}
break;
case '\r':
if (!sb.append('r')) {
return false;
}
break;
default:
MOZ_CRASH("Bad LineTerminator");
}
return true;
}
static MOZ_ALWAYS_INLINE bool AppendEscapedLineTerminator(StringBuffer& sb,
const char16_t c) {
switch (c) {
case '\n':
if (!sb.append('n')) {
return false;
}
break;
case '\r':
if (!sb.append('r')) {
return false;
}
break;
case 0x2028:
if (!sb.append("u2028")) {
return false;
}
break;
case 0x2029:
if (!sb.append("u2029")) {
return false;
}
break;
default:
MOZ_CRASH("Bad LineTerminator");
}
return true;
}
template <typename CharT>
static MOZ_ALWAYS_INLINE bool SetupBuffer(StringBuffer& sb,
const CharT* oldChars, size_t oldLen,
const CharT* it) {
if constexpr (std::is_same_v<CharT, char16_t>) {
if (!sb.ensureTwoByteChars()) {
return false;
}
}
if (!sb.reserve(oldLen + 1)) {
return false;
}
sb.infallibleAppend(oldChars, size_t(it - oldChars));
return true;
}
// Note: leaves the string buffer empty if no escaping need be performed.
template <typename CharT>
static bool EscapeRegExpPattern(StringBuffer& sb, const CharT* oldChars,
size_t oldLen) {
bool inBrackets = false;
bool previousCharacterWasBackslash = false;
for (const CharT* it = oldChars; it < oldChars + oldLen; ++it) {
CharT ch = *it;
if (!previousCharacterWasBackslash) {
if (inBrackets) {
if (ch == ']') {
inBrackets = false;
}
} else if (ch == '/') {
// There's a forward slash that needs escaping.
if (sb.empty()) {
// This is the first char we've seen that needs escaping,
// copy everything up to this point.
if (!SetupBuffer(sb, oldChars, oldLen, it)) {
return false;
}
}
if (!sb.append('\\')) {
return false;
}
} else if (ch == '[') {
inBrackets = true;
}
}
if (IsRegExpLineTerminator(ch)) {
// There's LineTerminator that needs escaping.
if (sb.empty()) {
// This is the first char we've seen that needs escaping,
// copy everything up to this point.
if (!SetupBuffer(sb, oldChars, oldLen, it)) {
return false;
}
}
if (!previousCharacterWasBackslash) {
if (!sb.append('\\')) {
return false;
}
}
if (!AppendEscapedLineTerminator(sb, ch)) {
return false;
}
} else if (!sb.empty()) {
if (!sb.append(ch)) {
return false;
}
}
if (previousCharacterWasBackslash) {
previousCharacterWasBackslash = false;
} else if (ch == '\\') {
previousCharacterWasBackslash = true;
}
}
return true;
}
// ES6 draft rev32 21.2.3.2.4.
JSLinearString* js::EscapeRegExpPattern(JSContext* cx, Handle<JSAtom*> src) {
// Step 2.
if (src->length() == 0) {
return cx->names().emptyRegExp_;
}
// We may never need to use |sb|. Start using it lazily.
JSStringBuilder sb(cx);
bool escapeFailed = false;
if (src->hasLatin1Chars()) {
JS::AutoCheckCannotGC nogc;
escapeFailed =
!::EscapeRegExpPattern(sb, src->latin1Chars(nogc), src->length());
} else {
JS::AutoCheckCannotGC nogc;
escapeFailed =
!::EscapeRegExpPattern(sb, src->twoByteChars(nogc), src->length());
}
if (escapeFailed) {
return nullptr;
}
// Step 3.
if (sb.empty()) {
return src;
}
return sb.finishString();
}
// ES6 draft rev32 21.2.5.14. Optimized for RegExpObject.
JSLinearString* RegExpObject::toString(JSContext* cx,
Handle<RegExpObject*> obj) {
// Steps 3-4.
Rooted<JSAtom*> src(cx, obj->getSource());
if (!src) {
return nullptr;
}
Rooted<JSLinearString*> escapedSrc(cx, EscapeRegExpPattern(cx, src));
// Step 7.
JSStringBuilder sb(cx);
size_t len = escapedSrc->length();
if (!sb.reserve(len + 2)) {
return nullptr;
}
sb.infallibleAppend('/');
if (!sb.append(escapedSrc)) {
return nullptr;
}
sb.infallibleAppend('/');
// Steps 5-7.
if (obj->hasIndices() && !sb.append('d')) {
return nullptr;
}
if (obj->global() && !sb.append('g')) {
return nullptr;
}
if (obj->ignoreCase() && !sb.append('i')) {
return nullptr;
}
if (obj->multiline() && !sb.append('m')) {
return nullptr;
}
if (obj->dotAll() && !sb.append('s')) {
return nullptr;
}
if (obj->unicode() && !sb.append('u')) {
return nullptr;
}
if (obj->unicodeSets() && !sb.append('v')) {
return nullptr;
}
if (obj->sticky() && !sb.append('y')) {
return nullptr;
}
return sb.finishString();
}
template <typename CharT>
static MOZ_ALWAYS_INLINE bool IsRegExpMetaChar(CharT ch) {
switch (ch) {
/* ES 2016 draft Mar 25, 2016 21.2.1 SyntaxCharacter. */
case '^':
case '$':
case '\\':
case '.':
case '*':
case '+':
case '?':
case '(':
case ')':
case '[':
case ']':
case '{':
case '}':
case '|':
return true;
default:
return false;
}
}
template <typename CharT>
bool js::HasRegExpMetaChars(const CharT* chars, size_t length) {
for (size_t i = 0; i < length; ++i) {
if (IsRegExpMetaChar<CharT>(chars[i])) {
return true;
}
}
return false;
}
template bool js::HasRegExpMetaChars<Latin1Char>(const Latin1Char* chars,
size_t length);
template bool js::HasRegExpMetaChars<char16_t>(const char16_t* chars,
size_t length);
bool js::StringHasRegExpMetaChars(JSLinearString* str) {
AutoCheckCannotGC nogc;
if (str->hasLatin1Chars()) {
return HasRegExpMetaChars(str->latin1Chars(nogc), str->length());
}
return HasRegExpMetaChars(str->twoByteChars(nogc), str->length());
}
/* RegExpShared */
RegExpShared::RegExpShared(JSAtom* source, RegExpFlags flags)
: CellWithTenuredGCPointer(source), pairCount_(0), flags(flags) {}
void RegExpShared::traceChildren(JSTracer* trc) {
TraceNullableCellHeaderEdge(trc, this, "RegExpShared source");
if (kind() == RegExpShared::Kind::Atom) {
TraceNullableEdge(trc, &patternAtom_, "RegExpShared pattern atom");
} else {
for (auto& comp : compilationArray) {
TraceNullableEdge(trc, &comp.jitCode, "RegExpShared code");
}
TraceNullableEdge(trc, &groupsTemplate_, "RegExpShared groups template");
}
}
void RegExpShared::discardJitCode() {
for (auto& comp : compilationArray) {
comp.jitCode = nullptr;
}
// We can also purge the tables used by JIT code.
tables.clearAndFree();
}
void RegExpShared::finalize(JS::GCContext* gcx) {
for (auto& comp : compilationArray) {
if (comp.byteCode) {
size_t length = comp.byteCodeLength();
gcx->free_(this, comp.byteCode, length, MemoryUse::RegExpSharedBytecode);
}
}
if (namedCaptureIndices_) {
size_t length = numNamedCaptures() * sizeof(uint32_t);
gcx->free_(this, namedCaptureIndices_, length,
MemoryUse::RegExpSharedNamedCaptureData);
}
tables.~JitCodeTables();
}
/* static */
bool RegExpShared::compileIfNecessary(JSContext* cx,
MutableHandleRegExpShared re,
Handle<JSLinearString*> input,
RegExpShared::CodeKind codeKind) {
if (codeKind == RegExpShared::CodeKind::Any) {
// We start by interpreting regexps, then compile them once they are
// sufficiently hot. For very long input strings, we tier up eagerly.
codeKind = RegExpShared::CodeKind::Bytecode;
if (re->markedForTierUp() || input->length() > 1000) {
codeKind = RegExpShared::CodeKind::Jitcode;
}
}
// Fall back to bytecode if native codegen is not available.
if (!IsNativeRegExpEnabled() && codeKind == RegExpShared::CodeKind::Jitcode) {
codeKind = RegExpShared::CodeKind::Bytecode;
}
bool needsCompile = false;
if (re->kind() == RegExpShared::Kind::Unparsed) {
needsCompile = true;
}
if (re->kind() == RegExpShared::Kind::RegExp) {
if (!re->isCompiled(input->hasLatin1Chars(), codeKind)) {
needsCompile = true;
}
}
if (needsCompile) {
return irregexp::CompilePattern(cx, re, input, codeKind);
}
return true;
}
/* static */
RegExpRunStatus RegExpShared::execute(JSContext* cx,
MutableHandleRegExpShared re,
Handle<JSLinearString*> input,
size_t start, VectorMatchPairs* matches) {
MOZ_ASSERT(matches);
// TODO: Add tracelogger support
/* Compile the code at point-of-use. */
if (!compileIfNecessary(cx, re, input, RegExpShared::CodeKind::Any)) {
return RegExpRunStatus::Error;
}
/*
* Ensure sufficient memory for output vector.
* No need to initialize it. The RegExp engine fills them in on a match.
*/
if (!matches->allocOrExpandArray(re->pairCount())) {
ReportOutOfMemory(cx);
return RegExpRunStatus::Error;
}
if (re->kind() == RegExpShared::Kind::Atom) {
return RegExpShared::executeAtom(re, input, start, matches);
}
/*
* Ensure sufficient memory for output vector.
* No need to initialize it. The RegExp engine fills them in on a match.
*/
if (!matches->allocOrExpandArray(re->pairCount())) {
ReportOutOfMemory(cx);
return RegExpRunStatus::Error;
}
uint32_t interruptRetries = 0;
const uint32_t maxInterruptRetries = 4;
do {
DebugOnly<bool> alreadyThrowing = cx->isExceptionPending();
RegExpRunStatus result = irregexp::Execute(cx, re, input, start, matches);
#ifdef DEBUG
// Check if we must simulate the interruption
if (js::irregexp::IsolateShouldSimulateInterrupt(cx->isolate)) {
js::irregexp::IsolateClearShouldSimulateInterrupt(cx->isolate);
cx->requestInterrupt(InterruptReason::CallbackUrgent);
}
#endif
if (result == RegExpRunStatus::Error) {
/* Execute can return RegExpRunStatus::Error:
*
* 1. If the native stack overflowed
* 2. If the backtrack stack overflowed
* 3. If an interrupt was requested during execution.
*
* In the first two cases, we want to throw an error. In the
* third case, we want to handle the interrupt and try again.
* We cap the number of times we will retry.
*/
if (cx->isExceptionPending()) {
// If this regexp is being executed by recovery instructions
// while bailing out to handle an exception, there may already
// be an exception pending. If so, just return that exception
// instead of reporting a new one.
MOZ_ASSERT(alreadyThrowing);
return RegExpRunStatus::Error;
}
if (cx->hasAnyPendingInterrupt()) {
if (!CheckForInterrupt(cx)) {
return RegExpRunStatus::Error;
}
if (interruptRetries++ < maxInterruptRetries) {
// The initial execution may have been interpreted, or the
// interrupt may have triggered a GC that discarded jitcode.
// To maximize the chance of succeeding before being
// interrupted again, we want to ensure we are compiled.
if (!compileIfNecessary(cx, re, input,
RegExpShared::CodeKind::Jitcode)) {
return RegExpRunStatus::Error;
}
continue;
}
}
// If we have run out of retries, this regexp takes too long to execute.
ReportOverRecursed(cx);
return RegExpRunStatus::Error;
}
MOZ_ASSERT(result == RegExpRunStatus::Success ||
result == RegExpRunStatus::Success_NotFound);
return result;
} while (true);
MOZ_CRASH("Unreachable");
}
void RegExpShared::useAtomMatch(Handle<JSAtom*> pattern) {
MOZ_ASSERT(kind() == RegExpShared::Kind::Unparsed);
kind_ = RegExpShared::Kind::Atom;
patternAtom_ = pattern;
pairCount_ = 1;
}
void RegExpShared::useRegExpMatch(size_t pairCount) {
MOZ_ASSERT(kind() == RegExpShared::Kind::Unparsed);
kind_ = RegExpShared::Kind::RegExp;
pairCount_ = pairCount;
ticks_ = jit::JitOptions.regexpWarmUpThreshold;
}
/* static */
void RegExpShared::InitializeNamedCaptures(JSContext* cx, HandleRegExpShared re,
uint32_t numNamedCaptures,
Handle<PlainObject*> templateObject,
uint32_t* captureIndices) {
MOZ_ASSERT(!re->groupsTemplate_);
MOZ_ASSERT(!re->namedCaptureIndices_);
re->numNamedCaptures_ = numNamedCaptures;
re->groupsTemplate_ = templateObject;
re->namedCaptureIndices_ = captureIndices;
uint32_t arraySize = numNamedCaptures * sizeof(uint32_t);
js::AddCellMemory(re, arraySize, MemoryUse::RegExpSharedNamedCaptureData);
}
void RegExpShared::tierUpTick() {
MOZ_ASSERT(kind() == RegExpShared::Kind::RegExp);
if (ticks_ > 0) {
ticks_--;
}
}
bool RegExpShared::markedForTierUp() const {
if (!IsNativeRegExpEnabled()) {
return false;
}
if (kind() != RegExpShared::Kind::RegExp) {
return false;
}
return ticks_ == 0;
}
// When either unicode flag is set and if |index| points to a trail surrogate,
// step back to the corresponding lead surrogate.
static size_t StepBackToLeadSurrogate(const JSLinearString* input,
size_t index) {
// |index| must be a position within a two-byte string, otherwise it can't
// point to the trail surrogate of a surrogate pair.
if (index == 0 || index >= input->length() || input->hasLatin1Chars()) {
return index;
}
/*
* ES 2017 draft rev 6a13789aa9e7c6de4e96b7d3e24d9e6eba6584ad
* 21.2.2.2 step 2.
* Let listIndex be the index into Input of the character that was obtained
* from element index of str.
*
* In the spec, pattern match is performed with decoded Unicode code points,
* but our implementation performs it with UTF-16 encoded strings. In step 2,
* we should decrement lastIndex (index) if it points to a trail surrogate
* that has a corresponding lead surrogate.
*
* var r = /\uD83D\uDC38/ug;
* r.lastIndex = 1;
* var str = "\uD83D\uDC38";
* var result = r.exec(str); // pattern match starts from index 0
* print(result.index); // prints 0
*
* Note: This doesn't match the current spec text and result in different
* values for `result.index` under certain conditions. However, the spec will
* change to match our implementation's behavior.
* See https://github.com/tc39/ecma262/issues/128.
*/
JS::AutoCheckCannotGC nogc;
const auto* chars = input->twoByteChars(nogc);
if (unicode::IsTrailSurrogate(chars[index]) &&
unicode::IsLeadSurrogate(chars[index - 1])) {
index--;
}
return index;
}
static RegExpRunStatus ExecuteAtomImpl(RegExpShared* re, JSLinearString* input,
size_t start, MatchPairs* matches) {
MOZ_ASSERT(re->pairCount() == 1);
size_t length = input->length();
size_t searchLength = re->patternAtom()->length();
if (re->unicode() || re->unicodeSets()) {
start = StepBackToLeadSurrogate(input, start);
}
if (re->sticky()) {
// First part checks size_t overflow.
if (searchLength + start < searchLength || searchLength + start > length) {
return RegExpRunStatus::Success_NotFound;
}
if (!HasSubstringAt(input, re->patternAtom(), start)) {
return RegExpRunStatus::Success_NotFound;
}
(*matches)[0].start = start;
(*matches)[0].limit = start + searchLength;
matches->checkAgainst(input->length());
return RegExpRunStatus::Success;
}
int res = StringFindPattern(input, re->patternAtom(), start);
if (res == -1) {
return RegExpRunStatus::Success_NotFound;
}
(*matches)[0].start = res;
(*matches)[0].limit = res + searchLength;
matches->checkAgainst(input->length());
return RegExpRunStatus::Success;
}
RegExpRunStatus js::ExecuteRegExpAtomRaw(RegExpShared* re,
JSLinearString* input, size_t start,
MatchPairs* matchPairs) {
AutoUnsafeCallWithABI unsafe;
return ExecuteAtomImpl(re, input, start, matchPairs);
}
/* static */
RegExpRunStatus RegExpShared::executeAtom(MutableHandleRegExpShared re,
Handle<JSLinearString*> input,
size_t start,
VectorMatchPairs* matches) {
return ExecuteAtomImpl(re, input, start, matches);
}
size_t RegExpShared::sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) {
size_t n = 0;
for (const auto& compilation : compilationArray) {
if (compilation.byteCode) {
n += mallocSizeOf(compilation.byteCode);
}
}
n += tables.sizeOfExcludingThis(mallocSizeOf);
for (size_t i = 0; i < tables.length(); i++) {
n += mallocSizeOf(tables[i].get());
}
return n;
}
/* RegExpRealm */
RegExpRealm::RegExpRealm()
: optimizableRegExpPrototypeShape_(nullptr),
optimizableRegExpInstanceShape_(nullptr) {
for (auto& shape : matchResultShapes_) {
shape = nullptr;
}
}
SharedShape* RegExpRealm::createMatchResultShape(JSContext* cx,
ResultShapeKind kind) {
MOZ_ASSERT(!matchResultShapes_[kind]);
/* Create template array object */
Rooted<ArrayObject*> templateObject(cx, NewDenseEmptyArray(cx));
if (!templateObject) {
return nullptr;
}
if (kind == ResultShapeKind::Indices) {
/* The |indices| array only has a |groups| property. */
if (!NativeDefineDataProperty(cx, templateObject, cx->names().groups,
UndefinedHandleValue, JSPROP_ENUMERATE)) {
return nullptr;
}
MOZ_ASSERT(templateObject->getLastProperty().slot() == IndicesGroupsSlot);
matchResultShapes_[kind].set(templateObject->sharedShape());
return matchResultShapes_[kind];
}
/* Set dummy index property */
if (!NativeDefineDataProperty(cx, templateObject, cx->names().index,
UndefinedHandleValue, JSPROP_ENUMERATE)) {
return nullptr;
}
MOZ_ASSERT(templateObject->getLastProperty().slot() ==
MatchResultObjectIndexSlot);
/* Set dummy input property */
if (!NativeDefineDataProperty(cx, templateObject, cx->names().input,
UndefinedHandleValue, JSPROP_ENUMERATE)) {
return nullptr;
}
MOZ_ASSERT(templateObject->getLastProperty().slot() ==
MatchResultObjectInputSlot);
/* Set dummy groups property */
if (!NativeDefineDataProperty(cx, templateObject, cx->names().groups,
UndefinedHandleValue, JSPROP_ENUMERATE)) {
return nullptr;
}
MOZ_ASSERT(templateObject->getLastProperty().slot() ==
MatchResultObjectGroupsSlot);
if (kind == ResultShapeKind::WithIndices) {
/* Set dummy indices property */
if (!NativeDefineDataProperty(cx, templateObject, cx->names().indices,
UndefinedHandleValue, JSPROP_ENUMERATE)) {
return nullptr;
}
MOZ_ASSERT(templateObject->getLastProperty().slot() ==
MatchResultObjectIndicesSlot);
}
#ifdef DEBUG
if (kind == ResultShapeKind::Normal) {
MOZ_ASSERT(templateObject->numFixedSlots() == 0);
MOZ_ASSERT(templateObject->numDynamicSlots() ==
MatchResultObjectNumDynamicSlots);
MOZ_ASSERT(templateObject->slotSpan() == MatchResultObjectSlotSpan);
}
#endif
matchResultShapes_[kind].set(templateObject->sharedShape());
return matchResultShapes_[kind];
}
void RegExpRealm::trace(JSTracer* trc) {
if (regExpStatics) {
regExpStatics->trace(trc);
}
for (auto& shape : matchResultShapes_) {
TraceNullableEdge(trc, &shape, "RegExpRealm::matchResultShapes_");
}
TraceNullableEdge(trc, &optimizableRegExpPrototypeShape_,
"RegExpRealm::optimizableRegExpPrototypeShape_");
TraceNullableEdge(trc, &optimizableRegExpInstanceShape_,
"RegExpRealm::optimizableRegExpInstanceShape_");
}
RegExpShared* RegExpZone::get(JSContext* cx, Handle<JSAtom*> source,
RegExpFlags flags) {
DependentAddPtr<Set> p(cx, set_, Key(source, flags));
if (p) {
return *p;
}
auto* shared = cx->newCell<RegExpShared>(source, flags);
if (!shared) {
return nullptr;
}
if (!p.add(cx, set_, Key(source, flags), shared)) {
return nullptr;
}
return shared;
}
size_t RegExpZone::sizeOfIncludingThis(
mozilla::MallocSizeOf mallocSizeOf) const {
return mallocSizeOf(this) + set_.sizeOfExcludingThis(mallocSizeOf);
}
RegExpZone::RegExpZone(Zone* zone) : set_(zone, zone) {}
/* Functions */
JSObject* js::CloneRegExpObject(JSContext* cx, Handle<RegExpObject*> regex) {
constexpr gc::AllocKind allocKind = RegExpObject::AllocKind;
static_assert(gc::GetGCKindSlots(allocKind) == RegExpObject::RESERVED_SLOTS);
MOZ_ASSERT(regex->asTenured().getAllocKind() == allocKind);
Rooted<SharedShape*> shape(cx, regex->sharedShape());
Rooted<RegExpObject*> clone(cx, NativeObject::create<RegExpObject>(
cx, allocKind, gc::Heap::Default, shape));
if (!clone) {
return nullptr;
}
RegExpShared* shared = RegExpObject::getShared(cx, regex);
if (!shared) {
return nullptr;
}
clone->initAndZeroLastIndex(shared->getSource(), shared->getFlags(), cx);
clone->setShared(shared);
return clone;
}
template <typename CharT>
static bool ParseRegExpFlags(const CharT* chars, size_t length,
RegExpFlags* flagsOut, char16_t* invalidFlag) {
*flagsOut = RegExpFlag::NoFlags;
for (size_t i = 0; i < length; i++) {
uint8_t flag;
if (!JS::MaybeParseRegExpFlag(chars[i], &flag) || *flagsOut & flag) {
*invalidFlag = chars[i];
return false;
}
// /u and /v flags are mutually exclusive.
if (((*flagsOut & RegExpFlag::Unicode) &&
(flag & RegExpFlag::UnicodeSets)) ||
((*flagsOut & RegExpFlag::UnicodeSets) &&
(flag & RegExpFlag::Unicode))) {
*invalidFlag = chars[i];
return false;
}
*flagsOut |= flag;
}
return true;
}
bool js::ParseRegExpFlags(JSContext* cx, JSString* flagStr,
RegExpFlags* flagsOut) {
JSLinearString* linear = flagStr->ensureLinear(cx);
if (!linear) {
return false;
}
size_t len = linear->length();
bool ok;
char16_t invalidFlag;
if (linear->hasLatin1Chars()) {
AutoCheckCannotGC nogc;
ok = ::ParseRegExpFlags(linear->latin1Chars(nogc), len, flagsOut,
&invalidFlag);
} else {
AutoCheckCannotGC nogc;
ok = ::ParseRegExpFlags(linear->twoByteChars(nogc), len, flagsOut,
&invalidFlag);
}
if (!ok) {
JS::TwoByteChars range(&invalidFlag, 1);
UniqueChars utf8(JS::CharsToNewUTF8CharsZ(cx, range).c_str());
if (!utf8) {
return false;
}
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr,
JSMSG_BAD_REGEXP_FLAG, utf8.get());
return false;
}
return true;
}
JS::ubi::Node::Size JS::ubi::Concrete<RegExpShared>::size(
mozilla::MallocSizeOf mallocSizeOf) const {
return js::gc::Arena::thingSize(gc::AllocKind::REGEXP_SHARED) +
get().sizeOfExcludingThis(mallocSizeOf);
}
/*
* Regular Expressions.
*/
JS_PUBLIC_API JSObject* JS::NewRegExpObject(JSContext* cx, const char* bytes,
size_t length, RegExpFlags flags) {
AssertHeapIsIdle();
CHECK_THREAD(cx);
UniqueTwoByteChars chars(InflateString(cx, bytes, length));
if (!chars) {
return nullptr;
}
return RegExpObject::create(cx, chars.get(), length, flags, GenericObject);
}
JS_PUBLIC_API JSObject* JS::NewUCRegExpObject(JSContext* cx,
const char16_t* chars,
size_t length,
RegExpFlags flags) {
AssertHeapIsIdle();
CHECK_THREAD(cx);
return RegExpObject::create(cx, chars, length, flags, GenericObject);
}
JS_PUBLIC_API bool JS::SetRegExpInput(JSContext* cx, HandleObject obj,
HandleString input) {
AssertHeapIsIdle();
CHECK_THREAD(cx);
cx->check(input);
Handle<GlobalObject*> global = obj.as<GlobalObject>();
RegExpStatics* res = GlobalObject::getRegExpStatics(cx, global);
if (!res) {
return false;
}
res->reset(input);
return true;
}
JS_PUBLIC_API bool JS::ClearRegExpStatics(JSContext* cx, HandleObject obj) {
AssertHeapIsIdle();
CHECK_THREAD(cx);
MOZ_ASSERT(obj);
Handle<GlobalObject*> global = obj.as<GlobalObject>();
RegExpStatics* res = GlobalObject::getRegExpStatics(cx, global);
if (!res) {
return false;
}
res->clear();
return true;
}
JS_PUBLIC_API bool JS::ExecuteRegExp(JSContext* cx, HandleObject obj,
HandleObject reobj, const char16_t* chars,
size_t length, size_t* indexp, bool test,
MutableHandleValue rval) {
AssertHeapIsIdle();
CHECK_THREAD(cx);
Handle<GlobalObject*> global = obj.as<GlobalObject>();
RegExpStatics* res = GlobalObject::getRegExpStatics(cx, global);
if (!res) {
return false;
}
Rooted<JSLinearString*> input(cx, NewStringCopyN<CanGC>(cx, chars, length));
if (!input) {
return false;
}
return ExecuteRegExpLegacy(cx, res, reobj.as<RegExpObject>(), input, indexp,
test, rval);
}
JS_PUBLIC_API bool JS::ExecuteRegExpNoStatics(JSContext* cx, HandleObject obj,
const char16_t* chars,
size_t length, size_t* indexp,
bool test,
MutableHandleValue rval) {
AssertHeapIsIdle();
CHECK_THREAD(cx);
Rooted<JSLinearString*> input(cx, NewStringCopyN<CanGC>(cx, chars, length));
if (!input) {
return false;
}
return ExecuteRegExpLegacy(cx, nullptr, obj.as<RegExpObject>(), input, indexp,
test, rval);
}
JS_PUBLIC_API bool JS::ObjectIsRegExp(JSContext* cx, HandleObject obj,
bool* isRegExp) {
cx->check(obj);
ESClass cls;
if (!GetBuiltinClass(cx, obj, &cls)) {
return false;
}
*isRegExp = cls == ESClass::RegExp;
return true;
}
JS_PUBLIC_API RegExpFlags JS::GetRegExpFlags(JSContext* cx, HandleObject obj) {
AssertHeapIsIdle();
CHECK_THREAD(cx);
RegExpShared* shared = RegExpToShared(cx, obj);
if (!shared) {
return RegExpFlag::NoFlags;
}
return shared->getFlags();
}
JS_PUBLIC_API JSString* JS::GetRegExpSource(JSContext* cx, HandleObject obj) {
AssertHeapIsIdle();
CHECK_THREAD(cx);
RegExpShared* shared = RegExpToShared(cx, obj);
if (!shared) {
return nullptr;
}
return shared->getSource();
}
JS_PUBLIC_API bool JS::CheckRegExpSyntax(JSContext* cx, const char16_t* chars,
size_t length, RegExpFlags flags,
MutableHandleValue error) {
AssertHeapIsIdle();
CHECK_THREAD(cx);
AutoReportFrontendContext fc(cx);
CompileOptions dummyOptions(cx);
frontend::DummyTokenStream dummyTokenStream(&fc, dummyOptions);
LifoAllocScope allocScope(&cx->tempLifoAlloc());
mozilla::Range<const char16_t> source(chars, length);
bool success = irregexp::CheckPatternSyntax(
cx->tempLifoAlloc(), cx->stackLimitForCurrentPrincipal(),
dummyTokenStream, source, flags);
error.set(UndefinedValue());
if (!success) {
if (!fc.convertToRuntimeErrorAndClear()) {
return false;
}
// We can fail because of OOM or over-recursion even if the syntax is valid.
if (cx->isThrowingOutOfMemory() || cx->isThrowingOverRecursed()) {
return false;
}
if (!cx->getPendingException(error)) {
return false;
}
cx->clearPendingException();
}
return true;
}
|