anchor icon indicating copy to clipboard operation
anchor copied to clipboard

Large Stack Variables Error in Anchor 0.29.0

Open chenmoonmo opened this issue 3 months ago • 17 comments

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

chenmoonmo avatar Mar 07 '24 03:03 chenmoonmo

https://github.com/chenmoonmo/anchor-dex/blob/46f7a9b4a6f4332d3f476112c2c9de6e25b9a099/programs/anchor-dex/src/instructions/init_pool.rs#L17-L20

You can Box the Mint accounts.

acheroncrypto avatar Mar 07 '24 15:03 acheroncrypto

After I do this, the error still appears, but when I import the code into solana playground, the compiled program can be called normally.

chenmoonmo avatar Mar 08 '24 01:03 chenmoonmo

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?

acheroncrypto avatar Mar 09 '24 04:03 acheroncrypto

In local testing the instruction is called correctly, but when I build and deploy to devnet the instruction is called with an error

chenmoonmo avatar Mar 11 '24 02:03 chenmoonmo

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

zfedoran avatar Mar 11 '24 18:03 zfedoran

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?

Bhargavamacha avatar Mar 14 '24 19:03 Bhargavamacha

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?

acheroncrypto avatar Mar 14 '24 21:03 acheroncrypto

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?

Bhargavamacha avatar Mar 14 '24 21:03 Bhargavamacha

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.

acheroncrypto avatar Mar 14 '24 21:03 acheroncrypto

okay I will investigate a bit further and get back

Bhargavamacha avatar Mar 14 '24 21:03 Bhargavamacha

Related: https://github.com/anza-xyz/agave/issues/252

acheroncrypto avatar Mar 15 '24 19:03 acheroncrypto

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.

acheroncrypto avatar Mar 27 '24 11:03 acheroncrypto

stack offset problems associated with bumps still exist

chenmoonmo avatar Mar 28 '24 03:03 chenmoonmo

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 image

in 0.29.0 it exceeds by a whopping 1368 bytes (even if I try https://github.com/febo/anchor boxed bumps) image

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

cc @chenmoonmo @acheroncrypto

ThallesP avatar Apr 02 '24 13:04 ThallesP

@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

Bhargavamacha avatar Apr 13 '24 16:04 Bhargavamacha