ic-wasi-polyfill 0.8.0

The project provides polyfill implementation of *wasi_snapshot_preview1* functions using IC System API.
Documentation
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
use stable_fs::{
    error::Error,
    fs::{Fd, FileSystem},
    storage::types::{DUMMY_DOT_DOT_ENTRY_INDEX, DUMMY_DOT_ENTRY_INDEX, DirEntry, DirEntryIndex},
};

#[cfg(target_arch = "wasm32")]
use crate::wasi;
#[cfg(not(all(target_arch = "wasm32")))]
use crate::wasi_mock as wasi;

pub fn get_file_name<'a>(path: *const u8, path_len: wasi::Size) -> &'a str {
    let path_bytes = unsafe { std::slice::from_raw_parts(path, path_len as wasi::Size) };
    let file_name = unsafe { std::str::from_utf8_unchecked(path_bytes) };

    file_name
}

pub const DIRENT_SIZE: usize = std::mem::size_of::<wasi::Dirent>();

pub fn into_errno(error: Error) -> i32 {
    let errno = match error {
        stable_fs::error::Error::ArgumentListTooLong => wasi::ERRNO_2BIG,
        stable_fs::error::Error::PermissionDenied => wasi::ERRNO_ACCES,
        stable_fs::error::Error::AddressInUse => wasi::ERRNO_ADDRINUSE,
        stable_fs::error::Error::AddressNotAvailable => wasi::ERRNO_ADDRNOTAVAIL,
        stable_fs::error::Error::AddressFamilyNotSupported => wasi::ERRNO_AFNOSUPPORT,
        stable_fs::error::Error::ResourceUnavailableOrOperationWouldBlock => wasi::ERRNO_AGAIN,
        stable_fs::error::Error::ConnectionAlreadyInProgress => wasi::ERRNO_ALREADY,
        stable_fs::error::Error::BadFileDescriptor => wasi::ERRNO_BADF,
        stable_fs::error::Error::BadMessage => wasi::ERRNO_BADMSG,
        stable_fs::error::Error::DeviceOrResourceBusy => wasi::ERRNO_BUSY,
        stable_fs::error::Error::OperationCanceled => wasi::ERRNO_CANCELED,
        stable_fs::error::Error::NoChildProcesses => wasi::ERRNO_CHILD,
        stable_fs::error::Error::ConnectionAborted => wasi::ERRNO_CONNABORTED,
        stable_fs::error::Error::ConnectionRefused => wasi::ERRNO_CONNREFUSED,
        stable_fs::error::Error::ConnectionReset => wasi::ERRNO_CONNRESET,
        stable_fs::error::Error::ResourceDeadlockWouldOccur => wasi::ERRNO_DEADLK,
        stable_fs::error::Error::DestinationAddressRequired => wasi::ERRNO_DESTADDRREQ,
        stable_fs::error::Error::MathematicsArgumentOutOfDomainOfFunction => wasi::ERRNO_DOM,
        stable_fs::error::Error::Reserved19 => wasi::ERRNO_DQUOT,
        stable_fs::error::Error::FileExists => wasi::ERRNO_EXIST,
        stable_fs::error::Error::BadAddress => wasi::ERRNO_FAULT,
        stable_fs::error::Error::FileTooLarge => wasi::ERRNO_FBIG,
        stable_fs::error::Error::HostIsUnreachable => wasi::ERRNO_HOSTUNREACH,
        stable_fs::error::Error::IdentifierRemoved => wasi::ERRNO_IDRM,
        stable_fs::error::Error::IllegalByteSequence => wasi::ERRNO_ILSEQ,
        stable_fs::error::Error::OperationInProgress => wasi::ERRNO_INPROGRESS,
        stable_fs::error::Error::InterruptedFunction => wasi::ERRNO_INTR,
        stable_fs::error::Error::InvalidArgument => wasi::ERRNO_INVAL,
        stable_fs::error::Error::IOError => wasi::ERRNO_IO,
        stable_fs::error::Error::SocketIsConnected => wasi::ERRNO_ISCONN,
        stable_fs::error::Error::IsDirectory => wasi::ERRNO_ISDIR,
        stable_fs::error::Error::TooManyLevelsOfSymbolicLinks => wasi::ERRNO_LOOP,
        stable_fs::error::Error::FileDescriptorValueTooLarge => wasi::ERRNO_MFILE,
        stable_fs::error::Error::TooManyLinks => wasi::ERRNO_MLINK,
        stable_fs::error::Error::MessageTooLarge => wasi::ERRNO_MSGSIZE,
        stable_fs::error::Error::Reserved36 => wasi::ERRNO_MULTIHOP,
        stable_fs::error::Error::FilenameTooLong => wasi::ERRNO_NAMETOOLONG,
        stable_fs::error::Error::NetworkIsDown => wasi::ERRNO_NETDOWN,
        stable_fs::error::Error::ConnectionAbortedByNetwork => wasi::ERRNO_NETRESET,
        stable_fs::error::Error::NetworkUnreachable => wasi::ERRNO_NETUNREACH,
        stable_fs::error::Error::TooManyFilesOpenInSystem => wasi::ERRNO_NFILE,
        stable_fs::error::Error::NoBufferSpaceAvailable => wasi::ERRNO_NOBUFS,
        stable_fs::error::Error::NoSuchDevice => wasi::ERRNO_NODEV,
        stable_fs::error::Error::NoSuchFileOrDirectory => wasi::ERRNO_NOENT,
        stable_fs::error::Error::ExecutableFileFormatError => wasi::ERRNO_NOEXEC,
        stable_fs::error::Error::NoLocksAvailable => wasi::ERRNO_NOLCK,
        stable_fs::error::Error::Reserved47 => wasi::ERRNO_NOLINK,
        stable_fs::error::Error::NotEnoughSpace => wasi::ERRNO_NOMEM,
        stable_fs::error::Error::NoMessageOfTheDesiredType => wasi::ERRNO_NOMSG,
        stable_fs::error::Error::ProtocolNotAvailable => wasi::ERRNO_NOPROTOOPT,
        stable_fs::error::Error::NoSpaceLeftOnDevice => wasi::ERRNO_NOSPC,
        stable_fs::error::Error::FunctionNotSupported => wasi::ERRNO_NOSYS,
        stable_fs::error::Error::SocketNotConnected => wasi::ERRNO_NOTCONN,
        stable_fs::error::Error::NotADirectoryOrSymbolicLink => wasi::ERRNO_NOTDIR,
        stable_fs::error::Error::DirectoryNotEmpty => wasi::ERRNO_NOTEMPTY,
        stable_fs::error::Error::StateNotRecoverable => wasi::ERRNO_NOTRECOVERABLE,
        stable_fs::error::Error::NotASocket => wasi::ERRNO_NOTSOCK,
        stable_fs::error::Error::NotSupportedOrOperationNotSupportedOnSocket => wasi::ERRNO_NOTSUP,
        stable_fs::error::Error::InappropriateIOControlOperation => wasi::ERRNO_NOTTY,
        stable_fs::error::Error::NoSuchDeviceOrAddress => wasi::ERRNO_NXIO,
        stable_fs::error::Error::ValueTooLargeToBeStoredInDataType => wasi::ERRNO_OVERFLOW,
        stable_fs::error::Error::PreviousOwnerDied => wasi::ERRNO_OWNERDEAD,
        stable_fs::error::Error::OperationNotPermitted => wasi::ERRNO_PERM,
        stable_fs::error::Error::BrokenPipe => wasi::ERRNO_PIPE,
        stable_fs::error::Error::ProtocolError => wasi::ERRNO_PROTO,
        stable_fs::error::Error::ProtocolNotSupported => wasi::ERRNO_PROTONOSUPPORT,
        stable_fs::error::Error::ProtocolWrongTypeForSocket => wasi::ERRNO_PROTOTYPE,
        stable_fs::error::Error::ResultTooLarge => wasi::ERRNO_RANGE,
        stable_fs::error::Error::ReadOnlyFileSystem => wasi::ERRNO_ROFS,
        stable_fs::error::Error::InvalidSeek => wasi::ERRNO_SPIPE,
        stable_fs::error::Error::NoSuchProcess => wasi::ERRNO_SRCH,
        stable_fs::error::Error::Reserved72 => wasi::ERRNO_STALE,
        stable_fs::error::Error::ConnectionTimedOut => wasi::ERRNO_TIMEDOUT,
        stable_fs::error::Error::TextFileBusy => wasi::ERRNO_TXTBSY,
        stable_fs::error::Error::CrossDeviceLink => wasi::ERRNO_XDEV,
        stable_fs::error::Error::ExtensionCapabilitiesInsufficient => wasi::ERRNO_NOTCAPABLE,
    };

    errno.raw() as i32
}

pub fn into_wasi_filetype(file_type: stable_fs::storage::types::FileType) -> wasi::Filetype {
    match file_type {
        stable_fs::storage::types::FileType::Directory => wasi::FILETYPE_DIRECTORY,
        stable_fs::storage::types::FileType::RegularFile => wasi::FILETYPE_REGULAR_FILE,
        stable_fs::storage::types::FileType::SymbolicLink => wasi::FILETYPE_SYMBOLIC_LINK,
    }
}

pub fn _into_stable_fs_filetype(
    file_type: wasi::Filetype,
) -> Result<stable_fs::storage::types::FileType, stable_fs::error::Error> {
    match file_type {
        wasi::FILETYPE_DIRECTORY => Ok(stable_fs::storage::types::FileType::Directory),
        wasi::FILETYPE_REGULAR_FILE => Ok(stable_fs::storage::types::FileType::RegularFile),
        wasi::FILETYPE_SYMBOLIC_LINK => Ok(stable_fs::storage::types::FileType::SymbolicLink),
        _ => Err(stable_fs::error::Error::InvalidArgument),
    }
}

pub fn fd_readdir(
    fs: &FileSystem,
    fd: Fd,
    cookie: i64,
    bytes: *mut u8,
    bytes_len: i32,
    res: *mut wasi::Size,
) -> i32 {
    let fd = fd as Fd;
    let bytes_len = bytes_len as usize;
    let mut result = 0usize;

    let buf = unsafe { std::slice::from_raw_parts_mut(bytes, bytes_len) };

    let entry_index = if cookie == 0 {
        None
    } else {
        Some(cookie as DirEntryIndex)
    };

    match fs.get_direntries(fd, entry_index) {
        Ok(vec) => {
            for (idx, entry) in vec {
                let put_result = put_single_entry(fs, idx, entry, &mut buf[result..]);

                if let Err(err) = put_result {
                    return into_errno(err);
                }
                let put_result = put_result.unwrap();

                result += put_result;

                if result == bytes_len {
                    break;
                }
            }

            unsafe {
                *res = std::cmp::min(result, bytes_len);
            }
        }
        Err(err) => return into_errno(err),
    }

    wasi::ERRNO_SUCCESS.raw() as i32
}

pub fn put_single_entry(
    fs: &FileSystem,
    index: DirEntryIndex,
    dir_entry: DirEntry,
    buf: &mut [u8],
) -> Result<usize, Error> {
    // we assume the next index can always be current index + 1, hence no need to rely on .._next index field
    // appart from the dummy "DOT" entries
    let mut next_index = index + 1;
    if index == DUMMY_DOT_DOT_ENTRY_INDEX {
        next_index = 1; // the first non-zero index in the folder
    }
    if index == DUMMY_DOT_ENTRY_INDEX {
        next_index = DUMMY_DOT_DOT_ENTRY_INDEX;
    }

    // TODO: store file type directly inside DirEntry
    let file_type = fs.metadata_from_node(dir_entry.node)?.file_type;

    let wasi_dirent = wasi::Dirent {
        d_next: next_index as u64,
        d_ino: dir_entry.node,
        d_namlen: (dir_entry.name.length as wasi::Dirnamlen),
        d_type: into_wasi_filetype(file_type),
    };

    let result = fill_buffer(wasi_dirent, buf, &dir_entry.name);

    Ok(result)
}

fn fill_buffer(
    wasi_dirent: wasi::Dirent,
    buf: &mut [u8],
    filename: &stable_fs::storage::types::FileName,
) -> usize {
    use std::slice;

    let p: *const wasi::Dirent = &wasi_dirent;
    let p: *const u8 = p as *const u8;

    let s: &[u8] = unsafe { slice::from_raw_parts(p, DIRENT_SIZE) };

    let result = usize::min(s.len(), buf.len());
    buf[0..result].copy_from_slice(&s[0..result]);

    // pre-fill 0 to avoid random data in buf
    if buf.len() >= DIRENT_SIZE {
        buf[DIRENT_SIZE - 3..DIRENT_SIZE].copy_from_slice(&[0, 0, 0]);
    }

    let buf_len = buf.len();
    let buf = &mut buf[result..buf_len];

    let filename = &filename.bytes[0..filename.length as usize];

    let result2 = usize::min(filename.len(), buf.len());
    buf[0..result2].copy_from_slice(&filename[0..result2]);
    result + result2
}

pub fn into_stable_fs_wence(whence: u8) -> stable_fs::fs::Whence {
    if whence == wasi::WHENCE_SET.raw() {
        return stable_fs::fs::Whence::SET;
    }

    if whence == wasi::WHENCE_CUR.raw() {
        return stable_fs::fs::Whence::CUR;
    }

    if whence == wasi::WHENCE_END.raw() {
        return stable_fs::fs::Whence::END;
    }

    panic!("Unsupported whence type!");
}

#[cfg(test)]
mod tests {
    use crate::{DIRENT_SIZE, wasi, wasi_helpers::put_single_entry};
    use ic_stable_structures::DefaultMemoryImpl;
    use stable_fs::{
        fs::{FdStat, FileSystem, OpenFlags},
        storage::{
            stable::StableStorage,
            transient::TransientStorage,
            types::{DirEntry, DirEntryIndex, FileName, Node},
        },
    };

    use super::{fd_readdir, fill_buffer};

    fn test_fs() -> FileSystem {
        FileSystem::new(Box::new(StableStorage::new(DefaultMemoryImpl::default()))).unwrap()
    }

    fn transient_fs() -> FileSystem {
        FileSystem::new(Box::new(TransientStorage::new())).unwrap()
    }

    #[test]
    fn test_fill_buffer_normal_and_trimmed() {
        let direntry = DirEntry {
            name: FileName::new("test.txt".as_bytes()).unwrap(),
            node: 45 as Node,
            next_entry: None,
            prev_entry: None,
        };

        let wasi_dirent = wasi::Dirent {
            d_next: 123 as wasi::Dircookie,
            d_ino: 234 as wasi::Inode,
            d_namlen: direntry.name.length as wasi::Dirnamlen,
            d_type: wasi::FILETYPE_REGULAR_FILE,
        };

        let expected = [
            123, 0, 0, 0, 0, 0, 0, 0, 234, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 4, 243, 243, 243, 116,
            101, 115, 116, 46, 116, 120, 116,
        ];

        let mut buf = [0u8; 100];
        let len = fill_buffer(wasi_dirent, &mut buf, &direntry.name);

        // stabilize test, the three bytes can take random value here...
        buf[DIRENT_SIZE - 3] = 243;
        buf[DIRENT_SIZE - 2] = 243;
        buf[DIRENT_SIZE - 1] = 243;

        assert_eq!(&expected[0..len], &buf[0..len]);
        assert_eq!(len, expected.len());

        let mut buf = [0u8; 27];
        let len = fill_buffer(wasi_dirent, &mut buf, &direntry.name);
        // stabilize test, the three bytes can take random value here...
        buf[DIRENT_SIZE - 3] = 243;
        buf[DIRENT_SIZE - 2] = 243;
        buf[DIRENT_SIZE - 1] = 243;

        assert_eq!(&expected[0..len], &buf[0..len]);
        assert_eq!(len, buf.len());

        let mut buf = [0u8; 3];
        let len = fill_buffer(wasi_dirent, &mut buf, &direntry.name);

        assert_eq!(&expected[0..len], &buf[0..len]);
        assert_eq!(len, buf.len());
    }

    #[test]
    fn test_put_single_entry() {
        let mut fs = test_fs();

        let dir_fd = fs.root_fd();

        let _fd1 = fs
            .open(dir_fd, "test.txt", FdStat::default(), OpenFlags::CREATE, 0)
            .unwrap();
        let _fd2 = fs
            .open(dir_fd, "test2.txt", FdStat::default(), OpenFlags::CREATE, 0)
            .unwrap();
        let _fd3 = fs
            .open(dir_fd, "test3.txt", FdStat::default(), OpenFlags::CREATE, 0)
            .unwrap();
        let _fd4 = fs
            .open(dir_fd, "test4.txt", FdStat::default(), OpenFlags::CREATE, 0)
            .unwrap();

        let meta = fs.metadata(dir_fd);

        let first_entry = meta.unwrap().first_dir_entry.unwrap();

        let expected = [
            2, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 4, 0, 0, 0, 116, 101, 115,
            116, 46, 116, 120, 116,
        ];

        let mut buf = [0u8; 100];

        let dir_entry = fs.get_direntry(dir_fd, first_entry).unwrap();

        let len = put_single_entry(&fs, first_entry as DirEntryIndex, dir_entry, &mut buf).unwrap();

        assert_eq!(&expected[0..len], &buf[0..len]);
        assert_eq!(len, expected.len());
    }

    #[test]
    fn test_fd_readdir() {
        for mut fs in [test_fs(), transient_fs()] {
            let dir_fd = fs.root_fd();

            let _fd1 = fs
                .open(dir_fd, "test.txt", FdStat::default(), OpenFlags::CREATE, 0)
                .unwrap();

            let _fd2 = fs
                .open(dir_fd, "test2.txt", FdStat::default(), OpenFlags::CREATE, 0)
                .unwrap();
            let _fd3 = fs
                .open(dir_fd, "test3.txt", FdStat::default(), OpenFlags::CREATE, 0)
                .unwrap();
            let _fd4 = fs
                .open(dir_fd, "test4.txt", FdStat::default(), OpenFlags::CREATE, 0)
                .unwrap();

            let mut buf = [0u8; 200];

            let p = &mut buf as *mut u8;

            let mut bytes_used: wasi::Size = 0usize;

            // read folder with special files
            let result = fd_readdir(
                &fs,
                fs.root_fd(),
                0,
                p,
                buf.len() as i32,
                &mut bytes_used as *mut wasi::Size,
            );

            assert_eq!(result, 0);

            // 6 file entries + 1 + 2 + corresponding file sizes
            let expected_bytes = DIRENT_SIZE * 6 + 1 + 2 + 8 + 9 + 9 + 9;
            assert_eq!(bytes_used, expected_bytes);

            // read files starting with test2.txt (it has entry index 2)
            let result = fd_readdir(
                &fs,
                fs.root_fd(),
                2,
                p,
                buf.len() as i32,
                &mut bytes_used as *mut wasi::Size,
            );

            assert_eq!(result, 0);

            // 3 file entries + corresponding file sizes
            let expected_bytes = (DIRENT_SIZE + 9) * 3;
            assert_eq!(bytes_used, expected_bytes);
        }
    }
}