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

fix(core-processor): fix empty allocations #3894

Merged
merged 3 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions core-processor/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,8 @@ pub struct DispatchResult {
pub system_reservation_context: SystemReservationContext,
/// Page updates.
pub page_update: BTreeMap<GearPage, PageBuf>,
// TODO: there is no difference between no-allocations and no-changes #3853
/// New allocations set for program if it has been changed.
pub allocations: BTreeSet<WasmPage>,
pub allocations: Option<BTreeSet<WasmPage>>,
/// Whether this execution sent out a reply.
pub reply_sent: bool,
}
Expand Down
6 changes: 2 additions & 4 deletions core-processor/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ pub struct ExtInfo {
pub gas_amount: GasAmount,
pub gas_reserver: GasReserver,
pub system_reservation_context: SystemReservationContext,
pub allocations: BTreeSet<WasmPage>,
pub allocations: Option<BTreeSet<WasmPage>>,
pub pages_data: BTreeMap<GearPage, PageBuf>,
pub generated_dispatches: Vec<(Dispatch, u32, Option<ReservationId>)>,
pub awakening: Vec<(MessageId, u32)>,
Expand Down Expand Up @@ -398,9 +398,7 @@ impl ProcessorExternalities for Ext {
gas_amount: gas_counter.to_amount(),
gas_reserver,
system_reservation_context,
allocations: (allocations != initial_allocations)
.then_some(allocations)
.unwrap_or_default(),
allocations: (allocations != initial_allocations).then_some(allocations),
pages_data,
generated_dispatches,
awakening,
Expand Down
2 changes: 1 addition & 1 deletion core-processor/src/processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ pub fn process_success(
})
}

if !allocations.is_empty() {
if let Some(allocations) = allocations {
journal.push(JournalNote::UpdateAllocations {
program_id,
allocations,
Expand Down
72 changes: 71 additions & 1 deletion pallets/gear/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ use crate::{
Error, Event, GasAllowanceOf, GasBalanceOf, GasHandlerOf, GasInfo, GearBank, Limits, MailboxOf,
ProgramStorageOf, QueueOf, Schedule, TaskPoolOf, WaitlistOf,
};
use alloc::collections::BTreeSet;
use common::{
event::*, scheduler::*, storage::*, ActiveProgram, CodeStorage, GasTree, LockId, LockableTree,
Origin as _, ProgramStorage, ReservableTree,
Origin as _, Program, ProgramStorage, ReservableTree,
};
use core_processor::common::ActorExecutionErrorReplyReason;
use frame_support::{
Expand All @@ -49,6 +50,7 @@ use gear_core::{
ContextSettings, DispatchKind, IncomingDispatch, IncomingMessage, MessageContext, Payload,
ReplyInfo, StoredDispatch, UserStoredMessage,
},
pages::WasmPage,
};
use gear_core_backend::error::{
TrapExplanation, UnrecoverableExecutionError, UnrecoverableExtError, UnrecoverableWaitError,
Expand Down Expand Up @@ -14865,6 +14867,74 @@ fn incorrect_store_context() {
});
}

#[test]
fn allocate_in_init_free_in_handle() {
let static_pages = 16u16;
let wat = format!(
r#"
(module
(import "env" "memory" (memory {static_pages}))
(import "env" "alloc" (func $alloc (param i32) (result i32)))
(import "env" "free" (func $free (param i32) (result i32)))
(export "init" (func $init))
(export "handle" (func $handle))
(func $init
(call $alloc (i32.const 1))
drop
)
(func $handle
(call $free (i32.const {static_pages}))
drop
)
)
"#
);

init_logger();
new_test_ext().execute_with(|| {
assert_ok!(Gear::upload_program(
RuntimeOrigin::signed(USER_1),
ProgramCodeKind::Custom(wat.as_str()).to_bytes(),
DEFAULT_SALT.to_vec(),
vec![],
1_000_000_000,
0,
false,
));

let program_id = get_last_program_id();

run_to_next_block(None);

let Some(Program::Active(program)) = ProgramStorageOf::<Test>::get_program(program_id)
else {
panic!("program must be active")
};
assert_eq!(
program.allocations,
BTreeSet::from([WasmPage::from(static_pages)])
);

Gear::send_message(
RuntimeOrigin::signed(USER_1),
program_id,
vec![],
1_000_000_000,
0,
true,
)
.unwrap();

run_to_next_block(None);

let Some(Program::Active(program)) = ProgramStorageOf::<Test>::get_program(program_id)
else {
panic!("program must be active")
};
assert_eq!(program.allocations, BTreeSet::new());
});
}

pub(crate) mod utils {
#![allow(unused)]

Expand Down
Loading