Skip to content

fix: fail Dart deploys early when the executable cannot start on Cloud Run - #11617

Draft
IzaakGough wants to merge 6 commits into
mainfrom
dart-elf-interp-check
Draft

IzaakGough wants to merge 6 commits into
mainfrom
dart-elf-interp-check

Conversation

@IzaakGough

Copy link
Copy Markdown
Contributor

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/store that 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.

  • Only the loader is checked, against the two standard x86-64 glibc paths. Files that are not ELF64 x86-64 are skipped rather than failed.

Tested with unit tests and real nixpkgs and official SDK binaries. Not tested with a live deploy or dart build cli output.

…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.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +8 to +11
export const CLOUD_RUN_LOADERS = [
"/lib64/ld-linux-x86-64.so.2",
"/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2",
];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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
  1. Extract magic numbers and static constants: ... Mark static configuration arrays and lookup tables as readonly or as const. (link)

Comment on lines +35 to +77
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();
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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();
  }

@IzaakGough
IzaakGough force-pushed the dart-elf-interp-check branch from 298d572 to 7c69bca Compare December 2, 2026 15:22

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dart cloud functions fails to deploy (terminated: Application failed to start: failed to load /workspace/bin/server: no such file or directory)

1 participant