Skip to content

fix(lang): Include system program AccountInfo in CPI invoke calls#4258

Open
awneesht wants to merge 1 commit intosolana-foundation:masterfrom
awneesht:fix/realloc-cpi-system-program
Open

fix(lang): Include system program AccountInfo in CPI invoke calls#4258
awneesht wants to merge 1 commit intosolana-foundation:masterfrom
awneesht:fix/realloc-cpi-system-program

Conversation

@awneesht
Copy link

Summary

Fixes #4132

  • Root cause: Anchor's codegen for realloc and init constraints used system program wrapper functions (transfer, create_account, allocate, assign) that called invoke_signed without including the system program's AccountInfo. This works at CPI depth 1 (runtime resolves from transaction accounts) but fails at depth 2+ with "sum of account balances before and after instruction do not match".

  • Fix: Replace all wrapper calls in constraints.rs and idl.rs with direct invoke/invoke_signed that include system_program.to_account_info() in the account infos array. Uses invoke (not invoke_signed) for transfer operations where the payer is already a Signer, avoiding unnecessary signer seed lookups. This also eliminates heap allocations from CpiContext/struct construction.

  • Files changed:

    • lang/syn/src/codegen/accounts/constraints.rs — Fixed generate_constraint_realloc() and generate_create_account() (covers realloc, init, init_if_needed for all account types)
    • lang/syn/src/codegen/program/idl.rs — Fixed __idl_resize_account
    • tests/realloc-cpi-bug/ — New reproduction test with caller/callee CPI pattern

Test plan

  • New tests/realloc-cpi-bug/ — 5/5 passing (direct realloc, CPI realloc grow, CPI shrink, CPI grow-after-shrink)
  • Existing tests/realloc/ — 5/5 passing (no regressions)
  • Existing tests/cpi-returns/ — 11/11 passing (verifies init codegen path)
  • cargo test -p anchor-syn — passes

…lana-foundation#4132)

When realloc or init constraints are used in a program invoked via CPI,
the generated system program calls (transfer, create_account, allocate,
assign) omitted the system program's AccountInfo from invoke, causing
"sum of account balances before and after instruction do not match" at
CPI depth 2+. Replace wrapper calls with direct invoke/invoke_signed
that include system_program.to_account_info() in all codegen paths.
@vercel
Copy link

vercel bot commented Feb 19, 2026

Someone is attempting to deploy a commit to the Solana Foundation Team on Vercel.

A member of the Team first needs to authorize it.

@awneesht
Copy link
Author

@jamie-osec Would you be able to review this PR when you get a chance? Thanks!

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.

CPI + #[account(realloc = ..., realloc::payer = ...)] causes sum of account balances ... do not match right before SPL Token transfer CPI

1 participant