Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions dotnet/targets/Xamarin.Shared.Sdk.targets
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,19 @@

<!-- Directory to store project specific stamp files used by certain targets for incremental builds -->
<_MaciOSStampDirectory>$(IntermediateOutputPath)stamp\</_MaciOSStampDirectory>

<!-- Our own runtime features -->

<!-- Set default DisposeTaggedPointers value, we stop disposing them by default in .NET 10 -->
<!-- The default logic here must match the default logic in NSObject.DisposeTaggedPointers -->
<DisposeTaggedPointers Condition="'$(DisposeTaggedPointers)' == '' And $([MSBuild]::VersionGreaterThanOrEquals($(TargetFrameworkVersion), 10.0))">false</DisposeTaggedPointers>
<DisposeTaggedPointers Condition="'$(DisposeTaggedPointers)' == ''">true</DisposeTaggedPointers>
</PropertyGroup>

<ItemGroup>
<RuntimeHostConfigurationOption Include="Foundation.NSObject.DisposeTaggedPointers" Condition="'$(DisposeTaggedPointers)' != ''" Value="$(DisposeTaggedPointers)" Trim="true" />
</ItemGroup>

<PropertyGroup>
<TargetPlatformSupported Condition=" '$(TargetPlatformIdentifier)' == '$(_PlatformName)' ">true</TargetPlatformSupported>
</PropertyGroup>
Expand Down
52 changes: 51 additions & 1 deletion src/Foundation/NSObject2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -991,14 +991,64 @@ internal void ClearHandle ()
handle = NativeHandle.Zero;
}

// This option is changed by setting the DisposeTaggedPointers MSBuild property in the project file.
static bool? dispose_tagged_pointers;
static bool DisposeTaggedPointers {
get {
if (!dispose_tagged_pointers.HasValue) {
if (AppContext.TryGetSwitch ("Foundation.NSObject.DisposeTaggedPointers", out var dtp)) {
dispose_tagged_pointers = dtp;
} else {
// The default logic here must match how we set the default value for the DisposeTaggedPointers MSBuild property.
#if NET10_0_OR_GREATER
dispose_tagged_pointers = false;
#else
dispose_tagged_pointers = true;
#endif
}
}
return dispose_tagged_pointers.Value;
}
}

/// <include file="../../docs/api/Foundation/NSObject.xml" path="/Documentation/Docs[@DocId='M:Foundation.NSObject.Dispose(System.Boolean)']/*" />
protected virtual void Dispose (bool disposing)
{
if (disposed)
return;
disposed = true;

if (handle != NativeHandle.Zero) {
var isTaggedPointerThatShouldNotBeDisposed = false;
if (!DisposeTaggedPointers) {
/* Tagged pointers are limited to 64bit, which is all we support anyway.
*
* The bit that identifies if a pointer is a tagged pointer is:
*
* Arm64 (everywhere): most significant bit
* Simulators (both on arm64 and x64 desktops): most significant bit
* Desktop/x64 (macOS + Mac Catalyst): least significant bit
*
* Ref: https://github.com/apple-oss-distributions/objc4/blob/89543e2c0f67d38ca5211cea33f42c51500287d5/runtime/objc-internal.h#L603-L672
*/
#if __MACOS__ || __MACCATALYST__
ulong _OBJC_TAG_MASK;
if (Runtime.IsARM64CallingConvention) {
_OBJC_TAG_MASK = 1UL << 63;
} else {
_OBJC_TAG_MASK = 1UL;
}
#else
const ulong _OBJC_TAG_MASK = 1UL << 63;
#endif

unchecked {
var ulongHandle = (ulong) (IntPtr) handle;
var isTaggedPointer = (ulongHandle & _OBJC_TAG_MASK) == _OBJC_TAG_MASK;
isTaggedPointerThatShouldNotBeDisposed = isTaggedPointer;
}
}

if (!isTaggedPointerThatShouldNotBeDisposed && handle != NativeHandle.Zero) {
if (disposing) {
ReleaseManagedRef ();
} else {
Expand Down
4 changes: 4 additions & 0 deletions src/ILLink.Substitutions.MacCatalyst.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<linker>
<assembly fullname="Microsoft.MacCatalyst">
<type fullname="Foundation.NSObject">
<method signature="System.Boolean get_DisposeTaggedPointers()" body="stub" feature="Foundation.NSObject.DisposeTaggedPointers" featurevalue="false" value="false" />
<method signature="System.Boolean get_DisposeTaggedPointers()" body="stub" feature="Foundation.NSObject.DisposeTaggedPointers" featurevalue="true" value="true" />
</type>
<type fullname="ObjCRuntime.Dlfcn">
<method signature="System.Void WarnOnce()" body="stub" feature="System.Diagnostics.Debugger.IsSupported" featurevalue="false" />
</type>
Expand Down
4 changes: 4 additions & 0 deletions src/ILLink.Substitutions.ios.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<linker>
<assembly fullname="Microsoft.iOS">
<type fullname="Foundation.NSObject">
<method signature="System.Boolean get_DisposeTaggedPointers()" body="stub" feature="Foundation.NSObject.DisposeTaggedPointers" featurevalue="false" value="false" />
<method signature="System.Boolean get_DisposeTaggedPointers()" body="stub" feature="Foundation.NSObject.DisposeTaggedPointers" featurevalue="true" value="true" />
</type>
<type fullname="ObjCRuntime.Dlfcn">
<method signature="System.Void WarnOnce()" body="stub" feature="System.Diagnostics.Debugger.IsSupported" featurevalue="false" />
</type>
Expand Down
4 changes: 4 additions & 0 deletions src/ILLink.Substitutions.macOS.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<linker>
<assembly fullname="Microsoft.macOS">
<type fullname="Foundation.NSObject">
<method signature="System.Boolean get_DisposeTaggedPointers()" body="stub" feature="Foundation.NSObject.DisposeTaggedPointers" featurevalue="false" value="false" />
<method signature="System.Boolean get_DisposeTaggedPointers()" body="stub" feature="Foundation.NSObject.DisposeTaggedPointers" featurevalue="true" value="true" />
</type>
<type fullname="ObjCRuntime.Dlfcn">
<method signature="System.Void WarnOnce()" body="stub" feature="System.Diagnostics.Debugger.IsSupported" featurevalue="false" />
</type>
Expand Down
4 changes: 4 additions & 0 deletions src/ILLink.Substitutions.tvos.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<linker>
<assembly fullname="Microsoft.tvOS">
<type fullname="Foundation.NSObject">
<method signature="System.Boolean get_DisposeTaggedPointers()" body="stub" feature="Foundation.NSObject.DisposeTaggedPointers" featurevalue="false" value="false" />
<method signature="System.Boolean get_DisposeTaggedPointers()" body="stub" feature="Foundation.NSObject.DisposeTaggedPointers" featurevalue="true" value="true" />
</type>
<type fullname="ObjCRuntime.Dlfcn">
<method signature="System.Void WarnOnce()" body="stub" feature="System.Diagnostics.Debugger.IsSupported" featurevalue="false" />
</type>
Expand Down
2 changes: 1 addition & 1 deletion tests/common/shared-dotnet.mk
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ endif
PLATFORM_UPPERCASE:=$(shell echo $(PLATFORM) | tr 'a-z' 'A-Z')

ifneq ($(RUNTIMEIDENTIFIERS)$(RUNTIMEIDENTIFIER),)
$(error "Don't set RUNTIMEIDENTIFIER or RUNTIMEIDENTIFIERS, set RID instead")
$(error "Don't set RUNTIMEIDENTIFIER or RUNTIMEIDENTIFIERS, set RID instead (RUNTIMEIDENTIFIER=$(RUNTIMEIDENTIFIER), RUNTIMEIDENTIFIERS=$(RUNTIMEIDENTIFIERS))")
endif

ifeq ($(RID),)
Expand Down
93 changes: 93 additions & 0 deletions tests/dotnet/DisposeTaggedPointersTestApp/AppDelegate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System;
using System.Runtime.InteropServices;

using Foundation;

namespace DisposeTaggedPointersTestApp {
public class Program {
static int Main (string [] args)
{
var testCaseString = Environment.GetEnvironmentVariable ("TEST_CASE");
if (string.IsNullOrEmpty (testCaseString)) {
Console.WriteLine ($"The environment variable TEST_CASE wasn't set.");
return 2;
}
#if NET10_0_OR_GREATER
var taggedPointersDisposedByDefault = false;
#else
var taggedPointersDisposedByDefault = true;
#endif
switch (testCaseString) {
case "DisableWithAppContext":
AppContext.SetSwitch ("Foundation.NSObject.DisposeTaggedPointers", false);
if (ThrowsObjectDisposedExceptions ()) {
Console.WriteLine ($"❌ {testCaseString}: Failure: unexpected ObjectDisposedException when DisposeTaggedPointers=false");
return 1;
}

Console.WriteLine ($"✅ {testCaseString}: Success");
return 0;
case "EnableWithAppContext":
AppContext.SetSwitch ("Foundation.NSObject.DisposeTaggedPointers", true);
if (!ThrowsObjectDisposedExceptions ()) {
Console.WriteLine ($"❌ {testCaseString}: Failure: unexpected lack of ObjectDisposedException when DisposeTaggedPointers=true");
return 1;
}

Console.WriteLine ($"✅ {testCaseString}: Success");
return 0;
case "DisableWithMSBuildPropertyTrimmed":
if (ThrowsObjectDisposedExceptions ()) {
Console.WriteLine ($"❌ {testCaseString}: Failure: unexpected ObjectDisposedException when DisposeTaggedPointers=false");
return 1;
}

Console.WriteLine ($"✅ {testCaseString}: Success");
return 0;
case "EnableWithMSBuildPropertyTrimmed":
if (!ThrowsObjectDisposedExceptions ()) {
Console.WriteLine ($"❌ {testCaseString}: Failure: unexpected lack of ObjectDisposedException when DisposeTaggedPointers=true");
return 1;
}

Console.WriteLine ($"✅ {testCaseString}: Success");
return 0;
case "DisableWithMSBuildPropertyUntrimmed": {
var throws = ThrowsObjectDisposedExceptions ();
if (throws == taggedPointersDisposedByDefault) {
Console.WriteLine ($"❌ {testCaseString}: Failure: unexpected ObjectDisposedException when DisposeTaggedPointers=false");
return 1;
}

Console.WriteLine ($"✅ {testCaseString}: Success");
return 0;
}
case "EnableWithMSBuildPropertyUntrimmed": {
var throws = ThrowsObjectDisposedExceptions ();
if (throws != taggedPointersDisposedByDefault) {
Console.WriteLine ($"❌ {testCaseString}: Failure: unexpected lack of ObjectDisposedException when DisposeTaggedPointers=true");
return 1;
}

Console.WriteLine ($"✅ {testCaseString}: Success");
return 0;
}
case "Default": {
var throws = ThrowsObjectDisposedExceptions ();
if (throws != taggedPointersDisposedByDefault) {
Console.WriteLine ($"❌ {testCaseString}: Failure: Expected ObjectDisposedException: {taggedPointersDisposedByDefault} Threw ObjectDisposedException: {throws}");
return 1;
}

Console.WriteLine ($"✅ {testCaseString}: Success");
return 0;
}
default:
Console.WriteLine ($"❌ Unknown test case: {testCaseString}");
return 2;
}
}

static bool ThrowsObjectDisposedExceptions () => MonoTouchFixtures.ObjCRuntime.TaggedPointerTest.ThrowsObjectDisposedExceptions ();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(SetAppendRuntimeIdentifierToOutputPathToFalse)' == 'true'">
<!-- Used by the AppendRuntimeIdentifierToOutputPath_* tests -->
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
</PropertyGroup>
<Import Project="$(MSBuildThisFileDirectory)../../Directory.Build.props" />
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net$(BundledNETCoreAppTargetFrameworkVersion)-maccatalyst</TargetFramework>
</PropertyGroup>
<Import Project="..\shared.csproj" />
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../shared.mk
2 changes: 2 additions & 0 deletions tests/dotnet/DisposeTaggedPointersTestApp/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
TOP=../../..
include $(TOP)/tests/common/shared-dotnet-test.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net$(BundledNETCoreAppTargetFrameworkVersion)-ios</TargetFramework>
</PropertyGroup>
<Import Project="..\shared.csproj" />
</Project>
1 change: 1 addition & 0 deletions tests/dotnet/DisposeTaggedPointersTestApp/iOS/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../shared.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net$(BundledNETCoreAppTargetFrameworkVersion)-macos</TargetFramework>
</PropertyGroup>
<Import Project="..\shared.csproj" />
</Project>
1 change: 1 addition & 0 deletions tests/dotnet/DisposeTaggedPointersTestApp/macOS/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../shared.mk
18 changes: 18 additions & 0 deletions tests/dotnet/DisposeTaggedPointersTestApp/shared.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup>
<OutputType>Exe</OutputType>

<ApplicationTitle>DisposeTaggedPointersTestApp</ApplicationTitle>
<ApplicationId>com.xamarin.disposetaggedpointerstestapp</ApplicationId>

<UseInterpreter>true</UseInterpreter> <!-- to speed up the test run -->
</PropertyGroup>

<Import Project="../../common/shared-dotnet.csproj" />

<ItemGroup>
<Compile Include="../*.cs" />
<Compile Include="../../../monotouch-test/ObjCRuntime/TaggedPointerTest.Shared.cs" />
</ItemGroup>
</Project>
44 changes: 44 additions & 0 deletions tests/dotnet/DisposeTaggedPointersTestApp/shared.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
TOP=../../../..
TESTNAME=DisposeTaggedPointersTestApp
include $(TOP)/tests/common/shared-dotnet.mk

export RUNTIMEIDENTIFIER=
export RUNTIMEIDENTIFIERS=

.stamp-restore:
$(Q) $(DOTNET) restore
$(Q) touch $@

define Test
clean-$(2):
$(Q) rm -f .stamp-build-$(2)
$(Q) rm -rf bin/$(1) obj/$(1)
CLEAN_TARGETS+=clean-$(2)

.stamp-build-$(2): .stamp-restore
$(MAKE) build CONFIG=$(1) BUILD_ARGUMENTS="/tl:off $(3)"
$(Q) touch $$@
BUILD_TARGETS+=.stamp-build-$(2)

run-$(2): export TEST_CASE=$(1)
run-$(2): .stamp-build-$(2)
$(MAKE) run-bare CONFIG=$(1)
RUN_TARGETS+=run-$(2)

test-$(2): run-$(2)
endef

$(eval $(call Test,Default,default))
$(eval $(call Test,DisableWithAppContext,disablewithappcontext))
$(eval $(call Test,EnableWithAppContext,enablewithappcontext))
$(eval $(call Test,DisableWithMSBuildPropertyUntrimmed,disablewithmsbuildpropertyuntrimmed,/p:DisposeTaggedPointers=false /p:_LinkMode=None))
$(eval $(call Test,EnableWithMSBuildPropertyUntrimmed,enablewithmsbuildpropertyuntrimmed,/p:DisposeTaggedPointers=true /p:_LinkMode=None))
$(eval $(call Test,DisableWithMSBuildPropertyTrimmed,disablewithmsbuildpropertytrimmed,/p:DisposeTaggedPointers=false /p:_LinkMode=SdkOnly))
$(eval $(call Test,EnableWithMSBuildPropertyTrimmed,enablewithmsbuildpropertytrimmed,/p:DisposeTaggedPointers=true /p:_LinkMode=SdkOnly))

build-all: $(BUILD_TARGETS)
clean-all:: $(CLEAN_TARGETS)
$(Q) rm -f .stamp-* *.binlog
test-all run-tests run-all run: $(RUN_TARGETS)

.PHONY: $(TEST_CASES)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net$(BundledNETCoreAppTargetFrameworkVersion)-tvos</TargetFramework>
</PropertyGroup>
<Import Project="..\shared.csproj" />
</Project>
1 change: 1 addition & 0 deletions tests/dotnet/DisposeTaggedPointersTestApp/tvOS/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../shared.mk
25 changes: 25 additions & 0 deletions tests/dotnet/UnitTests/ProjectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3727,6 +3727,31 @@ public void InvalidSupportedOSPlatformVersion (ApplePlatform platform, string ru
AssertErrorMessages (errors, $"The SupportedOSPlatformVersion value '{version}' in the project file is lower than the minimum value '{minVersion}'.");
}


[TestCase (ApplePlatform.MacOSX)]
[TestCase (ApplePlatform.MacCatalyst)]
public void DisposeTaggedPointersTest (ApplePlatform platform)
{
var project = "DisposeTaggedPointersTestApp";
Configuration.IgnoreIfIgnoredPlatform (platform);

var project_path = GetProjectPath (project, platform: platform);
Clean (project_path);

var arguments = new string [] {
"-C", Path.GetDirectoryName (project_path)!,
"clean-all"
};
AssertExecute ("make", arguments, out var _);

arguments = new string [] {
"-C", Path.GetDirectoryName (project_path)!,
"test-all",
"-j",
};
AssertExecute ("make", arguments, out var _);
}

// macOS doesn't support UseNativeHttpHandler / any of our native http handlers being the default http handler.
[Test]
[TestCase (ApplePlatform.MacCatalyst, "NSUrlSessionHandler", "true")]
Expand Down
Loading
Loading