createGrid consumes excessive memory and crashes on pages with extremely large elements
Product
axe-core
Product Version
4.6.3
Latest Version
- [X] I have tested the issue with the latest version of the product
Issue Description
Overview
Today, createGrid understands to avoid expanding grids for elements which are not visible:
https://github.com/dequelabs/axe-core/blob/d4522cdd7a90018336098f9b2119e353bd5a71ee/lib/commons/dom/create-grid.js#L87-L93
However, for elements which are partially visible and extremely large, this check isn't quite enough to avoid an undesirable explosion of grid size.
The motivating example is the Monaco editor. To render its main content area's background, it uses an element which is styled with width: 1e6; height: 1e6 (the linked playground page contains 3 editor areas, so it has 3 such elements).
The presence of these elements causes axe-core scans to eat up an enormous amount of memory (~2.5GB) and time (~4s wall time on my dev box), and often causes the whole target page to crash. Almost all of the perf hit comes from call stack createGrid > addNodeToGrid > loopGridPosition > loopNegativeIndexMatrix > loopNegativeIndexMatrix, where axe-core is allocating a separate JS object for each cell of a roughly 5000x5000 grid, almost every cell of which is not visible on screen.
I would imagine that making createGrid cooperate with elements like this would probably require coming up with a means to calculate the specific clipping of an element's clientRect(s) which are visible on screen and only add that to the grid, rather than adding the entire clientRect(s) whenever any part of the element is visible. I bet that's trickier than it sounds, though...
Expectation
axe-core should be able to scan the repro page in question with performance comparable to [email protected]
Actual
Scanning the repro page takes ~2.5GB JS heap, ~4s wall time on my dev box, and often crashes the page entirely
How to Reproduce
- In Chromium, open the Monaco editor
- Open the F12 dev tools' "Performance" tab and start a trace
- Perform an axe-core scan (eg, from F12 dev console,
await fetch("https://cdnjs.cloudflare.com/ajax/libs/axe-core/4.6.3/axe.js").then(r => r.text()).then(src => eval(src)); await axe.run();) - Stop the performance trace and observe results
Additional context
Regression introduced between 4.4.3 to 4.5.0 (presumably with the addition of the new grid mechanism)
Wow. I never expected such large elements. I'll have to do some investigating on that one. Thanks for pinpointing the issue for us.
FWIW, I've found that this function is relied on by the rules "color-contrast", "color-contrast-enhanced", and "target-size".
If a complete fix isn't coming soon it would be nice to get a check added. The Monaco editor is fairly widely used so this is hampering our testers when they try to evaluate pages in our application. Perhaps an additional check that if the width * height is larger than some number you return a warning saying you could not evaluate the element.
We looked into this and I think we've come up with a plan. The Monaco editor uses the .monaco-editor-background element (which is the 1e6 width x height element) for each of the code editors (javascript, html, and css) and the rendered output. Each of these elements is contained within an overflow: hidden ancestor (.editor-scrollable) which is the true size of the editor.
What we decided to do is to trim an elements bounding box to be the size of the non-overflow area of all it's ancestors. That way we only add to the grid when that grid cell is visible. We are already doing this for color-contrast when we get the bounding box of text nodes in get-visible-child-text-rects so we can reuse a lot of that code to determine the rect size of the element to add to the grid.
@straker - thanks for looking into this. The fix sounds reasonable, and we'll be looking forward to the next release!
Validated the ticket using latest axe-core develop branch code base
according to the fix , if excessive memory & page crash happens with extreamly large elements,
-- color-contrast issue should be in violations
but seeing it in incomplete
@straker , can you please confirm this is expected
used the code -
<div style="overflow: hidden; width: 300px; height: 300px;">
<span id="target" style="display: inline-block; width: 1000px; height: 1000px;">' +
'Hello world</span>
</div>
<style>
* {
box-sizing: border-box;
}
html,
body {
padding: 0;
margin: 0;
height: 100%;
background: #fff;
color: #000;
}
</style>
</head>
<body>
<div id="fixture">
<div style="overflow: hidden; width: 200px; height: 200px">
<!-- browsers each have their own CSS size limit so this number (which should be greater than the limit) will be capped to that -->
<div style="width: 1e9px; height: 1e9px">Hello World</div>
</div>
</div>
Verified with the latest axe-core develop branch code base,
axe is crashing with the code snippet <div style="width: 1e9px; height: 1e9px">Hello World</div> when it is consuming excessive memory, but we wrap the element in a parent div that restricts the size, so,
https://github.com/dequelabs/axe-core/assets/47409579/d3cd252a-3ea5-4fe2-9944-12b0e3fff765
Code block:
<div style="overflow: hidden; width: 200px; height: 200px">
<!-- browsers each have their own CSS size limit so this number (which should be greater than the limit) will be capped to that -->
<div style="width: 1e9px; height: 1e9px">Hello World</div>
</div>
and color-contrast is passing herre not crashing
Environment:
| Label | Value |
|---|---|
| Product | axe-core |
| Version | 4.7.2-canary.49eaa0e |
| OS-Details | _MAC- Intel Core i7 - 11.6.8 _ |
| BrowserDetails | Chrome Version 115.0.5790.170 (Official Build) (64-bit) |