Bug 1692937 [wpt PR 27636] - new parameter --include-file for wptrunner, a=testonly
[gecko.git] / layout / svg / AutoReferenceChainGuard.h
blobc25ba0ea703a4b1f86daef460fc6343021797e10
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef LAYOUT_SVG_AUTOREFERENCECHAINGUARD_H_
8 #define LAYOUT_SVG_AUTOREFERENCECHAINGUARD_H_
10 #include "Element.h"
11 #include "mozilla/Assertions.h"
12 #include "mozilla/Attributes.h"
13 #include "mozilla/ReentrancyGuard.h"
14 #include "mozilla/Likely.h"
15 #include "nsDebug.h"
16 #include "mozilla/dom/Document.h"
17 #include "nsIFrame.h"
19 namespace mozilla {
21 /**
22 * This helper class helps us to protect against two related issues that can
23 * occur in SVG content: reference loops, and reference chains that we deem to
24 * be too long.
26 * Some SVG effects can reference another effect of the same type to produce
27 * a chain of effects to be applied (e.g. clipPath), while in other cases it is
28 * possible that while processing an effect of a certain type another effect
29 * of the same type may be encountered indirectly (e.g. pattern). In order to
30 * avoid stack overflow crashes and performance issues we need to impose an
31 * arbitrary limit on the length of the reference chains that SVG content may
32 * try to create. (Some SVG authoring tools have been known to create absurdly
33 * long reference chains. For example, bug 1253590 details a case where Adobe
34 * Illustrator was used to created an SVG with a chain of 5000 clip paths which
35 * could cause us to run out of stack space and crash.)
37 * This class is intended to be used with the nsIFrame's of SVG effects that
38 * may involve reference chains. To use it add a boolean member, something
39 * like this:
41 * // Flag used to indicate whether a methods that may reenter due to
42 * // following a reference to another instance is currently executing.
43 * bool mIsBeingProcessed;
45 * Make sure to initialize the member to false in the class' constructons.
47 * Then add the following to the top of any methods that may be reentered due
48 * to following a reference:
50 * static int16_t sRefChainLengthCounter = AutoReferenceChainGuard::noChain;
52 * AutoReferenceChainGuard refChainGuard(this, &mIsBeingProcessed,
53 * &sRefChainLengthCounter);
54 * if (MOZ_UNLIKELY(!refChainGuard.Reference())) {
55 * return; // Break reference chain
56 * }
58 * Note that mIsBeingProcessed and sRefChainLengthCounter should never be used
59 * by the frame except when it initialize them as indicated above.
61 class MOZ_RAII AutoReferenceChainGuard {
62 static const int16_t sDefaultMaxChainLength = 10; // arbitrary length
64 public:
65 static const int16_t noChain = -2;
67 /**
68 * @param aFrame The frame for an effect that may involve a reference chain.
69 * @param aFrameInUse The member variable on aFrame that is used to indicate
70 * whether one of aFrame's methods that may involve following a reference
71 * to another effect of the same type is currently being executed.
72 * @param aChainCounter A static variable in the method in which this class
73 * is instantiated that is used to keep track of how many times the method
74 * is reentered (and thus how long the a reference chain is).
75 * @param aMaxChainLength The maximum number of links that are allowed in
76 * a reference chain.
78 AutoReferenceChainGuard(nsIFrame* aFrame, bool* aFrameInUse,
79 int16_t* aChainCounter,
80 int16_t aMaxChainLength = sDefaultMaxChainLength)
81 : mFrame(aFrame),
82 mFrameInUse(aFrameInUse),
83 mChainCounter(aChainCounter),
84 mMaxChainLength(aMaxChainLength),
85 mBrokeReference(false) {
86 MOZ_ASSERT(aFrame && aFrameInUse && aChainCounter);
87 MOZ_ASSERT(aMaxChainLength > 0);
88 MOZ_ASSERT(*aChainCounter == noChain ||
89 (*aChainCounter >= 0 && *aChainCounter < aMaxChainLength));
92 ~AutoReferenceChainGuard() {
93 if (mBrokeReference) {
94 // We didn't change mFrameInUse or mChainCounter
95 return;
98 *mFrameInUse = false;
100 // If we fail this assert then there were more destructor calls than
101 // Reference() calls (a consumer forgot to to call Reference()), or else
102 // someone messed with the variable pointed to by mChainCounter.
103 MOZ_ASSERT(*mChainCounter < mMaxChainLength);
105 (*mChainCounter)++;
107 if (*mChainCounter == mMaxChainLength) {
108 *mChainCounter = noChain; // reset ready for use next time
113 * Returns true on success (no reference loop/reference chain length is
114 * within the specified limits), else returns false on failure (there is a
115 * reference loop/the reference chain has exceeded the specified limits).
116 * If it returns false then an error message will be reported to the DevTools
117 * console (only once).
119 [[nodiscard]] bool Reference() {
120 if (MOZ_UNLIKELY(*mFrameInUse)) {
121 mBrokeReference = true;
122 ReportErrorToConsole();
123 return false;
126 if (*mChainCounter == noChain) {
127 // Initialize - we start at aMaxChainLength and decrement towards zero.
128 *mChainCounter = mMaxChainLength;
129 } else {
130 // If we fail this assertion then either a consumer failed to break a
131 // reference loop/chain, or else they called Reference() more than once
132 MOZ_ASSERT(*mChainCounter >= 0);
134 if (MOZ_UNLIKELY(*mChainCounter < 1)) {
135 mBrokeReference = true;
136 ReportErrorToConsole();
137 return false;
141 // We only set these once we know we're returning true.
142 *mFrameInUse = true;
143 (*mChainCounter)--;
145 return true;
148 private:
149 void ReportErrorToConsole() {
150 AutoTArray<nsString, 2> params;
151 dom::Element* element = mFrame->GetContent()->AsElement();
152 element->GetTagName(*params.AppendElement());
153 element->GetId(*params.AppendElement());
154 auto doc = mFrame->GetContent()->OwnerDoc();
155 auto warning = *mFrameInUse ? dom::Document::eSVGRefLoop
156 : dom::Document::eSVGRefChainLengthExceeded;
157 doc->WarnOnceAbout(warning, /* asError */ true, params);
160 nsIFrame* mFrame;
161 bool* mFrameInUse;
162 int16_t* mChainCounter;
163 const int16_t mMaxChainLength;
164 bool mBrokeReference;
167 } // namespace mozilla
169 #endif // LAYOUT_SVG_AUTOREFERENCECHAINGUARD_H_