Skip to content

Investigate use of new Apple JIT APIs #108423

Open

Description

@filipnavara

Apple silently released JIT APIs for iOS 17.4+ / iPadOS 18+: https://developer.apple.com/documentation/apple-silicon/porting-just-in-time-compilers-to-apple-silicon

They are the same APIs that were previously available on macOS - pthread_jit_write_with_callback_np and PTHREAD_JIT_WRITE_ALLOW_CALLBACKS_NP, and they require new entitlements. Unfortunately, they are also distinct from the original pthread_jit_write_protect_np API that was introduced with macOS 11 to support Apple Silicon, and it requires a different approach to use it.

Neither CoreCLR nor MonoVM is currently structured in a way to make it easy to adapt.

This may possibly be interesting if we revive the CoreCLR on iOS (Simulator) PR, or as an option to enable JIT for Hot Restart (MonoVM) scenarios when deploying to iOS from Windows.

Activity

  1. ghost added
    needs-area-label
    on Oct 1, 2024
  2. dotnet-policy-service commented on Oct 1, 2024

    @dotnet-policy-service
    Contributor

    Tagging subscribers to 'os-ios': @vitek-karas, @kotlarmilos, @ivanpovazan, @steveisok, @akoeplinger
    See info in area-owners.md if you want to be subscribed.

  3. added
    untriaged
    on Oct 1, 2024
  4. added this to the 10.0.0 milestone on Oct 1, 2024
  5. filipnavara commented on Oct 1, 2024

    @filipnavara
    MemberAuthor

    I hate to turn down the excitement. Unfortunately, the entitlements necessary to make this work are still restricted:

    To build a web browser that uses an alternative browser engine, including the one in this sample project, you need to apply for certain entitlements from WWDR. For more information on how to use these entitlements, see Creating browser extensions in Xcode.

    That severely restricts the usability. It still means that you can now officially use JIT on Mac Catalyst using supported APIs. I tested that scenario with ad-hoc signing but it should also work with the com.apple.security.cs.allow-jit entitlement which is not restricted on macOS.

    Furthermore, the BrowserEngineKit APIs be_memory_inline_jit_restrict_rwx_to_rw_with_witness and be_memory_inline_jit_restrict_rwx_to_rx_with_witness match the functionality of pthread_jit_write_protect_np and may be easier to use for some quick hack to test the dev loop scenario. They are, however, restricted for App Store usage, so they are not a good replacement.

  6. dotnet-policy-service commented on Oct 1, 2024

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @mangod9
    See info in area-owners.md if you want to be subscribed.

  7. removed
    untriaged
    on Oct 1, 2024
  8. removed
    needs-area-label
    on Oct 2, 2024
  9. Spidy123222 commented on Dec 29, 2024

    @Spidy123222

    There’s a growing community of iOS users who rely on jit-based emulators via debugging. However, with the introduction of net 9+, jit is no longer supported on Work-in-progress programs, causing them to remain stuck on 8 when newer code is on 9+. I hope this issue will be addressed when jit is reintroduced for iOS DotNet.

  10. vitek-karas commented on Jun 23, 2025

    @vitek-karas
    Member

    @Spidy123222 could you please clarify on the usage of "jit-based emulators". AFAIK .NET on iOS never had a JIT because of the restrictions from the OS. While this issue describes some new ways of allowing JIT, it is still very limitted and effectively unusable for release apps (to the store). Given that store apps are the primary target for .NET on iOS, we don't have any plans to implement JIT on iOS. We do support interpreter, which should have effectively the same functionality, just different perf characteristics.

  11. vitek-karas commented on Jun 23, 2025

    @vitek-karas
    Member

    @filipnavara I'm inclined to close this issue. I don't see us special casing macCatalyst on its own, and on iOS this is not very useful.

  12. filipnavara commented on Jun 23, 2025

    @filipnavara
    MemberAuthor

    Please don't close it just yet. I think it's still relevant for debugging scenarios (Mac Catalyst, iOS Simulator). We currently enable these scenarios by unsanctioned usage of the pthread_jit_write_protect_np which works locally but it's obviously ineligible for wide distribution. Apple is pushing these new APIs and we may eventually need to migrate on macOS too.

    I don't think the issue is immediately actionable though. We still have way too many places that use W^X memory. I've gradually tried to remove some of them and many were removed with the ongoing interpreter work.

    Using pthread_jit_write_with_callback_np is not easy with the current W^X memory abstraction in CoreCLR. The current abstraction uses an object on the stack which provides methods to get writable memory, while the new Apple API basically requires a callback that receives a temporary pointer to the writable memory. It's not trivially possible to modify the current abstraction to work with pthread_jit_write_with_callback_np without compromising the intent of the callback API (ie. not using a single process-wide callback) or using large temporary buffers.

    I am essentially waiting on the runtime to get in a better shape as part of the interpreter work to even pursue this task and experiment with it.

    As for the thing @Spidy123222 mentioned. The emulators still rely on a debugger trick to enable the JIT. When the debugger is attached (or the system thinks it's attached) it allows some limitet JITing for function evaluation.

  13. filipnavara commented on Jun 23, 2025

    @filipnavara
    MemberAuthor

    (Feel free to bump it to Future milestone though)

  14. modified the milestones: 10.0.0, 11.0.0, Future on Jun 23, 2025
  15. Spidy123222 commented on Jun 24, 2025

    @Spidy123222

    As for the thing @Spidy123222 mentioned. The emulators still rely on a debugger trick to enable the JIT. When the debugger is attached (or the system thinks it's attached) it allows some limited JITing for function evaluation.

    Yeah this is what's used with developer accounts of users for more power user methods to run advanced emulation (things that cannot run on a interpiter well) or tools like flutter with Hot Reload via Dart or like what you said function evaluation.

    I’m hoping to at least allow compilation for iOS to enable the execution of such programs with a flag to force the ability even if I'm not able to submit for AppStore or just allow it like before (whatever is preferred). This feature could also adapt to accommodate additional capabilities when iOS introduces more countries for just-in-time compilation with web browsers or other programs. However, Dotnet 9 rendered all of this functionality unusable when upgrading Dotnet 8 projects to Dotnet 9 to keep up with the original project's changes (These iOS projects tend to be forks or part of the main program like Flutter).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    • Status
      No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions