Stream: wasi

Topic: trouble implementing fd_readdir


view this post on Zulip bjorn3 (Aug 01 2020 at 14:45):

I currently have the following. For some reason the wasi program keeps running fd_readdir again with bigger and bigger buffers, but with the same 0 cookie. What am I doing wrong?

buffer32[bufused / 4] = 0;

let cookie = Number(big_cookie);
console.log(cookie, Object.keys(fds[fd].directory).slice(cookie));
if (buf_len > 5276240) throw "too big buf"; // FIXME
if (cookie >= Object.keys(fds[fd].directory).length) {
    console.log("end of dir");
    return -1;
}
let next_cookie = cookie + 1;
for (let name of Object.keys(fds[fd].directory).slice(cookie)) {
    let entry = fds[fd].directory[name];
    console.log(name, entry);
    let encoded_name = new TextEncoder("utf-8").encode(name + "\0");

    let offset = 16 + encoded_name.length;

    if ((buf_len - buffer32[bufused / 4]) < offset) {
        console.log("too small buf");
        break;
    } else {
        console.log(next_cookie, buf);
        write_le_u32(buffer8, buf, next_cookie);
        next_cookie += 1;
        write_le_u32(buffer8, buf + 4, 0); // inode
        write_le_u32(buffer8, buf + 8, encoded_name.length); // name
        write_le_u32(buffer8, buf + 12, entry.file_type);
        buffer8.set(encoded_name, buf + 16);
        console.log(buffer8[buf], buffer8.slice(buf, buf + offset));
        buf += offset;
        buffer32[bufused / 4] += offset;
    }
}

view this post on Zulip bjorn3 (Aug 01 2020 at 14:57):

I think I found the problem: cookie is 64bit, not 32bit

view this post on Zulip bjorn3 (Aug 01 2020 at 15:10):

It doesn't get stuck in an infinite loop anymore, but it still doesn't show any files:

buffer32[bufused / 4] = 0;

console.log(cookie, Object.keys(fds[fd].directory).slice(Number(cookie)));
if (cookie >= BigInt(Object.keys(fds[fd].directory).length)) {
    console.log("end of dir");
    return 0;
}
let next_cookie = cookie + 1n;
for (let name of Object.keys(fds[fd].directory).slice(Number(cookie))) {
    let entry = fds[fd].directory[name];
    console.log(name, entry);
    let encoded_name = new TextEncoder("utf-8").encode(name);

    let offset = 24 + encoded_name.length;

    if ((buf_len - buffer32[bufused / 4]) < offset) {
        console.log("too small buf");
        break;
    } else {
        console.log("next_cookie =", next_cookie, buf);
        write_le_u64(buffer8, buf, next_cookie);
        next_cookie += 1n;
        write_le_u64(buffer8, buf + 8, 1n); // inode
        write_le_u32(buffer8, buf + 16, encoded_name.length); // name
        buffer8[buf + 20] = entry.file_type;
        buffer8.set(encoded_name, buf + 24);
        console.log("buffer =", buffer8.slice(buf, buf + offset));
        buf += offset;
        buffer32[bufused / 4] += offset;
        console.log();
    }
}
console.log("used =", buffer32[bufused / 4]);
return 0;

view this post on Zulip bjorn3 (Aug 01 2020 at 15:12):

This is the test program: echo 'fn main() { for item in std::fs::read_dir("/sysroot/lib/rustlib/x86_64-unknown-linux-gnu/lib/").unwrap() { println!("{:?}", item); } }' | rustc --target wasm32-wasi -

view this post on Zulip bjorn3 (Aug 01 2020 at 15:16):

Never mind. I was overwriting the program output when the program completed successfully.

view this post on Zulip bjorn3 (Aug 01 2020 at 19:43):

I got rustc in the browser working: https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/rustc.20on.20wasm/near/205695027

One of the chat platforms for the Rust Programming Language.

Last updated: Oct 23 2024 at 20:03 UTC