-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Fix a race condition in GCStress #41540
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -744,23 +744,37 @@ void replaceSafePointInstructionWithGcStressInstr(UINT32 safePointOffset, LPVOID | |
| //Determine if instruction before the safe point is call using immediate (BLX Imm) or call by register (BLX Rm) | ||
| BOOL instructionIsACallThroughRegister = FALSE; | ||
| BOOL instructionIsACallThroughImmediate = FALSE; | ||
|
|
||
| #if defined(TARGET_ARM) | ||
|
|
||
| // POSSIBLE BUG: Note that we are looking backwards by 2 or 4 bytes, looking for particular call instruction encodings. | ||
| // However, we don't know if the previous instruction is 2 bytes or 4 bytes. Looking back 2 bytes could be looking into | ||
| // the middle of a 4-byte instruction. The only safe way to do this is by walking forward from the first instruction of | ||
| // the function. | ||
|
|
||
| // call by register instruction is two bytes (BL<c> Reg T1 encoding) | ||
| WORD instr = *((WORD*)savedInstrPtr - 1); | ||
|
|
||
| instr = instr & 0xff87; | ||
| if((instr ^ 0x4780) == 0) | ||
| if ((instr ^ 0x4780) == 0) | ||
| { | ||
| // It is call by register | ||
| instructionIsACallThroughRegister = TRUE; | ||
| } | ||
| else | ||
| { | ||
| // call using immediate instructions are 4 bytes (BL<c> <label> T1 encoding) | ||
| instr = *((WORD*)savedInstrPtr - 2); | ||
| instr = instr & 0xf800; | ||
| if ((instr ^ 0xf000) == 0) | ||
| { | ||
| if ((*(((WORD*)savedInstrPtr) - 1) & 0xd000) == 0xd000) | ||
| { | ||
| // It is call by immediate | ||
| instructionIsACallThroughImmediate = TRUE; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // call using immediate instructions are 4 bytes (BL<c> <label> T1 encoding) | ||
| instr = *((WORD*)savedInstrPtr - 2); | ||
| instr = instr & 0xf800; | ||
| if((instr ^ 0xf000) == 0) | ||
| if((*(((WORD*)savedInstrPtr)-1) & 0xd000) == 0xd000) | ||
| // It is call by immediate | ||
| instructionIsACallThroughImmediate = TRUE; | ||
| #elif defined(TARGET_ARM64) | ||
| DWORD instr = *((DWORD*)savedInstrPtr - 1); | ||
|
|
||
|
|
@@ -778,6 +792,7 @@ void replaceSafePointInstructionWithGcStressInstr(UINT32 safePointOffset, LPVOID | |
| instructionIsACallThroughRegister = TRUE; | ||
| } | ||
| #endif // _TARGET_XXXX_ | ||
|
|
||
| // safe point must always be after a call instruction | ||
| // and cannot be both call by register & immediate | ||
| // The safe points are also marked at jump calls( a special variant of | ||
|
|
@@ -1383,16 +1398,15 @@ void DoGcStress (PCONTEXT regs, NativeCodeVersion nativeCodeVersion) | |
| Thread *pThread = GetThread(); | ||
| _ASSERTE(pThread); | ||
|
|
||
| if (!IsGcCoverageInterruptInstruction(instrPtr)) | ||
| { | ||
| // This assert can fail if another thread changed original instruction to | ||
| // GCCoverage Interrupt instruction between these two commands. Uncomment it | ||
| // when threading issue gets resolved. | ||
| // _ASSERTE(IsOriginalInstruction(instrPtr, gcCover, offset)); | ||
|
|
||
| // Someone beat us to it, just go on running. | ||
| return; | ||
| } | ||
| // There is a race condition with the computation of `atCall`. Multiple threads could enter | ||
| // this function (DoGcStress) at the same time. If one reads `*instrPtr` and sets `atCall` | ||
| // to `true`, it will proceed to, lower down in this function, call `pThread->CommitGCStressInstructionUpdate()` | ||
| // to replace the GCStress instruction at the call back to the original call instruction. | ||
| // Other threads could then read `*instrPtr` and see the actual call instruction instead of the | ||
| // call-specific GCStress instruction (INTERRUPT_INSTR_CALL[_32]). If `atCall` is set to false as | ||
| // a result, then we'll do a GCStress as if this is a fully-interruptible code site, which is isn't, | ||
| // which can leads to asserts (or, presumably, other failures). So, we have to check | ||
| // `if (!IsGcCoverageInterruptInstruction(instrPtr))` after we read `*instrPtr`. | ||
|
|
||
| bool atCall; | ||
| bool afterCallProtect[2] = { false, false }; | ||
|
|
@@ -1418,14 +1432,15 @@ void DoGcStress (PCONTEXT regs, NativeCodeVersion nativeCodeVersion) | |
| } | ||
|
|
||
| #elif defined(TARGET_ARM) | ||
|
|
||
| forceStack[6] = (WORD*)instrPtr; // This is so I can see it fastchecked | ||
|
|
||
| size_t instrLen = GetARMInstructionLength(instrPtr); | ||
|
|
||
| if (instrLen == 2) | ||
| { | ||
| WORD instrVal = *(WORD*)instrPtr; | ||
| atCall = (instrVal == INTERRUPT_INSTR_CALL); | ||
| atCall = (instrVal == INTERRUPT_INSTR_CALL); | ||
| afterCallProtect[0] = (instrVal == INTERRUPT_INSTR_PROTECT_RET); | ||
| } | ||
| else | ||
|
|
@@ -1434,10 +1449,12 @@ void DoGcStress (PCONTEXT regs, NativeCodeVersion nativeCodeVersion) | |
|
|
||
| DWORD instrVal32 = *(DWORD*)instrPtr; | ||
|
|
||
| atCall = (instrVal32 == INTERRUPT_INSTR_CALL_32); | ||
| atCall = (instrVal32 == INTERRUPT_INSTR_CALL_32); | ||
| afterCallProtect[0] = (instrVal32 == INTERRUPT_INSTR_PROTECT_RET_32); | ||
| } | ||
|
|
||
| #elif defined(TARGET_ARM64) | ||
|
|
||
| DWORD instrVal = *(DWORD *)instrPtr; | ||
| forceStack[6] = &instrVal; // This is so I can see it fastchecked | ||
|
|
||
|
|
@@ -1446,6 +1463,17 @@ void DoGcStress (PCONTEXT regs, NativeCodeVersion nativeCodeVersion) | |
|
|
||
| #endif // _TARGET_* | ||
|
|
||
| if (!IsGcCoverageInterruptInstruction(instrPtr)) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the real fix: moving this code here from above the |
||
| { | ||
| // This assert can fail if another thread changed original instruction to | ||
| // GCCoverage Interrupt instruction between these two commands. Uncomment it | ||
| // when threading issue gets resolved. | ||
| // _ASSERTE(IsOriginalInstruction(instrPtr, gcCover, offset)); | ||
|
|
||
| // Someone beat us to it, just go on running. | ||
| return; | ||
| } | ||
|
|
||
| #ifdef TARGET_X86 | ||
| /* are we at the very first instruction? If so, capture the register state */ | ||
| bool bShouldUpdateProlog = true; | ||
|
|
@@ -1553,18 +1581,18 @@ void DoGcStress (PCONTEXT regs, NativeCodeVersion nativeCodeVersion) | |
|
|
||
| #if defined(TARGET_X86) || defined(TARGET_AMD64) || defined(TARGET_ARM) || defined(TARGET_ARM64) | ||
|
|
||
| /* In non-fully interrruptable code, if the EIP is just after a call instr | ||
| /* In non-fully interruptible code, if the EIP is just after a call instr | ||
| means something different because it expects that we are IN the | ||
| called method, not actually at the instruction just after the call. This | ||
| is important, because until the called method returns, IT is responsible | ||
| for protecting the return value. Thus just after a call instruction | ||
| we have to protect EAX if the method being called returns a GC pointer. | ||
|
|
||
| To figure this out, we need to stop AT the call so we can determine the | ||
| target (and thus whether it returns a GC pointer), and then place the | ||
| target (and thus whether it returns one or more GC pointers), and then place | ||
| a different interrupt instruction so that the GCCover harness protects | ||
| EAX before doing the GC). This effectively simulates a hijack in | ||
| non-fully interruptible code */ | ||
| the return value register(s) before doing the GC. This effectively simulates | ||
| a hijack in non-fully interruptible code */ | ||
|
|
||
| /* TODO. Simulating the hijack could cause problems in cases where the | ||
| return register is not always a valid GC ref on the return offset. | ||
|
|
@@ -1597,15 +1625,15 @@ void DoGcStress (PCONTEXT regs, NativeCodeVersion nativeCodeVersion) | |
| // We are in preemptive mode in JITTed code. This implies that we are into IL stub | ||
| // close to PINVOKE method. This call will never return objectrefs. | ||
| #ifdef TARGET_ARM | ||
| size_t instrLen = GetARMInstructionLength(nextInstr); | ||
| if (instrLen == 2) | ||
| *(WORD*)nextInstr = INTERRUPT_INSTR; | ||
| else | ||
| *(DWORD*)nextInstr = INTERRUPT_INSTR_32; | ||
| size_t instrLen = GetARMInstructionLength(nextInstr); | ||
| if (instrLen == 2) | ||
| *(WORD*)nextInstr = INTERRUPT_INSTR; | ||
| else | ||
| *(DWORD*)nextInstr = INTERRUPT_INSTR_32; | ||
| #elif defined(TARGET_ARM64) | ||
| *(DWORD*)nextInstr = INTERRUPT_INSTR; | ||
| *(DWORD*)nextInstr = INTERRUPT_INSTR; | ||
| #else | ||
| *nextInstr = INTERRUPT_INSTR; | ||
| *nextInstr = INTERRUPT_INSTR; | ||
| #endif | ||
| } | ||
| else | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only code change here is putting the
instructionIsACallThroughImmediatecase as anelseclause, if we didn't first see ablx reginstruction. The rest is just whitespace and semicolons.