Skip to content

A/V Exceptions in unsafe delegate cause runtime failure #12754

Closed

Description

@AaronRobinsonMSFT

An A/V in an unsafe delegate causes the runtime to crash. See below for example code.

On Windows the dereference of a null is converted to a System.NullReferenceException, but not on Linux. The IL instruction in this case is ldind.i4. Based on Ecma-335, that instruction can trigger a NullReferenceException for null as well as misalignment - which would seem to imply the catch block should handle the invalid dereference.

Related: #12741 https://github.com/dotnet/cli/issues/11403

cc @jkotas @janvorli

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

class Program
{
    static unsafe void CallbackImpl(string ss, int *v)
    {
        try
        {
            Console.WriteLine($"{ss} - {*v}");
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
    }
    static unsafe void Main(string[] args)
    {
        NativeLib.ExecuteCallback(CallbackImpl);
    }
    static class NativeLib
    {
        public unsafe delegate void Callback(string s, int *v);
        [DllImport(nameof(NativeLib))]
        public extern static void ExecuteCallback(Callback cb);
    }
}

Compiled on Ubuntu 18.04 LTS
$ gcc -g -shared -o libNativeLib.so -fPIC NativeLib.c

#include <stdio.h>

#define RUNE char
#define R(msg) msg

typedef void (*Callback)(const RUNE *, int *v);

__attribute__((visibility("default")))
void ExecuteCallback(Callback cb)
{
    int value = 27;
    cb(R("Foobar"), NULL);
}

Activity

  1. AaronRobinsonMSFT commented on May 29, 2019

    @AaronRobinsonMSFT
    MemberAuthor

    I believe this was a mistake during testing. Further investigation has been unable to reproduce the crashing behavior - even on the original machine. Closing.

  2. transferred this issue fromdotnet/coreclron Jan 31, 2020
  3. added this to the 3.0 milestone on Jan 31, 2020
  4. ghost locked as resolved and limited conversation to collaborators on Dec 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions