Coverage Report

Created: 2024-05-16 12:16

/__w/smoldot/smoldot/repo/lib/src/executor/host/tests/hash_algorithms.rs
Line
Count
Source (jump to first uncovered line)
1
// Smoldot
2
// Copyright (C) 2023  Pierre Krieger
3
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
4
5
// This program is free software: you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation, either version 3 of the License, or
8
// (at your option) any later version.
9
10
// This program is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
// GNU General Public License for more details.
14
15
// You should have received a copy of the GNU General Public License
16
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18
use super::super::{
19
    vm::ExecHint, Config, HeapPages, HostVm, HostVmPrototype, StorageProofSizeBehavior,
20
};
21
use super::with_core_version_custom_sections;
22
23
/*
24
25
The Wasm fixtures in the tests of this module have been generated by compiling variations of the
26
Rust code below.
27
28
Compilation options:
29
> rustc --crate-type cdylib -C opt-level=z --target wasm32-unknown-unknown ./foo.rs
30
31
32
extern "C" {
33
    fn ext_hashing_keccak_256_version_1(ptrsz: i64) -> i32;
34
}
35
36
#[no_mangle]
37
extern "C" fn test(_param_ptr: i32, _param_sz: i32) -> i64 {
38
    let slice = b"hello world";
39
    let ptrsz = u64::from(slice.len() as u32) << 32 | u64::from(slice.as_ptr() as usize as u32);
40
41
    let input: &[u8] = unsafe {
42
        core::slice::from_raw_parts(
43
            u32::from_ne_bytes(_param_ptr.to_ne_bytes()) as usize as *const u8,
44
            u32::from_ne_bytes(_param_sz.to_ne_bytes()) as usize,
45
        )
46
    };
47
48
    let outptr =
49
        unsafe { ext_hashing_keccak_256_version_1(i64::from_ne_bytes(ptrsz.to_ne_bytes())) };
50
    let outsz = 32;
51
    let out: &[u8] = unsafe {
52
        core::slice::from_raw_parts(
53
            u32::from_ne_bytes(outptr.to_ne_bytes()) as usize as *const u8,
54
            outsz as usize,
55
        )
56
    };
57
58
    if out != input {
59
        core::arch::wasm32::unreachable()
60
    }
61
62
    0
63
}
64
65
*/
66
67
macro_rules! gen_test {
68
    ($host_fn:ident, $host_fn_out_sz:expr, $expected_out:expr) => {
69
        // TODO: also generate tests for bad host function input
70
71
        #[test]
72
7
        fn $host_fn() {
73
7
            /*
74
7
            Source code: (with the host function substituted)
75
7
76
7
                extern "C" {
77
7
                    fn ext_hashing_keccak_256_version_1(ptrsz: i64) -> i32;
78
7
                }
79
7
80
7
                #[no_mangle]
81
7
                extern "C" fn test(_param_ptr: i32, _param_sz: i32) -> i64 {
82
7
                    let slice = b"hello world";
83
7
                    let ptrsz = u64::from(slice.len() as u32) << 32 | u64::from(slice.as_ptr() as usize as u32);
84
7
85
7
                    let input: &[u8] = unsafe {
86
7
                        core::slice::from_raw_parts(
87
7
                            u32::from_ne_bytes(_param_ptr.to_ne_bytes()) as usize as *const u8,
88
7
                            u32::from_ne_bytes(_param_sz.to_ne_bytes()) as usize,
89
7
                        )
90
7
                    };
91
7
92
7
                    let outptr =
93
7
                        unsafe { ext_hashing_keccak_256_version_1(i64::from_ne_bytes(ptrsz.to_ne_bytes())) };
94
7
                    let outsz = 32;
95
7
                    let out: &[u8] = unsafe {
96
7
                        core::slice::from_raw_parts(
97
7
                            u32::from_ne_bytes(outptr.to_ne_bytes()) as usize as *const u8,
98
7
                            outsz as usize,
99
7
                        )
100
7
                    };
101
7
102
7
                    if out != input {
103
7
                        core::arch::wasm32::unreachable()
104
7
                    }
105
7
106
7
                    0
107
7
            } */
108
7
            let module_bytes = with_core_version_custom_sections(
109
7
                wat::parse_str(concat!(
110
7
                    r#"
111
7
                    (module
112
7
                        (type (;0;) (func (param i64) (result i32)))
113
7
                        (type (;1;) (func (param i32 i32) (result i64)))
114
7
                        (type (;2;) (func (param i32 i32 i32 i32) (result i32)))
115
7
                        (type (;3;) (func (param i32 i32 i32) (result i32)))
116
7
                        (import "env" ""#,
117
7
                    stringify!($host_fn),
118
7
                    r#""(func (;0;) (type 0)))
119
7
                        (func (;1;) (type 1) (param i32 i32) (result i64)
120
7
                          block  ;; label = @1
121
7
                            i32.const 1048576
122
7
                            i64.extend_i32_u
123
7
                            i64.const 47244640256
124
7
                            i64.or
125
7
                            call 0
126
7
                            i32.const "#,
127
7
                    $host_fn_out_sz,
128
7
                    r#"
129
7
                            local.get 0
130
7
                            local.get 1
131
7
                            call 2
132
7
                            br_if 0 (;@1;)
133
7
                            i64.const 0
134
7
                            return
135
7
                          end
136
7
                          unreachable
137
7
                          unreachable)
138
7
                        (func (;2;) (type 2) (param i32 i32 i32 i32) (result i32)
139
7
                          (local i32)
140
7
                          i32.const 1
141
7
                          local.set 4
142
7
                          block  ;; label = @1
143
7
                            local.get 1
144
7
                            local.get 3
145
7
                            i32.ne
146
7
                            br_if 0 (;@1;)
147
7
                            local.get 0
148
7
                            local.get 2
149
7
                            local.get 1
150
7
                            call 4
151
7
                            i32.const 0
152
7
                            i32.ne
153
7
                            local.set 4
154
7
                          end
155
7
                          local.get 4)
156
7
                        (func (;3;) (type 3) (param i32 i32 i32) (result i32)
157
7
                          (local i32 i32 i32)
158
7
                          i32.const 0
159
7
                          local.set 3
160
7
                          block  ;; label = @1
161
7
                            local.get 2
162
7
                            i32.eqz
163
7
                            br_if 0 (;@1;)
164
7
                            block  ;; label = @2
165
7
                              loop  ;; label = @3
166
7
                                local.get 0
167
7
                                i32.load8_u
168
7
                                local.tee 4
169
7
                                local.get 1
170
7
                                i32.load8_u
171
7
                                local.tee 5
172
7
                                i32.ne
173
7
                                br_if 1 (;@2;)
174
7
                                local.get 0
175
7
                                i32.const 1
176
7
                                i32.add
177
7
                                local.set 0
178
7
                                local.get 1
179
7
                                i32.const 1
180
7
                                i32.add
181
7
                                local.set 1
182
7
                                local.get 2
183
7
                                i32.const -1
184
7
                                i32.add
185
7
                                local.tee 2
186
7
                                i32.eqz
187
7
                                br_if 2 (;@1;)
188
7
                                br 0 (;@3;)
189
7
                              end
190
7
                            end
191
7
                            local.get 4
192
7
                            local.get 5
193
7
                            i32.sub
194
7
                            local.set 3
195
7
                          end
196
7
                          local.get 3)
197
7
                        (func (;4;) (type 3) (param i32 i32 i32) (result i32)
198
7
                          local.get 0
199
7
                          local.get 1
200
7
                          local.get 2
201
7
                          call 3)
202
7
                        (table (;0;) 1 1 funcref)
203
7
                        (memory (;0;) 17)
204
7
                        (global (;0;) (mut i32) (i32.const 1048576))
205
7
                        (global (;1;) i32 (i32.const 1048587))
206
7
                        (global (;2;) i32 (i32.const 1048592))
207
7
                        (export "memory" (memory 0))
208
7
                        (export "test" (func 1))
209
7
                        (export "__data_end" (global 1))
210
7
                        (export "__heap_base" (global 2))
211
7
                        (data (;0;) (i32.const 1048576) "hello world"))
212
7
            "#,
213
7
                ))
214
7
                .unwrap(),
215
7
            );
216
217
21
            for 
exec_hint14
in ExecHint::available_engines() {
218
14
                let proto = HostVmPrototype::new(Config {
219
14
                    allow_unresolved_imports: false,
220
14
                    exec_hint,
221
14
                    heap_pages: HeapPages::new(1024),
222
14
                    module: &module_bytes,
223
14
                })
224
14
                .unwrap();
225
14
226
14
                let mut vm = HostVm::from(
227
14
                    proto
228
14
                        .run(
229
14
                            "test",
230
14
                            StorageProofSizeBehavior::proof_recording_disabled(),
231
14
                            &$expected_out,
232
14
                        )
233
14
                        .unwrap(),
234
14
                );
235
28
                loop {
236
28
                    match vm {
237
14
                        HostVm::ReadyToRun(r) => vm = r.run(),
238
14
                        HostVm::Finished(v) => {
239
14
                            assert_eq!(v.value().as_ref(), b"");
240
14
                            break;
241
                        }
242
0
                        _ => unreachable!(),
243
                    }
244
                }
245
            }
246
7
        }
_RNvNtNtNtNtCsN16ciHI6Qf_7smoldot8executor4host5tests15hash_algorithmss_32ext_hashing_keccak_256_version_1
Line
Count
Source
72
1
        fn $host_fn() {
73
1
            /*
74
1
            Source code: (with the host function substituted)
75
1
76
1
                extern "C" {
77
1
                    fn ext_hashing_keccak_256_version_1(ptrsz: i64) -> i32;
78
1
                }
79
1
80
1
                #[no_mangle]
81
1
                extern "C" fn test(_param_ptr: i32, _param_sz: i32) -> i64 {
82
1
                    let slice = b"hello world";
83
1
                    let ptrsz = u64::from(slice.len() as u32) << 32 | u64::from(slice.as_ptr() as usize as u32);
84
1
85
1
                    let input: &[u8] = unsafe {
86
1
                        core::slice::from_raw_parts(
87
1
                            u32::from_ne_bytes(_param_ptr.to_ne_bytes()) as usize as *const u8,
88
1
                            u32::from_ne_bytes(_param_sz.to_ne_bytes()) as usize,
89
1
                        )
90
1
                    };
91
1
92
1
                    let outptr =
93
1
                        unsafe { ext_hashing_keccak_256_version_1(i64::from_ne_bytes(ptrsz.to_ne_bytes())) };
94
1
                    let outsz = 32;
95
1
                    let out: &[u8] = unsafe {
96
1
                        core::slice::from_raw_parts(
97
1
                            u32::from_ne_bytes(outptr.to_ne_bytes()) as usize as *const u8,
98
1
                            outsz as usize,
99
1
                        )
100
1
                    };
101
1
102
1
                    if out != input {
103
1
                        core::arch::wasm32::unreachable()
104
1
                    }
105
1
106
1
                    0
107
1
            } */
108
1
            let module_bytes = with_core_version_custom_sections(
109
1
                wat::parse_str(concat!(
110
1
                    r#"
111
1
                    (module
112
1
                        (type (;0;) (func (param i64) (result i32)))
113
1
                        (type (;1;) (func (param i32 i32) (result i64)))
114
1
                        (type (;2;) (func (param i32 i32 i32 i32) (result i32)))
115
1
                        (type (;3;) (func (param i32 i32 i32) (result i32)))
116
1
                        (import "env" ""#,
117
1
                    stringify!($host_fn),
118
1
                    r#""(func (;0;) (type 0)))
119
1
                        (func (;1;) (type 1) (param i32 i32) (result i64)
120
1
                          block  ;; label = @1
121
1
                            i32.const 1048576
122
1
                            i64.extend_i32_u
123
1
                            i64.const 47244640256
124
1
                            i64.or
125
1
                            call 0
126
1
                            i32.const "#,
127
1
                    $host_fn_out_sz,
128
1
                    r#"
129
1
                            local.get 0
130
1
                            local.get 1
131
1
                            call 2
132
1
                            br_if 0 (;@1;)
133
1
                            i64.const 0
134
1
                            return
135
1
                          end
136
1
                          unreachable
137
1
                          unreachable)
138
1
                        (func (;2;) (type 2) (param i32 i32 i32 i32) (result i32)
139
1
                          (local i32)
140
1
                          i32.const 1
141
1
                          local.set 4
142
1
                          block  ;; label = @1
143
1
                            local.get 1
144
1
                            local.get 3
145
1
                            i32.ne
146
1
                            br_if 0 (;@1;)
147
1
                            local.get 0
148
1
                            local.get 2
149
1
                            local.get 1
150
1
                            call 4
151
1
                            i32.const 0
152
1
                            i32.ne
153
1
                            local.set 4
154
1
                          end
155
1
                          local.get 4)
156
1
                        (func (;3;) (type 3) (param i32 i32 i32) (result i32)
157
1
                          (local i32 i32 i32)
158
1
                          i32.const 0
159
1
                          local.set 3
160
1
                          block  ;; label = @1
161
1
                            local.get 2
162
1
                            i32.eqz
163
1
                            br_if 0 (;@1;)
164
1
                            block  ;; label = @2
165
1
                              loop  ;; label = @3
166
1
                                local.get 0
167
1
                                i32.load8_u
168
1
                                local.tee 4
169
1
                                local.get 1
170
1
                                i32.load8_u
171
1
                                local.tee 5
172
1
                                i32.ne
173
1
                                br_if 1 (;@2;)
174
1
                                local.get 0
175
1
                                i32.const 1
176
1
                                i32.add
177
1
                                local.set 0
178
1
                                local.get 1
179
1
                                i32.const 1
180
1
                                i32.add
181
1
                                local.set 1
182
1
                                local.get 2
183
1
                                i32.const -1
184
1
                                i32.add
185
1
                                local.tee 2
186
1
                                i32.eqz
187
1
                                br_if 2 (;@1;)
188
1
                                br 0 (;@3;)
189
1
                              end
190
1
                            end
191
1
                            local.get 4
192
1
                            local.get 5
193
1
                            i32.sub
194
1
                            local.set 3
195
1
                          end
196
1
                          local.get 3)
197
1
                        (func (;4;) (type 3) (param i32 i32 i32) (result i32)
198
1
                          local.get 0
199
1
                          local.get 1
200
1
                          local.get 2
201
1
                          call 3)
202
1
                        (table (;0;) 1 1 funcref)
203
1
                        (memory (;0;) 17)
204
1
                        (global (;0;) (mut i32) (i32.const 1048576))
205
1
                        (global (;1;) i32 (i32.const 1048587))
206
1
                        (global (;2;) i32 (i32.const 1048592))
207
1
                        (export "memory" (memory 0))
208
1
                        (export "test" (func 1))
209
1
                        (export "__data_end" (global 1))
210
1
                        (export "__heap_base" (global 2))
211
1
                        (data (;0;) (i32.const 1048576) "hello world"))
212
1
            "#,
213
1
                ))
214
1
                .unwrap(),
215
1
            );
216
217
3
            for 
exec_hint2
in ExecHint::available_engines() {
218
2
                let proto = HostVmPrototype::new(Config {
219
2
                    allow_unresolved_imports: false,
220
2
                    exec_hint,
221
2
                    heap_pages: HeapPages::new(1024),
222
2
                    module: &module_bytes,
223
2
                })
224
2
                .unwrap();
225
2
226
2
                let mut vm = HostVm::from(
227
2
                    proto
228
2
                        .run(
229
2
                            "test",
230
2
                            StorageProofSizeBehavior::proof_recording_disabled(),
231
2
                            &$expected_out,
232
2
                        )
233
2
                        .unwrap(),
234
2
                );
235
4
                loop {
236
4
                    match vm {
237
2
                        HostVm::ReadyToRun(r) => vm = r.run(),
238
2
                        HostVm::Finished(v) => {
239
2
                            assert_eq!(v.value().as_ref(), b"");
240
2
                            break;
241
                        }
242
0
                        _ => unreachable!(),
243
                    }
244
                }
245
            }
246
1
        }
_RNvNtNtNtNtCsN16ciHI6Qf_7smoldot8executor4host5tests15hash_algorithmss_32ext_hashing_keccak_512_version_1
Line
Count
Source
72
1
        fn $host_fn() {
73
1
            /*
74
1
            Source code: (with the host function substituted)
75
1
76
1
                extern "C" {
77
1
                    fn ext_hashing_keccak_256_version_1(ptrsz: i64) -> i32;
78
1
                }
79
1
80
1
                #[no_mangle]
81
1
                extern "C" fn test(_param_ptr: i32, _param_sz: i32) -> i64 {
82
1
                    let slice = b"hello world";
83
1
                    let ptrsz = u64::from(slice.len() as u32) << 32 | u64::from(slice.as_ptr() as usize as u32);
84
1
85
1
                    let input: &[u8] = unsafe {
86
1
                        core::slice::from_raw_parts(
87
1
                            u32::from_ne_bytes(_param_ptr.to_ne_bytes()) as usize as *const u8,
88
1
                            u32::from_ne_bytes(_param_sz.to_ne_bytes()) as usize,
89
1
                        )
90
1
                    };
91
1
92
1
                    let outptr =
93
1
                        unsafe { ext_hashing_keccak_256_version_1(i64::from_ne_bytes(ptrsz.to_ne_bytes())) };
94
1
                    let outsz = 32;
95
1
                    let out: &[u8] = unsafe {
96
1
                        core::slice::from_raw_parts(
97
1
                            u32::from_ne_bytes(outptr.to_ne_bytes()) as usize as *const u8,
98
1
                            outsz as usize,
99
1
                        )
100
1
                    };
101
1
102
1
                    if out != input {
103
1
                        core::arch::wasm32::unreachable()
104
1
                    }
105
1
106
1
                    0
107
1
            } */
108
1
            let module_bytes = with_core_version_custom_sections(
109
1
                wat::parse_str(concat!(
110
1
                    r#"
111
1
                    (module
112
1
                        (type (;0;) (func (param i64) (result i32)))
113
1
                        (type (;1;) (func (param i32 i32) (result i64)))
114
1
                        (type (;2;) (func (param i32 i32 i32 i32) (result i32)))
115
1
                        (type (;3;) (func (param i32 i32 i32) (result i32)))
116
1
                        (import "env" ""#,
117
1
                    stringify!($host_fn),
118
1
                    r#""(func (;0;) (type 0)))
119
1
                        (func (;1;) (type 1) (param i32 i32) (result i64)
120
1
                          block  ;; label = @1
121
1
                            i32.const 1048576
122
1
                            i64.extend_i32_u
123
1
                            i64.const 47244640256
124
1
                            i64.or
125
1
                            call 0
126
1
                            i32.const "#,
127
1
                    $host_fn_out_sz,
128
1
                    r#"
129
1
                            local.get 0
130
1
                            local.get 1
131
1
                            call 2
132
1
                            br_if 0 (;@1;)
133
1
                            i64.const 0
134
1
                            return
135
1
                          end
136
1
                          unreachable
137
1
                          unreachable)
138
1
                        (func (;2;) (type 2) (param i32 i32 i32 i32) (result i32)
139
1
                          (local i32)
140
1
                          i32.const 1
141
1
                          local.set 4
142
1
                          block  ;; label = @1
143
1
                            local.get 1
144
1
                            local.get 3
145
1
                            i32.ne
146
1
                            br_if 0 (;@1;)
147
1
                            local.get 0
148
1
                            local.get 2
149
1
                            local.get 1
150
1
                            call 4
151
1
                            i32.const 0
152
1
                            i32.ne
153
1
                            local.set 4
154
1
                          end
155
1
                          local.get 4)
156
1
                        (func (;3;) (type 3) (param i32 i32 i32) (result i32)
157
1
                          (local i32 i32 i32)
158
1
                          i32.const 0
159
1
                          local.set 3
160
1
                          block  ;; label = @1
161
1
                            local.get 2
162
1
                            i32.eqz
163
1
                            br_if 0 (;@1;)
164
1
                            block  ;; label = @2
165
1
                              loop  ;; label = @3
166
1
                                local.get 0
167
1
                                i32.load8_u
168
1
                                local.tee 4
169
1
                                local.get 1
170
1
                                i32.load8_u
171
1
                                local.tee 5
172
1
                                i32.ne
173
1
                                br_if 1 (;@2;)
174
1
                                local.get 0
175
1
                                i32.const 1
176
1
                                i32.add
177
1
                                local.set 0
178
1
                                local.get 1
179
1
                                i32.const 1
180
1
                                i32.add
181
1
                                local.set 1
182
1
                                local.get 2
183
1
                                i32.const -1
184
1
                                i32.add
185
1
                                local.tee 2
186
1
                                i32.eqz
187
1
                                br_if 2 (;@1;)
188
1
                                br 0 (;@3;)
189
1
                              end
190
1
                            end
191
1
                            local.get 4
192
1
                            local.get 5
193
1
                            i32.sub
194
1
                            local.set 3
195
1
                          end
196
1
                          local.get 3)
197
1
                        (func (;4;) (type 3) (param i32 i32 i32) (result i32)
198
1
                          local.get 0
199
1
                          local.get 1
200
1
                          local.get 2
201
1
                          call 3)
202
1
                        (table (;0;) 1 1 funcref)
203
1
                        (memory (;0;) 17)
204
1
                        (global (;0;) (mut i32) (i32.const 1048576))
205
1
                        (global (;1;) i32 (i32.const 1048587))
206
1
                        (global (;2;) i32 (i32.const 1048592))
207
1
                        (export "memory" (memory 0))
208
1
                        (export "test" (func 1))
209
1
                        (export "__data_end" (global 1))
210
1
                        (export "__heap_base" (global 2))
211
1
                        (data (;0;) (i32.const 1048576) "hello world"))
212
1
            "#,
213
1
                ))
214
1
                .unwrap(),
215
1
            );
216
217
3
            for 
exec_hint2
in ExecHint::available_engines() {
218
2
                let proto = HostVmPrototype::new(Config {
219
2
                    allow_unresolved_imports: false,
220
2
                    exec_hint,
221
2
                    heap_pages: HeapPages::new(1024),
222
2
                    module: &module_bytes,
223
2
                })
224
2
                .unwrap();
225
2
226
2
                let mut vm = HostVm::from(
227
2
                    proto
228
2
                        .run(
229
2
                            "test",
230
2
                            StorageProofSizeBehavior::proof_recording_disabled(),
231
2
                            &$expected_out,
232
2
                        )
233
2
                        .unwrap(),
234
2
                );
235
4
                loop {
236
4
                    match vm {
237
2
                        HostVm::ReadyToRun(r) => vm = r.run(),
238
2
                        HostVm::Finished(v) => {
239
2
                            assert_eq!(v.value().as_ref(), b"");
240
2
                            break;
241
                        }
242
0
                        _ => unreachable!(),
243
                    }
244
                }
245
            }
246
1
        }
_RNvNtNtNtNtCsN16ciHI6Qf_7smoldot8executor4host5tests15hash_algorithmss_30ext_hashing_sha2_256_version_1
Line
Count
Source
72
1
        fn $host_fn() {
73
1
            /*
74
1
            Source code: (with the host function substituted)
75
1
76
1
                extern "C" {
77
1
                    fn ext_hashing_keccak_256_version_1(ptrsz: i64) -> i32;
78
1
                }
79
1
80
1
                #[no_mangle]
81
1
                extern "C" fn test(_param_ptr: i32, _param_sz: i32) -> i64 {
82
1
                    let slice = b"hello world";
83
1
                    let ptrsz = u64::from(slice.len() as u32) << 32 | u64::from(slice.as_ptr() as usize as u32);
84
1
85
1
                    let input: &[u8] = unsafe {
86
1
                        core::slice::from_raw_parts(
87
1
                            u32::from_ne_bytes(_param_ptr.to_ne_bytes()) as usize as *const u8,
88
1
                            u32::from_ne_bytes(_param_sz.to_ne_bytes()) as usize,
89
1
                        )
90
1
                    };
91
1
92
1
                    let outptr =
93
1
                        unsafe { ext_hashing_keccak_256_version_1(i64::from_ne_bytes(ptrsz.to_ne_bytes())) };
94
1
                    let outsz = 32;
95
1
                    let out: &[u8] = unsafe {
96
1
                        core::slice::from_raw_parts(
97
1
                            u32::from_ne_bytes(outptr.to_ne_bytes()) as usize as *const u8,
98
1
                            outsz as usize,
99
1
                        )
100
1
                    };
101
1
102
1
                    if out != input {
103
1
                        core::arch::wasm32::unreachable()
104
1
                    }
105
1
106
1
                    0
107
1
            } */
108
1
            let module_bytes = with_core_version_custom_sections(
109
1
                wat::parse_str(concat!(
110
1
                    r#"
111
1
                    (module
112
1
                        (type (;0;) (func (param i64) (result i32)))
113
1
                        (type (;1;) (func (param i32 i32) (result i64)))
114
1
                        (type (;2;) (func (param i32 i32 i32 i32) (result i32)))
115
1
                        (type (;3;) (func (param i32 i32 i32) (result i32)))
116
1
                        (import "env" ""#,
117
1
                    stringify!($host_fn),
118
1
                    r#""(func (;0;) (type 0)))
119
1
                        (func (;1;) (type 1) (param i32 i32) (result i64)
120
1
                          block  ;; label = @1
121
1
                            i32.const 1048576
122
1
                            i64.extend_i32_u
123
1
                            i64.const 47244640256
124
1
                            i64.or
125
1
                            call 0
126
1
                            i32.const "#,
127
1
                    $host_fn_out_sz,
128
1
                    r#"
129
1
                            local.get 0
130
1
                            local.get 1
131
1
                            call 2
132
1
                            br_if 0 (;@1;)
133
1
                            i64.const 0
134
1
                            return
135
1
                          end
136
1
                          unreachable
137
1
                          unreachable)
138
1
                        (func (;2;) (type 2) (param i32 i32 i32 i32) (result i32)
139
1
                          (local i32)
140
1
                          i32.const 1
141
1
                          local.set 4
142
1
                          block  ;; label = @1
143
1
                            local.get 1
144
1
                            local.get 3
145
1
                            i32.ne
146
1
                            br_if 0 (;@1;)
147
1
                            local.get 0
148
1
                            local.get 2
149
1
                            local.get 1
150
1
                            call 4
151
1
                            i32.const 0
152
1
                            i32.ne
153
1
                            local.set 4
154
1
                          end
155
1
                          local.get 4)
156
1
                        (func (;3;) (type 3) (param i32 i32 i32) (result i32)
157
1
                          (local i32 i32 i32)
158
1
                          i32.const 0
159
1
                          local.set 3
160
1
                          block  ;; label = @1
161
1
                            local.get 2
162
1
                            i32.eqz
163
1
                            br_if 0 (;@1;)
164
1
                            block  ;; label = @2
165
1
                              loop  ;; label = @3
166
1
                                local.get 0
167
1
                                i32.load8_u
168
1
                                local.tee 4
169
1
                                local.get 1
170
1
                                i32.load8_u
171
1
                                local.tee 5
172
1
                                i32.ne
173
1
                                br_if 1 (;@2;)
174
1
                                local.get 0
175
1
                                i32.const 1
176
1
                                i32.add
177
1
                                local.set 0
178
1
                                local.get 1
179
1
                                i32.const 1
180
1
                                i32.add
181
1
                                local.set 1
182
1
                                local.get 2
183
1
                                i32.const -1
184
1
                                i32.add
185
1
                                local.tee 2
186
1
                                i32.eqz
187
1
                                br_if 2 (;@1;)
188
1
                                br 0 (;@3;)
189
1
                              end
190
1
                            end
191
1
                            local.get 4
192
1
                            local.get 5
193
1
                            i32.sub
194
1
                            local.set 3
195
1
                          end
196
1
                          local.get 3)
197
1
                        (func (;4;) (type 3) (param i32 i32 i32) (result i32)
198
1
                          local.get 0
199
1
                          local.get 1
200
1
                          local.get 2
201
1
                          call 3)
202
1
                        (table (;0;) 1 1 funcref)
203
1
                        (memory (;0;) 17)
204
1
                        (global (;0;) (mut i32) (i32.const 1048576))
205
1
                        (global (;1;) i32 (i32.const 1048587))
206
1
                        (global (;2;) i32 (i32.const 1048592))
207
1
                        (export "memory" (memory 0))
208
1
                        (export "test" (func 1))
209
1
                        (export "__data_end" (global 1))
210
1
                        (export "__heap_base" (global 2))
211
1
                        (data (;0;) (i32.const 1048576) "hello world"))
212
1
            "#,
213
1
                ))
214
1
                .unwrap(),
215
1
            );
216
217
3
            for 
exec_hint2
in ExecHint::available_engines() {
218
2
                let proto = HostVmPrototype::new(Config {
219
2
                    allow_unresolved_imports: false,
220
2
                    exec_hint,
221
2
                    heap_pages: HeapPages::new(1024),
222
2
                    module: &module_bytes,
223
2
                })
224
2
                .unwrap();
225
2
226
2
                let mut vm = HostVm::from(
227
2
                    proto
228
2
                        .run(
229
2
                            "test",
230
2
                            StorageProofSizeBehavior::proof_recording_disabled(),
231
2
                            &$expected_out,
232
2
                        )
233
2
                        .unwrap(),
234
2
                );
235
4
                loop {
236
4
                    match vm {
237
2
                        HostVm::ReadyToRun(r) => vm = r.run(),
238
2
                        HostVm::Finished(v) => {
239
2
                            assert_eq!(v.value().as_ref(), b"");
240
2
                            break;
241
                        }
242
0
                        _ => unreachable!(),
243
                    }
244
                }
245
            }
246
1
        }
_RNvNtNtNtNtCsN16ciHI6Qf_7smoldot8executor4host5tests15hash_algorithmss_32ext_hashing_blake2_256_version_1
Line
Count
Source
72
1
        fn $host_fn() {
73
1
            /*
74
1
            Source code: (with the host function substituted)
75
1
76
1
                extern "C" {
77
1
                    fn ext_hashing_keccak_256_version_1(ptrsz: i64) -> i32;
78
1
                }
79
1
80
1
                #[no_mangle]
81
1
                extern "C" fn test(_param_ptr: i32, _param_sz: i32) -> i64 {
82
1
                    let slice = b"hello world";
83
1
                    let ptrsz = u64::from(slice.len() as u32) << 32 | u64::from(slice.as_ptr() as usize as u32);
84
1
85
1
                    let input: &[u8] = unsafe {
86
1
                        core::slice::from_raw_parts(
87
1
                            u32::from_ne_bytes(_param_ptr.to_ne_bytes()) as usize as *const u8,
88
1
                            u32::from_ne_bytes(_param_sz.to_ne_bytes()) as usize,
89
1
                        )
90
1
                    };
91
1
92
1
                    let outptr =
93
1
                        unsafe { ext_hashing_keccak_256_version_1(i64::from_ne_bytes(ptrsz.to_ne_bytes())) };
94
1
                    let outsz = 32;
95
1
                    let out: &[u8] = unsafe {
96
1
                        core::slice::from_raw_parts(
97
1
                            u32::from_ne_bytes(outptr.to_ne_bytes()) as usize as *const u8,
98
1
                            outsz as usize,
99
1
                        )
100
1
                    };
101
1
102
1
                    if out != input {
103
1
                        core::arch::wasm32::unreachable()
104
1
                    }
105
1
106
1
                    0
107
1
            } */
108
1
            let module_bytes = with_core_version_custom_sections(
109
1
                wat::parse_str(concat!(
110
1
                    r#"
111
1
                    (module
112
1
                        (type (;0;) (func (param i64) (result i32)))
113
1
                        (type (;1;) (func (param i32 i32) (result i64)))
114
1
                        (type (;2;) (func (param i32 i32 i32 i32) (result i32)))
115
1
                        (type (;3;) (func (param i32 i32 i32) (result i32)))
116
1
                        (import "env" ""#,
117
1
                    stringify!($host_fn),
118
1
                    r#""(func (;0;) (type 0)))
119
1
                        (func (;1;) (type 1) (param i32 i32) (result i64)
120
1
                          block  ;; label = @1
121
1
                            i32.const 1048576
122
1
                            i64.extend_i32_u
123
1
                            i64.const 47244640256
124
1
                            i64.or
125
1
                            call 0
126
1
                            i32.const "#,
127
1
                    $host_fn_out_sz,
128
1
                    r#"
129
1
                            local.get 0
130
1
                            local.get 1
131
1
                            call 2
132
1
                            br_if 0 (;@1;)
133
1
                            i64.const 0
134
1
                            return
135
1
                          end
136
1
                          unreachable
137
1
                          unreachable)
138
1
                        (func (;2;) (type 2) (param i32 i32 i32 i32) (result i32)
139
1
                          (local i32)
140
1
                          i32.const 1
141
1
                          local.set 4
142
1
                          block  ;; label = @1
143
1
                            local.get 1
144
1
                            local.get 3
145
1
                            i32.ne
146
1
                            br_if 0 (;@1;)
147
1
                            local.get 0
148
1
                            local.get 2
149
1
                            local.get 1
150
1
                            call 4
151
1
                            i32.const 0
152
1
                            i32.ne
153
1
                            local.set 4
154
1
                          end
155
1
                          local.get 4)
156
1
                        (func (;3;) (type 3) (param i32 i32 i32) (result i32)
157
1
                          (local i32 i32 i32)
158
1
                          i32.const 0
159
1
                          local.set 3
160
1
                          block  ;; label = @1
161
1
                            local.get 2
162
1
                            i32.eqz
163
1
                            br_if 0 (;@1;)
164
1
                            block  ;; label = @2
165
1
                              loop  ;; label = @3
166
1
                                local.get 0
167
1
                                i32.load8_u
168
1
                                local.tee 4
169
1
                                local.get 1
170
1
                                i32.load8_u
171
1
                                local.tee 5
172
1
                                i32.ne
173
1
                                br_if 1 (;@2;)
174
1
                                local.get 0
175
1
                                i32.const 1
176
1
                                i32.add
177
1
                                local.set 0
178
1
                                local.get 1
179
1
                                i32.const 1
180
1
                                i32.add
181
1
                                local.set 1
182
1
                                local.get 2
183
1
                                i32.const -1
184
1
                                i32.add
185
1
                                local.tee 2
186
1
                                i32.eqz
187
1
                                br_if 2 (;@1;)
188
1
                                br 0 (;@3;)
189
1
                              end
190
1
                            end
191
1
                            local.get 4
192
1
                            local.get 5
193
1
                            i32.sub
194
1
                            local.set 3
195
1
                          end
196
1
                          local.get 3)
197
1
                        (func (;4;) (type 3) (param i32 i32 i32) (result i32)
198
1
                          local.get 0
199
1
                          local.get 1
200
1
                          local.get 2
201
1
                          call 3)
202
1
                        (table (;0;) 1 1 funcref)
203
1
                        (memory (;0;) 17)
204
1
                        (global (;0;) (mut i32) (i32.const 1048576))
205
1
                        (global (;1;) i32 (i32.const 1048587))
206
1
                        (global (;2;) i32 (i32.const 1048592))
207
1
                        (export "memory" (memory 0))
208
1
                        (export "test" (func 1))
209
1
                        (export "__data_end" (global 1))
210
1
                        (export "__heap_base" (global 2))
211
1
                        (data (;0;) (i32.const 1048576) "hello world"))
212
1
            "#,
213
1
                ))
214
1
                .unwrap(),
215
1
            );
216
217
3
            for 
exec_hint2
in ExecHint::available_engines() {
218
2
                let proto = HostVmPrototype::new(Config {
219
2
                    allow_unresolved_imports: false,
220
2
                    exec_hint,
221
2
                    heap_pages: HeapPages::new(1024),
222
2
                    module: &module_bytes,
223
2
                })
224
2
                .unwrap();
225
2
226
2
                let mut vm = HostVm::from(
227
2
                    proto
228
2
                        .run(
229
2
                            "test",
230
2
                            StorageProofSizeBehavior::proof_recording_disabled(),
231
2
                            &$expected_out,
232
2
                        )
233
2
                        .unwrap(),
234
2
                );
235
4
                loop {
236
4
                    match vm {
237
2
                        HostVm::ReadyToRun(r) => vm = r.run(),
238
2
                        HostVm::Finished(v) => {
239
2
                            assert_eq!(v.value().as_ref(), b"");
240
2
                            break;
241
                        }
242
0
                        _ => unreachable!(),
243
                    }
244
                }
245
            }
246
1
        }
_RNvNtNtNtNtCsN16ciHI6Qf_7smoldot8executor4host5tests15hash_algorithmss_29ext_hashing_twox_64_version_1
Line
Count
Source
72
1
        fn $host_fn() {
73
1
            /*
74
1
            Source code: (with the host function substituted)
75
1
76
1
                extern "C" {
77
1
                    fn ext_hashing_keccak_256_version_1(ptrsz: i64) -> i32;
78
1
                }
79
1
80
1
                #[no_mangle]
81
1
                extern "C" fn test(_param_ptr: i32, _param_sz: i32) -> i64 {
82
1
                    let slice = b"hello world";
83
1
                    let ptrsz = u64::from(slice.len() as u32) << 32 | u64::from(slice.as_ptr() as usize as u32);
84
1
85
1
                    let input: &[u8] = unsafe {
86
1
                        core::slice::from_raw_parts(
87
1
                            u32::from_ne_bytes(_param_ptr.to_ne_bytes()) as usize as *const u8,
88
1
                            u32::from_ne_bytes(_param_sz.to_ne_bytes()) as usize,
89
1
                        )
90
1
                    };
91
1
92
1
                    let outptr =
93
1
                        unsafe { ext_hashing_keccak_256_version_1(i64::from_ne_bytes(ptrsz.to_ne_bytes())) };
94
1
                    let outsz = 32;
95
1
                    let out: &[u8] = unsafe {
96
1
                        core::slice::from_raw_parts(
97
1
                            u32::from_ne_bytes(outptr.to_ne_bytes()) as usize as *const u8,
98
1
                            outsz as usize,
99
1
                        )
100
1
                    };
101
1
102
1
                    if out != input {
103
1
                        core::arch::wasm32::unreachable()
104
1
                    }
105
1
106
1
                    0
107
1
            } */
108
1
            let module_bytes = with_core_version_custom_sections(
109
1
                wat::parse_str(concat!(
110
1
                    r#"
111
1
                    (module
112
1
                        (type (;0;) (func (param i64) (result i32)))
113
1
                        (type (;1;) (func (param i32 i32) (result i64)))
114
1
                        (type (;2;) (func (param i32 i32 i32 i32) (result i32)))
115
1
                        (type (;3;) (func (param i32 i32 i32) (result i32)))
116
1
                        (import "env" ""#,
117
1
                    stringify!($host_fn),
118
1
                    r#""(func (;0;) (type 0)))
119
1
                        (func (;1;) (type 1) (param i32 i32) (result i64)
120
1
                          block  ;; label = @1
121
1
                            i32.const 1048576
122
1
                            i64.extend_i32_u
123
1
                            i64.const 47244640256
124
1
                            i64.or
125
1
                            call 0
126
1
                            i32.const "#,
127
1
                    $host_fn_out_sz,
128
1
                    r#"
129
1
                            local.get 0
130
1
                            local.get 1
131
1
                            call 2
132
1
                            br_if 0 (;@1;)
133
1
                            i64.const 0
134
1
                            return
135
1
                          end
136
1
                          unreachable
137
1
                          unreachable)
138
1
                        (func (;2;) (type 2) (param i32 i32 i32 i32) (result i32)
139
1
                          (local i32)
140
1
                          i32.const 1
141
1
                          local.set 4
142
1
                          block  ;; label = @1
143
1
                            local.get 1
144
1
                            local.get 3
145
1
                            i32.ne
146
1
                            br_if 0 (;@1;)
147
1
                            local.get 0
148
1
                            local.get 2
149
1
                            local.get 1
150
1
                            call 4
151
1
                            i32.const 0
152
1
                            i32.ne
153
1
                            local.set 4
154
1
                          end
155
1
                          local.get 4)
156
1
                        (func (;3;) (type 3) (param i32 i32 i32) (result i32)
157
1
                          (local i32 i32 i32)
158
1
                          i32.const 0
159
1
                          local.set 3
160
1
                          block  ;; label = @1
161
1
                            local.get 2
162
1
                            i32.eqz
163
1
                            br_if 0 (;@1;)
164
1
                            block  ;; label = @2
165
1
                              loop  ;; label = @3
166
1
                                local.get 0
167
1
                                i32.load8_u
168
1
                                local.tee 4
169
1
                                local.get 1
170
1
                                i32.load8_u
171
1
                                local.tee 5
172
1
                                i32.ne
173
1
                                br_if 1 (;@2;)
174
1
                                local.get 0
175
1
                                i32.const 1
176
1
                                i32.add
177
1
                                local.set 0
178
1
                                local.get 1
179
1
                                i32.const 1
180
1
                                i32.add
181
1
                                local.set 1
182
1
                                local.get 2
183
1
                                i32.const -1
184
1
                                i32.add
185
1
                                local.tee 2
186
1
                                i32.eqz
187
1
                                br_if 2 (;@1;)
188
1
                                br 0 (;@3;)
189
1
                              end
190
1
                            end
191
1
                            local.get 4
192
1
                            local.get 5
193
1
                            i32.sub
194
1
                            local.set 3
195
1
                          end
196
1
                          local.get 3)
197
1
                        (func (;4;) (type 3) (param i32 i32 i32) (result i32)
198
1
                          local.get 0
199
1
                          local.get 1
200
1
                          local.get 2
201
1
                          call 3)
202
1
                        (table (;0;) 1 1 funcref)
203
1
                        (memory (;0;) 17)
204
1
                        (global (;0;) (mut i32) (i32.const 1048576))
205
1
                        (global (;1;) i32 (i32.const 1048587))
206
1
                        (global (;2;) i32 (i32.const 1048592))
207
1
                        (export "memory" (memory 0))
208
1
                        (export "test" (func 1))
209
1
                        (export "__data_end" (global 1))
210
1
                        (export "__heap_base" (global 2))
211
1
                        (data (;0;) (i32.const 1048576) "hello world"))
212
1
            "#,
213
1
                ))
214
1
                .unwrap(),
215
1
            );
216
217
3
            for 
exec_hint2
in ExecHint::available_engines() {
218
2
                let proto = HostVmPrototype::new(Config {
219
2
                    allow_unresolved_imports: false,
220
2
                    exec_hint,
221
2
                    heap_pages: HeapPages::new(1024),
222
2
                    module: &module_bytes,
223
2
                })
224
2
                .unwrap();
225
2
226
2
                let mut vm = HostVm::from(
227
2
                    proto
228
2
                        .run(
229
2
                            "test",
230
2
                            StorageProofSizeBehavior::proof_recording_disabled(),
231
2
                            &$expected_out,
232
2
                        )
233
2
                        .unwrap(),
234
2
                );
235
4
                loop {
236
4
                    match vm {
237
2
                        HostVm::ReadyToRun(r) => vm = r.run(),
238
2
                        HostVm::Finished(v) => {
239
2
                            assert_eq!(v.value().as_ref(), b"");
240
2
                            break;
241
                        }
242
0
                        _ => unreachable!(),
243
                    }
244
                }
245
            }
246
1
        }
_RNvNtNtNtNtCsN16ciHI6Qf_7smoldot8executor4host5tests15hash_algorithmss_30ext_hashing_twox_128_version_1
Line
Count
Source
72
1
        fn $host_fn() {
73
1
            /*
74
1
            Source code: (with the host function substituted)
75
1
76
1
                extern "C" {
77
1
                    fn ext_hashing_keccak_256_version_1(ptrsz: i64) -> i32;
78
1
                }
79
1
80
1
                #[no_mangle]
81
1
                extern "C" fn test(_param_ptr: i32, _param_sz: i32) -> i64 {
82
1
                    let slice = b"hello world";
83
1
                    let ptrsz = u64::from(slice.len() as u32) << 32 | u64::from(slice.as_ptr() as usize as u32);
84
1
85
1
                    let input: &[u8] = unsafe {
86
1
                        core::slice::from_raw_parts(
87
1
                            u32::from_ne_bytes(_param_ptr.to_ne_bytes()) as usize as *const u8,
88
1
                            u32::from_ne_bytes(_param_sz.to_ne_bytes()) as usize,
89
1
                        )
90
1
                    };
91
1
92
1
                    let outptr =
93
1
                        unsafe { ext_hashing_keccak_256_version_1(i64::from_ne_bytes(ptrsz.to_ne_bytes())) };
94
1
                    let outsz = 32;
95
1
                    let out: &[u8] = unsafe {
96
1
                        core::slice::from_raw_parts(
97
1
                            u32::from_ne_bytes(outptr.to_ne_bytes()) as usize as *const u8,
98
1
                            outsz as usize,
99
1
                        )
100
1
                    };
101
1
102
1
                    if out != input {
103
1
                        core::arch::wasm32::unreachable()
104
1
                    }
105
1
106
1
                    0
107
1
            } */
108
1
            let module_bytes = with_core_version_custom_sections(
109
1
                wat::parse_str(concat!(
110
1
                    r#"
111
1
                    (module
112
1
                        (type (;0;) (func (param i64) (result i32)))
113
1
                        (type (;1;) (func (param i32 i32) (result i64)))
114
1
                        (type (;2;) (func (param i32 i32 i32 i32) (result i32)))
115
1
                        (type (;3;) (func (param i32 i32 i32) (result i32)))
116
1
                        (import "env" ""#,
117
1
                    stringify!($host_fn),
118
1
                    r#""(func (;0;) (type 0)))
119
1
                        (func (;1;) (type 1) (param i32 i32) (result i64)
120
1
                          block  ;; label = @1
121
1
                            i32.const 1048576
122
1
                            i64.extend_i32_u
123
1
                            i64.const 47244640256
124
1
                            i64.or
125
1
                            call 0
126
1
                            i32.const "#,
127
1
                    $host_fn_out_sz,
128
1
                    r#"
129
1
                            local.get 0
130
1
                            local.get 1
131
1
                            call 2
132
1
                            br_if 0 (;@1;)
133
1
                            i64.const 0
134
1
                            return
135
1
                          end
136
1
                          unreachable
137
1
                          unreachable)
138
1
                        (func (;2;) (type 2) (param i32 i32 i32 i32) (result i32)
139
1
                          (local i32)
140
1
                          i32.const 1
141
1
                          local.set 4
142
1
                          block  ;; label = @1
143
1
                            local.get 1
144
1
                            local.get 3
145
1
                            i32.ne
146
1
                            br_if 0 (;@1;)
147
1
                            local.get 0
148
1
                            local.get 2
149
1
                            local.get 1
150
1
                            call 4
151
1
                            i32.const 0
152
1
                            i32.ne
153
1
                            local.set 4
154
1
                          end
155
1
                          local.get 4)
156
1
                        (func (;3;) (type 3) (param i32 i32 i32) (result i32)
157
1
                          (local i32 i32 i32)
158
1
                          i32.const 0
159
1
                          local.set 3
160
1
                          block  ;; label = @1
161
1
                            local.get 2
162
1
                            i32.eqz
163
1
                            br_if 0 (;@1;)
164
1
                            block  ;; label = @2
165
1
                              loop  ;; label = @3
166
1
                                local.get 0
167
1
                                i32.load8_u
168
1
                                local.tee 4
169
1
                                local.get 1
170
1
                                i32.load8_u
171
1
                                local.tee 5
172
1
                                i32.ne
173
1
                                br_if 1 (;@2;)
174
1
                                local.get 0
175
1
                                i32.const 1
176
1
                                i32.add
177
1
                                local.set 0
178
1
                                local.get 1
179
1
                                i32.const 1
180
1
                                i32.add
181
1
                                local.set 1
182
1
                                local.get 2
183
1
                                i32.const -1
184
1
                                i32.add
185
1
                                local.tee 2
186
1
                                i32.eqz
187
1
                                br_if 2 (;@1;)
188
1
                                br 0 (;@3;)
189
1
                              end
190
1
                            end
191
1
                            local.get 4
192
1
                            local.get 5
193
1
                            i32.sub
194
1
                            local.set 3
195
1
                          end
196
1
                          local.get 3)
197
1
                        (func (;4;) (type 3) (param i32 i32 i32) (result i32)
198
1
                          local.get 0
199
1
                          local.get 1
200
1
                          local.get 2
201
1
                          call 3)
202
1
                        (table (;0;) 1 1 funcref)
203
1
                        (memory (;0;) 17)
204
1
                        (global (;0;) (mut i32) (i32.const 1048576))
205
1
                        (global (;1;) i32 (i32.const 1048587))
206
1
                        (global (;2;) i32 (i32.const 1048592))
207
1
                        (export "memory" (memory 0))
208
1
                        (export "test" (func 1))
209
1
                        (export "__data_end" (global 1))
210
1
                        (export "__heap_base" (global 2))
211
1
                        (data (;0;) (i32.const 1048576) "hello world"))
212
1
            "#,
213
1
                ))
214
1
                .unwrap(),
215
1
            );
216
217
3
            for 
exec_hint2
in ExecHint::available_engines() {
218
2
                let proto = HostVmPrototype::new(Config {
219
2
                    allow_unresolved_imports: false,
220
2
                    exec_hint,
221
2
                    heap_pages: HeapPages::new(1024),
222
2
                    module: &module_bytes,
223
2
                })
224
2
                .unwrap();
225
2
226
2
                let mut vm = HostVm::from(
227
2
                    proto
228
2
                        .run(
229
2
                            "test",
230
2
                            StorageProofSizeBehavior::proof_recording_disabled(),
231
2
                            &$expected_out,
232
2
                        )
233
2
                        .unwrap(),
234
2
                );
235
4
                loop {
236
4
                    match vm {
237
2
                        HostVm::ReadyToRun(r) => vm = r.run(),
238
2
                        HostVm::Finished(v) => {
239
2
                            assert_eq!(v.value().as_ref(), b"");
240
2
                            break;
241
                        }
242
0
                        _ => unreachable!(),
243
                    }
244
                }
245
            }
246
1
        }
_RNvNtNtNtNtCsN16ciHI6Qf_7smoldot8executor4host5tests15hash_algorithmss_30ext_hashing_twox_256_version_1
Line
Count
Source
72
1
        fn $host_fn() {
73
1
            /*
74
1
            Source code: (with the host function substituted)
75
1
76
1
                extern "C" {
77
1
                    fn ext_hashing_keccak_256_version_1(ptrsz: i64) -> i32;
78
1
                }
79
1
80
1
                #[no_mangle]
81
1
                extern "C" fn test(_param_ptr: i32, _param_sz: i32) -> i64 {
82
1
                    let slice = b"hello world";
83
1
                    let ptrsz = u64::from(slice.len() as u32) << 32 | u64::from(slice.as_ptr() as usize as u32);
84
1
85
1
                    let input: &[u8] = unsafe {
86
1
                        core::slice::from_raw_parts(
87
1
                            u32::from_ne_bytes(_param_ptr.to_ne_bytes()) as usize as *const u8,
88
1
                            u32::from_ne_bytes(_param_sz.to_ne_bytes()) as usize,
89
1
                        )
90
1
                    };
91
1
92
1
                    let outptr =
93
1
                        unsafe { ext_hashing_keccak_256_version_1(i64::from_ne_bytes(ptrsz.to_ne_bytes())) };
94
1
                    let outsz = 32;
95
1
                    let out: &[u8] = unsafe {
96
1
                        core::slice::from_raw_parts(
97
1
                            u32::from_ne_bytes(outptr.to_ne_bytes()) as usize as *const u8,
98
1
                            outsz as usize,
99
1
                        )
100
1
                    };
101
1
102
1
                    if out != input {
103
1
                        core::arch::wasm32::unreachable()
104
1
                    }
105
1
106
1
                    0
107
1
            } */
108
1
            let module_bytes = with_core_version_custom_sections(
109
1
                wat::parse_str(concat!(
110
1
                    r#"
111
1
                    (module
112
1
                        (type (;0;) (func (param i64) (result i32)))
113
1
                        (type (;1;) (func (param i32 i32) (result i64)))
114
1
                        (type (;2;) (func (param i32 i32 i32 i32) (result i32)))
115
1
                        (type (;3;) (func (param i32 i32 i32) (result i32)))
116
1
                        (import "env" ""#,
117
1
                    stringify!($host_fn),
118
1
                    r#""(func (;0;) (type 0)))
119
1
                        (func (;1;) (type 1) (param i32 i32) (result i64)
120
1
                          block  ;; label = @1
121
1
                            i32.const 1048576
122
1
                            i64.extend_i32_u
123
1
                            i64.const 47244640256
124
1
                            i64.or
125
1
                            call 0
126
1
                            i32.const "#,
127
1
                    $host_fn_out_sz,
128
1
                    r#"
129
1
                            local.get 0
130
1
                            local.get 1
131
1
                            call 2
132
1
                            br_if 0 (;@1;)
133
1
                            i64.const 0
134
1
                            return
135
1
                          end
136
1
                          unreachable
137
1
                          unreachable)
138
1
                        (func (;2;) (type 2) (param i32 i32 i32 i32) (result i32)
139
1
                          (local i32)
140
1
                          i32.const 1
141
1
                          local.set 4
142
1
                          block  ;; label = @1
143
1
                            local.get 1
144
1
                            local.get 3
145
1
                            i32.ne
146
1
                            br_if 0 (;@1;)
147
1
                            local.get 0
148
1
                            local.get 2
149
1
                            local.get 1
150
1
                            call 4
151
1
                            i32.const 0
152
1
                            i32.ne
153
1
                            local.set 4
154
1
                          end
155
1
                          local.get 4)
156
1
                        (func (;3;) (type 3) (param i32 i32 i32) (result i32)
157
1
                          (local i32 i32 i32)
158
1
                          i32.const 0
159
1
                          local.set 3
160
1
                          block  ;; label = @1
161
1
                            local.get 2
162
1
                            i32.eqz
163
1
                            br_if 0 (;@1;)
164
1
                            block  ;; label = @2
165
1
                              loop  ;; label = @3
166
1
                                local.get 0
167
1
                                i32.load8_u
168
1
                                local.tee 4
169
1
                                local.get 1
170
1
                                i32.load8_u
171
1
                                local.tee 5
172
1
                                i32.ne
173
1
                                br_if 1 (;@2;)
174
1
                                local.get 0
175
1
                                i32.const 1
176
1
                                i32.add
177
1
                                local.set 0
178
1
                                local.get 1
179
1
                                i32.const 1
180
1
                                i32.add
181
1
                                local.set 1
182
1
                                local.get 2
183
1
                                i32.const -1
184
1
                                i32.add
185
1
                                local.tee 2
186
1
                                i32.eqz
187
1
                                br_if 2 (;@1;)
188
1
                                br 0 (;@3;)
189
1
                              end
190
1
                            end
191
1
                            local.get 4
192
1
                            local.get 5
193
1
                            i32.sub
194
1
                            local.set 3
195
1
                          end
196
1
                          local.get 3)
197
1
                        (func (;4;) (type 3) (param i32 i32 i32) (result i32)
198
1
                          local.get 0
199
1
                          local.get 1
200
1
                          local.get 2
201
1
                          call 3)
202
1
                        (table (;0;) 1 1 funcref)
203
1
                        (memory (;0;) 17)
204
1
                        (global (;0;) (mut i32) (i32.const 1048576))
205
1
                        (global (;1;) i32 (i32.const 1048587))
206
1
                        (global (;2;) i32 (i32.const 1048592))
207
1
                        (export "memory" (memory 0))
208
1
                        (export "test" (func 1))
209
1
                        (export "__data_end" (global 1))
210
1
                        (export "__heap_base" (global 2))
211
1
                        (data (;0;) (i32.const 1048576) "hello world"))
212
1
            "#,
213
1
                ))
214
1
                .unwrap(),
215
1
            );
216
217
3
            for 
exec_hint2
in ExecHint::available_engines() {
218
2
                let proto = HostVmPrototype::new(Config {
219
2
                    allow_unresolved_imports: false,
220
2
                    exec_hint,
221
2
                    heap_pages: HeapPages::new(1024),
222
2
                    module: &module_bytes,
223
2
                })
224
2
                .unwrap();
225
2
226
2
                let mut vm = HostVm::from(
227
2
                    proto
228
2
                        .run(
229
2
                            "test",
230
2
                            StorageProofSizeBehavior::proof_recording_disabled(),
231
2
                            &$expected_out,
232
2
                        )
233
2
                        .unwrap(),
234
2
                );
235
4
                loop {
236
4
                    match vm {
237
2
                        HostVm::ReadyToRun(r) => vm = r.run(),
238
2
                        HostVm::Finished(v) => {
239
2
                            assert_eq!(v.value().as_ref(), b"");
240
2
                            break;
241
                        }
242
0
                        _ => unreachable!(),
243
                    }
244
                }
245
            }
246
1
        }
247
    };
248
}
249
250
gen_test!(
251
    ext_hashing_keccak_256_version_1,
252
    32,
253
    [
254
        0x47, 0x17, 0x32, 0x85, 0xa8, 0xd7, 0x34, 0x1e, 0x5e, 0x97, 0x2f, 0xc6, 0x77, 0x28, 0x63,
255
        0x84, 0xf8, 0x02, 0xf8, 0xef, 0x42, 0xa5, 0xec, 0x5f, 0x03, 0xbb, 0xfa, 0x25, 0x4c, 0xb0,
256
        0x1f, 0xad,
257
    ]
258
);
259
260
gen_test!(
261
    ext_hashing_keccak_512_version_1,
262
    64,
263
    [
264
        0x3e, 0xe2, 0xb4, 0x00, 0x47, 0xb8, 0x06, 0x0f, 0x68, 0xc6, 0x72, 0x42, 0x17, 0x56, 0x60,
265
        0xf4, 0x17, 0x4d, 0x0a, 0xf5, 0xc0, 0x1d, 0x47, 0x16, 0x8e, 0xc2, 0x0e, 0xd6, 0x19, 0xb0,
266
        0xb7, 0xc4, 0x21, 0x81, 0xf4, 0x0a, 0xa1, 0x04, 0x6f, 0x39, 0xe2, 0xef, 0x9e, 0xfc, 0x69,
267
        0x10, 0x78, 0x2a, 0x99, 0x8e, 0x00, 0x13, 0xd1, 0x72, 0x45, 0x89, 0x57, 0x95, 0x7f, 0xac,
268
        0x94, 0x05, 0xb6, 0x7d
269
    ]
270
);
271
272
gen_test!(
273
    ext_hashing_sha2_256_version_1,
274
    32,
275
    [
276
        0xb9, 0x4d, 0x27, 0xb9, 0x93, 0x4d, 0x3e, 0x08, 0xa5, 0x2e, 0x52, 0xd7, 0xda, 0x7d, 0xab,
277
        0xfa, 0xc4, 0x84, 0xef, 0xe3, 0x7a, 0x53, 0x80, 0xee, 0x90, 0x88, 0xf7, 0xac, 0xe2, 0xef,
278
        0xcd, 0xe9
279
    ]
280
);
281
282
gen_test!(
283
    ext_hashing_blake2_256_version_1,
284
    32,
285
    [
286
        0x25, 0x6c, 0x83, 0xb2, 0x97, 0x11, 0x4d, 0x20, 0x1b, 0x30, 0x17, 0x9f, 0x3f, 0x0e, 0xf0,
287
        0xca, 0xce, 0x97, 0x83, 0x62, 0x2d, 0xa5, 0x97, 0x43, 0x26, 0xb4, 0x36, 0x17, 0x8a, 0xee,
288
        0xf6, 0x10
289
    ]
290
);
291
292
gen_test!(
293
    ext_hashing_twox_64_version_1,
294
    8,
295
    [104, 105, 30, 178, 52, 103, 171, 69]
296
);
297
298
gen_test!(
299
    ext_hashing_twox_128_version_1,
300
    16,
301
    [104, 105, 30, 178, 52, 103, 171, 69, 199, 183, 31, 36, 197, 3, 27, 176]
302
);
303
304
gen_test!(
305
    ext_hashing_twox_256_version_1,
306
    32,
307
    [
308
        104, 105, 30, 178, 52, 103, 171, 69, 199, 183, 31, 36, 197, 3, 27, 176, 98, 176, 136, 150,
309
        36, 146, 130, 146, 10, 183, 18, 206, 20, 156, 91, 136
310
    ]
311
);