Bug 37147 - InvalidProgramException on return null from int Main()
Summary: InvalidProgramException on return null from int Main()
Alias: None
Product: Runtime
Classification: Mono
Component: Verifier ()
Version: 4.2.0 (C6)
Hardware: PC Other
: --- normal
Target Milestone: ---
Assignee: Rodrigo Kumpera
Depends on:
Reported: 2015-12-18 16:32 UTC by Friedrich von Never
Modified: 2015-12-18 18:54 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

CIL code of the failing program (1.11 KB, text/x-csrc)
2015-12-18 16:32 UTC, Friedrich von Never

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Friedrich von Never 2015-12-18 16:32:11 UTC
Created attachment 14349 [details]
CIL code of the failing program

I am one of the authors of a prototype LISP to CLI compiler https://github.com/codingteam/naggum

I am trying to compile the following line: (System.Console.WriteLine "ok")

(No, you do not have to mess with that custom compiler, I've tested the bug with the CIL code attached.)

I've compiled CIL with the following command:

    > ilasm Program.il

After that I've executed it on .NET and Mono with the following results:

    > Program.exe

    > mono Program.exe

    Unhandled Exception:
    System.InvalidProgramException: Invalid IL code in Program:Main (): IL_000b: ret

    [ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidProgramException: Invalid IL code in Program:Main (): IL_000b: ret

I am using the latest available Mono for Windows:

    > mono --version
    Mono JIT compiler version 4.2.1 (Visual Studio built mono)
    Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
            TLS:           normal
            SIGSEGV:       normal
            Notification:  Thread + polling
            Architecture:  x86
            Disabled:      none
            Misc:          softdebug
            LLVM:          supported, not enabled.
            GC:            sgen

I've tried to decompile this program with ilspy and got this weird result:

    public static int Main()
        return null;

Yes, that's very strange that null is returned from the int method, but again: official CLR (I use 4.6.1) have no problems executing that.

Program.il file is attached. The bug is reproducible on Windows with Mono 4.2.1 and also on Travis (it uses 4.2.1 also), see the report here: https://travis-ci.org/codingteam/naggum#L1127
Comment 1 Rodrigo Kumpera 2015-12-18 18:54:45 UTC
Hi Friedrich,

You're generating invalid IL. And for that the expected implementation behavior is to fail with the above exception.

dotnet, for some reason, gives large amplitude of what's the acceptable IR beyond the spec.

Given that those loosen restrictions aren not specified nor is the expected behavior, we side with not accepting such IL.

My suggestion is that you stick with generating verifiable code and passes peverify.

For a reference of what's ok or not, see the ECMA-335 spec: http://www.ecma-international.org/publications/standards/Ecma-335.htm