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
qqmljsshadowcheck.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3// Qt-Security score:significant
4
6
8
9using namespace Qt::StringLiterals;
10
11/*!
12 * \internal
13 * \class QQmlJSShadowCheck
14 *
15 * This pass looks for possible shadowing when accessing members of QML-exposed
16 * types. A member can be shadowed if a non-final property is re-declared in a
17 * derived class. As the QML engine will always pick up the most derived variant
18 * of that property, we cannot rely on any property of a type to be actually
19 * accessible, unless one of a few special cases holds:
20 *
21 * 1. We are dealing with a direct scope lookup, without an intermediate object.
22 * Such lookups are protected from shadowing. For example "property int a: b"
23 * always works.
24 * 2. The object we are retrieving the property from is identified by an ID, or
25 * an attached property or a singleton. Such objects cannot be replaced.
26 * Therefore we can be sure to see all the type information at compile time.
27 * 3. The property is declared final.
28 * 4. The object we are retrieving the property from is a value type. Value
29 * types cannot be used polymorphically.
30 *
31 * If the property is potentially shadowed, we can still retrieve it, but we
32 * don't know its type. We should assume "var" then.
33 *
34 * All of the above also holds for methods. There we have to transform the
35 * arguments and return types into "var".
36 */
37
38QQmlJSCompilePass::BlocksAndAnnotations QQmlJSShadowCheck::run(const Function *function)
39{
40 m_function = function;
41 m_state = initialState(function);
42 decode(m_function->code.constData(), static_cast<uint>(m_function->code.size()));
43
44 for (const auto &store : std::as_const(m_resettableStores))
45 checkResettable(store.accumulatorIn, store.instructionOffset);
46
47 // Re-check all base types. We may have made them var after detecting them.
48 for (const auto &base : std::as_const(m_baseTypes)) {
49 if (checkBaseType(base) == Shadowable)
50 break;
51 }
52
53 return { std::move(m_basicBlocks), std::move(m_annotations) };
54}
55
56void QQmlJSShadowCheck::generate_LoadProperty(int nameIndex)
57{
58 if (!m_state.readsRegister(Accumulator))
59 return; // enum lookup cannot be shadowed.
60
61 auto accumulatorIn = m_state.registers.find(Accumulator);
62 if (accumulatorIn != m_state.registers.end()) {
63 checkShadowing(
64 accumulatorIn.value().content, m_jsUnitGenerator->stringForIndex(nameIndex),
65 Accumulator);
66 }
67}
68
69void QQmlJSShadowCheck::generate_GetLookup(int index)
70{
71 if (!m_state.readsRegister(Accumulator))
72 return; // enum lookup cannot be shadowed.
73
74 auto accumulatorIn = m_state.registers.find(Accumulator);
75 if (accumulatorIn != m_state.registers.end()) {
76 checkShadowing(
77 accumulatorIn.value().content, m_jsUnitGenerator->lookupName(index), Accumulator);
78 }
79}
80
81void QQmlJSShadowCheck::generate_GetOptionalLookup(int index, int offset)
82{
83 Q_UNUSED(offset);
84 generate_GetLookup(index);
85}
86
87void QQmlJSShadowCheck::handleStore(int base, const QString &memberName)
88{
89 const int instructionOffset = currentInstructionOffset();
90 QQmlJSRegisterContent readAccumulator
91 = m_annotations[instructionOffset].readRegisters[Accumulator].content;
92 const auto baseType = m_state.registers[base].content;
93
94 // If the accumulator is already read as var, we don't have to do anything.
95 if (readAccumulator.contains(m_typeResolver->varType())) {
96 if (checkBaseType(baseType) == NotShadowable)
97 m_baseTypes.append(baseType);
98 return;
99 }
100
101 if (checkShadowing(baseType, memberName, base) == Shadowable)
102 return;
103
104 // If the property isn't shadowable, we have to turn the read register into
105 // var if the accumulator can hold undefined. This has to be done in a second pass
106 // because the accumulator may still turn into var due to its own shadowing.
107 const QQmlJSRegisterContent member = m_typeResolver->memberType(baseType, memberName);
108 if (member.isProperty())
109 m_resettableStores.append({m_state.accumulatorIn(), instructionOffset});
110}
111
112void QQmlJSShadowCheck::generate_StoreProperty(int nameIndex, int base)
113{
114 handleStore(base, m_jsUnitGenerator->stringForIndex(nameIndex));
115}
116
117void QQmlJSShadowCheck::generate_SetLookup(int index, int base)
118{
119 handleStore(base, m_jsUnitGenerator->lookupName(index));
120}
121
122void QQmlJSShadowCheck::generate_CallProperty(int nameIndex, int base, int argc, int argv)
123{
124 Q_UNUSED(argc);
125 Q_UNUSED(argv);
126 checkShadowing(m_state.registers[base].content, m_jsUnitGenerator->lookupName(nameIndex), base);
127}
128
129void QQmlJSShadowCheck::generate_CallPropertyLookup(int nameIndex, int base, int argc, int argv)
130{
131 Q_UNUSED(argc);
132 Q_UNUSED(argv);
133 checkShadowing(m_state.registers[base].content, m_jsUnitGenerator->lookupName(nameIndex), base);
134}
135
136QV4::Moth::ByteCodeHandler::Verdict QQmlJSShadowCheck::startInstruction(QV4::Moth::Instr::Type)
137{
138 m_state = nextStateFromAnnotations(m_state, m_annotations);
139 return (m_state.hasInternalSideEffects() || m_state.changedRegisterIndex() != InvalidRegister)
140 ? ProcessInstruction
141 : SkipInstruction;
142}
143
144void QQmlJSShadowCheck::endInstruction(QV4::Moth::Instr::Type)
145{
146}
147
148QQmlJSShadowCheck::Shadowability QQmlJSShadowCheck::checkShadowing(
149 QQmlJSRegisterContent baseType, const QString &memberName, int baseRegister)
150{
151 if (checkBaseType(baseType) == Shadowable)
152 return Shadowable;
153 else
154 m_baseTypes.append(baseType);
155
156 // JavaScript objects are not shadowable, as far as we can analyze them at all.
157 if (baseType.containedType()->isJavaScriptBuiltin())
158 return NotShadowable;
159
160 if (!baseType.containedType()->isReferenceType())
161 return NotShadowable;
162
163 switch (baseType.variant()) {
164 case QQmlJSRegisterContent::Singleton:
165 // composite singletons can't have a create() function. The create() function
166 // is what allows a derived class to be returned.
167 if (baseType.containedType()->isComposite())
168 return NotShadowable;
169
170 // Extended singletons aren't shadowable. There is no space for the extra members
171 // in between the type's metaobject and the extension's metaobject. The derived type
172 // can't be known in advance since you can only produce it through a factory function
173 // that covertly returns a derived type rather than the declared one.
174 if (baseType.containedType()->extensionType().extensionSpecifier
175 != QQmlJSScope::NotExtension) {
176 return NotShadowable;
177 }
178
179 Q_FALLTHROUGH();
180 case QQmlJSRegisterContent::Attachment:
181 // The attached object factory can return an instance of a type derived from the declared
182 // attached type, just like a singleton factory. Members can therefore be shadowed.
183 case QQmlJSRegisterContent::MethodCall:
184 case QQmlJSRegisterContent::Property:
185 case QQmlJSRegisterContent::TypeByName:
186 case QQmlJSRegisterContent::Cast:
187 case QQmlJSRegisterContent::Unknown: {
188 const QQmlJSRegisterContent member = m_typeResolver->memberType(baseType, memberName);
189
190 // You can have something like parent.QtQuick.Screen.pixelDensity
191 // In that case "QtQuick" cannot be resolved as member type and we would later have to look
192 // for "QtQuick.Screen" instead.
193 if (!member.isValid()) {
194 Q_ASSERT(m_typeResolver->isPrefix(memberName));
195 return NotShadowable;
196 }
197
198 if (member.isProperty()) {
199 if (member.property().isFinal())
200 return NotShadowable; // final properties can't be shadowed
201 } else if (!member.isMethod()) {
202 return NotShadowable; // Only properties and methods can be shadowed
203 }
204
205 const QQmlJS::LoggerWarningId category = m_logger->wasCategoryChanged(qmlShadowableMember)
206 ? qmlShadowableMember
207 : qmlCompiler;
208
209 m_logger->log(
210 u"Member %1 of %2 can be shadowed"_s.arg(memberName, baseType.descriptiveName()),
211 category, currentSourceLocation());
212
213 // Make it "var". We don't know what it is.
214 const QQmlJSScope::ConstPtr varType = m_typeResolver->varType();
215 InstructionAnnotation &currentAnnotation = m_annotations[currentInstructionOffset()];
216 currentAnnotation.isShadowable = true;
217
218 if (currentAnnotation.changedRegisterIndex != InvalidRegister) {
219 m_typeResolver->adjustOriginalType(currentAnnotation.changedRegister, varType);
220 m_adjustedTypes.insert(currentAnnotation.changedRegister);
221 }
222
223 for (auto it = currentAnnotation.readRegisters.begin(),
224 end = currentAnnotation.readRegisters.end();
225 it != end; ++it) {
226 if (it.key() != baseRegister)
227 it->second.content = m_typeResolver->convert(it->second.content, varType);
228 }
229
230 return Shadowable;
231 }
232 default:
233 // In particular ObjectById is fine as that cannot change into something else.
234 return NotShadowable;
235 }
236}
237
238void QQmlJSShadowCheck::checkResettable(
239 QQmlJSRegisterContent accumulatorIn, int instructionOffset)
240{
241 if (!m_typeResolver->canHoldUndefined(accumulatorIn))
242 return;
243
244 QQmlJSRegisterContent &readAccumulator
245 = m_annotations[instructionOffset].readRegisters[Accumulator].content;
246 readAccumulator = m_typeResolver->convert(readAccumulator, m_typeResolver->varType());
247}
248
249QQmlJSShadowCheck::Shadowability QQmlJSShadowCheck::checkBaseType(
250 QQmlJSRegisterContent baseType)
251{
252 if (!m_adjustedTypes.contains(baseType))
253 return NotShadowable;
254 addError(u"Cannot use shadowable base type for further lookups: %1"_s.arg(baseType.descriptiveName()));
255 return Shadowable;
256}
257
258QT_END_NAMESPACE
Combined button and popup list for selecting options.