-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
lang: Fix account try_from
usage
#2770
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "try-from" | ||
version = "0.1.0" | ||
description = "Created with Anchor" | ||
edition = "2021" | ||
|
||
[lib] | ||
crate-type = ["cdylib", "lib"] | ||
name = "try_from" | ||
|
||
[features] | ||
no-entrypoint = [] | ||
no-idl = [] | ||
no-log-ix-name = [] | ||
cpi = ["no-entrypoint"] | ||
default = [] | ||
|
||
[dependencies] | ||
anchor-lang = { path = "../../../../lang" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[target.bpfel-unknown-unknown.dependencies.std] | ||
features = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use anchor_lang::prelude::*; | ||
|
||
declare_id!("TryFrom111111111111111111111111111111111111"); | ||
|
||
#[program] | ||
pub mod try_from { | ||
use super::*; | ||
|
||
pub fn init(ctx: Context<Init>, field: u8) -> Result<()> { | ||
ctx.accounts.my_account.field = field; | ||
Ok(()) | ||
} | ||
|
||
pub fn try_from(ctx: Context<TryFrom>, field: u8) -> Result<()> { | ||
let my_account = try_from!(Account::<MyAccount>, ctx.accounts.my_account)?; | ||
msg!("Field: {}", my_account.field); | ||
require_eq!(my_account.field, field); | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[derive(Accounts)] | ||
pub struct Init<'info> { | ||
#[account(init, payer = payer, space = 8 + 1)] | ||
pub my_account: Account<'info, MyAccount>, | ||
#[account(mut)] | ||
pub payer: Signer<'info>, | ||
pub system_program: Program<'info, System>, | ||
} | ||
|
||
#[derive(Accounts)] | ||
pub struct TryFrom<'info> { | ||
pub my_account: UncheckedAccount<'info>, | ||
} | ||
|
||
#[account] | ||
pub struct MyAccount { | ||
pub field: u8, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[scripts] | ||
test = "yarn run ts-mocha -t 1000000 ./tests/try-from/*.ts" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import * as anchor from "@coral-xyz/anchor"; | ||
|
||
import { TryFrom, IDL } from "../../target/types/try_from"; | ||
|
||
describe(IDL.name, () => { | ||
anchor.setProvider(anchor.AnchorProvider.env()); | ||
|
||
const program = anchor.workspace.TryFrom as anchor.Program<TryFrom>; | ||
|
||
it("Can use `try_from`", async () => { | ||
const myAccountKp = anchor.web3.Keypair.generate(); | ||
const FIELD = 5; | ||
await program.methods | ||
.init(FIELD) | ||
.accounts({ | ||
myAccount: myAccountKp.publicKey, | ||
}) | ||
.signers([myAccountKp]) | ||
.rpc(); | ||
|
||
await program.methods | ||
.tryFrom(FIELD) | ||
.accounts({ | ||
myAccount: myAccountKp.publicKey, | ||
}) | ||
.rpc(); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
it is not correct to transmute
AccountInfo<'a>
into a shorter lifetime because it containsRc<RefCell<&'a mut>>
, making it invariant, the issue is explained in https://doc.rust-lang.org/nomicon/subtyping.html#variance . Basically, somewhere else in the code might still hold the reference to longer lifetime'a
, when you transmute it to a shorter lifetime'b
and write to it, you make a use-after-free bug.I wonder why use
RefCell
holding a mutable reference at all? I did not dig into anchor code yet so I don't know why it is needed.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.
Generally speaking, what you said is correct, but I don't think it applies here because the main reason for using this macro is to first declare an account as an
UncheckedAccount
, then convert it to a checked type, e.g.Account
. When you do this, you're declaring you'll let Anchor handle the serialization of the data to the account (in the exit routine), meaning it would be incorrect to write to the raw account data yourself independent of the "use-after-free" bug you mentioned.That being said, this PR is stale and won't get merged. #3340 helps this specific issue too, but it still requires declaring the lifetimes of the accounts struct and the context are the same (e.g.
Context<'info, MyIx<'info>>
). We can improve this further by handling the annotations automatically during macro generation.That part is not coming from Anchor code, as
AccountInfo
is defined insolana-program
. I think mistakes were made in the initial days though, that type doesn't make much sense.