-
Notifications
You must be signed in to change notification settings - Fork 271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sdk: refactor: clean up entrypoint #2635
sdk: refactor: clean up entrypoint #2635
Conversation
dbff5a6
to
01eac50
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the contribution! Can you separate the PR into two? One for the program-test changes, and another for the entrypoint change?
program-test/src/lib.rs
Outdated
// Re-fetch the instruction context. The previous reference may have been | ||
// invalidated due to the `set_invoke_context` in a CPI. | ||
let transaction_context = &invoke_context.transaction_context; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment here is very important, I spent a couple of hours chasing this bug down a few years ago! This can't be removed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does not the comment refer to the next line after the deleted line?
let instruction_context = transaction_context.get_current_instruction_context()?;
How can the CPI call invalidate the reference created by:
let transaction_context = &invoke_context.transaction_context;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved this change to #2694. I don't have strong feelings about this line, I am just curious to understand the bug, since it looks like also removing the line:
let instruction_context = transaction_context.get_current_instruction_context()?;
does not make any test from program-test
crate fail.
pub fn invoke_builtin_function(
builtin_function: solana_sdk::entrypoint::ProcessInstruction,
invoke_context: &mut InvokeContext,
) -> Result<u64, Box<dyn std::error::Error>> {
set_invoke_context(invoke_context);
The invoke_context: &mut InvokeContext
reference which is used across the invoke_builtin_function
should always be valid I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you CPI into a program that's also a fake builtin function, set_invoke_context
will overwrite the previous invoke context, potentially invalidating your reference. It's a classic problem of doing unsafe
code incorrectly 😅
810e0a7
to
df42901
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the contribution!
refactor: remove redundant references in entrypoint! macro
Problem
entrypoint!
macro unnecessarily creates references forprogram_id
andinstruction_data
, even if they are already references.Summary of Changes
entrypoint!
macro.