@@ -7,6 +7,8 @@ use wasmer::{DeserializeError, Module, Store};
7
7
use crate :: checksum:: Checksum ;
8
8
use crate :: errors:: { VmError , VmResult } ;
9
9
10
+ use crate :: modules:: current_wasmer_module_version;
11
+
10
12
/// Bump this version whenever the module system changes in a way
11
13
/// that old stored modules would be corrupt when loaded in the new system.
12
14
/// This needs to be done e.g. when switching between the jit/native engine.
@@ -32,16 +34,12 @@ use crate::errors::{VmError, VmResult};
32
34
/// Version for Wasmer 2.2.0 which contains a [module breaking change to 2.1.x](https://github.com/wasmerio/wasmer/pull/2747).
33
35
const MODULE_SERIALIZATION_VERSION : & str = "v3" ;
34
36
35
- /// This header prefix contains the module type (wasmer-universal),
36
- /// the magic value WASMER\0\0 and a little endian encoded uint32 version number.
37
- /// The full header also contains a length that we do not check.
38
- const EXPECTED_MODULE_HEADER_PREFIX : & [ u8 ] = b"wasmer-universalWASMER\0 \0 \x01 \0 \0 \0 " ;
39
-
40
37
/// Representation of a directory that contains compiled Wasm artifacts.
41
38
pub struct FileSystemCache {
42
39
/// The base path this cache operates in. Within this path, versioned directories are created.
43
40
/// A sophisticated version of this cache might be able to read multiple input versions in the future.
44
41
base_path : PathBuf ,
42
+ wasmer_module_version : u32 ,
45
43
}
46
44
47
45
impl FileSystemCache {
@@ -53,16 +51,17 @@ impl FileSystemCache {
53
51
/// This method is unsafe because there's no way to ensure the artifacts
54
52
/// stored in this cache haven't been corrupted or tampered with.
55
53
pub unsafe fn new ( path : impl Into < PathBuf > ) -> io:: Result < Self > {
56
- if !current_wasmer_module_header ( ) . starts_with ( EXPECTED_MODULE_HEADER_PREFIX ) {
57
- panic ! ( "Wasmer module format changed. Please update the expected version accordingly and bump MODULE_SERIALIZATION_VERSION." ) ;
58
- }
54
+ let wasmer_module_version = current_wasmer_module_version ( ) ;
59
55
60
56
let path: PathBuf = path. into ( ) ;
61
57
if path. exists ( ) {
62
58
let metadata = path. metadata ( ) ?;
63
59
if metadata. is_dir ( ) {
64
60
if !metadata. permissions ( ) . readonly ( ) {
65
- Ok ( Self { base_path : path } )
61
+ Ok ( Self {
62
+ base_path : path,
63
+ wasmer_module_version,
64
+ } )
66
65
} else {
67
66
// This directory is readonly.
68
67
Err ( io:: Error :: new (
@@ -83,7 +82,10 @@ impl FileSystemCache {
83
82
} else {
84
83
// Create the directory and any parent directories if they don't yet exist.
85
84
fs:: create_dir_all ( & path) ?;
86
- Ok ( Self { base_path : path } )
85
+ Ok ( Self {
86
+ base_path : path,
87
+ wasmer_module_version,
88
+ } )
87
89
}
88
90
}
89
91
@@ -125,23 +127,14 @@ impl FileSystemCache {
125
127
126
128
/// The path to the latest version of the modules.
127
129
fn latest_modules_path ( & self ) -> PathBuf {
128
- self . base_path . join ( MODULE_SERIALIZATION_VERSION )
130
+ let version = format ! (
131
+ "{}-wasmer{}" ,
132
+ MODULE_SERIALIZATION_VERSION , self . wasmer_module_version
133
+ ) ;
134
+ self . base_path . join ( version)
129
135
}
130
136
}
131
137
132
- fn current_wasmer_module_header ( ) -> Vec < u8 > {
133
- use crate :: wasm_backend:: compile;
134
- // echo "(module)" > my.wat && wat2wasm my.wat && hexdump -C my.wasm
135
- const WASM : & [ u8 ] = b"\x00 \x61 \x73 \x6d \x01 \x00 \x00 \x00 " ;
136
- let module = compile ( WASM , None , & [ ] ) . unwrap ( ) ;
137
- let mut bytes = module. serialize ( ) . unwrap_or_default ( ) ;
138
-
139
- const ENGINE_TYPE_LEN : usize = 16 ; // https://github.com/wasmerio/wasmer/blob/2.2.0-rc1/lib/engine-universal/src/artifact.rs#L48
140
- const METADATA_HEADER_LEN : usize = 16 ; // https://github.com/wasmerio/wasmer/blob/2.2.0-rc1/lib/engine/src/artifact.rs#L251-L252
141
- bytes. truncate ( ENGINE_TYPE_LEN + METADATA_HEADER_LEN ) ;
142
- bytes
143
- }
144
-
145
138
#[ cfg( test) ]
146
139
mod tests {
147
140
use super :: * ;
@@ -211,13 +204,11 @@ mod tests {
211
204
let module = compile ( & wasm, None , & [ ] ) . unwrap ( ) ;
212
205
cache. store ( & checksum, & module) . unwrap ( ) ;
213
206
214
- let file_path = format ! ( "{}/v3/{}" , tmp_dir. path( ) . to_string_lossy( ) , checksum) ;
207
+ let file_path = format ! (
208
+ "{}/v3-wasmer1/{}" ,
209
+ tmp_dir. path( ) . to_string_lossy( ) ,
210
+ checksum
211
+ ) ;
215
212
let _serialized_module = fs:: read ( file_path) . unwrap ( ) ;
216
213
}
217
-
218
- #[ test]
219
- fn current_wasmer_module_header_works ( ) {
220
- let header = current_wasmer_module_header ( ) ;
221
- assert ! ( header. starts_with( EXPECTED_MODULE_HEADER_PREFIX ) ) ;
222
- }
223
214
}
0 commit comments