dotnet / msbuild

The Microsoft Build Engine (MSBuild) is the build platform for .NET and Visual Studio.
https://docs.microsoft.com/visualstudio/msbuild/msbuild
MIT License
5.21k stars 1.35k forks source link

msbuild crash when logging invalid utf16 with binlog enabled #6011

Open radekdoulik opened 3 years ago

radekdoulik commented 3 years ago

Issue Description

msbuild with binlog enabled crashes when logging output contains invalid UTF16 "\uDDD0".

Steps to Reproduce

Repro case from https://github.com/xamarin/xamarin-android/issues/4909 - https://github.com/xamarin/xamarin-android/files/4899592/InvalidUTF16.zip

Expected Behavior

msbuild should not crash

Actual Behavior

msbuild crashes when binlog is requested with -bl: option.

=====================
9/7/2020 4:51:11 PM
System.Text.EncoderFallbackException: Unable to translate Unicode character \uDDD0 at index 147 to specified code page.
   at System.Text.EncoderExceptionFallbackBuffer.Fallback(Char charUnknown, Int32 index)
   at System.Text.EncoderFallbackBuffer.InternalFallback(Char ch, Char*& chars)
   at System.Text.UTF8Encoding.GetByteCount(Char* chars, Int32 count, EncoderNLS baseEncoder)
   at System.Text.UTF8Encoding.GetByteCount(String chars)
   at System.IO.BinaryWriter.Write(String value)
   at Microsoft.Build.Logging.BuildEventArgsWriter.WriteBaseFields(BuildEventArgs e, BuildEventArgsFieldFlags flags)
   at Microsoft.Build.Logging.BuildEventArgsWriter.WriteMessageFields(BuildMessageEventArgs e)
   at Microsoft.Build.Logging.BuildEventArgsWriter.Write(BuildMessageEventArgs e)
   at Microsoft.Build.Logging.BuildEventArgsWriter.Write(BuildEventArgs e)
   at Microsoft.Build.Logging.BinaryLogger.Write(BuildEventArgs e)
   at Microsoft.Build.Logging.BinaryLogger.EventSource_AnyEventRaised(Object sender, BuildEventArgs e)
   at Microsoft.Build.Evaluation.ProjectCollection.ReusableLogger.AnyEventRaisedHandler(Object sender, BuildEventArgs e)
   at Microsoft.Build.Framework.AnyEventHandler.Invoke(Object sender, BuildEventArgs e)
   at Microsoft.Build.BackEnd.Logging.EventSourceSink.RaiseAnyEvent(Object sender, BuildEventArgs buildEvent)
===================
9/7/2020 4:51:11 PM
Microsoft.Build.Exceptions.InternalLoggerException: The build stopped unexpectedly because of an unexpected logger failure. ---> System.Text.EncoderFallbackException: Unable to translate Unicode character \uDDD0 at index 147 to specified code page.
   at System.Text.EncoderExceptionFallbackBuffer.Fallback(Char charUnknown, Int32 index)
   at System.Text.EncoderFallbackBuffer.InternalFallback(Char ch, Char*& chars)
   at System.Text.UTF8Encoding.GetByteCount(Char* chars, Int32 count, EncoderNLS baseEncoder)
   at System.Text.UTF8Encoding.GetByteCount(String chars)
   at System.IO.BinaryWriter.Write(String value)
   at Microsoft.Build.Logging.BuildEventArgsWriter.WriteBaseFields(BuildEventArgs e, BuildEventArgsFieldFlags flags)
   at Microsoft.Build.Logging.BuildEventArgsWriter.WriteMessageFields(BuildMessageEventArgs e)
   at Microsoft.Build.Logging.BuildEventArgsWriter.Write(BuildMessageEventArgs e)
   at Microsoft.Build.Logging.BuildEventArgsWriter.Write(BuildEventArgs e)
   at Microsoft.Build.Logging.BinaryLogger.Write(BuildEventArgs e)
   at Microsoft.Build.Logging.BinaryLogger.EventSource_AnyEventRaised(Object sender, BuildEventArgs e)
   at Microsoft.Build.Evaluation.ProjectCollection.ReusableLogger.AnyEventRaisedHandler(Object sender, BuildEventArgs e)
   at Microsoft.Build.Framework.AnyEventHandler.Invoke(Object sender, BuildEventArgs e)
   at Microsoft.Build.BackEnd.Logging.EventSourceSink.RaiseAnyEvent(Object sender, BuildEventArgs buildEvent)
   --- End of inner exception stack trace ---
   at Microsoft.Build.Exceptions.InternalLoggerException.Throw(Exception innerException, BuildEventArgs e, String messageResourceName, Boolean initializationException, String[] messageArgs)
   at Microsoft.Build.BackEnd.Logging.EventSourceSink.RaiseAnyEvent(Object sender, BuildEventArgs buildEvent)
   at Microsoft.Build.BackEnd.Logging.EventSourceSink.RaiseMessageEvent(Object sender, BuildMessageEventArgs buildEvent)
   at Microsoft.Build.BackEnd.Logging.EventSourceSink.Consume(BuildEventArgs buildEvent)
   at Microsoft.Build.BackEnd.Logging.EventSourceSink.Consume(BuildEventArgs buildEvent, Int32 sinkId)
   at Microsoft.Build.BackEnd.Logging.EventRedirectorToSink.Microsoft.Build.Framework.IEventRedirector.ForwardEvent(BuildEventArgs buildEvent)
   at Microsoft.Build.BackEnd.Logging.CentralForwardingLogger.EventSource_AnyEventRaised(Object sender, BuildEventArgs buildEvent)
   at Microsoft.Build.BackEnd.Logging.EventSourceSink.RaiseAnyEvent(Object sender, BuildEventArgs buildEvent)
===================

For more information please check https://github.com/xamarin/xamarin-android/issues/4909

benvillalobos commented 3 years ago

Does this only happen with the binlog enabled? Some of the stacks in the linked issue indicate that it's happening whenever a message with invalid unicode is serialized between MSBuild nodes.

An idea for a minimal repro: Create some task that logs a message that specifically uses \uDDD0

benvillalobos commented 3 years ago

@radekdoulik This does look like a bug but we don't know when we'll get to it. Is it super important for your team?