|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +// Regression test for a WebAssembly R2R (crossgen) codegen bug in finally |
| 5 | +// calls. When a callfinally's continuation was the lexically next block, |
| 6 | +// codegen fell through to it without a branch. If a try_table ended between |
| 7 | +// the two, the fall-through landed on the validation `unreachable` emitted |
| 8 | +// after the try_table `end` and trapped (RuntimeError: unreachable). |
| 9 | +// |
| 10 | +// The shape needs a user try/finally that ends the async method (so the |
| 11 | +// continuation is outside the state machine's try/catch) and a finally too |
| 12 | +// large to be cloned onto the normal path. |
| 13 | +// |
| 14 | +// Reproduces only under crossgen wasm R2R (TargetOS=browser). Passes trivially |
| 15 | +// on all other targets. |
| 16 | + |
| 17 | +using System; |
| 18 | +using System.Runtime.CompilerServices; |
| 19 | +using System.Threading.Tasks; |
| 20 | +using Xunit; |
| 21 | + |
| 22 | +public class Runtime_134264 |
| 23 | +{ |
| 24 | + private static int s_value; |
| 25 | + |
| 26 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 27 | + private static void Touch(int value) => s_value += value; |
| 28 | + |
| 29 | + private static async Task TryCatchFinallyAsync(bool enter) |
| 30 | + { |
| 31 | + bool entered = false; |
| 32 | + try |
| 33 | + { |
| 34 | + if (enter) |
| 35 | + { |
| 36 | + await Task.CompletedTask; |
| 37 | + entered = true; |
| 38 | + } |
| 39 | + |
| 40 | + Touch(1); |
| 41 | + } |
| 42 | + catch (Exception ex) |
| 43 | + { |
| 44 | + GC.KeepAlive(ex); |
| 45 | + Touch(-100); |
| 46 | + } |
| 47 | + finally |
| 48 | + { |
| 49 | + if (entered) |
| 50 | + { |
| 51 | + Touch(1); Touch(2); Touch(3); Touch(4); Touch(5); Touch(6); Touch(7); Touch(8); |
| 52 | + Touch(9); Touch(10); Touch(11); Touch(12); Touch(13); Touch(14); Touch(15); Touch(16); |
| 53 | + } |
| 54 | + |
| 55 | + Touch(1000); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private static async Task NestedTryFinallyAsync(bool enter) |
| 60 | + { |
| 61 | + bool entered = false; |
| 62 | + try |
| 63 | + { |
| 64 | + try |
| 65 | + { |
| 66 | + if (enter) |
| 67 | + { |
| 68 | + await Task.CompletedTask; |
| 69 | + entered = true; |
| 70 | + } |
| 71 | + |
| 72 | + Touch(1); |
| 73 | + } |
| 74 | + finally |
| 75 | + { |
| 76 | + if (entered) |
| 77 | + { |
| 78 | + Touch(1); Touch(2); Touch(3); Touch(4); Touch(5); Touch(6); Touch(7); Touch(8); |
| 79 | + Touch(9); Touch(10); Touch(11); Touch(12); Touch(13); Touch(14); Touch(15); Touch(16); |
| 80 | + } |
| 81 | + |
| 82 | + Touch(1000); |
| 83 | + } |
| 84 | + } |
| 85 | + catch (Exception ex) |
| 86 | + { |
| 87 | + GC.KeepAlive(ex); |
| 88 | + Touch(-100); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + [Theory] |
| 93 | + [InlineData(true, 1137)] |
| 94 | + [InlineData(false, 1001)] |
| 95 | + public static async Task TestEntryPoint(bool enter, int expected) |
| 96 | + { |
| 97 | + s_value = 0; |
| 98 | + await TryCatchFinallyAsync(enter); |
| 99 | + Assert.Equal(expected, s_value); |
| 100 | + |
| 101 | + s_value = 0; |
| 102 | + await NestedTryFinallyAsync(enter); |
| 103 | + Assert.Equal(expected, s_value); |
| 104 | + } |
| 105 | +} |
0 commit comments