Skip to content
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

Merged
merged 1 commit into from
Aug 22, 2024

Conversation

andreisilviudragnea
Copy link

@andreisilviudragnea andreisilviudragnea commented Aug 16, 2024

Problem

  • entrypoint! macro unnecessarily creates references for program_id and instruction_data, even if they are already references.

Summary of Changes

  • I removed redundant references from entrypoint! macro.

@mergify mergify bot requested a review from a team August 16, 2024 16:59
@joncinque joncinque added the CI Pull Request is ready to enter CI label Aug 16, 2024
@anza-team anza-team removed the CI Pull Request is ready to enter CI label Aug 16, 2024
@andreisilviudragnea andreisilviudragnea force-pushed the clean-up-entrypoint branch 3 times, most recently from dbff5a6 to 01eac50 Compare August 21, 2024 16:49
Copy link

@joncinque joncinque left a 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?

Comment on lines 154 to 159
// 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;

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

Copy link
Author

@andreisilviudragnea andreisilviudragnea Aug 22, 2024

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;

Copy link
Author

@andreisilviudragnea andreisilviudragnea Aug 22, 2024

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.

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 😅

@andreisilviudragnea andreisilviudragnea force-pushed the clean-up-entrypoint branch 2 times, most recently from 810e0a7 to df42901 Compare August 22, 2024 11:33
@joncinque joncinque added the CI Pull Request is ready to enter CI label Aug 22, 2024
@anza-team anza-team removed the CI Pull Request is ready to enter CI label Aug 22, 2024
@joncinque joncinque changed the title refactor: clean up entrypoint sdk: refactor: clean up entrypoint Aug 22, 2024
Copy link

@joncinque joncinque left a 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!

@joncinque joncinque merged commit 22eec1c into anza-xyz:master Aug 22, 2024
52 checks passed
@andreisilviudragnea andreisilviudragnea deleted the clean-up-entrypoint branch August 23, 2024 11:28
ray-kast pushed a commit to abklabs/agave that referenced this pull request Nov 27, 2024
refactor: remove redundant references in entrypoint! macro
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants