Bug 10106 - Console.OpenStandardInput() fails when STD_INPUT_HANDLE is INVALID_HANDLE_VALUE
Summary: Console.OpenStandardInput() fails when STD_INPUT_HANDLE is INVALID_HANDLE_VALUE
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib (show other bugs)
Version: unspecified
Hardware: PC Windows
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2013-02-06 22:29 UTC by Jonathan Pryor
Modified: 2013-02-06 22:30 UTC (History)
1 user (show)

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


Attachments
invalid-handle.zip (1.71 KB, application/zip)
2013-02-06 22:29 UTC, Jonathan Pryor
Details

Description Jonathan Pryor 2013-02-06 22:29:02 UTC
Created attachment 3339 [details]
invalid-handle.zip

This is a Windows-specific issue.

If the caller/parent process mono calls SetStdHandle(STD_INPUT_HANDLE, INVALID_HANDLE_VALUE), then mono will bomb during process startup.

To reproduce:

1. Download invalid-handle.zip
2. Extract it.
3. From CMD.EXE:

> cd path\to\invalid-handle
> NMAKE all
> NMAKE run

Expected results: To do what .NET does:

> .\EXEC.EXE HW.EXE
> # Clearing stdin...
> [stdout] Hello, world!
> [stdout]
> [stderr]
> # exit code: 0

Actual results: standard error contains:

> [ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException: An exception was thrown by the type initializer for System.Console ---> System.ArgumentException: handle
> Parameter name: Invalid.
>   at System.IO.FileStream..ctor (IntPtr handle, FileAccess access, Boolean ownsHandle, Int32 bufferSize, Boolean isAsync, Boolean isZeroSize)
>   at (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (intptr,System.IO.FileAccess,bool,int,bool,bool)
>   at System.Console.Open (IntPtr handle, FileAccess access, Int32 bufferSize)
>   at System.Console.OpenStandardInput (Int32 bufferSize)
>   at System.Console.SetupStreams (System.Text.Encoding inputEncoding, System.Text.Encoding outputEncoding)
>   at System.Console..cctor ()
>   --- End of inner exception stack trace ---
>   at Test.Main ()

Export MONO_LOG_LEVEL=debug, and the last useful message written to stdout is:

> Mono: Searching for 'GetConsoleOutputCP'.

Based on those two items, what appears to be happening is that Console.OpenStandardInput(int) calls Console.Open(MonoIO.ConsoleInput, FileAccess.Read, bufferSize).

https://github.com/mono/mono/blob/master/mcs/class/corlib/System/Console.cs#L245

MonoIO.ConsoleInput is an internal call into mono/metadata/file-io.c!ves_icall_System_IO_MonoIO_get_ConsoleInput():

https://github.com/mono/mono/blob/master/mono/metadata/file-io.c#L1014

The problem is that because the caller has deliberately/explicitly set STD_INPUT_HANDLE to INVALID_HANDLE_VALUE, MonoIO.ConsoleInput returns INVALID_HANDLE_VALUE, which causes the FileStream constructor to throw.

What should happen? Perhaps ves_icall_System_IO_MonoIO_get_ConsoleInput() should return NULL if GetStdHandle() returns INVALID_FILE_HANDLE?

Note You need to log in before you can comment on or make changes to this bug.