Skip to content

Commit

Permalink
JIT: Make sure static ctor flag check uses a volatile load (dotnet#10…
Browse files Browse the repository at this point in the history
…5832)

Otherwise the hardware is allowed to reorder loads of its static fields
to happen before the initialization check.
  • Loading branch information
jakobbotsch authored Aug 4, 2024
1 parent 1cc0186 commit dee8a8b
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/coreclr/jit/helperexpansion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1417,7 +1417,7 @@ bool Compiler::fgExpandStaticInitForCall(BasicBlock** pBlock, Statement* stmt, G

// Don't fold ADD(CNS1, CNS2) here since the result won't be reloc-friendly for AOT
GenTree* offsetNode = gtNewOperNode(GT_ADD, TYP_I_IMPL, baseAddr, gtNewIconNode(isInitOffset));
isInitedActualValueNode = gtNewIndir(TYP_I_IMPL, offsetNode, GTF_IND_NONFAULTING);
isInitedActualValueNode = gtNewIndir(TYP_I_IMPL, offsetNode, GTF_IND_NONFAULTING | GTF_IND_VOLATILE);

// 0 means "initialized" on NativeAOT
isInitedExpectedValue = gtNewIconNode(0, TYP_I_IMPL);
Expand All @@ -1427,6 +1427,8 @@ bool Compiler::fgExpandStaticInitForCall(BasicBlock** pBlock, Statement* stmt, G
assert(isInitOffset == 0);

isInitedActualValueNode = gtNewIndOfIconHandleNode(TYP_INT, (size_t)flagAddr.addr, GTF_ICON_GLOBAL_PTR, false);
isInitedActualValueNode->gtFlags |= GTF_IND_VOLATILE;
isInitedActualValueNode->SetHasOrderingSideEffect();

// Check ClassInitFlags::INITIALIZED_FLAG bit
isInitedActualValueNode = gtNewOperNode(GT_AND, TYP_INT, isInitedActualValueNode, gtNewIconNode(1));
Expand Down

0 comments on commit dee8a8b

Please sign in to comment.