Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for issue 40444 - Thread.MemoryBarrier() should prevent hoisting of memory reads #40821

Merged
merged 1 commit into from
Aug 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/coreclr/src/jit/optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7920,10 +7920,11 @@ bool Compiler::optComputeLoopSideEffectsOfBlock(BasicBlock* blk)
}
break;

case GT_LOCKADD: // Binop
case GT_XADD: // Binop
case GT_XCHG: // Binop
case GT_CMPXCHG: // Specialop
case GT_LOCKADD:
case GT_XADD:
case GT_XCHG:
case GT_CMPXCHG:
case GT_MEMORYBARRIER:
{
assert(!tree->OperIs(GT_LOCKADD) && "LOCKADD should not appear before lowering");
memoryHavoc |= memoryKindSet(GcHeap, ByrefExposed);
Expand Down
66 changes: 66 additions & 0 deletions src/tests/JIT/Regression/JitBlue/Runtime_40444/Runtime_40444.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Threading;
using System.Runtime.CompilerServices;

class Runtime_40444
{

public static int t2_result = 0;
public static int t2_finished;

static void Thread2()
{
t2_result++;
t2_finished = 1;
}

// [MethodImpl(MethodImplOptions.NoInlining)]
static int TestVolatileRead(ref int address)
{
int ret = address;
Thread.MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
return ret;
}

static bool Test()
{
bool result = false;
t2_finished = 0;

// Run Thread2() in a new thread
new Thread(new ThreadStart(Thread2)).Start();

// Wait for Thread2 to signal that it has a result by setting
// t2_finished to 1.
for (int i=0; i<10000000; i++)
{
if (TestVolatileRead(ref t2_finished)==1)
{
Console.WriteLine("{1}: result = {0}", t2_result, i);
result = true;
break;
}
}
if (result == false)
{
Console.WriteLine("FAILED");
}
return result;
}

static int Main()
{
for (int i=0; i<100; i++)
{
if (!Test())
{
return -1;
}
}
Console.WriteLine("Passed");
return t2_result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
</PropertyGroup>
<PropertyGroup>
<DebugType />
<Optimize>True</Optimize>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
</ItemGroup>
</Project>