Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
a8214fd
Add logic, test, broken benchmark
yarikbratashchuk May 18, 2022
143c158
Merge branch 'master' into seal_reentrant_count
yarikbratashchuk May 20, 2022
219eb84
Merge branch 'master' into seal_reentrant_count
yarikbratashchuk May 22, 2022
fccd751
account_entrance_count
yarikbratashchuk May 23, 2022
0a70239
Addressing comments
yarikbratashchuk May 29, 2022
6f3655a
Address @agryaznov's comments
yarikbratashchuk Jun 1, 2022
f29884b
Add test for account_entrance_count, fix ci
yarikbratashchuk Jun 2, 2022
72c8f83
Cargo fmt
yarikbratashchuk Jun 2, 2022
0f6f894
Fix tests
yarikbratashchuk Jun 2, 2022
949c438
Fix tests
yarikbratashchuk Jun 5, 2022
f34b6da
Remove delegated call from test, address comments
yarikbratashchuk Jun 16, 2022
a036585
Minor fixes and indentation in wat files
yarikbratashchuk Jun 21, 2022
3be772e
Update test for account_entrance_count
yarikbratashchuk Jun 21, 2022
5fdc100
Update reentrant_count_call test
yarikbratashchuk Jun 21, 2022
b656c88
Delegate call test
yarikbratashchuk Jun 23, 2022
17bb81f
Cargo +nightly fmt
yarikbratashchuk Jun 24, 2022
2f7f405
Address comments
yarikbratashchuk Jun 24, 2022
5f01797
Update reentrant_count_works test
yarikbratashchuk Jun 24, 2022
013e3bf
Merge branch 'master' into master
yarikbratashchuk Jun 24, 2022
3948142
Apply weights diff
yarikbratashchuk Jul 3, 2022
9c0c06f
Add fixture descriptions
yarikbratashchuk Jul 3, 2022
9c5bb47
Update comments as suggested
yarikbratashchuk Jul 3, 2022
35c0349
Update reentrant_count_call test to use seal_address
yarikbratashchuk Jul 3, 2022
3400632
Merge branch 'paritytech:master' into master
RustNinja Aug 2, 2022
ebde8e1
Merge branch 'paritytech:master' into master
Artemka374 Aug 10, 2022
321a828
change account_entrance_count_call fixture to use seal_caller
Artemka374 Sep 11, 2022
ee63f37
Merge remote-tracking branch 'paritytech/master'
Artemka374 Nov 2, 2022
3463795
fix compilation errors and apply some suggestions
Artemka374 Nov 4, 2022
1649d37
apply suggestions
Artemka374 Nov 5, 2022
8d94425
cargo fmt
Artemka374 Nov 7, 2022
0e48a2d
apply suggestions
Artemka374 Nov 10, 2022
7db2660
Merge remote-tracking branch 'paritytech/master'
Artemka374 Nov 10, 2022
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
Prev Previous commit
Next Next commit
Minor fixes and indentation in wat files
  • Loading branch information
yarikbratashchuk committed Jun 21, 2022
commit a036585d76e85c578e09cfc5c4cdc49e70e89ff6
6 changes: 3 additions & 3 deletions frame/contracts/fixtures/account_entrance_count_call.wat
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
(func (export "call")
(local $account_entrance_count i32)

;; Reading "callee" contract address (which is the address of the caller)
(call $seal_input (i32.const 0) (i32.const 32))
;; Reading "callee" contract address (which is the address of the caller)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(which is the address of the caller)

This is not the case in general for this particular fixture, and solely depends on arguments it's been called with. In your account_entrance_count_works() test you indeed call it with the same address as the caller's, but I think it's better not to make the fixture so tightly coupled with the test. So I would suggest two ways to improve this:

  1. Use seal_caller and expect different seal_account_entrance_count results for the cases callee == caller and callee != caller; or
  2. Make the fixture accept both callee and caller addresses in its input, and still xpect different seal_account_entrance_count results for the cases callee == caller and callee != caller.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, I'm not sure about it, but do callee and caller actually equal in test? Cause as I see the origin of call is ALICE and we are calling contract address, and if we are changing fixture the way you explained above, it says that caller != callee. So it is interesting to know am I getting this right=)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant here that the way the contract fixture is being called, and hence the expected state of the call stack - this all should be set in the outer unit test, and no assumption on this regard should be hard-coded into the fixture. Just return the account_entrance_count with $seal_return and check it outside the contract.

(call $seal_input (i32.const 0) (i32.const 32))

(set_local $account_entrance_count
(call $seal_account_entrance_count (i32.const 0))
)

;; assert account_entrance_count == 1
(call $assert
(i32.eq (get_local $account_entrance_count) (i32.const 1))
(i32.eq (get_local $account_entrance_count) (i32.const 1))
)
)

Expand Down
62 changes: 31 additions & 31 deletions frame/contracts/fixtures/reentrant_count_call.wat
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,46 @@
)
)
(func (export "call")
(local $exit_code i32)
(local $exit_code i32)
(local $reentrant_count i32)

(set_local $reentrant_count
(call $seal_reentrant_count)
)

(get_local $reentrant_count)
(if
(then
;; assert reentrant_count == 1
(call $assert
(i32.eq (get_local $reentrant_count) (i32.const 1))
)
)
(else
;; Reading "callee" contract address (which is the address of the caller)
(call $seal_input (i32.const 0) (i32.const 32))
(if
(then
;; assert reentrant_count == 1
(call $assert
(i32.eq (get_local $reentrant_count) (i32.const 1))
)
)
(else
;; Reading "callee" contract address (which is the address of the caller)
(call $seal_input (i32.const 0) (i32.const 32))

;; Call to itself
(set_local $exit_code
(call $seal_call
(i32.const 0) ;; Pointer to "callee" address.
(i32.const 32) ;; Length of "callee" address.
(i64.const 0) ;; How much gas to devote for the execution. 0 = all.
(i32.const 0) ;; Pointer to the buffer with value to transfer
(i32.const 0) ;; Length of the buffer with value to transfer.
(i32.const 0) ;; Pointer to input data buffer address
(i32.const 32) ;; Length of input data buffer
(i32.const 0xffffffff) ;; u32 max sentinel value: do not copy output
(i32.const 0) ;; Ptr to output buffer len
)
)
;; Call to itself
(set_local $exit_code
(call $seal_call
(i32.const 0) ;; Pointer to "callee" address.
(i32.const 32) ;; Length of "callee" address.
(i64.const 0) ;; How much gas to devote for the execution. 0 = all.
(i32.const 0) ;; Pointer to the buffer with value to transfer
(i32.const 0) ;; Length of the buffer with value to transfer.
(i32.const 0) ;; Pointer to input data buffer address
(i32.const 32) ;; Length of input data buffer
(i32.const 0xffffffff) ;; u32 max sentinel value: do not copy output
(i32.const 0) ;; Ptr to output buffer len
)
)

;; assert reentrant_count == 0
(call $assert
(i32.eq (get_local $reentrant_count) (i32.const 0))
)
)
)
;; assert reentrant_count == 0
(call $assert
(i32.eq (get_local $reentrant_count) (i32.const 0))
)
)
)
)

(func (export "deploy"))
Expand Down
2 changes: 1 addition & 1 deletion frame/contracts/src/benchmarking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2098,7 +2098,7 @@ benchmarks! {
},
],
call_body: Some(body::repeated_dyn(r * API_BENCHMARK_BATCH_SIZE, vec![
Counter(0 as u32, account_id_len as u32), // account_ptr
Counter(0, account_id_len as u32), // account_ptr
Regular(Instruction::Call(0)),
Regular(Instruction::Drop),
])),
Expand Down
8 changes: 3 additions & 5 deletions frame/contracts/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ pub trait Ext: sealing::Sealed {
/// Sets new code hash for existing contract.
fn set_code_hash(&mut self, hash: CodeHash<Self::T>) -> Result<(), DispatchError>;

/// Returns then number of times currently executing contract exists on the call stack in
/// Returns the number of times the currently executing contract exists on the call stack in
/// addition to the calling instance. A value of 0 means no reentrancy.
fn reentrant_count(&self) -> u32;

Expand Down Expand Up @@ -1239,14 +1239,12 @@ where

fn reentrant_count(&self) -> u32 {
let id: &AccountIdOf<Self::T> = &self.top_frame().account_id;
self.account_entrance_count(id).checked_sub(1).unwrap_or(0)
self.account_entrance_count(id).saturating_sub(1)
}

fn account_entrance_count(&self, account_id: &AccountIdOf<Self::T>) -> u32 {
self.frames()
.filter_map(|f| {
(f.delegate_caller.is_none() && &f.account_id == account_id).then(|| true)
})
.filter(|f| f.delegate_caller.is_none() && &f.account_id == account_id)
.count() as u32
}
}
Expand Down
8 changes: 4 additions & 4 deletions frame/contracts/src/wasm/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2087,15 +2087,15 @@ define_env!(Env, <E: Ext>,
}
},

// Returns then number of times currently executing contract exists on the call stack in addition
// Returns the number of times the currently executing contract exists on the call stack in addition
// to the calling instance.
//
// # Return Value
//
// Returns 0 when there is no reentrancy
// Returns 0 when there is no reentrancy.
[__unstable__] seal_reentrant_count(ctx) -> u32 => {
ctx.charge_gas(RuntimeCosts::ReentrantCount)?;
Ok(ctx.ext.reentrant_count() as u32)
Ok(ctx.ext.reentrant_count())
},

// Returns the number of times specified contract exists on the call stack. Delegated calls are
Expand All @@ -2111,6 +2111,6 @@ define_env!(Env, <E: Ext>,
[__unstable__] seal_account_entrance_count(ctx, account_ptr: u32) -> u32 => {
ctx.charge_gas(RuntimeCosts::AccountEntranceCount)?;
let account_id: <<E as Ext>::T as frame_system::Config>::AccountId = ctx.read_sandbox_memory_as(account_ptr)?;
Ok(ctx.ext.account_entrance_count(&account_id) as u32)
Ok(ctx.ext.account_entrance_count(&account_id))
},
);