Skip to content

Commit

Permalink
[mini] publish global patches after JitInfo has been added
Browse files Browse the repository at this point in the history
Fixes mono#14080

Consider the following example:

```csharp
static void CommonCallTarget () { }

static void TailA () {
    tailcall CommonCallTarget ();
}

static void TailB () {
    tailcall CommonCallTarget ();
}
```

Since `TailA` and `TailB` are tailcalling into `CommonCallTarget`, the resolution at patch-time is a bit tricky, that is, since it's a jump-like instruction the patching machinery won't know where it was called from. That's why we maintain a global hashtable `jump_target_hash` where each jump-site is signed up to be patched. At patch-time we know the target method (in the example `CommonCallTarget`), but since we don't know where we are coming from, we will just apply all patches for that target.

This works since ages, so why did it crash on arm64 sometimes?
When the patching happens, we check if the displacement between jump-site and target fits into it (24bit). If not, which happens not very often, we have to allocate a _thunk_:
https://github.com/mono/mono/blob/36296ce291f8a7b19de3eccb7a32c7e4ed9df8f2/mono/mini/mini-arm64.c#L928-L942

So instead of jumping to the target directly, we'll branch to the thunk. This is a little trampoline that loads the full address of the target and then finally branches to it. This one will live close-by the jump-site, because during compilation we will reserve specifically for that scenario some space after the generated code. For this, however, we need the JitInfo of the jump-site. And that's where the origin of the race is. Let's say:

* Thread A compiles `TailA`, and then jumps into it. Thus one patch point is in the `jump_target_hash`.
* Now Thread B compiles `TailB`, registers the patch point but has _not_ yet registered its JitInfo.
* Then Thread A continues, does the tailcall into `CommonCallTarget`, enters the patching machinery, which sees two patches. Now assume when applying the patch for `TailB` the displacement is too large, thus it tries to allocate a thunk but can't find the relevant JitInfo for it that it needs to emit the thunk. So it crashes as reported in mono#14080

As far as I can tell this only affects ARM64, ARM and PPC.
  • Loading branch information
lewurm committed Aug 30, 2019
1 parent 52c1d4a commit 9bf1f0a
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions mono/mini/mini.c
Expand Up @@ -2122,6 +2122,21 @@ mono_postprocess_patches (MonoCompile *cfg)
patch_info->data.table->table = (MonoBasicBlock**)table;
break;
}
default:
/* do nothing */
break;
}
}
}

/* Those patches require the JitInfo of the compiled method already be in place when used */
static void
mono_postprocess_patches_after_ji_publish (MonoCompile *cfg)
{
MonoJumpInfo *patch_info;

for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
switch (patch_info->type) {
case MONO_PATCH_INFO_METHOD_JUMP: {
unsigned char *ip = cfg->native_code + patch_info->ip.i;

Expand Down Expand Up @@ -3876,6 +3891,9 @@ mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, JitFl

if (cfg->method->dynamic)
mono_dynamic_code_hash_lookup (cfg->domain, cfg->method)->ji = cfg->jit_info;

mono_postprocess_patches_after_ji_publish (cfg);

mono_domain_unlock (cfg->domain);
}

Expand Down

0 comments on commit 9bf1f0a

Please sign in to comment.