Repository navigation
fix: fail Dart deploys early when the executable cannot start on Cloud Run - #11617
IzaakGough wants to merge 6 commits into
Conversation
…d Run Binaries built with the nixpkgs Dart SDK request a program loader under /nix/store, which Cloud Run does not have, so the deploy uploaded and then failed with a container start timeout. Read the ELF loader path before packaging and stop with a clear error instead.
There was a problem hiding this comment.
Code Review
This pull request introduces a check that validates Dart function executables before upload to ensure they do not require a program loader missing from Cloud Run (such as those from the nixpkgs Dart SDK). It implements ELF64 parsing to inspect the PT_INTERP header of the binary and integrates this validation into the preparation phase. Feedback on the changes suggests marking the static CLOUD_RUN_LOADERS array as 'as const' to align with the repository's style guide, and adding robust error handling with defensive size checks during ELF parsing to prevent unexpected failures or memory issues.
| export const CLOUD_RUN_LOADERS = [ | ||
| "/lib64/ld-linux-x86-64.so.2", | ||
| "/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2", | ||
| ]; |
There was a problem hiding this comment.
According to the repository style guide, static configuration arrays and lookup tables should be marked as readonly or as const to prevent accidental mutation and ensure type safety.
| export const CLOUD_RUN_LOADERS = [ | |
| "/lib64/ld-linux-x86-64.so.2", | |
| "/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2", | |
| ]; | |
| export const CLOUD_RUN_LOADERS = [ | |
| "/lib64/ld-linux-x86-64.so.2", | |
| "/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2" | |
| ] as const; |
References
- Extract magic numbers and static constants: ... Mark static configuration arrays and lookup tables as
readonlyoras const. (link)
| try { | ||
| const { size } = await handle.stat(); | ||
| const header = await readAt(handle, 0, ELF_HEADER_SIZE, size); | ||
| if ( | ||
| !header || | ||
| header.readUInt32BE(0) !== 0x7f454c46 || | ||
| header[4] !== ELFCLASS64 || | ||
| header[5] !== ELFDATA2LSB || | ||
| header.readUInt16LE(0x12) !== EM_X86_64 | ||
| ) { | ||
| return undefined; | ||
| } | ||
| const phoff = Number(header.readBigUInt64LE(0x20)); | ||
| const phentsize = header.readUInt16LE(0x36); | ||
| const phnum = header.readUInt16LE(0x38); | ||
| if (phentsize < PROGRAM_HEADER_SIZE) { | ||
| return undefined; | ||
| } | ||
| const programHeaders = await readAt(handle, phoff, phentsize * phnum, size); | ||
| if (!programHeaders) { | ||
| return undefined; | ||
| } | ||
| for (let i = 0; i < phnum; i++) { | ||
| const ph = programHeaders.subarray(i * phentsize); | ||
| if (ph.readUInt32LE(0) !== PT_INTERP) { | ||
| continue; | ||
| } | ||
| const offset = Number(ph.readBigUInt64LE(8)); | ||
| const filesz = Number(ph.readBigUInt64LE(32)); | ||
| if (filesz > MAX_INTERP_SIZE) { | ||
| return undefined; | ||
| } | ||
| const interp = await readAt(handle, offset, filesz, size); | ||
| if (!interp) { | ||
| return undefined; | ||
| } | ||
| const nul = interp.indexOf(0); | ||
| return { interpreter: interp.subarray(0, nul === -1 ? interp.length : nul).toString() }; | ||
| } | ||
| return { interpreter: undefined }; | ||
| } finally { | ||
| await handle.close(); | ||
| } |
There was a problem hiding this comment.
To ensure robust error handling and defensive programming, we should add a catch block to the second try block. This ensures that any unexpected I/O or parsing errors (e.g., from handle.stat() or handle.read()) are caught and safely return undefined (meaning the file cannot be inspected), rather than propagating and failing the deployment. Additionally, we should add a defensive limit check on phentsize * phnum to prevent potential out-of-memory issues or RangeErrors when allocating memory for malformed ELF files.
try {
const { size } = await handle.stat();
const header = await readAt(handle, 0, ELF_HEADER_SIZE, size);
if (
!header ||
header.readUInt32BE(0) !== 0x7f454c46 ||
header[4] !== ELFCLASS64 ||
header[5] !== ELFDATA2LSB ||
header.readUInt16LE(0x12) !== EM_X86_64
) {
return undefined;
}
const phoff = Number(header.readBigUInt64LE(0x20));
const phentsize = header.readUInt16LE(0x36);
const phnum = header.readUInt16LE(0x38);
if (phentsize < PROGRAM_HEADER_SIZE || phentsize * phnum > 1024 * 1024) {
return undefined;
}
const programHeaders = await readAt(handle, phoff, phentsize * phnum, size);
if (!programHeaders) {
return undefined;
}
for (let i = 0; i < phnum; i++) {
const ph = programHeaders.subarray(i * phentsize);
if (ph.readUInt32LE(0) !== PT_INTERP) {
continue;
}
const offset = Number(ph.readBigUInt64LE(8));
const filesz = Number(ph.readBigUInt64LE(32));
if (filesz > MAX_INTERP_SIZE) {
return undefined;
}
const interp = await readAt(handle, offset, filesz, size);
if (!interp) {
return undefined;
}
const nul = interp.indexOf(0);
return { interpreter: interp.subarray(0, nul === -1 ? interp.length : nul).toString() };
}
return { interpreter: undefined };
} catch {
return undefined;
} finally {
await handle.close();
}298d572 to
7c69bca
Compare
Fixes #10976.
Deploying Dart functions built with the nixpkgs Dart SDK uploaded fine, then failed with a container start timeout, because the executable needs a program loader under
/nix/storethat Cloud Run does not have.This checks the executable before upload and stops the deploy with an error naming the missing loader and how to build with the official SDK.
Tested with unit tests and real nixpkgs and official SDK binaries. Not tested with a live deploy or
dart build clioutput.