Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qv4regexp.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant
4// TODO: verifyj critical part should be in YARR
5
6#include "qv4regexp_p.h"
7#include "qv4engine_p.h"
9#include <private/qv4mm_p.h>
10#include <runtime/VM.h>
11
12using namespace QV4;
13
14#if ENABLE(YARR_JIT)
15static constexpr qsizetype LongStringJitThreshold = 1024;
16static constexpr int LongStringJitBoost = 3;
17#endif
18
19static JSC::RegExpFlags jscFlags(quint8 flags)
20{
21 JSC::RegExpFlags jscFlags = JSC::NoFlags;
22 if (flags & CompiledData::RegExp::RegExp_Global)
23 jscFlags = static_cast<JSC::RegExpFlags>(jscFlags | JSC::FlagGlobal);
24 if (flags & CompiledData::RegExp::RegExp_IgnoreCase)
25 jscFlags = static_cast<JSC::RegExpFlags>(jscFlags | JSC::FlagIgnoreCase);
26 if (flags & CompiledData::RegExp::RegExp_Multiline)
27 jscFlags = static_cast<JSC::RegExpFlags>(jscFlags | JSC::FlagMultiline);
28 if (flags & CompiledData::RegExp::RegExp_Unicode)
29 jscFlags = static_cast<JSC::RegExpFlags>(jscFlags | JSC::FlagUnicode);
30 if (flags & CompiledData::RegExp::RegExp_Sticky)
31 jscFlags = static_cast<JSC::RegExpFlags>(jscFlags | JSC::FlagSticky);
32 return jscFlags;
33}
34
35RegExpCache::~RegExpCache()
36{
37 for (RegExpCache::Iterator it = begin(), e = end(); it != e; ++it) {
38 if (RegExp *re = it.value().as<RegExp>())
39 re->d()->cache = nullptr;
40 }
41}
42
44
45uint RegExp::match(const QString &string, int start, uint *matchOffsets)
46{
47 if (!isValid())
48 return JSC::Yarr::offsetNoMatch;
49
50#if ENABLE(YARR_JIT)
51 auto *priv = d();
52
53 auto regenerateByteCode = [](Heap::RegExp *regexp) {
54 const void* stackLimit = regexp->internalClass->engine->cppStackLimit;
55 JSC::Yarr::ErrorCode error = JSC::Yarr::ErrorCode::NoError;
56 JSC::Yarr::YarrPattern yarrPattern(WTF::String(*regexp->pattern), jscFlags(regexp->flags),
57 error, const_cast<void *>(stackLimit));
58
59 // As we successfully parsed the pattern before, we should still be able to.
60 Q_ASSERT(error == JSC::Yarr::ErrorCode::NoError);
61
62 regexp->byteCode = JSC::Yarr::byteCompile(
63 yarrPattern,
64 regexp->internalClass->engine->bumperPointerAllocator).release();
65 };
66
67 auto removeJitCode = [](Heap::RegExp *regexp) {
68 delete regexp->jitCode;
69 regexp->jitCode = nullptr;
70 regexp->jitFailed = true;
71 };
72
73 auto removeByteCode = [](Heap::RegExp *regexp) {
74 delete regexp->byteCode;
75 regexp->byteCode = nullptr;
76 };
77
78 if (!priv->jitCode) {
79
80 // Long strings count as more calls. We want the JIT to run earlier.
81 const bool longString = string.length() > LongStringJitThreshold;
82 if (longString)
83 priv->interpreterCallCount += LongStringJitBoost;
84
85 if (priv->internalClass->engine->canJIT(priv)) {
86 removeByteCode(priv);
87
88 JSC::Yarr::ErrorCode error = JSC::Yarr::ErrorCode::NoError;
89 const void* stackLimit = internalClass()->engine->cppStackLimit;
90 JSC::Yarr::YarrPattern yarrPattern(
91 WTF::String(*priv->pattern), jscFlags(priv->flags), error, const_cast<void *>(stackLimit));
92 if (!yarrPattern.m_containsBackreferences) {
93 priv->jitCode = new JSC::Yarr::YarrCodeBlock;
94 JSC::VM *vm = static_cast<JSC::VM *>(priv->internalClass->engine);
95 JSC::Yarr::jitCompile(yarrPattern, JSC::Yarr::Char16, vm, *priv->jitCode);
96 }
97
98 if (!priv->hasValidJITCode()) {
99 removeJitCode(priv);
100 regenerateByteCode(priv);
101 }
102 } else if (!longString) {
103 // Short strings do the regular post-increment to honor
104 // QV4_JIT_CALL_THRESHOLD.
105 ++priv->interpreterCallCount;
106 }
107 }
108#endif
109
110 WTF::String s(string);
111
112#if ENABLE(YARR_JIT)
113 if (priv->hasValidJITCode()) {
114 static const uint offsetJITFail = std::numeric_limits<unsigned>::max() - 1;
115 uint ret = JSC::Yarr::offsetNoMatch;
116#if ENABLE(YARR_JIT_ALL_PARENS_EXPRESSIONS)
117 char buffer[8192];
118 ret = uint(priv->jitCode->execute(s.characters16(), start, s.size(),
119 (int*)matchOffsets, buffer, 8192).start);
120#else
121 ret = uint(priv->jitCode->execute(s.characters16(), start, s.length(),
122 (int*)matchOffsets).start);
123#endif
124 if (ret != offsetJITFail)
125 return ret;
126
127 removeJitCode(priv);
128 // JIT failed. We need byteCode to run the interpreter.
129 Q_ASSERT(!priv->byteCode);
130 regenerateByteCode(priv);
131 }
132#endif // ENABLE(YARR_JIT)
133
134 return JSC::Yarr::interpret(byteCode(), s.characters16(), string.size(), start, matchOffsets);
135}
136
137QString RegExp::getSubstitution(const QString &matched, const QString &str, int position, const Value *captures, int nCaptures, const QString &replacement)
138{
139 QString result;
140
141 int matchedLength = matched.size();
142 Q_ASSERT(position >= 0 && position <= str.size());
143 int tailPos = position + matchedLength;
144 int seenDollar = -1;
145 for (int i = 0; i < replacement.size(); ++i) {
146 QChar ch = replacement.at(i);
147 if (seenDollar >= 0) {
148 if (ch.unicode() == '$') {
149 result += QLatin1Char('$');
150 } else if (ch.unicode() == '&') {
151 result += matched;
152 } else if (ch.unicode() == '`') {
153 result += str.left(position);
154 } else if (ch.unicode() == '\'') {
155 result += str.mid(tailPos);
156 } else if (ch.unicode() >= '0' && ch.unicode() <= '9') {
157 int n = ch.unicode() - '0';
158 if (i + 1 < replacement.size()) {
159 ch = replacement.at(i + 1);
160 if (ch.unicode() >= '0' && ch.unicode() <= '9') {
161 n = n*10 + (ch.unicode() - '0');
162 ++i;
163 }
164 }
165 if (n > 0 && n <= nCaptures) {
166 String *s = captures[n].stringValue();
167 if (s)
168 result += s->toQString();
169 } else {
170 for (int j = seenDollar; j <= i; ++j)
171 result += replacement.at(j);
172 }
173 } else {
174 result += QLatin1Char('$');
175 result += ch;
176 }
177 seenDollar = -1;
178 } else {
179 if (ch == QLatin1Char('$')) {
180 seenDollar = i;
181 continue;
182 }
183 result += ch;
184 }
185 }
186 if (seenDollar >= 0)
187 result += QLatin1Char('$');
188 return result;
189}
190
191Heap::RegExp *RegExp::create(
192 ExecutionEngine *engine, const QString &pattern, CompiledData::RegExp::Flags flags)
193{
194 RegExpCacheKey key(pattern, flags);
195
196 RegExpCache *cache = engine->regExpCache;
197 if (!cache)
198 cache = engine->regExpCache = new RegExpCache;
199
200 QV4::WeakValue &cachedValue = (*cache)[key];
201 if (QV4::RegExp *result = cachedValue.as<RegExp>())
202 return result->d();
203
204 Scope scope(engine);
205 Scoped<RegExp> result(scope, engine->memoryManager->alloc<RegExp>(engine, pattern, flags));
206
207 result->d()->cache = cache;
208 cachedValue.set(engine, result);
209
210 return result->d();
211}
212
213void Heap::RegExp::init(ExecutionEngine *engine, const QString &pattern, uint flags)
214{
215 Base::init();
216 this->pattern = new QString(pattern);
217 this->flags = flags;
218
219 JSC::Yarr::ErrorCode error = JSC::Yarr::ErrorCode::NoError;
220 const void* stackLimit = internalClass->engine->cppStackLimit;
221 JSC::Yarr::YarrPattern yarrPattern(WTF::String(pattern), jscFlags(flags), error, const_cast<void *>(stackLimit));
222 if (error != JSC::Yarr::ErrorCode::NoError)
223 return;
224 subPatternCount = yarrPattern.m_numSubpatterns;
225 Q_UNUSED(engine);
226 byteCode = JSC::Yarr::byteCompile(yarrPattern, internalClass->engine->bumperPointerAllocator).release();
227 if (byteCode)
228 valid = true;
229}
230
231void Heap::RegExp::destroy()
232{
233 if (cache) {
234 RegExpCacheKey key(this);
235 cache->remove(key);
236 }
237#if ENABLE(YARR_JIT)
238 delete jitCode;
239#endif
240 delete byteCode;
241 delete pattern;
242 Base::destroy();
243}
DEFINE_MANAGED_VTABLE(RegExp)
static JSC::RegExpFlags jscFlags(quint8 flags)
Definition qv4regexp.cpp:19