Skip to content

Commit 1d75035

Browse files
committed
[wasm] Split CoreCLR thunks by target OS (browser vs wasi)
Browser and WASI CoreLib expose different sets of P/Invokes and UCO callbacks: * WASI CoreLib pulls in the Unix Interop.Libraries.cs path, so it references Sys (sockets, fcntl, host entry, etc.) symbols that browser CoreLib does not. * Browser CoreLib references JS-interop pinvokes/callbacks (SystemJS_*, SystemInteropJS_*, JavaScriptExports.*) that WASI does not. A single generated callhelpers-{pinvoke,reverse,interp-to-managed}.cpp cannot be correct for both. Split the checked-in baseline copies into src/coreclr/vm/wasm/{browser,wasi}/ and pick the appropriate subdir in vm/CMakeLists.txt based on CLR_CMAKE_TARGET_WASI. Also parameterize the WasmAppBuilder.CoreCLR generator with a TargetOS property (default "browser" to preserve existing behavior). The generator now filters DllImport and UnmanagedCallersOnly methods by the target's [SupportedOSPlatform] / [UnsupportedOSPlatform] attributes so a future regen against a wasi build naturally drops browser-only entries and vice-versa. BrowserWasmApp.CoreCLR.targets explicitly passes TargetOS="browser".
1 parent 1dd8f87 commit 1d75035

11 files changed

Lines changed: 4806 additions & 17 deletions

‎src/coreclr/vm/CMakeLists.txt‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -954,13 +954,18 @@ elseif(CLR_CMAKE_TARGET_ARCH_WASM)
954954
exceptionhandling.cpp
955955
gcinfodecoder.cpp
956956
)
957+
if(CLR_CMAKE_TARGET_WASI)
958+
set(WASM_THUNK_SUBDIR wasi)
959+
else()
960+
set(WASM_THUNK_SUBDIR browser)
961+
endif()
957962
set(VM_SOURCES_WKS_GEN
958-
${ARCH_SOURCES_DIR}/callhelpers-interp-to-managed.cpp
959-
${ARCH_SOURCES_DIR}/callhelpers-reverse.cpp
963+
${ARCH_SOURCES_DIR}/${WASM_THUNK_SUBDIR}/callhelpers-interp-to-managed.cpp
964+
${ARCH_SOURCES_DIR}/${WASM_THUNK_SUBDIR}/callhelpers-reverse.cpp
960965
)
961966
if (GEN_PINVOKE)
962967
list(APPEND VM_SOURCES_WKS_GEN
963-
${ARCH_SOURCES_DIR}/callhelpers-pinvoke.cpp
968+
${ARCH_SOURCES_DIR}/${WASM_THUNK_SUBDIR}/callhelpers-pinvoke.cpp
964969
)
965970
endif(GEN_PINVOKE)
966971
endif()

‎src/coreclr/vm/wasm/browser/callhelpers-interp-to-managed.cpp‎

Lines changed: 770 additions & 0 deletions
Large diffs are not rendered by default.

‎src/coreclr/vm/wasm/browser/callhelpers-pinvoke.cpp‎

Lines changed: 344 additions & 0 deletions
Large diffs are not rendered by default.

‎src/coreclr/vm/wasm/browser/callhelpers-reverse.cpp‎

Lines changed: 1290 additions & 0 deletions
Large diffs are not rendered by default.

‎src/coreclr/vm/wasm/wasi/callhelpers-interp-to-managed.cpp‎

Lines changed: 791 additions & 0 deletions
Large diffs are not rendered by default.

‎src/coreclr/vm/wasm/wasi/callhelpers-pinvoke.cpp‎

Lines changed: 444 additions & 0 deletions
Large diffs are not rendered by default.

‎src/coreclr/vm/wasm/wasi/callhelpers-reverse.cpp‎

Lines changed: 1137 additions & 0 deletions
Large diffs are not rendered by default.

‎src/mono/browser/build/BrowserWasmApp.CoreCLR.targets‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@
428428
ReversePInvokeOutputPath="$(_WasmReversePInvokeTablePath)"
429429
InterpToNativeOutputPath="$(_WasmInterpToNativeTablePath)"
430430
CacheFilePath="$(_WasmM2NCachePath)"
431+
TargetOS="browser"
431432
IsLibraryMode="$(_IsLibraryMode)">
432433
<Output TaskParameter="FileWrites" ItemName="FileWrites" />
433434
</ManagedToNativeGenerator>

‎src/tasks/WasmAppBuilder/coreclr/ManagedToNativeGenerator.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public class ManagedToNativeGenerator : Task
3333

3434
public bool IsLibraryMode { get; set; }
3535

36+
public string TargetOS { get; set; } = "browser";
37+
3638
[Output]
3739
public string[]? FileWrites { get; private set; }
3840

@@ -67,7 +69,7 @@ private void ExecuteInternal(LogAdapter log)
6769
{
6870
Dictionary<string, string> _symbolNameFixups = new();
6971
List<string> managedAssemblies = FilterOutUnmanagedBinaries(Assemblies);
70-
var pinvoke = new PInvokeTableGenerator(FixupSymbolName, log, IsLibraryMode);
72+
var pinvoke = new PInvokeTableGenerator(FixupSymbolName, log, IsLibraryMode, TargetOS);
7173
var internalCallCollector = new InternalCallSignatureCollector(log);
7274

7375
var resolver = new PathAssemblyResolver(managedAssemblies);

‎src/tasks/WasmAppBuilder/coreclr/PInvokeCollector.cs‎

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,14 @@ public int GetHashCode(PInvoke pinvoke)
6161

6262
internal sealed class PInvokeCollector {
6363
private readonly Dictionary<Assembly, bool> _assemblyDisableRuntimeMarshallingAttributeCache = new();
64-
private readonly Dictionary<Type, bool> _typeUnsupportedOnBrowserCache = new();
64+
private readonly Dictionary<Type, bool> _typeUnsupportedOnPlatformCache = new();
65+
private readonly string _targetOS;
6566
private LogAdapter Log { get; init; }
6667

67-
public PInvokeCollector(LogAdapter log)
68+
public PInvokeCollector(LogAdapter log, string targetOS = "browser")
6869
{
6970
Log = log;
71+
_targetOS = targetOS;
7072
}
7173

7274
public void CollectPInvokes(List<PInvoke> pinvokes, List<PInvokeCallback> callbacks, HashSet<string> signatures, Type type)
@@ -104,6 +106,9 @@ void CollectPInvokesForMethod(MethodInfo method)
104106
{
105107
if ((method.Attributes & MethodAttributes.PinvokeImpl) != 0)
106108
{
109+
if (IsUnsupportedOnPlatform(method.DeclaringType))
110+
return;
111+
107112
var dllimport = method.CustomAttributes.First(attr => attr.AttributeType.Name == "DllImportAttribute");
108113
var wasmLinkage = method.CustomAttributes.Any(attr => attr.AttributeType.Name == "WasmImportLinkageAttribute");
109114
var module = (string)dllimport.ConstructorArguments[0].Value!;
@@ -126,7 +131,7 @@ bool DoesMethodHaveCallbacks(MethodInfo method, LogAdapter log)
126131
if (!MethodHasCallbackAttributes(method))
127132
return false;
128133

129-
if (IsUnsupportedOnBrowser(method.DeclaringType))
134+
if (IsUnsupportedOnPlatform(method.DeclaringType))
130135
return false;
131136

132137
if (TryIsMethodGetParametersUnsupported(method, out string? reason))
@@ -213,23 +218,23 @@ private bool HasAssemblyDisableRuntimeMarshallingAttribute(Assembly assembly)
213218
return value;
214219
}
215220

216-
private bool IsUnsupportedOnBrowser(Type? type)
221+
private bool IsUnsupportedOnPlatform(Type? type)
217222
{
218223
if (type is null)
219224
return false;
220225

221-
if (!_typeUnsupportedOnBrowserCache.TryGetValue(type, out bool value))
226+
if (!_typeUnsupportedOnPlatformCache.TryGetValue(type, out bool value))
222227
{
223228
value = false;
224229
bool hasSupportedOSPlatform = false;
225-
bool hasSupportedBrowser = false;
230+
bool hasSupportedTarget = false;
226231
foreach (CustomAttributeData cattr in CustomAttributeData.GetCustomAttributes(type))
227232
{
228233
try
229234
{
230235
if (cattr.AttributeType.FullName == "System.Runtime.Versioning.UnsupportedOSPlatformAttribute" &&
231236
cattr.ConstructorArguments.Count > 0 &&
232-
cattr.ConstructorArguments[0].Value?.ToString() == "browser")
237+
cattr.ConstructorArguments[0].Value?.ToString() == _targetOS)
233238
{
234239
value = true;
235240
break;
@@ -238,8 +243,8 @@ private bool IsUnsupportedOnBrowser(Type? type)
238243
cattr.ConstructorArguments.Count > 0)
239244
{
240245
hasSupportedOSPlatform = true;
241-
if (cattr.ConstructorArguments[0].Value?.ToString() == "browser")
242-
hasSupportedBrowser = true;
246+
if (cattr.ConstructorArguments[0].Value?.ToString() == _targetOS)
247+
hasSupportedTarget = true;
243248
}
244249
}
245250
catch
@@ -248,10 +253,10 @@ private bool IsUnsupportedOnBrowser(Type? type)
248253
}
249254
}
250255

251-
if (!value && hasSupportedOSPlatform && !hasSupportedBrowser)
256+
if (!value && hasSupportedOSPlatform && !hasSupportedTarget)
252257
value = true;
253258

254-
_typeUnsupportedOnBrowserCache[type] = value;
259+
_typeUnsupportedOnPlatformCache[type] = value;
255260
}
256261

257262
return value;

0 commit comments

Comments
 (0)