Skip to content
This repository has been archived by the owner on Jan 17, 2022. It is now read-only.

Contract creation fails if "deploy" modifies memory #130

Open
geigerzaehler opened this issue Aug 30, 2019 · 1 comment
Open

Contract creation fails if "deploy" modifies memory #130

geigerzaehler opened this issue Aug 30, 2019 · 1 comment

Comments

@geigerzaehler
Copy link
Contributor

The contract created by a pwasm module might be invalid if the deploy code modifies memory.

For example consider the following Wasm module.

(module
  (type (;0;) (func))
  (func $call (type 0))
  (func $deploy (type 0)
    i32.const 0
    i32.const -1
    i32.store
    )
  (memory (;0;) 1 16)
  (export "memory" (memory 0))
  (export "call" (func $call))
  (export "deploy" (func $deploy)))

Here the $deploy function modifies the memory at location 0..3 by setting all bytes to 0xff.

With wasm-build this code is transformed to

(module
  (type (;0;) (func))
  (type (;1;) (func (param i32 i32)))
  (import "env" "memory" (memory (;0;) 1 16))
  (import "env" "ret" (func (;0;) (type 1)))
  (func (;1;) (type 0)
    i32.const 0
    i32.const -1
    i32.store)
  (func (;2;) (type 0)
    call 1
    i32.const 0
    i32.const 52
    call 0)
  (export "call" (func 2))
  (data (;0;) (i32.const 0) "\00asm\01\00\00\00\01\04\01`\00\00\02\10\01\03env\06memory\02\01\01\10\03\02\01\00\07\08\01\04call\00\00\0a\04\01\02\00\0b"))

If this contract is deployed the same modification to the memory happens before ret is called (call 0). However the memory at 0..3 is part of the contract code that is returned and was initialized correctly with the data section to \00asm. After func 2 is called Parity Ethereum will read the memory at 0..51 and store this as the contract data. With the memory modification this now starts with \ff\ff\ff\ff and is not valid Wasm.

@NikVolf
Copy link
Contributor

NikVolf commented Aug 30, 2019

might be so, but this kind of code should not be generated, at least using Rust

@paritytech paritytech deleted a comment from Tv-igtmomsp Jun 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants