Download native ARM64 browsers on Linux ARM64 mozdownload's platform autodetection does not look at the CPU architecture, but picks the x86-64 (linux64) build on any 64-bit Linux, so since the previous commit all Firefox versions installed on Linux ARM64 were x86-64 builds that fail to run with "Exec format error". Pass the 'linux-arm64' platform to mozdownload explicitly again. The earlier explicit platform failed for release versions with KeyError: 'linux-arm64', because mozdownload's ReleaseScraper has no binary_regex entry for it (even in the latest mozdownload 1.30.0), even though it knows the linux-aarch64 download directory. On Linux the release file name does not contain the platform, so reuse the linux64 regex for it. Require mozdownload 1.29.0, which added the linux-aarch64 directory. @puppeteer/browsers similarly falls back to installing the linux64 build of Chrome versions that have no linux-arm64 build (Chrome for Testing has linux-arm64 builds only from Chrome 153 onwards). Verify on Linux that installed Chrome and Firefox executables match the host CPU, and fail the install otherwise. Reinstall Firefox if an earlier wrong-architecture install is found. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
diff --git a/emsdk.py b/emsdk.py index 859dddd..8083477 100644 --- a/emsdk.py +++ b/emsdk.py
@@ -1341,11 +1341,45 @@ return success +def get_elf_arch(path): + """Returns 'x86_64', 'arm64', 'x86' or 'arm' for an ELF binary, or None if + the file is not an ELF binary of a known architecture.""" + try: + with open(path, 'rb') as f: + header = f.read(20) + except OSError: + return None + if len(header) < 20 or header[:4] != b'\x7fELF': + return None + machine = int.from_bytes(header[18:20], 'little' if header[5] == 1 else 'big') + return {0x3E: 'x86_64', 0xB7: 'arm64', 0x03: 'x86', 0x28: 'arm'}.get(machine) + + +def is_browser_for_host_arch(exe): + """On Linux, checks that a downloaded browser executable matches the CPU + architecture of this system. Browser download tools can silently fall back + to x86-64 builds when a native build is not available.""" + if not LINUX: + return True + arch = get_elf_arch(exe) + if arch is not None and arch != ARCH: + errlog(f'{exe} is a {arch} executable, but this system is {ARCH}.') + return False + return True + + def download_chrome(tool): debug_print(f'download_chrome({tool})') + chrome_arch_error = (f'Chrome {tool.version} is not available for linux-arm64: Chrome for Testing ' + 'provides linux-arm64 builds only from Chrome 153 onwards.') + # Is already downloaded? if tool.version not in {'stable', 'canary'} and is_chrome_installed(tool): + chrome_exe = sdk_path(read_file(os.path.join(tool.installation_dir(), 'actual.txt'))) + if not is_browser_for_host_arch(chrome_exe): + errlog(chrome_arch_error) + return False print(f'Chrome {tool.version} already downloaded. Skipping.') return True @@ -1360,6 +1394,12 @@ chrome_path = output[output.find(' ') + 1:].strip() print(f'Chrome: {chrome_path}') + # @puppeteer/browsers silently installs the linux64 (x86-64) build of Chrome + # versions that have no linux-arm64 build, so verify what we got. + if not is_browser_for_host_arch(chrome_path): + errlog(chrome_arch_error) + return False + # Create a pretend alias which maps the emsdk path to the actually installed version. pretend_version_dir = os.path.normpath(tool.installation_path()) actual_file = os.path.join(pretend_version_dir, 'actual.txt') @@ -1390,8 +1430,37 @@ # version. So prepare to handle both further down below. extension = 'tar.xz' + # mozdownload's platform autodetection does not look at the CPU architecture, + # but picks the x86-64 build on any 64-bit Linux, so on Linux ARM64 pass the + # platform explicitly. + platform = None + if LINUX and ARCH == 'arm64': + platform = 'linux-arm64' + import mozdownload.scraper + if 'linux-arm64' not in mozdownload.scraper.PLATFORM_FRAGMENTS: + errlog('Downloading Firefox for Linux ARM64 requires mozdownload 1.29.0 or newer. ' + f'Run "{sys.executable} -m pip install --upgrade mozdownload" to update.') + return False + if tool.version == 'nightly': - scraper = FactoryScraper('daily', extension=extension, locale='en-US') + scraper = FactoryScraper('daily', extension=extension, locale='en-US', platform=platform) + elif platform == 'linux-arm64': + from mozdownload.scraper import ReleaseScraper + + class ReleaseScraperLinuxArm64(ReleaseScraper): + # mozdownload's ReleaseScraper knows the linux-aarch64 download directory, + # but has no binary_regex entry for 'linux-arm64' (raises a KeyError). + # On Linux the release file name does not contain the platform, so it + # is the same as for linux64. + @property + def binary_regex(self): + self.platform = 'linux64' + try: + return super().binary_regex + finally: + self.platform = 'linux-arm64' + + scraper = ReleaseScraperLinuxArm64(extension=extension, locale='en-US', platform=platform, version=tool.version) else: scraper = FactoryScraper('release', extension=extension, locale='en-US', version=tool.version) @@ -1425,9 +1494,14 @@ exe_dir = os.path.join(root, 'Contents', 'MacOS') if MACOS else root firefox_exe = os.path.join(exe_dir, exe_suffix('firefox')) if os.path.isfile(firefox_exe): - print(f'{firefox_exe} is already installed, skipping..') - save_actual_version() - return True + if is_browser_for_host_arch(firefox_exe): + print(f'{firefox_exe} is already installed, skipping..') + save_actual_version() + return True + # A Firefox for the wrong CPU architecture was installed here earlier + # (by mozdownload platform autodetection), so replace it. + print(f'Removing {root} to reinstall Firefox for {ARCH}.') + remove_tree(root) print('Downloading Firefox from ' + scraper.url) filename = scraper.download() @@ -1519,10 +1593,12 @@ run(['defaults', 'write', '-app', root, 'ApplePersistenceIgnoreState', 'YES']) run(['defaults', 'write', '-app', root, 'NSQuitAlwaysKeepsWindows', '-bool', 'false']) - save_actual_version() + # If we didn't get a Firefox executable for this CPU, then installation failed. + if not os.path.isfile(firefox_exe) or not is_browser_for_host_arch(firefox_exe): + return False - # If we didn't get a Firefox executable, then installation failed. - return os.path.isfile(firefox_exe) + save_actual_version() + return True def is_chrome_installed(tool):