Skip to content

Commit

Permalink
Fix CoreRT build breaks
Browse files Browse the repository at this point in the history
  • Loading branch information
jkotas committed May 29, 2019
1 parent 706ed49 commit 05e0578
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 144 deletions.
6 changes: 3 additions & 3 deletions src/System.Private.CoreLib/src/System.Private.CoreLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<AssemblyKey>MSFT</AssemblyKey>
<IsDotNetFrameworkProductAssembly>true</IsDotNetFrameworkProductAssembly>
<!-- Disable nullability-related warnings -->
<NoWarn>$(NoWarn);CS8600;CS8601;CS8602;CS8603;CS8604;CS8605;CS8606;CS8608;CS8609;CS8610;CS8611;CS8612;CS8614;CS8615;CS8617;CS8618;CS8620;CS8622;CS8625;CS8631;CS8632;CS8634;CS8653</NoWarn>
<NoWarn>$(NoWarn);CS8600;CS8601;CS8602;CS8603;CS8604;CS8605;CS8606;CS8608;CS8609;CS8610;CS8611;CS8612;CS8614;CS8615;CS8617;CS8618;CS8620;CS8622;CS8625;CS8626;CS8631;CS8632;CS8634;CS8653</NoWarn>
</PropertyGroup>
<!-- Default configurations to help VS understand the options -->
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'" />
Expand Down Expand Up @@ -307,8 +307,8 @@
<Compile Include="System\Runtime\CompilerServices\ClassConstructorRunner.cs" />
<Compile Include="System\Runtime\CompilerServices\ClassConstructorRunner.NonPortable.cs" />
<Compile Include="System\Runtime\CompilerServices\RuntimeHelpers.CoreRT.cs" />
<Compile Include="System\WeakReference.cs" />
<Compile Include="System\WeakReferenceOfT.cs" />
<Compile Include="System\WeakReference.CoreRT.cs" />
<Compile Include="System\WeakReference.T.CoreRT.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetsWindows)'=='true' and '$(EnableWinRT)'!='true'">
<Compile Include="..\..\Common\src\Interop\Windows\Kernel32\Interop.ExitProcess.cs">
Expand Down
2 changes: 0 additions & 2 deletions src/System.Private.CoreLib/src/System/Exception.CoreRT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ internal void SetMessage(string msg)

private IDictionary CreateDataContainer() => new ListDictionaryInternal();

private string GetStackTrace(bool needFileInfo) => StackTrace;

private string SerializationStackTraceString => StackTrace;
private string SerializationRemoteStackTraceString => null;
private string SerializationWatsonBuckets => null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

/*============================================================
**
**
** Purpose: A wrapper for establishing a WeakReference to an Object.
**
===========================================================*/

using System;
using System.Runtime;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
Expand All @@ -20,9 +12,7 @@

namespace System
{
[Serializable]
[System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
public class WeakReference : ISerializable
public partial class WeakReference
{
// If you fix bugs here, please fix them in WeakReference<T> at the same time.

Expand All @@ -32,40 +22,16 @@ public class WeakReference : ISerializable
internal volatile IntPtr m_handle;
internal bool m_IsLongReference;

// Creates a new WeakReference that keeps track of target.
// Assumes a Short Weak Reference (ie TrackResurrection is false.)
//
public WeakReference(object target)
: this(target, false)
{
}

//Creates a new WeakReference that keeps track of target.
//
public WeakReference(object target, bool trackResurrection)
private void Create(object target, bool trackResurrection)
{
m_IsLongReference = trackResurrection;
m_handle = GCHandle.ToIntPtr(GCHandle.Alloc(target, trackResurrection ? GCHandleType.WeakTrackResurrection : GCHandleType.Weak));

// Set the conditional weak table if the target is a __ComObject.
TrySetComTarget(target);
}

protected WeakReference(SerializationInfo info, StreamingContext context)
{
if (info == null)
if (target != null)
{
throw new ArgumentNullException(nameof(info));
// Set the conditional weak table if the target is a __ComObject.
TrySetComTarget(target);
}

object target = info.GetValue("TrackedObject", typeof(object)); // Do not rename (binary serialization)
bool trackResurrection = info.GetBoolean("TrackResurrection"); // Do not rename (binary serialization)

m_IsLongReference = trackResurrection;
m_handle = GCHandle.ToIntPtr(GCHandle.Alloc(target, trackResurrection ? GCHandleType.WeakTrackResurrection : GCHandleType.Weak));

// Set the conditional weak table if the target is a __ComObject.
TrySetComTarget(target);
}

//Determines whether or not this instance of WeakReference still refers to an object
Expand Down Expand Up @@ -224,17 +190,6 @@ private void TrySetComTarget(object target)
#endif // ENABLE_WINRT
}

public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
{
if (info == null)
{
throw new ArgumentNullException(nameof(info));
}

info.AddValue("TrackedObject", Target, typeof(object)); // Do not rename (binary serialization)
info.AddValue("TrackResurrection", m_IsLongReference); // Do not rename (binary serialization)
}

// Free all system resources associated with this reference.
~WeakReference()
{
Expand All @@ -244,5 +199,7 @@ public virtual void GetObjectData(SerializationInfo info, StreamingContext conte
if (handle != default(IntPtr))
((GCHandle)handle).Free();
}

private bool IsTrackResurrection() => m_IsLongReference;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,26 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

/*============================================================
**
**
** Purpose: A wrapper for establishing a WeakReference to a generic type.
**
===========================================================*/

using System;
using System.Runtime;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Threading;
using System.Diagnostics;

using Internal.Runtime.Augments;
using Internal.Runtime.CompilerServices;

namespace System
{
// This class is sealed to mitigate security issues caused by Object::MemberwiseClone.
[Serializable]
[System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
public sealed class WeakReference<T> : ISerializable where T : class
public sealed partial class WeakReference<T>
where T : class?
{
// If you fix bugs here, please fix them in WeakReference at the same time.

internal volatile IntPtr m_handle;
private bool m_trackResurrection;

// Creates a new WeakReference that keeps track of target.
// Assumes a Short Weak Reference (ie TrackResurrection is false.)
//
public WeakReference(T target)
: this(target, false)
{
}

//Creates a new WeakReference that keeps track of target.
//
public WeakReference(T target, bool trackResurrection)
private void Create(T target, bool trackResurrection)
{
m_handle = (IntPtr)GCHandle.Alloc(target, trackResurrection ? GCHandleType.WeakTrackResurrection : GCHandleType.Weak);
m_trackResurrection = trackResurrection;
Expand All @@ -54,41 +33,6 @@ public WeakReference(T target, bool trackResurrection)
}
}

internal WeakReference(SerializationInfo info, StreamingContext context)
{
if (info == null)
{
throw new ArgumentNullException(nameof(info));
}

T target = (T)info.GetValue("TrackedObject", typeof(T)); // Do not rename (binary serialization)
bool trackResurrection = info.GetBoolean("TrackResurrection"); // Do not rename (binary serialization)

m_handle = (IntPtr)GCHandle.Alloc(target, trackResurrection ? GCHandleType.WeakTrackResurrection : GCHandleType.Weak);

if (target != null)
{
// Set the conditional weak table if the target is a __ComObject.
TrySetComTarget(target);
}
}

//
// We are exposing TryGetTarget instead of a simple getter to avoid a common problem where people write incorrect code like:
//
// WeakReference ref = ...;
// if (ref.Target != null)
// DoSomething(ref.Target)
//
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryGetTarget(out T target)
{
// Call the worker method that has more performant but less user friendly signature.
T o = GetTarget();
target = o;
return o != null;
}

public void SetTarget(T target)
{
if (m_handle == default(IntPtr))
Expand All @@ -101,31 +45,33 @@ public void SetTarget(T target)
GC.KeepAlive(this);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private T GetTarget()
private T Target
{
IntPtr h = m_handle;
get
{
IntPtr h = m_handle;

// Should only happen for corner cases, like using a
// WeakReference from a finalizer.
if (default(IntPtr) == h)
return null;
// Should only happen for corner cases, like using a
// WeakReference from a finalizer.
if (default(IntPtr) == h)
return default;

T target = Unsafe.As<T>(RuntimeImports.RhHandleGet(h));
T target = Unsafe.As<T>(RuntimeImports.RhHandleGet(h));

if (target == null)
{
target = TryGetComTarget() as T;
}
if (target == null)
{
target = TryGetComTarget() as T;
}

// We want to ensure that the handle was still alive when we fetched the target,
// so we double check m_handle here. Note that the reading of the handle
// value has to be volatile for this to work, but reading of m_handle does not.
// We want to ensure that the handle was still alive when we fetched the target,
// so we double check m_handle here. Note that the reading of the handle
// value has to be volatile for this to work, but reading of m_handle does not.

if (default(IntPtr) == m_handle)
return null;
if (default(IntPtr) == m_handle)
return default;

return target;
return target;
}
}

/// <summary>
Expand All @@ -134,7 +80,7 @@ private T GetTarget()
/// and gets\create a new RCW in case it is alive.
/// </summary>
/// <returns></returns>
private object TryGetComTarget()
private object? TryGetComTarget()
{
#if ENABLE_WINRT
WinRTInteropCallbacks callbacks = WinRTInterop.UnsafeCallbacks;
Expand Down Expand Up @@ -184,16 +130,6 @@ private void TrySetComTarget(object target)
}
}


public void GetObjectData(SerializationInfo info, StreamingContext context)
{
if (info == null)
{
throw new ArgumentNullException(nameof(info));
}

info.AddValue("TrackedObject", this.GetTarget(), typeof(T)); // Do not rename (binary serialization)
info.AddValue("TrackResurrection", m_trackResurrection); // Do not rename (binary serialization)
}
private bool IsTrackResurrection() => m_trackResurrection;
}
}

0 comments on commit 05e0578

Please sign in to comment.