-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Substrate Runtime Interface #2334
Comments
Related PR: #2381. It can be seen as a first step towards that direction. |
I think we should go farther and avoid raw pointers in our APIs. |
Then how would you implement low-level APIs that depend on pointers, e.g. |
@pepyakin I do not believe that the host needs to provide |
More specifically, I want to avoid needing unsafe code on the host side, which is a potential security risk. |
@demimarie-parity this does not work... We provide our own allocator, but even the allocator interface uses raw pointers.... Pointers are not bad. |
Well, technically the code on the host side wont be unsafe since it deals only with wasm memory (for reference, take a look at For wasm we decided to provide the allocator. See the discussion in #300 |
This is a prerequisite for my work on sandboxing. |
I believe this was implemented in some form. |
The current substrate runtime API is not the easiest to work with:
with_std
/without_std
- or rather, staticly linked native code and wasm runtime), however interface should be the same.impl_function_executor
, which allows you declare parameters such asusize
which, however, will haveu32
type, which might be baffling for new people working with this code. There was a PR that introduces an honest Rust interface, but unfortunately it has a downside: there is a desire for signatures of host functions to be trivially copyable without any changes to reduce error proness.Basically, this is very error-prone and very boilerplaty code. This sounds as a good use-case for a code-generation. Here is my strawman proposal for such code generator:
We could introduce a special AST that describes an interface between the substrate runtime and the substrate host. It would support rather high-level types, e.g.:
bytes_vec
/bytes
,bool
,(T, J)
(a tuple),*T
(a raw pointer),[T; N]
for parameters and return values. We could also declare if a function traps, maybe with a typeResult
or a special annotation.Here is an example how it could look like:
and etc.
(Note that a new language is not necessary for this, we only need a model definition, which could be a rust expresion creating the model struct or it could be yaml)
Having this model, we can use some build.rs code generation for building definitions for several crates, such as:
sri-guest
- declarations of every API function for usage from the runtime side (hence guest). Probably haswithout_std
andwith_std
versions generated. Supersedes most part ofsr-io
, allocator part fromsr-std
, externs part ofsrml-sandbox
.sri-host-wasmi
- a glue code that dispatches a call toExternals
to the appropriate function in some trait, supersedes current impl_function_executor. This trait would be implemented in today's wasm_executor.rs.Code generation gives a lot of benefits, here are some:
Externals
for wasmi, potentiallyextern "C" fn
for others).impl_runtime_apis
macro, but the other way around? : ) )The text was updated successfully, but these errors were encountered: