Skip to content

Commit

Permalink
Bundle libMainThreadChecker.dylib to detect main thread violations (#…
Browse files Browse the repository at this point in the history
…2277)

This PR introduces the integration of libMainThreadChecker.dylib to
enhance quality checks by detecting main thread violations. By passing
the feature=include_main_thread_checker flag, the library
`libMainThreadChecker.dylib` will be embedded. Additionally,
modifications to the test runner implementation ensure the
DYLD_INSERT_LIBRARIES is updated to recognize this dylib. This aims to
bolster both unit and UI test reliability.

https://developer.apple.com/documentation/xcode/diagnosing-memory-thread-and-crash-issues-early
  • Loading branch information
chiragramani authored Mar 13, 2024
1 parent 48476c1 commit cc39679
Show file tree
Hide file tree
Showing 23 changed files with 765 additions and 1 deletion.
7 changes: 7 additions & 0 deletions apple/internal/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ apple_mac_tools_toolchain(
dsym_info_plist_template = "//apple/internal/templates:dsym_info_plist_template",
environment_plist_tool = "//tools/environment_plist",
imported_dynamic_framework_processor = "//tools/imported_dynamic_framework_processor",
main_thread_checker_tool = "//tools/main_thread_checker_tool",
plisttool = "//tools/plisttool",
process_and_sign_template = "//tools/bundletool:process_and_sign_template",
provisioning_profile_tool = "//tools/provisioning_profile_tool",
Expand Down Expand Up @@ -319,6 +320,7 @@ bzl_library(
"//apple/internal/aspects:framework_provider_aspect",
"//apple/internal/aspects:resource_aspect",
"//apple/internal/utils:clang_rt_dylibs",
"//apple/internal/utils:main_thread_checker_dylibs",
"@bazel_skylib//lib:collections",
"@bazel_tools//tools/cpp:toolchain_utils.bzl",
"@build_bazel_rules_swift//swift",
Expand Down Expand Up @@ -393,6 +395,7 @@ bzl_library(
"//apple/internal/aspects:framework_provider_aspect",
"//apple/internal/aspects:resource_aspect",
"//apple/internal/utils:clang_rt_dylibs",
"//apple/internal/utils:main_thread_checker_dylibs",
],
)

Expand Down Expand Up @@ -431,6 +434,7 @@ bzl_library(
"//apple/internal/partials:framework_import",
"//apple/internal/partials:framework_provider",
"//apple/internal/partials:macos_additional_contents",
"//apple/internal/partials:main_thread_checker_dylibs",
"//apple/internal/partials:messages_stub",
"//apple/internal/partials:provisioning_profile",
"//apple/internal/partials:resources",
Expand Down Expand Up @@ -663,6 +667,7 @@ bzl_library(
"//apple/internal/aspects:framework_provider_aspect",
"//apple/internal/aspects:resource_aspect",
"//apple/internal/utils:clang_rt_dylibs",
"//apple/internal/utils:main_thread_checker_dylibs",
"@bazel_tools//tools/cpp:toolchain_utils.bzl",
"@build_bazel_rules_swift//swift",
],
Expand Down Expand Up @@ -698,6 +703,7 @@ bzl_library(
"//apple/internal/aspects:framework_provider_aspect",
"//apple/internal/aspects:resource_aspect",
"//apple/internal/utils:clang_rt_dylibs",
"//apple/internal/utils:main_thread_checker_dylibs",
"@bazel_skylib//lib:sets",
"@bazel_tools//tools/cpp:toolchain_utils.bzl",
],
Expand Down Expand Up @@ -732,6 +738,7 @@ bzl_library(
"//apple/internal/aspects:framework_provider_aspect",
"//apple/internal/aspects:resource_aspect",
"//apple/internal/utils:clang_rt_dylibs",
"//apple/internal/utils:main_thread_checker_dylibs",
"@bazel_skylib//lib:sets",
"@bazel_tools//tools/cpp:toolchain_utils.bzl",
],
Expand Down
13 changes: 13 additions & 0 deletions apple/internal/apple_toolchains.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ A `struct` from `ctx.resolve_tools` referencing a tool to process an imported dy
such that the given framework only contains the same slices as the app binary, every file belonging
to the dynamic framework is copied to a temporary location, and the dynamic framework is codesigned
and zipped as a cacheable artifact.
""",
"resolved_main_thread_checker_tool": """\
A `struct` from `ctx.resolve_tools` referencing a tool to find libMainThreadChecker.dylib linked to a
binary.
""",
"resolved_plisttool": """\
A `struct` from `ctx.resolve_tools` referencing a tool to perform plist operations such as variable
Expand Down Expand Up @@ -168,6 +172,10 @@ def _apple_mac_tools_toolchain_impl(ctx):
attr_name = "clangrttool",
rule_ctx = ctx,
),
resolved_main_thread_checker_tool = _resolve_tools_for_executable(
attr_name = "main_thread_checker_tool",
rule_ctx = ctx,
),
resolved_environment_plist_tool = _resolve_tools_for_executable(
attr_name = "environment_plist_tool",
rule_ctx = ctx,
Expand Down Expand Up @@ -255,6 +263,11 @@ copied to a temporary location, and the dynamic framework is codesigned and zipp
artifact.
""",
),
"main_thread_checker_tool": attr.label(
cfg = "target",
executable = True,
doc = "A `File` referencing a tool to find libMainThreadChecker.dylib linked to a binary.",
),
"plisttool": attr.label(
cfg = "target",
executable = True,
Expand Down
58 changes: 58 additions & 0 deletions apple/internal/ios_rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ load(
"@build_bazel_rules_apple//apple/internal/utils:clang_rt_dylibs.bzl",
"clang_rt_dylibs",
)
load(
"@build_bazel_rules_apple//apple/internal/utils:main_thread_checker_dylibs.bzl",
"main_thread_checker_dylibs",
)
load(
"@build_bazel_rules_apple//apple/internal:apple_product_type.bzl",
"apple_product_type",
Expand Down Expand Up @@ -319,6 +323,15 @@ def _ios_application_impl(ctx):
platform_prerequisites = platform_prerequisites,
dylibs = clang_rt_dylibs.get_from_toolchain(ctx),
),
partials.main_thread_checker_dylibs_partial(
actions = actions,
apple_mac_toolchain_info = apple_mac_toolchain_info,
binary_artifact = binary_artifact,
features = features,
label_name = label.name,
platform_prerequisites = platform_prerequisites,
dylibs = main_thread_checker_dylibs.get_from_toolchain(ctx),
),
partials.debug_symbols_partial(
actions = actions,
bundle_extension = bundle_extension,
Expand Down Expand Up @@ -651,6 +664,15 @@ def _ios_app_clip_impl(ctx):
platform_prerequisites = platform_prerequisites,
dylibs = clang_rt_dylibs.get_from_toolchain(ctx),
),
partials.main_thread_checker_dylibs_partial(
actions = actions,
apple_mac_toolchain_info = apple_mac_toolchain_info,
binary_artifact = binary_artifact,
features = features,
label_name = label.name,
platform_prerequisites = platform_prerequisites,
dylibs = main_thread_checker_dylibs.get_from_toolchain(ctx),
),
partials.debug_symbols_partial(
actions = actions,
bundle_extension = bundle_extension,
Expand Down Expand Up @@ -946,6 +968,15 @@ def _ios_framework_impl(ctx):
platform_prerequisites = platform_prerequisites,
dylibs = clang_rt_dylibs.get_from_toolchain(ctx),
),
partials.main_thread_checker_dylibs_partial(
actions = actions,
apple_mac_toolchain_info = apple_mac_toolchain_info,
binary_artifact = binary_artifact,
features = features,
label_name = label.name,
platform_prerequisites = platform_prerequisites,
dylibs = main_thread_checker_dylibs.get_from_toolchain(ctx),
),
partials.debug_symbols_partial(
actions = actions,
bundle_extension = bundle_extension,
Expand Down Expand Up @@ -1234,6 +1265,15 @@ def _ios_extension_impl(ctx):
platform_prerequisites = platform_prerequisites,
dylibs = clang_rt_dylibs.get_from_toolchain(ctx),
),
partials.main_thread_checker_dylibs_partial(
actions = actions,
apple_mac_toolchain_info = apple_mac_toolchain_info,
binary_artifact = binary_artifact,
features = features,
label_name = label.name,
platform_prerequisites = platform_prerequisites,
dylibs = main_thread_checker_dylibs.get_from_toolchain(ctx),
),
partials.debug_symbols_partial(
actions = actions,
bundle_extension = bundle_extension,
Expand Down Expand Up @@ -1505,6 +1545,15 @@ def _ios_dynamic_framework_impl(ctx):
platform_prerequisites = platform_prerequisites,
dylibs = clang_rt_dylibs.get_from_toolchain(ctx),
),
partials.main_thread_checker_dylibs_partial(
actions = actions,
apple_mac_toolchain_info = apple_mac_toolchain_info,
binary_artifact = binary_artifact,
features = features,
label_name = label.name,
platform_prerequisites = platform_prerequisites,
dylibs = main_thread_checker_dylibs.get_from_toolchain(ctx),
),
partials.debug_symbols_partial(
actions = actions,
bundle_extension = bundle_extension,
Expand Down Expand Up @@ -2138,6 +2187,15 @@ def _ios_imessage_extension_impl(ctx):
platform_prerequisites = platform_prerequisites,
dylibs = clang_rt_dylibs.get_from_toolchain(ctx),
),
partials.main_thread_checker_dylibs_partial(
actions = actions,
apple_mac_toolchain_info = apple_mac_toolchain_info,
binary_artifact = binary_artifact,
features = features,
label_name = label.name,
platform_prerequisites = platform_prerequisites,
dylibs = main_thread_checker_dylibs.get_from_toolchain(ctx),
),
partials.debug_symbols_partial(
actions = actions,
bundle_extension = bundle_extension,
Expand Down
85 changes: 85 additions & 0 deletions apple/internal/macos_rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ load(
"@build_bazel_rules_apple//apple/internal/utils:clang_rt_dylibs.bzl",
"clang_rt_dylibs",
)
load(
"@build_bazel_rules_apple//apple/internal/utils:main_thread_checker_dylibs.bzl",
"main_thread_checker_dylibs",
)
load(
"@build_bazel_rules_apple//apple/internal:cc_info_support.bzl",
"cc_info_support",
Expand Down Expand Up @@ -280,6 +284,15 @@ def _macos_application_impl(ctx):
platform_prerequisites = platform_prerequisites,
dylibs = clang_rt_dylibs.get_from_toolchain(ctx),
),
partials.main_thread_checker_dylibs_partial(
actions = actions,
apple_mac_toolchain_info = apple_mac_toolchain_info,
binary_artifact = binary_artifact,
features = features,
label_name = label.name,
platform_prerequisites = platform_prerequisites,
dylibs = main_thread_checker_dylibs.get_from_toolchain(ctx),
),
partials.codesigning_dossier_partial(
actions = actions,
apple_mac_toolchain_info = apple_mac_toolchain_info,
Expand Down Expand Up @@ -571,6 +584,15 @@ def _macos_bundle_impl(ctx):
platform_prerequisites = platform_prerequisites,
dylibs = clang_rt_dylibs.get_from_toolchain(ctx),
),
partials.main_thread_checker_dylibs_partial(
actions = actions,
apple_mac_toolchain_info = apple_mac_toolchain_info,
binary_artifact = binary_artifact,
features = features,
label_name = label.name,
platform_prerequisites = platform_prerequisites,
dylibs = main_thread_checker_dylibs.get_from_toolchain(ctx),
),
partials.codesigning_dossier_partial(
actions = actions,
apple_mac_toolchain_info = apple_mac_toolchain_info,
Expand Down Expand Up @@ -823,6 +845,15 @@ def _macos_extension_impl(ctx):
platform_prerequisites = platform_prerequisites,
dylibs = clang_rt_dylibs.get_from_toolchain(ctx),
),
partials.main_thread_checker_dylibs_partial(
actions = actions,
apple_mac_toolchain_info = apple_mac_toolchain_info,
binary_artifact = binary_artifact,
features = features,
label_name = label.name,
platform_prerequisites = platform_prerequisites,
dylibs = main_thread_checker_dylibs.get_from_toolchain(ctx),
),
partials.codesigning_dossier_partial(
actions = actions,
apple_mac_toolchain_info = apple_mac_toolchain_info,
Expand Down Expand Up @@ -1072,6 +1103,15 @@ def _macos_quick_look_plugin_impl(ctx):
platform_prerequisites = platform_prerequisites,
dylibs = clang_rt_dylibs.get_from_toolchain(ctx),
),
partials.main_thread_checker_dylibs_partial(
actions = actions,
apple_mac_toolchain_info = apple_mac_toolchain_info,
binary_artifact = binary_artifact,
features = features,
label_name = label.name,
platform_prerequisites = platform_prerequisites,
dylibs = main_thread_checker_dylibs.get_from_toolchain(ctx),
),
partials.codesigning_dossier_partial(
actions = actions,
apple_mac_toolchain_info = apple_mac_toolchain_info,
Expand Down Expand Up @@ -1313,6 +1353,15 @@ def _macos_kernel_extension_impl(ctx):
platform_prerequisites = platform_prerequisites,
dylibs = clang_rt_dylibs.get_from_toolchain(ctx),
),
partials.main_thread_checker_dylibs_partial(
actions = actions,
apple_mac_toolchain_info = apple_mac_toolchain_info,
binary_artifact = binary_artifact,
features = features,
label_name = label.name,
platform_prerequisites = platform_prerequisites,
dylibs = main_thread_checker_dylibs.get_from_toolchain(ctx),
),
partials.codesigning_dossier_partial(
actions = actions,
apple_mac_toolchain_info = apple_mac_toolchain_info,
Expand Down Expand Up @@ -1544,6 +1593,15 @@ def _macos_spotlight_importer_impl(ctx):
platform_prerequisites = platform_prerequisites,
dylibs = clang_rt_dylibs.get_from_toolchain(ctx),
),
partials.main_thread_checker_dylibs_partial(
actions = actions,
apple_mac_toolchain_info = apple_mac_toolchain_info,
binary_artifact = binary_artifact,
features = features,
label_name = label.name,
platform_prerequisites = platform_prerequisites,
dylibs = main_thread_checker_dylibs.get_from_toolchain(ctx),
),
partials.codesigning_dossier_partial(
actions = actions,
apple_mac_toolchain_info = apple_mac_toolchain_info,
Expand Down Expand Up @@ -1774,6 +1832,15 @@ def _macos_xpc_service_impl(ctx):
platform_prerequisites = platform_prerequisites,
dylibs = clang_rt_dylibs.get_from_toolchain(ctx),
),
partials.main_thread_checker_dylibs_partial(
actions = actions,
apple_mac_toolchain_info = apple_mac_toolchain_info,
binary_artifact = binary_artifact,
features = features,
label_name = label.name,
platform_prerequisites = platform_prerequisites,
dylibs = main_thread_checker_dylibs.get_from_toolchain(ctx),
),
partials.codesigning_dossier_partial(
actions = actions,
apple_mac_toolchain_info = apple_mac_toolchain_info,
Expand Down Expand Up @@ -2818,6 +2885,15 @@ def _macos_framework_impl(ctx):
platform_prerequisites = platform_prerequisites,
dylibs = clang_rt_dylibs.get_from_toolchain(ctx),
),
partials.main_thread_checker_dylibs_partial(
actions = actions,
apple_mac_toolchain_info = apple_mac_toolchain_info,
binary_artifact = binary_artifact,
features = features,
label_name = label.name,
platform_prerequisites = platform_prerequisites,
dylibs = main_thread_checker_dylibs.get_from_toolchain(ctx),
),
partials.debug_symbols_partial(
actions = actions,
bundle_extension = bundle_extension,
Expand Down Expand Up @@ -3087,6 +3163,15 @@ def _macos_dynamic_framework_impl(ctx):
platform_prerequisites = platform_prerequisites,
dylibs = clang_rt_dylibs.get_from_toolchain(ctx),
),
partials.main_thread_checker_dylibs_partial(
actions = actions,
apple_mac_toolchain_info = apple_mac_toolchain_info,
binary_artifact = binary_artifact,
features = features,
label_name = label.name,
platform_prerequisites = platform_prerequisites,
dylibs = main_thread_checker_dylibs.get_from_toolchain(ctx),
),
partials.debug_symbols_partial(
actions = actions,
bundle_extension = bundle_extension,
Expand Down
5 changes: 5 additions & 0 deletions apple/internal/partials.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ load(
"@build_bazel_rules_apple//apple/internal/partials:clang_rt_dylibs.bzl",
_clang_rt_dylibs_partial = "clang_rt_dylibs_partial",
)
load(
"@build_bazel_rules_apple//apple/internal/partials:main_thread_checker_dylibs.bzl",
_main_thread_checker_dylibs_partial = "main_thread_checker_dylibs_partial",
)
load(
"@build_bazel_rules_apple//apple/internal/partials:codesigning_dossier.bzl",
_codesigning_dossier_partial = "codesigning_dossier_partial",
Expand Down Expand Up @@ -113,6 +117,7 @@ partials = struct(
apple_bundle_info_partial = _apple_bundle_info_partial,
binary_partial = _binary_partial,
clang_rt_dylibs_partial = _clang_rt_dylibs_partial,
main_thread_checker_dylibs_partial = _main_thread_checker_dylibs_partial,
codesigning_dossier_partial = _codesigning_dossier_partial,
debug_symbols_partial = _debug_symbols_partial,
embedded_bundles_partial = _embedded_bundles_partial,
Expand Down
15 changes: 15 additions & 0 deletions apple/internal/partials/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,21 @@ bzl_library(
],
)

bzl_library(
name = "main_thread_checker_dylibs",
srcs = ["main_thread_checker_dylibs.bzl"],
visibility = [
"//apple/internal:__pkg__",
],
deps = [
"//apple/internal:intermediates",
"//apple/internal:processor",
"//apple/internal/utils:main_thread_checker_dylibs",
"@bazel_skylib//lib:partial",
"@build_bazel_apple_support//lib:apple_support",
],
)

bzl_library(
name = "codesigning_dossier",
srcs = ["codesigning_dossier.bzl"],
Expand Down
Loading

0 comments on commit cc39679

Please sign in to comment.