Skip to content

[x86/Linux] Catch exceptions thrown from unsafe code #7244

@parjong

Description

@parjong

CoreCLR with recent PRs (#8889, dotnet/coreclr#8911, dotnet/coreclr#8913, dotnet/coreclr#8914, dotnet/coreclr#8916, dotnet/coreclr#8964) begins to support exception handling for x86/Linux. (It can run very simple example presented in #7216).

Unfortunately, CLR cannot catch an exception thrown from unsafe code. For example, let us consider the following code:

using System;
using System.Runtime.CompilerServices;

public class BringUpTest
{
    [MethodImplAttribute(MethodImplOptions.NoInlining)]
    public static unsafe void Localloc(byte n)
    {
        Console.WriteLine("Enter Localloc({0})", n);
        byte* a = stackalloc byte[n];

        Console.WriteLine("Here - 1");
        *a = 0;
        Console.WriteLine("Here - 2");

        byte i;
        for (i=1; i < n; ++i) { a[i] = i; }

        Console.WriteLine("Here - 2");

        for (i=1; i < n; ++i) { Console.WriteLine(a[i]); }

        Console.WriteLine("Leave Localloc({0})", n);
    }

    public static void Main()
    {
        try
        {
            Console.WriteLine("Before");
            Localloc(0);
            Console.WriteLine("After");
        }
        catch (Exception)
        {
            Console.WriteLine("Caught (Enter)");
            Console.WriteLine("Caught (Leave)");
        }

        Console.WriteLine("Done");
    }
}

We except to see the following output for the above code:

Before
Enter Localloc(0)
Here - 1
Caught (Enter)
Caught (Leave)
Done

Currently, however, we get the following output:

Before
Enter Localloc(0)
Here - 1
Segmentation fault (core dumped)

Metadata

Metadata

Assignees

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions