-
Notifications
You must be signed in to change notification settings - Fork 576
[tools] Add a tool to launch processes disclaming responsibility. #24249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rolfbjarne
wants to merge
16
commits into
main
Choose a base branch
from
dev/rolf/spawner
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
071a5d1
[tools] Add a tool to launch processes disclaming responsibility.
rolfbjarne 66dad41
Auto-format source code
4b8eabf
[devops] Build the spawner before running the tests.
rolfbjarne ad04bca
For command-line things too.
rolfbjarne 773dd92
Merge remote-tracking branch 'origin/main' into dev/rolf/spawner
rolfbjarne 03a79fc
spawner needs Xcode
rolfbjarne 955ed8d
improvements
rolfbjarne e6f925a
Merge branch 'main' into dev/rolf/spawner
rolfbjarne 027f8af
Address review feedback on spawner
rolfbjarne 1537bcd
[tools] Fix building the spawner when macOS is a disabled platform
rolfbjarne d3fc1fd
[xharness] Improve diagnostic output a little bit.
rolfbjarne a25b162
Merge remote-tracking branch 'origin/main' into dev/rolf/spawner
rolfbjarne b95622a
[devops] Lower timeout for tests to 2h45m.
rolfbjarne e09f46f
[xharness] Disable system-permission tests by default on Mac Catalyst…
rolfbjarne ccc70fb
[tests] Avoid writing to the Documents directory in FileManagerTest.G…
rolfbjarne bb4002f
[tests] Avoid writing to the Documents directory in UrlTest and Audio…
rolfbjarne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,4 +14,8 @@ SUBDIRS+=mlaunch | |
|
|
||
| SUBDIRS += dotnet-linker | ||
|
|
||
| ifndef NO_XCODE | ||
| SUBDIRS += spawner | ||
| endif | ||
|
|
||
| SUBDIRS += assembly-preparer | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| .libs | ||
| spawner | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| TOP=../.. | ||
| include $(TOP)/Make.config | ||
| include $(TOP)/mk/rules.mk | ||
|
|
||
| # The spawner is a host tool: it always runs on the macOS build machine, and is | ||
| # needed regardless of which platforms are enabled. Don't use the generic | ||
| # per-RID rules from mk/rules.mk, because those are only defined for the | ||
| # platforms that are enabled - so when macOS is disabled (which is the case for | ||
| # most of the test jobs in CI) the .libs/osx-*/spawner.o rules don't exist, and | ||
| # the build fails with "No rule to make target '.libs/osx-arm64/spawner.o'". | ||
| # For the same reason we compute the macOS SDK path directly instead of relying | ||
| # on $(macos_SDK). | ||
|
|
||
| SPAWNER_ARCHS=arm64 x86_64 | ||
| SPAWNER_SDK=$(shell $(XCODE_DEVELOPER_ROOT)/usr/bin/xcodebuild -version -sdk macosx Path 2>/dev/null) | ||
|
|
||
| define SpawnerTemplate | ||
| .libs/$(1): | ||
| $$(Q) mkdir -p $$@ | ||
|
|
||
| .libs/$(1)/spawner.o: spawner.c | .libs/$(1) | ||
| $$(Q_CC) $$(CLANG) -arch $(1) -isysroot $$(SPAWNER_SDK) -c $$< -o $$@ | ||
|
|
||
| .libs/$(1)/spawner: .libs/$(1)/spawner.o | ||
| $$(Q_CCLD) $$(CLANG) -arch $(1) -isysroot $$(SPAWNER_SDK) $$< -o $$@ | ||
| endef | ||
| $(foreach arch,$(SPAWNER_ARCHS),$(eval $(call SpawnerTemplate,$(arch)))) | ||
|
|
||
| spawner: $(foreach arch,$(SPAWNER_ARCHS),.libs/$(arch)/spawner) | ||
| $(Q_LIPO) $(LIPO) $^ -create -output $@ | ||
| $(Q) chmod +x $@ | ||
|
|
||
| all-local:: spawner |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| SPAWNER | ||
| ======= | ||
|
|
||
| This is a very simple tool, which executes another process, disclaiming any | ||
| responsibility for it. | ||
|
|
||
| This is important when executing tests apps, because when macOS sees that an | ||
| app uses API that needs specific entries in the Info.plist (such as the | ||
| `NSAppleMusicUsageDescription`), the responsible process is where macOS looks | ||
| for said key. | ||
|
|
||
| Example crash report: | ||
|
|
||
| ``` | ||
| Process: introspection [85822] | ||
| Path: /Users/USER/*/introspection.app/Contents/MacOS/introspection | ||
| Identifier: com.xamarin.introspection | ||
| Version: 1.0 (1.0) | ||
| Code Type: ARM-64 (Native) | ||
| Parent Process: dotnet [81129] | ||
| Responsible: Electron [68966] | ||
| User ID: 501 | ||
|
|
||
| Date/Time: 2025-11-13 17:33:10.8123 +0100 | ||
| OS Version: macOS 15.7.2 (24G325) | ||
| Report Version: 12 | ||
| Anonymous UUID: F22C0F06-0F16-E475-C0CB-264A0FF4F6A3 | ||
|
|
||
|
|
||
| Time Awake Since Boot: 27000 seconds | ||
|
|
||
| System Integrity Protection: enabled | ||
|
|
||
| Crashed Thread: 16 Dispatch queue: com.apple.root.default-qos | ||
|
|
||
| Exception Type: EXC_CRASH (SIGKILL) | ||
| Exception Codes: 0x0000000000000000, 0x0000000000000000 | ||
|
|
||
| Termination Reason: Namespace TCC, Code 0 | ||
| This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSAppleMusicUsageDescription key with a string value explaining to the user how the app uses this data. | ||
| ``` | ||
|
|
||
| The app crashed because macOS says it needs the `NSAppleMusicUsageDescription` entry in its `Info.plist` file. | ||
|
|
||
| This is confusing, because introspection _has_ an `NSAppleMusicUsageDescription` entry in its `Info.plist` file. | ||
|
|
||
| Here's what happens: | ||
|
|
||
| Note that there's a "Responsible [Process]" (Electron 68966) line, which is not the same as "Process" (introspection 85822), and this is the crux of the matter. | ||
|
|
||
| In this particular case: | ||
|
|
||
| * I opened the xharness project in VSCode. | ||
| * I launched the xharness project in the debugger, and then ran introspection for Mac Catalyst. | ||
| * The responsible process ended up being VS Code (aka Electron, with pid 68966), and that's where macOS ended up looking for the `NSAppleMusicUsageDescription` key. | ||
|
|
||
| The fix is to launch `introspection` (and any other test app on macOS) using | ||
| this `spawner` tool, which disclaims responsibility for anything it launches, | ||
| thus letting `introspection` be a grown up process and fully responsible for | ||
| itself. | ||
|
|
||
| Usage is simple: just pass the executable + any arguments to `spawner`. | ||
|
|
||
| References: | ||
|
|
||
| * https://gitlab.com/gnachman/iterm2/-/issues/10360 | ||
| * https://github.com/llvm/llvm-project/commit/041c7b84a4b925476d1e21ed302786033bb6035f#diff-a38ae411ccf0c85f3d7c0c45d8e1ad035030d5171d59e478b86a094941d3209dR16-R17 | ||
| * https://lldb.llvm.org/cpp_reference/PosixSpawnResponsible_8h_source.html | ||
| * https://steipete.me/posts/2025/applescript-cli-macos-complete-guide |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #include <errno.h> | ||
| #include <signal.h> | ||
| #include <spawn.h> | ||
| #include <stdbool.h> | ||
| #include <stdio.h> | ||
| #include <string.h> | ||
|
|
||
| errno_t responsibility_spawnattrs_setdisclaim (posix_spawnattr_t *attrs, bool disclaim); | ||
|
|
||
| int main (int argc, char** argv, char** envp) | ||
| { | ||
| if (argc < 2) { | ||
| fprintf (stderr, | ||
| "spawner: launch a subprocess, disclaiming all responsibilities with regards to TCC:\n" | ||
| "usage: spawner <command> [arguments]\n"); | ||
| return 1; | ||
| } | ||
|
|
||
| int rv; | ||
| // Behave as exec | ||
| short flags = POSIX_SPAWN_SETEXEC; | ||
| posix_spawnattr_t spawnattr; | ||
| sigset_t sigset; | ||
|
|
||
| rv = posix_spawnattr_init (&spawnattr); | ||
| if (rv) { | ||
| fprintf (stderr, "Failed to execute 'posix_spawnattr_init': %i (%s)\n", rv, strerror (rv)); | ||
| return 1; | ||
| } | ||
|
|
||
| // Reset the signal mask | ||
| sigemptyset (&sigset); | ||
| rv = posix_spawnattr_setsigmask (&spawnattr, &sigset); | ||
| if (rv) { | ||
| fprintf (stderr, "Failed to execute 'posix_spawnattr_setsigmask': %i (%s)\n", rv, strerror (rv)); | ||
| return 1; | ||
| } | ||
| flags |= POSIX_SPAWN_SETSIGMASK; | ||
|
|
||
| // Reset all signals to their default handlers | ||
| sigfillset (&sigset); | ||
| rv = posix_spawnattr_setsigdefault (&spawnattr, &sigset); | ||
| if (rv) { | ||
| fprintf (stderr, "Failed to execute 'posix_spawnattr_setsigdefault': %i (%s)\n", rv, strerror (rv)); | ||
| return 1; | ||
| } | ||
| flags |= POSIX_SPAWN_SETSIGDEF; | ||
|
|
||
| rv = posix_spawnattr_setflags (&spawnattr, flags); | ||
| if (rv) { | ||
| fprintf (stderr, "Failed to execute 'posix_spawnattr_setflags': %i (%s)\n", rv, strerror (rv)); | ||
| return 1; | ||
| } | ||
|
|
||
| rv = responsibility_spawnattrs_setdisclaim (&spawnattr, 1); | ||
| if (rv) { | ||
| fprintf (stderr, "Failed to execute 'responsibility_spawnattrs_setdisclaim': %i (%s)\n", rv, strerror (rv)); | ||
| return 1; | ||
| } | ||
|
rolfbjarne marked this conversation as resolved.
|
||
|
|
||
| pid_t pid = 0; | ||
| rv = posix_spawnp (&pid, argv [1], NULL, &spawnattr, argv + 1, envp); | ||
| posix_spawnattr_destroy (&spawnattr); | ||
|
|
||
| // posix_spawnp shouldn't return (because we set the POSIX_SPAWN_SETEXEC flag) | ||
| // so if it did, something went wrong | ||
| fprintf (stderr, "Failed to execute '%s': %i (%s)\n", argv [1], rv, strerror (rv)); | ||
| return 1; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.