rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
98.65k stars 12.75k forks source link

Space constraints while running from Docker Image #130707

Closed cryptanu closed 1 month ago

cryptanu commented 1 month ago

Code

use anchor_lang::{prelude::*, system_program::{transfer, Transfer}};

declare_id!("3tF8wDtNHnNXuAGrUBgV4raiCTYEyhdjRsTNoVzjB3gg");

#[program]
pub mod vault {
    use super::*;

    pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
        ctx.accounts.initialize(&ctx.bumps)?;

        Ok(())
    }

    pub fn deposit(ctx: Context<Deposit>, amount: u64) -> Result<()> {
        ctx.accounts.deposit(amount)?;

        Ok(())
    }

    pub fn withdraw(ctx: Context<Withdraw>, amount: u64) -> Result<()> {
        ctx.accounts.withdraw(amount)?;

        Ok(())
    }

    pub fn close(ctx: Context<Close>) -> Result<()> {
        ctx.accounts.close()?;

        Ok(())
    }
}

#[derive(Accounts)]
pub struct Initialize<'info> {
    #[account(mut)]
    pub user: Signer<'info>,
    #[account(
        init,
        payer = user,
        seeds = [b"state", user.key().as_ref()], 
        bump,
        space = VaultState::INIT_SPACE,
    )]
    pub vault_state: Account<'info, VaultState>,
    #[account(
        seeds = [b"vault", vault_state.key().as_ref()],
        bump,
    )]
    pub vault: SystemAccount<'info>,
    pub system_program: Program<'info, System>,
}

impl<'info> Initialize<'info> {
    pub fn initialize(&mut self, bumps: &InitializeBumps) -> Result<()> {
        self.vault_state.vault_bump = bumps.vault;
        self.vault_state.state_bump = bumps.vault_state;

        Ok(())
    }  
}

#[derive(Accounts)]
pub struct Deposit<'info> {
    #[account(mut)]
    pub user: Signer<'info>,
    #[account(
        mut,
        seeds = [b"vault", vault_state.key().as_ref()], 
        // Our program will be able sign transactions on behalf of the vault account.
        bump = vault_state.vault_bump,
    )]
    pub vault: SystemAccount<'info>,
    #[account(
        seeds = [b"state", user.key().as_ref()],
        bump = vault_state.state_bump,
    )]
    pub vault_state: Account<'info, VaultState>,
    pub system_program: Program<'info, System>,
}

impl<'info> Deposit<'info> {
    pub fn deposit(&mut self, amount: u64) -> Result<()> {

        let cpi_program = self.system_program.to_account_info();

        let cpi_accounts = Transfer {
            from: self.user.to_account_info(),
            to: self.vault.to_account_info(),
        };

        let cpi_ctx = CpiContext::new(cpi_program, cpi_accounts);

        transfer(cpi_ctx, amount)?;

        Ok(())
    }
}

#[derive(Accounts)]
pub struct Withdraw<'info> {
    #[account(mut)]
    pub user: Signer<'info>,
    #[account(
        mut,
        seeds = [b"vault", vault_state.key().as_ref()],
        bump = vault_state.vault_bump,
    )]
    pub vault: SystemAccount<'info>,
    #[account(
        seeds = [b"state", user.key().as_ref()],
        bump = vault_state.state_bump,
    )]
    pub vault_state: Account<'info, VaultState>,
    pub system_program: Program<'info, System>,
}

impl<'info> Withdraw<'info> {
    pub fn withdraw(&mut self, amount: u64) -> Result<()> {
        let cpi_program = self.system_program.to_account_info();

        let cpi_accounts = Transfer {
            from: self.vault.to_account_info(),
            to: self.user.to_account_info(),
        };

        let seeds = &[
            b"vault",
            self.vault_state.to_account_info().key.as_ref(),
            &[self.vault_state.vault_bump],
        ];

        let signer_seeds = &[&seeds[..]];

        let cpi_ctx = CpiContext::new_with_signer(cpi_program, cpi_accounts, signer_seeds);

        transfer(cpi_ctx, amount)?;

        Ok(())
    }
}

#[derive(Accounts)]
pub struct Close<'info> {
    #[account(mut)]
    pub user: Signer<'info>,
    #[account(
        mut,
        seeds = [b"vault", vault_state.key().as_ref()],
        bump = vault_state.vault_bump,
    )]
    pub vault: SystemAccount<'info>,
    #[account(
        mut,
        seeds = [b"state", user.key().as_ref()],
        bump = vault_state.state_bump,
        close = user,
    )]
    pub vault_state: Account<'info, VaultState>,
    pub system_program: Program<'info, System>,
}

impl<'info> Close<'info> {
    pub fn close(&mut self) -> Result<()> {
        let cpi_program = self.system_program.to_account_info();

        let cpi_accounts = Transfer {
            from: self.vault.to_account_info(),
            to: self.user.to_account_info(),
        };

        let seeds = &[
            b"vault",
            self.vault_state.to_account_info().key.as_ref(),
            &[self.vault_state.vault_bump],
        ];

        let signer_seeds = &[&seeds[..]];

        let cpi_ctx = CpiContext::new_with_signer(cpi_program, cpi_accounts, signer_seeds);

        transfer(cpi_ctx, self.vault.lamports())?;

        Ok(())
    }
}

#[account]
pub struct VaultState {
    pub vault_bump: u8,
    pub state_bump: u8,
}

impl Space for VaultState {
    const INIT_SPACE: usize = 8 + 1 + 1;
}
import * as anchor from "@coral-xyz/anchor";
import { Program } from "@coral-xyz/anchor";
import { Vault } from "../target/types/vault";

describe("vault", () => {
  // Configure the client to use the local cluster.
  const provider = anchor.AnchorProvider.env();
  anchor.setProvider(provider);

  const program = anchor.workspace.Vault as Program<Vault>;

  const vaultState = anchor.web3.PublicKey.findProgramAddressSync([Buffer.from("state"), provider.publicKey.toBytes()], program.programId)[0];
  const vault = anchor.web3.PublicKey.findProgramAddressSync([Buffer.from("vault"), vaultState.toBytes()], program.programId)[0];

  it("Is initialized!", async () => {
    // Add your test here.
    const tx = await program.methods
    .initialize()
    .accountsPartial({
      user: provider.wallet.publicKey,
      vaultState,
      vault,
      systemProgram: anchor.web3.SystemProgram.programId,
    })
    .rpc();

    console.log("\nYour transaction signature", tx);
    console.log("Your vault info", (await provider.connection.getAccountInfo(vault)));
  });

  it("Deposit 2 SOL", async () => {
    const tx = await program.methods
    .deposit(new anchor.BN(2 * anchor.web3.LAMPORTS_PER_SOL))
    .accountsPartial({
      user: provider.wallet.publicKey,
      vaultState,
      vault,
      systemProgram: anchor.web3.SystemProgram.programId,
    })
    .rpc();

    console.log("\nYour transaction signature", tx);
    console.log("Your vault info", (await provider.connection.getAccountInfo(vault)));
    console.log("Your vault balance", (await provider.connection.getBalance(vault)).toString());
  });

  it("Withdraw 1 SOL", async () => {
    const tx = await program.methods
    .withdraw(new anchor.BN(1 * anchor.web3.LAMPORTS_PER_SOL))
    .accountsPartial({
      user: provider.wallet.publicKey,
      vaultState,
      vault,
      systemProgram: anchor.web3.SystemProgram.programId,
    })
    .rpc();

    console.log("\nYour transaction signature", tx);
    console.log("Your vault balance", (await provider.connection.getBalance(vault)).toString());
  });

  it("Close vault", async () => {
    const tx = await program.methods
    .close()
    .accountsPartial({
      user: provider.wallet.publicKey,
      vaultState,
      vault,
      systemProgram: anchor.web3.SystemProgram.programId,
    })
    .rpc();

    console.log("\nYour transaction signature", tx);
    console.log("Your vault info", (await provider.connection.getAccountInfo(vault)));
  });
});

Meta

This is done in the Ackee Blockchain's docker image provided for their fuzzing classes rustc --version --verbose:

rustc 1.79.0 (129f3b996 2024-06-10)
binary: rustc
commit-hash: 129f3b9964af4d4a709d1383930ade12dfe7c081
commit-date: 2024-06-10
host: aarch64-unknown-linux-gnu
release: 1.79.0
LLVM version: 18.1.7

Error output

thread 'rustc' panicked at /platform-tools/out/rust/compiler/rustc_serialize/src/opaque.rs:233:42:
range start index 1089330 out of range for slice of length 954368
stack backtrace:
   0:     0xffff7dadaf30 - std::backtrace::Backtrace::create::hdf88e3421b4de45f
   1:     0xffff7e6262c4 - <alloc[f76929e5eeb9cfba]::boxed::Box<rustc_driver_impl[bd883d086d130c8]::install_ice_hook::{closure#0}> as core[946e9da878f6d747]::ops::function::Fn<(&dyn for<'a, 'b> core[946e9da878f6d747]::ops::function::Fn<(&'a core[946e9da878f6d747]::panic::panic_info::PanicInfo<'b>,), Output = ()> + core[946e9da878f6d747]::marker::Send + core[946e9da878f6d747]::marker::Sync, &core[946e9da878f6d747]::panic::panic_info::PanicInfo)>>::call
   2:     0xffff7dab9294 - std::panicking::rust_panic_with_hook::h921238c79859469e
   3:     0xffff7dad6750 - std::panicking::begin_panic_handler::{{closure}}::hb4087be21c55a89b
   4:     0xffff7dad6388 - std::sys_common::backtrace::__rust_end_short_backtrace::hbad59d6c2f9e3740
   5:     0xffff7dab8e5c - rust_begin_unwind
   6:     0xffff7da96f24 - core::panicking::panic_fmt::hd43d879a3abf890d
   7:     0xffff7da97474 - core::slice::index::slice_start_index_len_fail::h3289a0a7b90bf5fb
   8:     0xffff82906cdc - <rustc_metadata[d0354569a7eabaa7]::rmeta::decoder::MetadataBlob>::get_header
   9:     0xffff828fa12c - <rustc_metadata[d0354569a7eabaa7]::locator::CrateLocator>::extract_one
  10:     0xffff828f9118 - <rustc_metadata[d0354569a7eabaa7]::locator::CrateLocator>::extract_lib
  11:     0xffff828f7268 - <rustc_metadata[d0354569a7eabaa7]::locator::CrateLocator>::maybe_load_library_crate
  12:     0xffff8282f950 - <rustc_metadata[d0354569a7eabaa7]::creader::CrateLoader>::load
  13:     0xffff8282cf38 - <rustc_metadata[d0354569a7eabaa7]::creader::CrateLoader>::maybe_resolve_crate
  14:     0xffff82831e30 - <rustc_metadata[d0354569a7eabaa7]::creader::CrateLoader>::maybe_process_path_extern
  15:     0xffff818d3570 - <rustc_resolve[81b83bec02bd23a6]::Resolver>::extern_prelude_get
  16:     0xffff818d62e0 - <rustc_resolve[81b83bec02bd23a6]::Resolver>::early_resolve_ident_in_lexical_scope
  17:     0xffff818b9e1c - <rustc_resolve[81b83bec02bd23a6]::Resolver>::resolve_path_with_ribs
  18:     0xffff818c4130 - <rustc_resolve[81b83bec02bd23a6]::Resolver as rustc_expand[bdb021bd46e76c75]::base::ResolverExpand>::resolve_imports
  19:     0xffff82a83ac8 - <rustc_expand[bdb021bd46e76c75]::expand::MacroExpander>::fully_expand_fragment
  20:     0xffff82a8375c - <rustc_expand[bdb021bd46e76c75]::expand::MacroExpander>::expand_crate
  21:     0xffff7e802100 - <rustc_session[df7738147c5de37c]::session::Session>::time::<rustc_ast[eb0b44960e423305]::ast::Crate, rustc_interface[ca4f025b1d7eaf35]::passes::configure_and_expand::{closure#1}>
  22:     0xffff7e7cd0f4 - rustc_interface[ca4f025b1d7eaf35]::passes::resolver_for_lowering
  23:     0xffff81e78654 - rustc_query_impl[e099081b5025bca]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[e099081b5025bca]::query_impl::resolver_for_lowering::dynamic_query::{closure#2}::{closure#0}, rustc_middle[34379fe3e90da086]::query::erase::Erased<[u8; 8usize]>>
  24:     0xffff81ebd928 - <rustc_query_impl[e099081b5025bca]::query_impl::resolver_for_lowering::dynamic_query::{closure#2} as core[946e9da878f6d747]::ops::function::FnOnce<(rustc_middle[34379fe3e90da086]::ty::context::TyCtxt, ())>>::call_once
  25:     0xffff8204d3c8 - rustc_query_system[2310cbf2b66a2dbc]::query::plumbing::try_execute_query::<rustc_query_impl[e099081b5025bca]::DynamicConfig<rustc_query_system[2310cbf2b66a2dbc]::query::caches::SingleCache<rustc_middle[34379fe3e90da086]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[e099081b5025bca]::plumbing::QueryCtxt, false>
  26:     0xffff8212c580 - rustc_query_impl[e099081b5025bca]::query_impl::resolver_for_lowering::get_query_non_incr::__rust_end_short_backtrace
  27:     0xffff7e61afe4 - <rustc_middle[34379fe3e90da086]::ty::context::GlobalCtxt>::enter::<rustc_driver_impl[bd883d086d130c8]::run_compiler::{closure#1}::{closure#2}::{closure#4}, &rustc_data_structures[d89fb9a39af2443e]::steal::Steal<(rustc_middle[34379fe3e90da086]::ty::ResolverAstLowering, alloc[f76929e5eeb9cfba]::sync::Arc<rustc_ast[eb0b44960e423305]::ast::Crate>)>>
  28:     0xffff7e671848 - <rustc_interface[ca4f025b1d7eaf35]::interface::Compiler>::enter::<rustc_driver_impl[bd883d086d130c8]::run_compiler::{closure#1}::{closure#2}, core[946e9da878f6d747]::result::Result<core[946e9da878f6d747]::option::Option<rustc_interface[ca4f025b1d7eaf35]::queries::Linker>, rustc_span[69c68ca801ac8eee]::ErrorGuaranteed>>
  29:     0xffff7e64c750 - rustc_span[69c68ca801ac8eee]::set_source_map::<core[946e9da878f6d747]::result::Result<(), rustc_span[69c68ca801ac8eee]::ErrorGuaranteed>, rustc_interface[ca4f025b1d7eaf35]::interface::run_compiler<core[946e9da878f6d747]::result::Result<(), rustc_span[69c68ca801ac8eee]::ErrorGuaranteed>, rustc_driver_impl[bd883d086d130c8]::run_compiler::{closure#1}>::{closure#0}::{closure#0}>
  30:     0xffff7e61c16c - <scoped_tls[7cbd6921f62c0702]::ScopedKey<rustc_span[69c68ca801ac8eee]::SessionGlobals>>::set::<rustc_interface[ca4f025b1d7eaf35]::util::run_in_thread_pool_with_globals<rustc_interface[ca4f025b1d7eaf35]::interface::run_compiler<core[946e9da878f6d747]::result::Result<(), rustc_span[69c68ca801ac8eee]::ErrorGuaranteed>, rustc_driver_impl[bd883d086d130c8]::run_compiler::{closure#1}>::{closure#0}, core[946e9da878f6d747]::result::Result<(), rustc_span[69c68ca801ac8eee]::ErrorGuaranteed>>::{closure#0}, core[946e9da878f6d747]::result::Result<(), rustc_span[69c68ca801ac8eee]::ErrorGuaranteed>>
  31:     0xffff7e64d374 - rustc_span[69c68ca801ac8eee]::create_session_globals_then::<core[946e9da878f6d747]::result::Result<(), rustc_span[69c68ca801ac8eee]::ErrorGuaranteed>, rustc_interface[ca4f025b1d7eaf35]::util::run_in_thread_pool_with_globals<rustc_interface[ca4f025b1d7eaf35]::interface::run_compiler<core[946e9da878f6d747]::result::Result<(), rustc_span[69c68ca801ac8eee]::ErrorGuaranteed>, rustc_driver_impl[bd883d086d130c8]::run_compiler::{closure#1}>::{closure#0}, core[946e9da878f6d747]::result::Result<(), rustc_span[69c68ca801ac8eee]::ErrorGuaranteed>>::{closure#0}>
  32:     0xffff7e624c48 - std[5bcf4cc1627b3572]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[ca4f025b1d7eaf35]::util::run_in_thread_with_globals<rustc_interface[ca4f025b1d7eaf35]::util::run_in_thread_pool_with_globals<rustc_interface[ca4f025b1d7eaf35]::interface::run_compiler<core[946e9da878f6d747]::result::Result<(), rustc_span[69c68ca801ac8eee]::ErrorGuaranteed>, rustc_driver_impl[bd883d086d130c8]::run_compiler::{closure#1}>::{closure#0}, core[946e9da878f6d747]::result::Result<(), rustc_span[69c68ca801ac8eee]::ErrorGuaranteed>>::{closure#0}, core[946e9da878f6d747]::result::Result<(), rustc_span[69c68ca801ac8eee]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[946e9da878f6d747]::result::Result<(), rustc_span[69c68ca801ac8eee]::ErrorGuaranteed>>
  33:     0xffff7e64ae8c - <<std[5bcf4cc1627b3572]::thread::Builder>::spawn_unchecked_<rustc_interface[ca4f025b1d7eaf35]::util::run_in_thread_with_globals<rustc_interface[ca4f025b1d7eaf35]::util::run_in_thread_pool_with_globals<rustc_interface[ca4f025b1d7eaf35]::interface::run_compiler<core[946e9da878f6d747]::result::Result<(), rustc_span[69c68ca801ac8eee]::ErrorGuaranteed>, rustc_driver_impl[bd883d086d130c8]::run_compiler::{closure#1}>::{closure#0}, core[946e9da878f6d747]::result::Result<(), rustc_span[69c68ca801ac8eee]::ErrorGuaranteed>>::{closure#0}, core[946e9da878f6d747]::result::Result<(), rustc_span[69c68ca801ac8eee]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[946e9da878f6d747]::result::Result<(), rustc_span[69c68ca801ac8eee]::ErrorGuaranteed>>::{closure#1} as core[946e9da878f6d747]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  34:     0xffff7dad7210 - std::sys::unix::thread::Thread::new::thread_start::h5c3187c659b8887d
  35:     0xffff7d8ed5c8 - <unknown>
  36:     0xffff7d955edc - <unknown>
  37:                0x0 - <unknown>

rustc version: 1.75.0-dev
platform: aarch64-unknown-linux-gnu

query stack during panic:
#0 [resolver_for_lowering] getting the resolver for lowering
end of query stack
query stack during panic: #0 [resolver_for_lowering] getting the resolver for lowering end of query stack 0: rust_begin_unwind 1: core::panicking::panic_fmt 2: core::slice::index::slice_start_index_len_fail 3: ::get_header 4: ::extract_one 5: ::extract_lib 6: ::maybe_load_library_crate 7: ::load 8: ::maybe_resolve_crate 9: ::maybe_process_path_extern 10: ::extern_prelude_get 11: ::early_resolve_ident_in_lexical_scope 12: ::resolve_path_with_ribs 13: ::resolve_imports 14: ::fully_expand_fragment 15: ::expand_crate 16: ::time:: 17: rustc_interface::passes::resolver_for_lowering [... omitted 2 frames ...] 18: ::enter::)>> 19: ::enter::, rustc_span::ErrorGuaranteed>> 20: rustc_span::set_source_map::, rustc_interface::interface::run_compiler, rustc_driver_impl::run_compiler::{closure#1}>::{closure#0}::{closure#0}> 21: >::set::, rustc_driver_impl::run_compiler::{closure#1}>::{closure#0}, core::result::Result<(), rustc_span::ErrorGuaranteed>>::{closure#0}, core::result::Result<(), rustc_span::ErrorGuaranteed>> 22: rustc_span::create_session_globals_then::, rustc_interface::util::run_in_thread_pool_with_globals, rustc_driver_impl::run_compiler::{closure#1}>::{closure#0}, core::result::Result<(), rustc_span::ErrorGuaranteed>>::{closure#0}> error: could not compile `borsh-derive-internal` (lib) error: could not compile `borsh-derive-internal` (lib) note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace. error: could not compile `borsh-schema-derive-internal` (lib) error: the compiler unexpectedly panicked. this is a bug. note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md note: please attach the file at `/root/.cargo/registry/src/index.crates.io-6f17d22bba15001f/borsh-schema-derive-internal-0.10.3/rustc-ice-2024-09-22T15_52_34-3162.txt` to your bug report note: compiler flags: --crate-type lib -C opt-level=3 -C embed-bitcode=no -C codegen-units=1 -C overflow-checks=on note: some of the compiler flags provided by cargo are hidden query stack during panic: #0 [resolver_for_lowering] getting the resolver for lowering end of query stack error: the compiler unexpectedly panicked. this is a bug.

``` ```

saethlin commented 1 month ago

I fixed this in one of 1.78, 1.79, or 1.80. This ICE is from a 1.75 nightly, you should try updating your toolchain. If nothing else, you'll get an error message that explains that you ran out of disk space.