anchor
anchor copied to clipboard
Large Stack Variables Error in Anchor 0.29.0
I encountered an error when I tried to compile my Solana program (https://github.com/chenmoonmo/anchor-dex): Error: Function ZN157$LT$anchor_dex..instructions..init_pool..InitializePool$u20$as$u20$anchor_lang..Accounts$LT$anchor_dex..instructions..init_pool..InitializePoolBumps$GT$$GT$12try_accounts17hc6503ec5b47d6f18E Stack offset of 5672 exceeded max offset of 4096 by 1576 bytes, please minimize large stack variables
how can i fix it?
rustc 1.76.0 anchor-cli 0.29.0 solana-cli 1.18.3 solana-program 1.18.3
https://github.com/chenmoonmo/anchor-dex/blob/46f7a9b4a6f4332d3f476112c2c9de6e25b9a099/programs/anchor-dex/src/instructions/init_pool.rs#L17-L20
You can Box
the Mint
accounts.
After I do this, the error still appears, but when I import the code into solana playground, the compiled program can be called normally.
Does it only happen in solana-cli 1.18
? Solana Playground has the same Anchor version but it's not on solana-cli 1.18
.
Also, this error is more of a warning and usually doesn't cause runtime errors. Does it work if you test it locally?
In local testing the instruction is called correctly, but when I build and deploy to devnet the instruction is called with an error
Also seeing something similar but in a different place.
Error: Function _ZN112_$LT$solana_program..instruction..InstructionError$u20$as$u20$solana_frozen_abi..abi_example..AbiEnumVisitor$GT$13visit_for_abi17h31964a789f899580E Stack offset of 4504 exceeded max offset of 4096 by 408 bytes, please minimize large stack variables
I suspect there is something up with the latest solana program or cli. I'm on 1.18.4
, going to try older versions...
Hey @acheroncrypto we upgraded a different program to v29 and got a Stack offset exceeded max offset by a huge amount. Could the bumps change cause an increased stack usage by any chance?
Yes, the bumps change increased the stack usage slightly but it shouldn't be a huge amount. Did you also upgrade to 1.18
CLI? That also has some memory issues.
Some people have reported an issue regards to it but not a lot. Does https://github.com/coral-xyz/anchor/pull/2766 fix your issue?
I did yeah, I haven't checked, will do and get back. Could it be possible the new struct is over allocating if you cross a threshold of accounts?
I think it may be getting copied multiple times since the fields are u8
, which is Copy
, and combined with a lot of accounts it could cause excessive stack usage.
okay I will investigate a bit further and get back
Related: https://github.com/anza-xyz/agave/issues/252
Solana 1.18.8 includes fixes to memory related issues (https://github.com/anza-xyz/agave/pull/362).
Try to see if upgrading fixes this problem.
stack offset problems associated with bumps still exist
same here, i was getting some errors with stack size in 0.28.0 and thought of giving 0.29.0 a try again because someone told me that 0.29.0 reduces heap stack a little.
in 0.28.0 my stack would only exceed by 40 bytes or so
in 0.29.0 it exceeds by a whopping 1368 bytes (even if I try https://github.com/febo/anchor boxed bumps)
And before we blame it in the handler, my entire handler is commented out, it just returns Ok(())
.
All of my accounts are boxed and I'm on solana-cli 1.18.8.
cc @chenmoonmo @acheroncrypto
@acheroncrypto I dont think the issue is fixed in 1.18.8. Can you think of anything that changed in 1.18 that could be causing this