Skip to content

[DXIL] Avoid infinite loop / stack overflow on invalid instruction - #7048

Merged
Joshua Batista (bob80905) merged 2 commits into
microsoft:mainfrom
bob80905:prevent_stack_overflow00
Jan 14, 2025
Merged

Joshua Batista (bob80905) merged 2 commits into
microsoft:mainfrom
bob80905:prevent_stack_overflow00

Conversation

@bob80905

Copy link
Copy Markdown
Collaborator

The simplify instruction pass has a function called findScalarElement.
This function will possibly recurse infinitely into the first operand of the instruction that it is passed. If this instruction, for example, is %2 = insertelement <2 x i32> %2, i32 %sub, i32 0
Then the pass will recurse into the first operand of the insertelement instruction, which is the same insertelement instruction, and so on. This PR prevents infinite recursion and stack overflow by bailing out of the function early if there is a detection of the same value in the first operand.
Fixes #7034

@bob80905
Joshua Batista (bob80905) requested a review from a team as a code owner January 6, 2025 21:04
@github-actions

github-actions Bot commented Jan 6, 2025 •

Copy link
Copy Markdown
Contributor

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff a8a4e98a2367080af683c48feedd7f7481a31a96 d5a7b5f3df4ffe8289fb1d808844afe753a57147 -- lib/Analysis/VectorUtils2.cpp
View the diff from clang-format here.
diff --git a/lib/Analysis/VectorUtils2.cpp b/lib/Analysis/VectorUtils2.cpp
index b5f57e3b..97bae15a 100644
--- a/lib/Analysis/VectorUtils2.cpp
+++ b/lib/Analysis/VectorUtils2.cpp
@@ -42,7 +42,7 @@ llvm::Value *llvm::findScalarElement(llvm::Value *V, unsigned EltNo) {
     if (EltNo == IIElt)
       return III->getOperand(1);
 
-// Guard against infinite loop on malformed, unreachable IR.
+    // Guard against infinite loop on malformed, unreachable IR.
     if (III == III->getOperand(0))
       return nullptr;
 
  • Check this box to apply formatting changes to this branch.

@tex3d Tex Riddell (tex3d) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I still worry about the root cause of a malformed insertelement instruction, but this is a good guard against the downstream effect.

@bob80905
Joshua Batista (bob80905) merged commit 5bf6f77 into microsoft:main Jan 14, 2025
Mxr (SjMxr233) pushed a commit to ShaderHelper/DirectXShaderCompiler that referenced this pull request Jul 24, 2025
…icrosoft#7048)

The simplify instruction pass has a function called `findScalarElement`.
This function will possibly recurse infinitely into the first operand of
the instruction that it is passed. If this instruction, for example, is
`%2 = insertelement <2 x i32> %2, i32 %sub, i32 0`
Then the pass will recurse into the first operand of the insertelement
instruction, which is the same insertelement instruction, and so on.
This PR prevents infinite recursion and stack overflow by bailing out of
the function early if there is a detection of the same value in the
first operand.
Fixes microsoft#7034

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Stack overflow when compiling for cs_6_6

3 participants