|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as assert from 'assert'; |
| 7 | +import * as path from 'path'; |
| 8 | +import { open, stat, readdir, realpath } from 'fs/promises'; |
| 9 | +import { spawn, ExitCodeError } from '@malept/cross-spawn-promise'; |
| 10 | + |
| 11 | +const MACHO_PREFIX = 'Mach-O '; |
| 12 | +const MACHO_64_MAGIC_LE = 0xfeedfacf; |
| 13 | +const MACHO_UNIVERSAL_MAGIC_LE = 0xbebafeca; |
| 14 | +const MACHO_ARM64_CPU_TYPE = new Set([ |
| 15 | + 0x0c000001, |
| 16 | + 0x0100000c, |
| 17 | +]); |
| 18 | +const MACHO_X86_64_CPU_TYPE = new Set([ |
| 19 | + 0x07000001, |
| 20 | + 0x01000007, |
| 21 | +]); |
| 22 | + |
| 23 | +async function read(file: string, buf: Buffer, offset: number, length: number, position: number) { |
| 24 | + let filehandle; |
| 25 | + try { |
| 26 | + filehandle = await open(file); |
| 27 | + await filehandle.read(buf, offset, length, position); |
| 28 | + } finally { |
| 29 | + await filehandle?.close(); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +async function checkMachOFiles(appPath: string, arch: string) { |
| 34 | + const visited = new Set(); |
| 35 | + const invalidFiles: string[] = []; |
| 36 | + const header = Buffer.alloc(8); |
| 37 | + const file_header_entry_size = 20; |
| 38 | + const checkx86_64Arch = (arch === 'x64'); |
| 39 | + const checkArm64Arch = (arch === 'arm64'); |
| 40 | + const checkUniversalArch = (arch === 'universal'); |
| 41 | + const traverse = async (p: string) => { |
| 42 | + p = await realpath(p); |
| 43 | + if (visited.has(p)) { |
| 44 | + return; |
| 45 | + } |
| 46 | + visited.add(p); |
| 47 | + |
| 48 | + const info = await stat(p); |
| 49 | + if (info.isSymbolicLink()) { |
| 50 | + return; |
| 51 | + } |
| 52 | + if (info.isFile()) { |
| 53 | + let fileOutput = ''; |
| 54 | + try { |
| 55 | + fileOutput = await spawn('file', ['--brief', '--no-pad', p]); |
| 56 | + } catch (e) { |
| 57 | + if (e instanceof ExitCodeError) { |
| 58 | + /* silently accept error codes from "file" */ |
| 59 | + } else { |
| 60 | + throw e; |
| 61 | + } |
| 62 | + } |
| 63 | + if (fileOutput.startsWith(MACHO_PREFIX)) { |
| 64 | + console.log(`Verifying architecture of ${p}`); |
| 65 | + read(p, header, 0, 8, 0).then(_ => { |
| 66 | + const header_magic = header.readUInt32LE(); |
| 67 | + if (header_magic === MACHO_64_MAGIC_LE) { |
| 68 | + const cpu_type = header.readUInt32LE(4); |
| 69 | + if (checkUniversalArch) { |
| 70 | + invalidFiles.push(p); |
| 71 | + } else if (checkArm64Arch && !MACHO_ARM64_CPU_TYPE.has(cpu_type)) { |
| 72 | + invalidFiles.push(p); |
| 73 | + } else if (checkx86_64Arch && !MACHO_X86_64_CPU_TYPE.has(cpu_type)) { |
| 74 | + invalidFiles.push(p); |
| 75 | + } |
| 76 | + } else if (header_magic === MACHO_UNIVERSAL_MAGIC_LE) { |
| 77 | + const num_binaries = header.readUInt32BE(4); |
| 78 | + assert.equal(num_binaries, 2); |
| 79 | + const file_entries_size = file_header_entry_size * num_binaries; |
| 80 | + const file_entries = Buffer.alloc(file_entries_size); |
| 81 | + read(p, file_entries, 0, file_entries_size, 8).then(_ => { |
| 82 | + for (let i = 0; i < num_binaries; i++) { |
| 83 | + const cpu_type = file_entries.readUInt32LE(file_header_entry_size * i); |
| 84 | + if (!MACHO_ARM64_CPU_TYPE.has(cpu_type) && !MACHO_X86_64_CPU_TYPE.has(cpu_type)) { |
| 85 | + invalidFiles.push(p); |
| 86 | + } |
| 87 | + } |
| 88 | + }); |
| 89 | + } |
| 90 | + }); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + if (info.isDirectory()) { |
| 95 | + for (const child of await readdir(p)) { |
| 96 | + await traverse(path.resolve(p, child)); |
| 97 | + } |
| 98 | + } |
| 99 | + }; |
| 100 | + await traverse(appPath); |
| 101 | + return invalidFiles; |
| 102 | +} |
| 103 | + |
| 104 | +const archToCheck = process.argv[2]; |
| 105 | +assert(process.env['APP_PATH'], 'APP_PATH not set'); |
| 106 | +assert(archToCheck === 'x64' || archToCheck === 'arm64' || archToCheck === 'universal', `Invalid architecture ${archToCheck} to check`); |
| 107 | +checkMachOFiles(process.env['APP_PATH'], archToCheck).then(invalidFiles => { |
| 108 | + if (invalidFiles.length > 0) { |
| 109 | + console.error('\x1b[31mThe following files are built for the wrong architecture:\x1b[0m'); |
| 110 | + for (const file of invalidFiles) { |
| 111 | + console.error(`\x1b[31m${file}\x1b[0m`); |
| 112 | + } |
| 113 | + process.exit(1); |
| 114 | + } else { |
| 115 | + console.log('\x1b[32mAll files are valid\x1b[0m'); |
| 116 | + } |
| 117 | +}).catch(err => { |
| 118 | + console.error(err); |
| 119 | + process.exit(1); |
| 120 | +}); |
0 commit comments