Porting to Apple Silicon: Firefox Case

With the introduction of Apple Silicon, macOS developers will need to port their programs to the new CPU. For complex programs, this may be no small feat, though, as Firefox engineer manager Gian-Carlo Pascotto recounted.

While Apple Rosetta 2 binary translation technology ensures most existing programs for macOS will be able to run on the new platform without any modifications, this will not enable them to take full advantage of the new CPU characteristics. Furthermore, when Apple considers the transition to the M1 completed, they will remove Rosetta 2 from their new OSes, as it was the case with the original Rosetta used for transitioning from the PowerPC to the Intel platform a few years ago.

In general, the first step in the transition to Apple Silicon for any program will be compile the code for the ARM 64 platform. Usually, this will be no major issues for programs that support both macOS and iOS, since all iOS devices from Apple uses the ARM64 architecture. This was not the case with Firefox, though, since its iOS version is little more than a wrapper around WebKit and does not use Firefox own engine due to restrictions imposed by Apple. Yet, Firefox did already have an ARM64 version for Android and Linux, so this was not a major concern.

So with 64-bit ARM support already in the codebase, the first pass of work was to go through all the Firefox code, dependencies, and various third-party build systems to see if they correctly dealt with the novel idea that a Mac could have an ARM chip inside.

One key concern, explains Pascotto, was the availability of a working Rust compiler for Apple Silicon, which is required by core parts of Firefox, including Servo and other key parts. When the Rust compiler became available, the Firefox team could adapt and fix all low-level details of Firefox codebase, as well as of the crates it required, that had to do with calling conventions and interfaces with other languages, including JavaScript and C++.

One area of major complexity in Firefox is dealing with video streaming formats used by services such as Hulu, Disney+, or Amazon Prime, which requires using closed source, proprietary DRM software that is downloaded on demand when the user visits any of those services. For the port to Apple Silicon, Firefox could not rely on those codecs to be ready on time, so they resorted to a specific approach to enable the use of the existing x64 codecs under Rosetta emulation:

We ended up leveraging a technique that we are also using for the Windows on ARM version of Firefox. The DRM video decoder already executes in a separate process so we can sandbox the proprietary code from the user’s system.

A final area that required some work was adapting to macOS Big Sur, the latest version of macOS which included many deprecations and backward incompatibilities.

With this, Mozilla engineers had tackled all major technical hurdles in the transition to Apple Silicon and were among the first to announce Apple Silicon support. But the road to a stable release has proved to be not that easy in spite of that. As Pascotto explains, the app update process also revealed additional hurdles that Firefox engineers had to overcome and the WebRender was disabled in the first version to work around some graphics driver bugs in Big Sur. Finally, Native support for Apple Silicon became officially available in Firefox 84, released last December.

Pascotto adds much more detail in his writing that can be addressed here, including future plans for Firefox on Apple Silicon and performance considerations, so you may want to make sure you read his story, too, if you are interested.

Source link

The post Porting to Apple Silicon: Firefox Case appeared first on TechFans.