| 1 |
use { |
| 2 |
anyhow::{Context, Result}, |
| 3 |
wit_component::StringEncoding, |
| 4 |
wit_parser::Resolve, |
| 5 |
}; |
| 6 |
|
| 7 |
const FOO: &str = r#" |
| 8 |
(module |
| 9 |
(@dylink.0 |
| 10 |
(mem-info (memory 4 4)) |
| 11 |
(needed "libc.so") |
| 12 |
) |
| 13 |
(type (func)) |
| 14 |
(type (func (param i32) (result i32))) |
| 15 |
(import "env" "memory" (memory 1)) |
| 16 |
(import "env" "__indirect_function_table" (table 0 funcref)) |
| 17 |
(import "env" "__stack_pointer" (global $__stack_pointer (mut i32))) |
| 18 |
(import "env" "__memory_base" (global $__memory_base i32)) |
| 19 |
(import "env" "__table_base" (global $__table_base i32)) |
| 20 |
(import "env" "malloc" (func $malloc (type 1))) |
| 21 |
(import "env" "abort" (func $abort (type 0))) |
| 22 |
(import "GOT.mem" "um" (global $um (mut i32))) |
| 23 |
(import "test:test/test" "bar" (func $bar (type 1))) |
| 24 |
(func $__wasm_call_ctors (type 0)) |
| 25 |
(func $__wasm_apply_data_relocs (type 0)) |
| 26 |
(func $foo (type 1) (param i32) (result i32) |
| 27 |
global.get $__stack_pointer |
| 28 |
i32.const 16 |
| 29 |
i32.sub |
| 30 |
global.set $__stack_pointer |
| 31 |
|
| 32 |
i32.const 4 |
| 33 |
call $malloc |
| 34 |
|
| 35 |
i32.const 0 |
| 36 |
i32.eq |
| 37 |
if |
| 38 |
call $abort |
| 39 |
unreachable |
| 40 |
end |
| 41 |
|
| 42 |
local.get 0 |
| 43 |
global.get $um |
| 44 |
i32.load offset=16 |
| 45 |
i32.add |
| 46 |
i32.const 42 |
| 47 |
i32.add |
| 48 |
|
| 49 |
call $bar |
| 50 |
|
| 51 |
global.get $__stack_pointer |
| 52 |
i32.const 16 |
| 53 |
i32.add |
| 54 |
global.set $__stack_pointer |
| 55 |
) |
| 56 |
(global i32 i32.const 0) |
| 57 |
(export "__wasm_call_ctors" (func $__wasm_call_ctors)) |
| 58 |
(export "__wasm_apply_data_relocs" (func $__wasm_apply_data_relocs)) |
| 59 |
(export "foo" (func $foo)) |
| 60 |
(export "well" (global 4)) |
| 61 |
(data $.data (global.get $__memory_base) "\04\00\00\00") |
| 62 |
) |
| 63 |
"#; |
| 64 |
|
| 65 |
const BAR: &str = r#" |
| 66 |
(module |
| 67 |
(@dylink.0 |
| 68 |
(mem-info (memory 20 4)) |
| 69 |
(needed "libfoo.so") |
| 70 |
) |
| 71 |
(type (func (param i32) (result i32))) |
| 72 |
(type (func)) |
| 73 |
(import "env" "memory" (memory 1)) |
| 74 |
(import "env" "__indirect_function_table" (table 0 funcref)) |
| 75 |
(import "env" "__memory_base" (global $__memory_base i32)) |
| 76 |
(import "env" "__table_base" (global $__table_base i32)) |
| 77 |
(import "env" "foo" (func $foo (type 0))) |
| 78 |
(import "GOT.mem" "well" (global $well (mut i32))) |
| 79 |
(func $__wasm_call_ctors (type 1)) |
| 80 |
(func $__wasm_apply_data_relocs (type 1)) |
| 81 |
(func $bar (type 0) (param i32) (result i32) |
| 82 |
local.get 0 |
| 83 |
call $foo |
| 84 |
global.get $well |
| 85 |
i32.load |
| 86 |
i32.add |
| 87 |
) |
| 88 |
(global i32 i32.const 0) |
| 89 |
(export "__wasm_call_ctors" (func $__wasm_call_ctors)) |
| 90 |
(export "__wasm_apply_data_relocs" (func $__wasm_apply_data_relocs)) |
| 91 |
(export "test:test/test#bar" (func $bar)) |
| 92 |
(export "um" (global 3)) |
| 93 |
(data $.data (global.get $__memory_base) "\01\00\00\00\02\00\00\00\03\00\00\00\04\00\00\00\05\00\00\00") |
| 94 |
) |
| 95 |
"#; |
| 96 |
|
| 97 |
const LIBC: &str = r#" |
| 98 |
(module |
| 99 |
(@dylink.0) |
| 100 |
(type (func)) |
| 101 |
(type (func (param i32) (result i32))) |
| 102 |
(import "GOT.mem" "__heap_base" (global $__heap_base (mut i32))) |
| 103 |
(import "GOT.mem" "__heap_end" (global $__heap_end (mut i32))) |
| 104 |
(global $heap (mut i32) i32.const 0) |
| 105 |
(func $start (type 0) |
| 106 |
global.get $__heap_base |
| 107 |
global.set $heap |
| 108 |
) |
| 109 |
(func $malloc (type 1) (param i32) (result i32) |
| 110 |
global.get $heap |
| 111 |
global.get $heap |
| 112 |
local.get 0 |
| 113 |
i32.add |
| 114 |
global.set $heap |
| 115 |
) |
| 116 |
(func $abort (type 0) |
| 117 |
unreachable |
| 118 |
) |
| 119 |
(export "malloc" (func $malloc)) |
| 120 |
(export "abort" (func $abort)) |
| 121 |
(start $start) |
| 122 |
) |
| 123 |
"#; |
| 124 |
|
| 125 |
const WIT: &str = r#" |
| 126 |
package test:test; |
| 127 |
|
| 128 |
interface test { |
| 129 |
bar: func(v: s32) -> s32; |
| 130 |
} |
| 131 |
|
| 132 |
world bar { |
| 133 |
import test; |
| 134 |
export test; |
| 135 |
} |
| 136 |
"#; |
| 137 |
|
| 138 |
fn encode(wat: &str, wit: Option<&str>) -> Result<Vec<u8>> { |
| 139 |
let mut module = wat::parse_str(wat)?; |
| 140 |
|
| 141 |
if let Some(wit) = wit { |
| 142 |
let mut resolve = Resolve::default(); |
| 143 |
let pkg = resolve.push_str("test.wit", wit)?; |
| 144 |
let world = resolve.select_world(&[pkg], None)?; |
| 145 |
|
| 146 |
wit_component::embed_component_metadata( |
| 147 |
&mut module, |
| 148 |
&resolve, |
| 149 |
world, |
| 150 |
StringEncoding::UTF8, |
| 151 |
)?; |
| 152 |
} |
| 153 |
|
| 154 |
wasmparser::validate(&module)?; |
| 155 |
|
| 156 |
Ok(module) |
| 157 |
} |
| 158 |
|
| 159 |
#[test] |
| 160 |
fn linking() -> Result<()> { |
| 161 |
let component = [ |
| 162 |
("libfoo.so", FOO, None), |
| 163 |
("libbar.so", BAR, Some(WIT)), |
| 164 |
("libc.so", LIBC, None), |
| 165 |
] |
| 166 |
.into_iter() |
| 167 |
.try_fold( |
| 168 |
wit_component::Linker::default().validate(true), |
| 169 |
|linker, (name, wat, wit)| { |
| 170 |
linker.library( |
| 171 |
name, |
| 172 |
&encode(wat, wit).with_context(|| name.to_owned())?, |
| 173 |
false, |
| 174 |
) |
| 175 |
}, |
| 176 |
)? |
| 177 |
.encode()?; |
| 178 |
|
| 179 |
#[cfg(target_family = "wasm")] |
| 180 |
{ |
| 181 |
_ = component; |
| 182 |
} |
| 183 |
|
| •184 |
#[cfg(feature = "wasmtime")] |
| 185 |
{ |
| 186 |
use { |
| 187 |
anyhow::anyhow, |
| 188 |
wasmtime::{ |
| 189 |
Config, Engine, Store, |
| 190 |
component::{Component, Linker}, |
| 191 |
}, |
| 192 |
}; |
| 193 |
|
| 194 |
let mut config = Config::new(); |
| 195 |
config.wasm_component_model(true); |
| 196 |
|
| 197 |
let engine = Engine::new(&config)?; |
| 198 |
let mut linker = Linker::new(&engine); |
| 199 |
linker |
| 200 |
.instance("test:test/test")? |
| 201 |
.func_wrap("bar", |_store, (v,): (i32,)| Ok((v + 7,)))?; |
| 202 |
let mut store = Store::new(&engine, ()); |
| 203 |
let instance = linker.instantiate(&mut store, &Component::new(&engine, &component)?)?; |
| 204 |
let func = instance |
| 205 |
.get_export_index(&mut store, None, "test:test/test") |
| 206 |
.and_then(|i| instance.get_export_index(&mut store, Some(&i), "bar")) |
| 207 |
.and_then(|f| { |
| 208 |
instance |
| 209 |
.get_typed_func::<(i32,), (i32,)>(&mut store, &f) |
| 210 |
.ok() |
| 211 |
}) |
| 212 |
.ok_or_else(|| anyhow!("func `test:test/test/bar` not found"))?; |
| 213 |
|
| 214 |
assert_eq!(65, func.call(&mut store, (7,))?.0); |
| 215 |
} |
| 216 |
|
| 217 |
Ok(()) |
| 218 |
} |
| 219 |
|
| 220 |
const GOT_IMPORT: &str = r#" |
| 221 |
(module |
| 222 |
(@dylink.0 |
| 223 |
(mem-info) |
| 224 |
(needed "libc.so") |
| 225 |
(import-info "env" "foobar" binding-weak undefined) |
| 226 |
) |
| 227 |
(type (;0;) (func (result i32))) |
| 228 |
(import "env" "foobar" (func (;0;) (type 0))) |
| 229 |
(import "GOT.func" "foobar" (global (;0;) (mut i32))) |
| 230 |
(export "foo" (func 1)) |
| 231 |
(func (;1;) (type 0) (result i32) |
| 232 |
global.get 0 |
| 233 |
i32.eqz |
| 234 |
if ;; label = @1 |
| 235 |
i32.const 0 |
| 236 |
return |
| 237 |
end |
| 238 |
call 0 |
| 239 |
) |
| 240 |
) |
| 241 |
"#; |
| 242 |
|
| 243 |
const GOT_IMPORT_WIT: &str = r#" |
| 244 |
package test:test; |
| 245 |
|
| 246 |
world bar { |
| 247 |
export foo: func() -> u32; |
| 248 |
} |
| 249 |
"#; |
| 250 |
|
| 251 |
#[test] |
| •252 |
#[cfg(feature = "wasmtime")] |
| 253 |
fn linking_got_weak() -> Result<()> { |
| 254 |
let component = [ |
| 255 |
("libfoo.so", GOT_IMPORT, Some(GOT_IMPORT_WIT)), |
| 256 |
("libc.so", LIBC, None), |
| 257 |
] |
| 258 |
.into_iter() |
| 259 |
.try_fold( |
| 260 |
wit_component::Linker::default().validate(true), |
| 261 |
|linker, (name, wat, wit)| { |
| 262 |
linker.library( |
| 263 |
name, |
| 264 |
&encode(wat, wit).with_context(|| name.to_owned())?, |
| 265 |
false, |
| 266 |
) |
| 267 |
}, |
| 268 |
)? |
| 269 |
.encode()?; |
| 270 |
|
| 271 |
#[cfg(target_family = "wasm")] |
| 272 |
{ |
| 273 |
_ = component; |
| 274 |
} |
| 275 |
|
| 276 |
#[cfg(not(target_family = "wasm"))] |
| 277 |
{ |
| 278 |
use { |
| 279 |
anyhow::anyhow, |
| 280 |
wasmtime::{ |
| 281 |
Config, Engine, Store, |
| 282 |
component::{Component, Linker}, |
| 283 |
}, |
| 284 |
}; |
| 285 |
|
| 286 |
let config = Config::new(); |
| 287 |
let engine = Engine::new(&config)?; |
| 288 |
let mut linker = Linker::new(&engine); |
| 289 |
linker.instance("test:test/test")?; |
| 290 |
let mut store = Store::new(&engine, ()); |
| 291 |
let instance = linker.instantiate(&mut store, &Component::new(&engine, &component)?)?; |
| 292 |
let func = instance |
| 293 |
.get_export(&mut store, None, "foo") |
| 294 |
.and_then(|(_, f)| instance.get_typed_func::<(), (u32,)>(&mut store, &f).ok()) |
| 295 |
.ok_or_else(|| anyhow!("func `foo` not found"))?; |
| 296 |
|
| 297 |
assert_eq!(0, func.call(&mut store, ())?.0); |
| 298 |
} |
| 299 |
Ok(()) |
| 300 |
} |