Skip to content

Commit 1ed98eb

Browse files
author
Samson radu
committed
deps: v8: cherrypick e540e1161b02 b69a1a1d8613 19e7a57bce3e d315af3d83cf
Fixes nodejs#66277 Original commits: [regexp] Fix modifiers for ChoiceNodes during Analysis During the analysis pass we didn't reset flags after visiting alternatives of a ChoiceNode, which is required as the sub-graph of an alternative might modify the flags. Fixed: 510487690 Change-Id: I6b2660a98b34cd12740538af4ce61685609934a0 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/7826630 Commit-Queue: Patrick Thier <[email protected]> Auto-Submit: Patrick Thier <[email protected]> Reviewed-by: Jakob Linke <[email protected]> Commit-Queue: Jakob Linke <[email protected]> Cr-Commit-Position: refs/heads/main@{#107120} ----------------------------------------------------------------------- [regexp] Record flags on deferred worklist items ActionNode::Emit for MODIFY_FLAGS was previously changed to restore outer_flags on the way out to avoid leaking inner flags to trailing deferred nodes. However, this caused nodes deferred from within the modifier group to be compiled with the outer flags when processed by Compiler::Assemble. Instead of restoring flags in ActionNode::Emit, record the active flags at the point where a node is deferred in Compiler::AddWork, and restore them from the work-list item before emitting it. This partially reverts https://chromium-review.googlesource.com/c/v8/v8/+/8174589. Change-Id: I3a552f9223b88ba99889e4375b8d1ea761257b94 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/8213283 Reviewed-by: Patrick Thier <[email protected]> Auto-Submit: Jakob Linke <[email protected]> Commit-Queue: Patrick Thier <[email protected]> Cr-Commit-Position: refs/heads/main@{#109102} ----------------------------------------------------------------------- [regexp] Reset flags after resetting state in Parser When restoring the previous state after parsing a closing parenthesis we also need to restore flags explicitly from the restored state. Fixed: 523687962 Change-Id: I63a5bd9fec2f9b7f6fb43b6893027c3c3d0755f0 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/7950157 Auto-Submit: Patrick Thier <[email protected]> Commit-Queue: Jakob Linke <[email protected]> Reviewed-by: Jakob Linke <[email protected]> Cr-Commit-Position: refs/heads/main@{#108021} ----------------------------------------------------------------------- [regexp] Reset compiler flags between word boundary branches When compiling \b and \B word boundary checks in AssertionNode where the succeeding character type is unknown, two code paths (before_non_word and before_word) are generated sequentially. If the boundary assertion is placed inside a modifier group (e.g. (?i:\.|\bNULL)), emitting the first branch traverses through on_success and reaches the ActionNode that restores outer flags at group exit. This mutates flags without restoring it when returning to emit the second branch. Consequently, subsequent nodes in the second branch (before_word) were incorrectly compiled with outer flags instead of the modifier group's flags. This CL saves compiler->flags() and restores them before emitting the before_word branch in AssertionNode::EmitBoundaryCheck. TAG=agy Fixed: 544575761 Change-Id: I7f08bcb99c749d5e22636c8bd6eb44bd1d3e2e39 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/8257209 Reviewed-by: Victor Gomes <[email protected]> Commit-Queue: Patrick Thier <[email protected]> Cr-Commit-Position: refs/heads/main@{#109236} -----------------------------------------------------------------------
1 parent 458a0a8 commit 1ed98eb

4 files changed

Lines changed: 50 additions & 6 deletions

File tree

‎deps/v8/src/regexp/regexp-compiler.cc‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ RegExpCompiler::CompilationResult RegExpCompiler::Assemble(
346346
return CompilationResult::RegExpTooBig();
347347
};
348348

349-
ZoneVector<RegExpNode*> work_list(zone());
349+
ZoneVector<WorkItem> work_list(zone());
350350
work_list_ = &work_list;
351351
Label fail;
352352
macro_assembler_->PushBacktrack(&fail);
@@ -357,11 +357,13 @@ RegExpCompiler::CompilationResult RegExpCompiler::Assemble(
357357
macro_assembler_->BindJumpTarget(&fail);
358358
macro_assembler_->Fail();
359359
while (!work_list.empty()) {
360-
RegExpNode* node = work_list.back();
360+
WorkItem item = work_list.back();
361+
RegExpNode* node = item.node;
361362
TRACE_WITH_NODE(this, "Popping from worklist ", node);
362363
work_list.pop_back();
363364
node->set_on_work_list(false);
364365
if (!node->label()->is_bound()) {
366+
set_flags(item.flags);
365367
if (node->Emit(this, &new_trace).IsError()) {
366368
return ReportError();
367369
}
@@ -2132,6 +2134,7 @@ EmitResult AssertionNode::EmitBoundaryCheck(RegExpCompiler* compiler,
21322134
}
21332135
bool at_boundary = (assertion_type_ == AssertionNode::AT_BOUNDARY);
21342136
if (next_is_word_character == Trace::UNKNOWN) {
2137+
const Flags flags = compiler->flags();
21352138
Label before_non_word;
21362139
Label before_word;
21372140
if (trace->characters_preloaded() != 1) {
@@ -2145,7 +2148,8 @@ EmitResult AssertionNode::EmitBoundaryCheck(RegExpCompiler* compiler,
21452148
RETURN_IF_ERROR(BacktrackIfPrevious(compiler, trace,
21462149
at_boundary ? kIsNonWord : kIsWord));
21472150
assembler->GoTo(&ok);
2148-
2151+
compiler->set_flags(flags);
2152+
21492153
assembler->Bind(&before_word);
21502154
RETURN_IF_ERROR(BacktrackIfPrevious(compiler, trace,
21512155
at_boundary ? kIsWord : kIsNonWord));
@@ -3841,8 +3845,13 @@ class Analysis : public NodeVisitor {
38413845
}
38423846

38433847
void VisitChoice(ChoiceNode* that) override {
3848+
// Flags need to be reset to the state of the ChoiceNode at the beginning
3849+
// of each alternative, as flags might be modified when visiting an
3850+
// alternative.
3851+
Flags header_flags = flags();
38443852
for (int i = 0; i < that->alternatives()->length(); i++) {
38453853
EnsureAnalyzed(that->alternatives()->at(i).node());
3854+
set_flags(header_flags);
38463855
if (has_failed()) return;
38473856
STATIC_FOR_EACH(Propagators::VisitChoice(that, i));
38483857
}

‎deps/v8/src/regexp/regexp-compiler.h‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -570,10 +570,15 @@ class RegExpCompiler {
570570
// lead surrogate and start matching from there.
571571
RegExpNode* OptionallyStepBackToLeadSurrogate(RegExpNode* on_success);
572572

573+
struct WorkItem {
574+
RegExpNode* node;
575+
Flags flags;
576+
};
577+
573578
inline void AddWork(RegExpNode* node) {
574579
if (!node->on_work_list() && !node->label()->is_bound()) {
575580
node->set_on_work_list(true);
576-
work_list_->push_back(node);
581+
work_list_->push_back({node, flags()});
577582
}
578583
}
579584

@@ -644,7 +649,7 @@ class RegExpCompiler {
644649
int next_register_;
645650
int unicode_lookaround_stack_register_;
646651
int unicode_lookaround_position_register_;
647-
ZoneVector<RegExpNode*>* work_list_;
652+
ZoneVector<WorkItem>* work_list_;
648653
int recursion_depth_;
649654
RegExpFlags flags_;
650655
RegExpMacroAssembler* macro_assembler_;

‎deps/v8/src/regexp/regexp-parser.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -988,7 +988,7 @@ RegExpTree* RegExpParserImpl<CharT>::ParseDisjunction() {
988988
// Restore previous state.
989989
state = state->previous_state();
990990
builder = state->builder();
991-
991+
flags_ = builder->flags();
992992
builder->AddAtom(body);
993993
// For compatibility with JSC and ES3, we allow quantifiers after
994994
// lookaheads, and break in all cases.

‎deps/v8/test/mjsunit/regexp-modifiers.js‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,33 @@ test(
7373
/(?m:^f(?si:.o)$)/, ['foo', '\nfoO', 'f\no\n', '\nf\rO\n'],
7474
['Foo', 'F\no\n']);
7575
test(/(?i:.oo)/, ['Foo', 'FOO', 'fOo', 'foO']);
76+
77+
// Nodes inside a modifier group deferred to the work list must be compiled with
78+
// the group's inner flags, not the outer flags.
79+
test(
80+
/^(?i:(?:a|b)(?:c|d)(?:e|f)(?:g|h)(?:i|j)(?:k|l)(?:m|n)X)/,
81+
['acegikmx', 'acegikmX', 'bdfhjlnx', 'bdfhjlnX'],
82+
['acegikmy', 'bdfhjlny']);
83+
84+
// Symmetrically, nodes inside a (?-i:...) group under outer /i must compile
85+
// case-sensitively even when deferred.
86+
test(
87+
/^(?-i:(?:a|b)(?:c|d)(?:e|f)(?:g|h)(?:i|j)(?:k|l)(?:m|n)x)/i,
88+
['acegikmx', 'bdfhjlnx'],
89+
['acegikmX', 'bdfhjlnX']);
90+
91+
test(/(?i:x|[a-f])/, ['F']);
92+
test(/(?i:foo)[x-z]/v, ['Foox', 'fOoz'], ['fooX','FooZ']);
93+
94+
// Boundary assertions inside modifier groups with alternations must not leak
95+
// flags between branches during code generation.
96+
test(/(?i:\.|\bNULL)/, ['null', 'NULL', 'Null', '.foo'], ['xyz']);
97+
test(/(?i:\bNULL|\.)/, ['null', 'NULL', '.foo'], ['xyz']);
98+
test(/(?i:\.|\BNULL)/, ['xnull', 'xNULL'], ['null', 'xyz']);
99+
test(/(?i:\W|\bNULL)/, ['null', 'NULL', '!'], ['x']);
100+
test(/(?i:!|\?|\bNULL)/, ['null', 'NULL', '!', '?'], ['x']);
101+
test(/(?i:\.|\bnUll)/, ['null', 'NULL']);
102+
test(/(?i:\.|\bnuLl)/, ['null', 'NULL']);
103+
test(/(?i:\.|\bnulL)/, ['null', 'NULL']);
104+
test(/(?-i:(?i:\.|\bNULL))/, ['null', 'NULL', '.foo'], ['xyz']);
105+
test(/(?i:(?-i:\.|\bNULL))/, ['NULL', '.foo'], ['null', 'xyz']);

0 commit comments

Comments
 (0)