Skip to content

Commit

Permalink
[release/7.0] JIT: Fix segfault for modulo computations involving add…
Browse files Browse the repository at this point in the history
…ress-of expressions (#76171)

* Add a test

* JIT: Use gtCloneExpr in fgMorphModToSubMulDiv for potentially complex trees

We may get here for any invariant dividend/divisor but these can be
'complex' address-of trees that gtClone does not handle.

Fix #76051

* Fix test build

Co-authored-by: Jakob Botsch Nielsen <jakob.botsch.nielsen@gmail.com>
  • Loading branch information
github-actions[bot] and jakobbotsch authored Sep 28, 2022
1 parent 0b0c060 commit a2058b5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/coreclr/jit/morph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13494,8 +13494,8 @@ GenTree* Compiler::fgMorphModToSubMulDiv(GenTreeOp* tree)
GenTree* dividend = div->IsReverseOp() ? opB : opA;
GenTree* divisor = div->IsReverseOp() ? opA : opB;

div->gtOp1 = gtClone(dividend);
div->gtOp2 = gtClone(divisor);
div->gtOp1 = gtCloneExpr(dividend);
div->gtOp2 = gtCloneExpr(divisor);

var_types type = div->gtType;
GenTree* const mul = gtNewOperNode(GT_MUL, type, div, divisor);
Expand Down
19 changes: 19 additions & 0 deletions src/tests/JIT/Regression/JitBlue/Runtime_76051/Runtime_76051.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.CompilerServices;

public class Runtime_76051
{
public static int Main(string[] args)
{
GetIndex(1);
return 100;
}

// This tests an assertion failure (debug)/segfault (release) in
// fgMorphModToSubMulDiv due to the use of gtClone that fails for the
// complex tree seen for the address-of expression.
[MethodImpl(MethodImplOptions.NoInlining)]
static unsafe int GetIndex(uint cellCount) => (int)((ulong)&cellCount % cellCount);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<Optimize>True</Optimize>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
</ItemGroup>
</Project>

0 comments on commit a2058b5

Please sign in to comment.