Skip to content

Commit 922777a

Browse files
authored
Disable default marshalling for bool (dotnet#65749)
1 parent 0e6a73a commit 922777a

File tree

324 files changed

+660
-234
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

324 files changed

+660
-234
lines changed

docs/design/libraries/DllImportGenerator/Compatibility.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ When marshalling as [ANSI](https://docs.microsoft.com/windows/win32/intl/code-pa
5858

5959
The p/invoke source generator does not provide an equivalent to using `CharSet.Auto` in the built-in system. If platform-dependent behaviour is desired, it is left to the user to define different p/invokes with different marshalling configurations.
6060

61+
### `bool` marshalling
62+
63+
We have decided to use `System.Runtime.CompilerServices.DisableRuntimeMarshalling` to enable our custom value type marshalling support. As a result, when a value type that has a `bool` field is passed to native code through source-generated marshalling, the `bool` field is treated as a 1-byte value and is not normalized. Since this default is a little odd and unlikely to be the majority use case, we're going to generally take a stance that all `bool` marshalling must be explicitly specified via `MarshalAs` or other mechanisms.
64+
65+
To aid in conversion from `DllImport` to source-generated marshalling, the code-fix will automatically apply a `[MarshalAs(UnmangedType.Bool)]` attribute to `bool` parameters and return values to ensure the marshalling rules are not changed by the code fix.
66+
6167
### Custom marshaller support
6268

6369
Using a custom marshaller (i.e. [`ICustomMarshaler`](https://docs.microsoft.com/dotnet/api/system.runtime.interopservices.icustommarshaler)) with the `UnmanagedType.CustomMarshaler` value on `MarshalAsAttribute` is not supported. This also implies `MarshalAsAttribute` fields: [`MarshalTypeRef`](https://docs.microsoft.com/dotnet/api/system.runtime.interopservices.marshalasattribute.marshaltyperef), [`MarshalType`](https://docs.microsoft.com/dotnet/api/system.runtime.interopservices.marshalasattribute.marshaltype), and [`MarshalCookie`](https://docs.microsoft.com/dotnet/api/system.runtime.interopservices.marshalasattribute.marshalcookie) are unsupported.

src/coreclr/System.Private.CoreLib/src/System/CLRConfig.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ internal static bool GetBoolValue(string switchName, out bool exist)
1616
}
1717

1818
[GeneratedDllImport(RuntimeHelpers.QCall, EntryPoint = "ClrConfig_GetConfigBoolValue", StringMarshalling = StringMarshalling.Utf16)]
19-
private static partial bool GetConfigBoolValue(string configSwitchName, out bool exist);
19+
[return: MarshalAs(UnmanagedType.Bool)]
20+
private static partial bool GetConfigBoolValue(string configSwitchName, [MarshalAs(UnmanagedType.Bool)] out bool exist);
2021
}
2122
}

src/coreclr/System.Private.CoreLib/src/System/Diagnostics/Debugger.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public static void NotifyOfCrossThreadDependency()
5252
}
5353

5454
[GeneratedDllImport(RuntimeHelpers.QCall, EntryPoint = "DebugDebugger_Launch")]
55+
[return: MarshalAs(UnmanagedType.Bool)]
5556
private static partial bool LaunchInternal();
5657

5758
// Returns whether or not a debugger is attached to the process.

src/coreclr/System.Private.CoreLib/src/System/Diagnostics/Eventing/EventPipe.CoreCLR.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,11 @@ private static unsafe partial ulong Enable(
5050
// These PInvokes are used as part of the EventPipeEventDispatcher.
5151
//
5252
[GeneratedDllImport(RuntimeHelpers.QCall, EntryPoint = "EventPipeInternal_GetSessionInfo")]
53+
[return: MarshalAs(UnmanagedType.Bool)]
5354
internal static unsafe partial bool GetSessionInfo(ulong sessionID, EventPipeSessionInfo* pSessionInfo);
5455

5556
[GeneratedDllImport(RuntimeHelpers.QCall, EntryPoint = "EventPipeInternal_GetNextEvent")]
57+
[return: MarshalAs(UnmanagedType.Bool)]
5658
internal static unsafe partial bool GetNextEvent(ulong sessionID, EventPipeEventInstanceData* pInstance);
5759

5860
[GeneratedDllImport(RuntimeHelpers.QCall, EntryPoint = "EventPipeInternal_GetWaitHandle")]

src/coreclr/System.Private.CoreLib/src/System/Diagnostics/Eventing/NativeRuntimeEventSource.PortableThreadPool.NativeSinks.CoreCLR.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ internal static partial void LogThreadPoolWorkerThreadAdjustmentStats(
5252
internal static partial void LogThreadPoolIOEnqueue(
5353
IntPtr NativeOverlapped,
5454
IntPtr Overlapped,
55-
bool MultiDequeues,
55+
[MarshalAs(UnmanagedType.Bool)] bool MultiDequeues,
5656
ushort ClrInstanceID);
5757

5858
[NonEvent]

src/coreclr/System.Private.CoreLib/src/System/GC.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public static GCMemoryInfo GetGCMemoryInfo(GCKind kind)
7878
}
7979

8080
[GeneratedDllImport(RuntimeHelpers.QCall, EntryPoint = "GCInterface_StartNoGCRegion")]
81-
internal static partial int _StartNoGCRegion(long totalSize, bool lohSizeKnown, long lohSize, bool disallowFullBlockingGC);
81+
internal static partial int _StartNoGCRegion(long totalSize, [MarshalAs(UnmanagedType.Bool)] bool lohSizeKnown, long lohSize, [MarshalAs(UnmanagedType.Bool)] bool disallowFullBlockingGC);
8282

8383
[GeneratedDllImport(RuntimeHelpers.QCall, EntryPoint = "GCInterface_EndNoGCRegion")]
8484
internal static partial int _EndNoGCRegion();

src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ internal static partial int DefineField(QCallModule module, int tkParent, string
160160
FieldAttributes attributes);
161161

162162
[GeneratedDllImport(RuntimeHelpers.QCall, EntryPoint = "TypeBuilder_SetMethodIL")]
163-
private static partial void SetMethodIL(QCallModule module, int tk, bool isInitLocals,
163+
private static partial void SetMethodIL(QCallModule module, int tk, [MarshalAs(UnmanagedType.Bool)] bool isInitLocals,
164164
byte[]? body, int bodyLength,
165165
byte[] LocalSig, int sigLength,
166166
int maxStackSize,

src/coreclr/System.Private.CoreLib/src/System/Reflection/LoaderAllocator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ internal sealed partial class LoaderAllocatorScout
2626
internal IntPtr m_nativeLoaderAllocator;
2727

2828
[GeneratedDllImport(RuntimeHelpers.QCall, EntryPoint = "LoaderAllocator_Destroy")]
29+
[return: MarshalAs(UnmanagedType.Bool)]
2930
private static partial bool Destroy(IntPtr nativeLoaderAllocator);
3031

3132
~LoaderAllocatorScout()

src/coreclr/System.Private.CoreLib/src/System/Reflection/Metadata/MetadataUpdater.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public static partial class MetadataUpdater
1313
private static unsafe partial void ApplyUpdate(QCallAssembly assembly, byte* metadataDelta, int metadataDeltaLength, byte* ilDelta, int ilDeltaLength, byte* pdbDelta, int pdbDeltaLength);
1414

1515
[GeneratedDllImport(RuntimeHelpers.QCall, EntryPoint = "AssemblyNative_IsApplyUpdateSupported")]
16+
[return: MarshalAs(UnmanagedType.Bool)]
1617
private static unsafe partial bool IsApplyUpdateSupported();
1718

1819
/// <summary>

src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public override event ModuleResolveEventHandler? ModuleResolve
6969
}
7070

7171
[GeneratedDllImport(RuntimeHelpers.QCall, EntryPoint = "AssemblyNative_GetCodeBase")]
72+
[return: MarshalAs(UnmanagedType.Bool)]
7273
private static partial bool GetCodeBase(QCallAssembly assembly,
7374
StringHandleOnStack retString);
7475

@@ -177,8 +178,8 @@ public override MethodInfo? EntryPoint
177178
[GeneratedDllImport(RuntimeHelpers.QCall, EntryPoint = "AssemblyNative_GetType", StringMarshalling = StringMarshalling.Utf16)]
178179
private static partial void GetType(QCallAssembly assembly,
179180
string name,
180-
bool throwOnError,
181-
bool ignoreCase,
181+
[MarshalAs(UnmanagedType.Bool)] bool throwOnError,
182+
[MarshalAs(UnmanagedType.Bool)] bool ignoreCase,
182183
ObjectHandleOnStack type,
183184
ObjectHandleOnStack keepAlive,
184185
ObjectHandleOnStack assemblyLoadContext);
@@ -349,7 +350,7 @@ internal static RuntimeAssembly InternalLoad(AssemblyName assemblyName,
349350
private static partial void InternalLoad(ObjectHandleOnStack assemblyName,
350351
ObjectHandleOnStack requestingAssembly,
351352
StackCrawlMarkHandle stackMark,
352-
bool throwOnFileNotFound,
353+
[MarshalAs(UnmanagedType.Bool)] bool throwOnFileNotFound,
353354
ObjectHandleOnStack assemblyLoadContext,
354355
ObjectHandleOnStack retAssembly);
355356

@@ -605,8 +606,8 @@ public override Assembly GetSatelliteAssembly(CultureInfo culture!!, Version? ve
605606

606607
[GeneratedDllImport(RuntimeHelpers.QCall, EntryPoint = "AssemblyNative_GetModules")]
607608
private static partial void GetModules(QCallAssembly assembly,
608-
bool loadIfNotFound,
609-
bool getResourceModules,
609+
[MarshalAs(UnmanagedType.Bool)] bool loadIfNotFound,
610+
[MarshalAs(UnmanagedType.Bool)] bool getResourceModules,
610611
ObjectHandleOnStack retModuleHandles);
611612

612613
private RuntimeModule[] GetModulesInternal(bool loadIfNotFound,

0 commit comments

Comments
 (0)