Tail Call bug in RyuJIT - Incorrect parameters passed
The following critical bug is known and fixed internally (not released) by Microsoft (per private discussions while security was evaluated). In this bug "we" refers to Marc Gravell (@mgravell) and I who tracked down the issue on the Stack Overflow side on the fence. I am posting it here for public visibility and to help users hitting the same issue. I have also created a blog post addressing discovery and severity here: Why you should wait on upgrading to .Net 4.6.
There is an issue in the .Net 4.6 RTM RyuJIT implementation that incorrectly hooks up parameters during some tail call optimizations. The net result of this is that tail call methods can get the wrong parameters passed. Here's our code exhibiting the issue:
void Set<T>(string key, T val, int? durationSecs, bool sliding, bool broadcastRemoveFromCache = false)
{
SetWithPriority<T>(key, val, durationSecs, sliding, CacheItemPriority.Default);
}
void SetWithPriority<T>(string key, T val, int? durationSecs, bool isSliding, CacheItemPriority priority)
{
key = KeyInContext(key);
RawSet(key, val, durationSecs, isSliding, priority);
}
void RawSet(string cacheKey, object val, int? durationSecs, bool isSliding, CacheItemPriority priority)
{
var absolute = !isSliding && durationSecs.HasValue
? DateTime.UtcNow.AddSeconds(durationSecs.Value)
: Cache.NoAbsoluteExpiration;
var sliding = isSliding && durationSecs.HasValue
? TimeSpan.FromSeconds(durationSecs.Value)
: Cache.NoSlidingExpiration;
HttpRuntime.Cache.Insert(cacheKey, val, null, absolute, sliding, priority, null);
}
The RawSet() is the last in the chain, and only when optimizations are enabled, is incorrectly optimized. If for example we call Set<T> with a durationSecs of 3600, we would expect that value to go all the way down the method chain. This doesn't happen. Instead, the value passed to the tail method (RawSet) is seemingly random (our assumption is it pulls some other value from the stack). For example, it may be 30, or 57, or null.
The net result for us is that items are getting cached for drastically shorter durations, or not at all (null often gets passed). On Stack Overflow this causes unpredictable local HTTP cache usage and more hits and heavier load to anything populating it. This is a production blocker on deployment for us, and (we believe) should be for anyone else.
We have collapsed the reproduction of this bug into a project you can run locally, available on GitHub. Here's an example test run (see the repo README for a full description):
I can't stress how serious of a bug this is, due to the subtleness of the occurrence, the "only production" likelihood given the RELEASE-only nature, and the scary examples we can easily come up with. When the parameters you're passing aren't the ones the method is getting, all sanity goes out the window.
What if your method says how much stock to buy? What if it gives dosing information to a patient? What if it tells a plane what altitude to climb to?
This bug is critical, I am posting the issue for several reasons:
- We're not jerks
- To raise awareness
- To prevent as many people as possible from deploying affected code
- To usher a fix and deployment from Microsoft as soon as possible
If you have already deployed .Net 4.6, our recommendation at this point is to disable RyuJIT immediately.
I understand from various sources that given the normal patch workflow, the next hotfix window is September. For the appropriate people at Microsoft, please consider an out-of-band emergency fix for this. This issue is a show-stopper for us in moving to .NET 4.6.
Thanks @NickCraver for raising visibility.
I'll be talking with the team later today.
@vcsjones - For clarity, is this a show-stopper because you are concerned or because you have a repro w/your workload?
/cc @CarolEidt, @cmckinsey
Has anyone been able to understand the root cause of this? Narrowing the problem to certain use-cases can help "calm" people down.
@richlander not to be difficult, but given the nature of the bug...how would you (as a developer) know? This is such a subtle production-only bug for most cases I think most wouldn't find it, even in testing.
@YuvalItzchakov the issue here is it can affect any library as well, not just your code. Even if you had specific patterns, I'm not sure that would help any here. In our case it was a RELEASE library distributed by our internal NuGet server...so really like any other NuGet package.
@richlander @NickCraver pointed out that this was originally discovered while investigating an issue in MiniProfiler, which we actively use. We are re-opening a few incidents we had on an application to determine if this is the cause, or if it is related. At the time we did not have a mini-dump available to investigate, but that is the next thing we are working on.
@vcsjones the "mini profiler" thing here was just what was visible; the actual glitch manifested in a different library that was providing a custom storage provider for mini profilier. So: the issue didn't actually happen in mini-profiler, but at the same time: I can't say that mini-profiler isn't affected. Just: don't focus on the mention of mini-profiler.
@mgravell Agree. My point being, this was seen in a real library. It isn't just some mocked together code that uses tricks to make the issue manifest. It doesn't take special work to hit it. Thus it is very likely this affects other real-world applications and it has severe consequences.
Yes; absolutely; that's what scares us. Especially if Windows 10 deploys .NET 4.6... Wednesday could see random eager people installing, and potentially have their desktop business apps start behaving different in subtle, hard to spot, yet dangerous (whether to business well-being, or to personal safety) ways... Plus of course the VS2015 release prompting server-side installs.
How likely is this to affect current work in progress to get RyuJit working for the 32- and 64-bit versions of ARM? Is this bug AMD64 specific or platform agnostic?
The team is taking this very seriously. We're going to talk about it later today as folks get into the office.
Definitely appreciate all the attention on the issue.
@mgravell would the release have any effect if the apps are targeting < 4.6 though? Probably too late to slipstream RyuJIT as disabled considering the mode of distribution. :+1: Good find
@ChrisMcKee Yes this affects all applications that target 4.5.x (possibly earlier?) through 4.6. Just having RyuJIT installed is enough to introduce the problem. http://nickcraver.com/blog/2015/07/27/why-you-should-wait-on-dotnet-46/#comment-2159337569
@vcsjones My understanding is that the same JIT compiler would be used in any 4.x environment, possibly even things targetting .NET 2, but running under the 4.x CLR?
Wait, this will glitch out on 4.5 as well?
@masonwheeler Correct, if .Net 4.6 is installed the JIT appears to be used for both. Our repro case targets .Net 4.5 and .Net 4.5.2 in the CorruptionRepro and StackRedis projects respectively.
@NickCraver even bigger 'good catch'. I was sat whistling, ah well they'll fix 4.6. Nobody expects the regression fail.
Thanks for the heads up @NickCraver, was going to push to get 4.6 on our build server this week. Going to hold back till a patch is issued now.
Is this the same tailcall bug as raised here? https://github.com/Microsoft/visualfsharp/issues/536
I have found some troubles with asm code.
Part 1.
The call of OnLogDuration from SetWithPriority:
mov r8,5BFFFCC3E8h
mov r8,qword ptr [r8] ; "LocalCache.SetWithPriority"
mov rcx,rsi ; key
mov rdx,qword ptr [rsp+70h] ; durationSecs
call 00007FFA7292EF80 ; OnLogDuration
We can see that durationSecs stores at the address [rsp+70h]. So far, everything looks fine.
Part 2.
The call of OnLogDuration from RawSet:
mov r8,5BFFFCC3F0h
mov r8,qword ptr [r8] ; "RawSet"
mov rcx,rsi ; key
mov rdx,qword ptr [rsp+0C8h] ; durationSecs
call 00007FFA7292EF80 ; OnLogDuration
We can see that RyuJIT tries to read durationSecs from [rsp+0C8h]. But:
RSP = 0x0000005787AAD6C0
durationSecs address = 0x0000005787AAD790
durationSecs offset = 0x00000000000000D0
As we can see, the calculated durationSecs offset is 0C8h, but the actual offset is 0D0h. In this case, the TCO calculation error is 8 bytes.
@latkin I don't think so; my reasoning: the bug you link to threw a fatal execution exception. It was broken in a way that was detectable. Contrast: silently substituting parameter values.
