1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use context::Context;
use holochain_core_types::error::{
    HcResult, HolochainError, RibosomeErrorCode, RibosomeReturnCode,
};
use holochain_wasm_utils::memory_allocation::decode_encoded_allocation;
use nucleus::{
    ribosome::{api::ZomeApiFunction, memory::SinglePageManager, Runtime},
    ZomeFnCall, ZomeFnResult,
};
use std::{str::FromStr, sync::Arc};
use wasmi::{
    self, Error as InterpreterError, FuncInstance, FuncRef, ImportsBuilder, ModuleImportResolver,
    ModuleInstance, NopExternals, RuntimeValue, Signature, ValueType,
};

/// Executes an exposed zome function in a wasm binary.
/// Multithreaded function
/// panics if wasm binary isn't valid.
pub fn run_dna(
    dna_name: &str,
    context: Arc<Context>,
    wasm: Vec<u8>,
    zome_call: &ZomeFnCall,
    parameters: Option<Vec<u8>>,
) -> ZomeFnResult {
    // Create wasm module from wasm binary
    let module = wasmi::Module::from_buffer(wasm).expect("wasm binary should be valid");

    // invoke_index and resolve_func work together to enable callable host functions
    // within WASM modules, which is how the core API functions
    // read about the Externals trait for more detail

    // Correlate the names of the core ZomeApiFunction's with their indexes
    // and declare its function signature (which is always the same)
    struct RuntimeModuleImportResolver;
    impl ModuleImportResolver for RuntimeModuleImportResolver {
        fn resolve_func(
            &self,
            field_name: &str,
            _signature: &Signature,
        ) -> Result<FuncRef, InterpreterError> {
            let api_fn = match ZomeApiFunction::from_str(&field_name) {
                Ok(api_fn) => api_fn,
                Err(_) => {
                    return Err(InterpreterError::Function(format!(
                        "host module doesn't export function with name {}",
                        field_name
                    )));
                }
            };

            match api_fn {
                // Abort is a way to receive useful debug info from
                // assemblyscript memory allocators, see enum definition for function signature
                ZomeApiFunction::Abort => Ok(FuncInstance::alloc_host(
                    Signature::new(
                        &[
                            ValueType::I32,
                            ValueType::I32,
                            ValueType::I32,
                            ValueType::I32,
                        ][..],
                        None,
                    ),
                    api_fn as usize,
                )),
                // All of our Zome API Functions have the same signature
                _ => Ok(FuncInstance::alloc_host(
                    Signature::new(&[ValueType::I32][..], Some(ValueType::I32)),
                    api_fn as usize,
                )),
            }
        }
    }

    // Create Imports with previously described Resolver
    let mut imports = ImportsBuilder::new();
    imports.push_resolver("env", &RuntimeModuleImportResolver);

    // Create module instance from wasm module, and start it if start is defined
    let wasm_instance = ModuleInstance::new(&module, &imports)
        .expect("Failed to instantiate module")
        .run_start(&mut NopExternals)
        .map_err(|_| HolochainError::RibosomeFailed("Module failed to start".to_string()))?;

    // write input arguments for module call in memory Buffer
    let input_parameters: Vec<_> = parameters.unwrap_or_default();

    // instantiate runtime struct for passing external state data over wasm but not to wasm
    let mut runtime = Runtime {
        memory_manager: SinglePageManager::new(&wasm_instance),
        context,
        zome_call: zome_call.clone(),
        dna_name: dna_name.to_string(),
    };

    // Write input arguments in wasm memory
    // scope for mutable borrow of runtime
    let encoded_allocation_of_input: u32;
    {
        let mut_runtime = &mut runtime;
        let maybe_allocation_of_input = mut_runtime.memory_manager.write(&input_parameters);
        encoded_allocation_of_input = match maybe_allocation_of_input {
            // No allocation to write is ok
            Err(RibosomeErrorCode::ZeroSizedAllocation) => 0,
            // Any other error is memory related
            Err(err) => {
                return Err(HolochainError::RibosomeFailed(err.to_string()));
            }
            // Write successful, encode allocation
            Ok(allocation_of_input) => allocation_of_input.encode(),
        }
    }

    // scope for mutable borrow of runtime
    let returned_encoded_allocation: u32;
    {
        let mut_runtime = &mut runtime;

        // invoke function in wasm instance
        // arguments are info for wasm on how to retrieve complex input arguments
        // which have been set in memory module
        returned_encoded_allocation = wasm_instance
            .invoke_export(
                zome_call.fn_name.clone().as_str(),
                &[RuntimeValue::I32(encoded_allocation_of_input as i32)],
                mut_runtime,
            )
            .map_err(|err| HolochainError::RibosomeFailed(err.to_string()))?
            .unwrap()
            .try_into()
            .unwrap();
    }

    // Handle result returned by called zome function
    let maybe_allocation = decode_encoded_allocation(returned_encoded_allocation);
    let return_log_msg: String;
    let return_result: HcResult<String>;
    match maybe_allocation {
        // Nothing in memory, return result depending on return_code received.
        Err(return_code) => {
            return_log_msg = return_code.to_string();
            return_result = match return_code {
                RibosomeReturnCode::Success => Ok(String::new()),
                RibosomeReturnCode::Failure(err_code) => {
                    Err(HolochainError::RibosomeFailed(err_code.to_string()))
                }
            };
        }
        // Something in memory, try to read and return it
        Ok(valid_allocation) => {
            let result = runtime.memory_manager.read(valid_allocation);
            let maybe_zome_result = String::from_utf8(result);
            match maybe_zome_result {
                Err(err) => {
                    return_log_msg = err.to_string();
                    return_result = Err(HolochainError::RibosomeFailed(err.to_string()));
                }
                Ok(json_str) => {
                    return_log_msg = json_str.clone();
                    return_result = Ok(json_str);
                }
            }
        }
    };
    // Log & done
    runtime
        .context
        .log(&format!(
            "Zome Function '{}' returned: {}",
            zome_call.fn_name, return_log_msg,
        ))
        .expect("Logger should work");
    return return_result;
}