Repository: tetratelabs/wazero (go)

Issue:
Pass automated tests for zig stdlib
**Is your feature request related to a problem? Please describe.**
One of our goals of 1.0 is to reduce the amount of glitches using syscall ABIs, notably WASI. While there is an emerging [wasi-testsuite](https://github.com/WebAssembly/wasi-testsuite), and we have a number of ad-hoc tests, we should also pass zig tests as an integration suite.

**Describe the solution you'd like**

#1029 runs automated tests, but we don't yet pass them

from zig clone
```bash
$ zig test --test-cmd wazero --test-cmd run --test-cmd -mount=.:/ --test-cmd-bin -target wasm32-wasi --zig-lib-dir ./lib ./lib/std/std.zig
```

The actual mounts may need to vary once above actually passes. e.g. mounting tmp for writes

**Describe alternatives you've considered**

We currently do this manually and backfill notable gaps as unit tests, but the logic zig uses changes from time to time.

**Additional context**

As of Jan 13, this doesn't proceed as we've not fully implemented wasi syscalls needed, nor have the correct flags to proceed. e.g. /tmp
```
$ zig test --test-cmd wazero --test-cmd run --test-cmd -hostlogging=filesystem --test-cmd -mount=.:/ --test-cmd-bin -target wasm32-wasi --zig-lib-dir ./lib ./lib/std/std.zig
unexpected errno: 21
Unable to dump stack trace: not implemented for Wasm
unexpected errno: 21
Unable to dump stack trace: not implemented for Wasm
expected error.EnvironmentVariableNotFound, found error.OutOfMemory
==> wasi_snapshot_preview1.path_create_directory(fd=3,path=zig-cache)
<== errno=EEXIST
==> wasi_snapshot_preview1.path_open(fd=3,dirflags=SYMLINK_FOLLOW,path=zig-cache,oflags=DIRECTORY,fs_rights_base=FDSTAT_SET_FLAGS|PATH_CREATE_DIRECTORY|PATH_CREATE_FILE|PATH_LINK_SOURCE|PATH_LINK_TARGET|PATH_OPEN|FD_READDIR|PATH_READLINK|PATH_RENAME_SOURCE|PATH_RENAME_TARGET|PATH_FILESTAT_GET|PATH_FILESTAT_SET_SIZE|PATH_FILESTAT_SET_TIMES|FD_FILESTAT_GET|FD_FILESTAT_SET_TIMES|PATH_SYMLINK|PATH_REMOVE_DIRECTORY|PATH_UNLINK_FILE,fs_rights_inheriting=FD_DATASYNC|FD_READ|FD_SEEK|FDSTAT_SET_FLAGS|FD_SYNC|FD_TELL|FD_WRITE|FD_ADVISE|FD_ALLOCATE|PATH_CREATE_DIRECTORY|PATH_CREATE_FILE|PATH_LINK_SOURCE|PATH_LINK_TARGET|PATH_OPEN|FD_READDIR|PATH_READLINK|PATH_RENAME_SOURCE|PATH_RENAME_TARGET|PATH_FILESTAT_GET|PATH_FILESTAT_SET_SIZE|PATH_FILESTAT_SET_TIMES|FD_FILESTAT_GET|FD_FILESTAT_SET_SIZE|FD_FILESTAT_SET_TIMES|PATH_SYMLINK|PATH_REMOVE_DIRECTORY|PATH_UNLINK_FILE|POLL_FD_READWRITE,fdflags=)
<== (opened_fd=4,errno=ESUCCESS)
==> wasi_snapshot_preview1.path_create_directory(fd=4,path=tmp)
<== errno=EEXIST
--snip--
```


Pull request:
wasi: implements link related system calls
This fixes #1029

Patch:
diff --git a/.github/workflows/integration.yaml b/.github/workflows/integration.yaml
index b4a497e0..5ad76976 100644
--- a/.github/workflows/integration.yaml
+++ b/.github/workflows/integration.yaml
@@ -108,8 +108,6 @@ jobs:
 
       - name: Run the test binary with wazero CLI
         run: go run ./cmd/wazero run -mount=:/ test.wasm
-        # TODO: remove when #1029 is complete
-        continue-on-error: true
 
   tinygo:
     name: TinyGo
diff --git a/imports/wasi_snapshot_preview1/fs.go b/imports/wasi_snapshot_preview1/fs.go
index 1d628df1..eea74c39 100644
--- a/imports/wasi_snapshot_preview1/fs.go
+++ b/imports/wasi_snapshot_preview1/fs.go
@@ -6,8 +6,11 @@ import (
 	"io"
 	"io/fs"
 	"math"
+	"os"
 	pathutil "path"
+	"reflect"
 	"syscall"
+	"unsafe"
 
 	"github.com/tetratelabs/wazero/api"
 	"github.com/tetratelabs/wazero/internal/platform"
@@ -244,7 +247,9 @@ func fdFilestatGetFunc(mod api.Module, fd, resultBuf uint32) Errno {
 		return ToErrno(err)
 	}
 
-	writeFilestat(buf, stat)
+	if err = writeFilestat(buf, f.File, stat); err != nil {
+		return ToErrno(err)
+	}
 
 	return ErrnoSuccess
 }
@@ -265,20 +270,24 @@ func getWasiFiletype(fileMode fs.FileMode) uint8 {
 	return wasiFileType
 }
 
-func writeFilestat(buf []byte, stat fs.FileInfo) {
+func writeFilestat(buf []byte, f fs.File, stat fs.FileInfo) (err error) {
 	device, inode := platform.StatDeviceInode(stat)
 	filetype := getWasiFiletype(stat.Mode())
 	filesize := uint64(stat.Size())
-	atimeNsec, mtimeNsec, ctimeNsec := platform.StatTimes(stat)
+	atimeNsec, mtimeNsec, ctimeNsec, nlink, err := platform.Stat(f, stat)
+	if err != nil {
+		return err
+	}
 
 	le.PutUint64(buf, device)
 	le.PutUint64(buf[8:], inode)
 	le.PutUint64(buf[16:], uint64(filetype))
-	le.PutUint64(buf[24:], 1) // nlink
+	le.PutUint64(buf[24:], nlink)
 	le.PutUint64(buf[32:], filesize)
 	le.PutUint64(buf[40:], uint64(atimeNsec))
 	le.PutUint64(buf[48:], uint64(mtimeNsec))
 	le.PutUint64(buf[56:], uint64(ctimeNsec))
+	return
 }
 
 // fdFilestatSetSize is the WASI function named FdFilestatSetSizeName which
@@ -1287,21 +1296,26 @@ func pathFilestatGetFn(_ context.Context, mod api.Module, params []uint64) Errno
 		return errno
 	}
 
-	resultBuf := uint32(params[4])
-
 	// Stat the file without allocating a file descriptor
-	stat, err := sysfs.StatPath(preopen, pathName)
+	f, err := preopen.OpenFile(pathName, os.O_RDONLY, 0)
+	if err != nil {
+		return ToErrno(err)
+	}
+	stat, err := f.Stat()
 	if err != nil {
 		return ToErrno(err)
 	}
 
 	// Write the stat result to memory
+	resultBuf := uint32(params[4])
 	buf, ok := mod.Memory().Read(resultBuf, 64)
 	if !ok {
 		return ErrnoFault
 	}
-	writeFilestat(buf, stat)
 
+	if err = writeFilestat(buf, f, stat); err != nil {
+		return ToErrno(err)
+	}
 	return ErrnoSuccess
 }
 
@@ -1319,12 +1333,46 @@ var pathFilestatSetTimes = stubFunction(
 // timestamps of a file or directory.
 //
 // See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#path_link
-var pathLink = stubFunction(
-	PathLinkName,
+var pathLink = newHostFunc(
+	PathLinkName, pathLinkFn,
 	[]wasm.ValueType{i32, i32, i32, i32, i32, i32, i32},
 	"old_fd", "old_flags", "old_path", "old_path_len", "new_fd", "new_path", "new_path_len",
 )
 
+func pathLinkFn(_ context.Context, mod api.Module, params []uint64) Errno {
+	mem := mod.Memory()
+	fsc := mod.(*wasm.CallContext).Sys.FS()
+
+	oldFd := uint32(params[0])
+	// TODO: use old_flags?
+	_ = uint32(params[1])
+	oldPath := uint32(params[2])
+	oldPathLen := uint32(params[3])
+
+	oldFS, oldName, errno := atPath(fsc, mem, oldFd, oldPath, oldPathLen)
+	if errno != ErrnoSuccess {
+		return errno
+	}
+
+	newFd := uint32(params[4])
+	newPath := uint32(params[5])
+	newPathLen := uint32(params[6])
+
+	newFS, newName, errno := atPath(fsc, mem, newFd, newPath, newPathLen)
+	if errno != ErrnoSuccess {
+		return errno
+	}
+
+	if oldFS != newFS { // TODO: handle link across filesystems
+		return ErrnoNosys
+	}
+
+	if err := oldFS.Link(oldName, newName); err != nil {
+		return ToErrno(err)
+	}
+	return ErrnoSuccess
+}
+
 // pathOpen is the WASI function named PathOpenName which opens a file or
 // directory. This returns ErrnoBadf if the fd is invalid.
 //
@@ -1508,12 +1556,48 @@ func openFlags(oflags, fdflags uint16) (openFlags int, isDir bool) {
 // contents of a symbolic link.
 //
 // See: https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#-path_readlinkfd-fd-path-string-buf-pointeru8-buf_len-size---errno-size
-var pathReadlink = stubFunction(
-	PathReadlinkName,
+var pathReadlink = newHostFunc(
+	PathReadlinkName, pathReadlinkFn,
 	[]wasm.ValueType{i32, i32, i32, i32, i32, i32},
 	"fd", "path", "path_len", "buf", "buf_len", "result.bufused",
 )
 
+func pathReadlinkFn(_ context.Context, mod api.Module, params []uint64) Errno {
+	fsc := mod.(*wasm.CallContext).Sys.FS()
+
+	fd := uint32(params[0])
+	path := uint32(params[1])
+	pathLen := uint32(params[2])
+	bufPtr := uint32(params[3])
+	bufLen := uint32(params[4])
+	resultBufUsedPtr := uint32(params[5])
+
+	if pathLen == 0 || bufLen == 0 {
+		return ErrnoInval
+	}
+
+	mem := mod.Memory()
+	preopen, p, en := atPath(fsc, mem, fd, path, pathLen)
+	if en != ErrnoSuccess {
+		return en
+	}
+
+	buf, ok := mem.Read(bufPtr, bufLen)
+	if !ok {
+		return ErrnoFault
+	}
+
+	n, err := preopen.Readlink(p, buf)
+	if err != nil {
+		return ToErrno(err)
+	}
+
+	if !mem.WriteUint32Le(resultBufUsedPtr, uint32(n)) {
+		return ErrnoFault
+	}
+	return ErrnoSuccess
+}
+
 // pathRemoveDirectory is the WASI function named PathRemoveDirectoryName which
 // removes a directory.
 //
@@ -1628,12 +1712,64 @@ func pathRenameFn(_ context.Context, mod api.Module, params []uint64) Errno {
 // symbolic link.
 //
 // See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#path_symlink
-var pathSymlink = stubFunction(
-	PathSymlinkName,
+var pathSymlink = newHostFunc(
+	PathSymlinkName, pathSymlinkFn,
 	[]wasm.ValueType{i32, i32, i32, i32, i32},
 	"old_path", "old_path_len", "fd", "new_path", "new_path_len",
 )
 
+func pathSymlinkFn(_ context.Context, mod api.Module, params []uint64) Errno {
+	fsc := mod.(*wasm.CallContext).Sys.FS()
+
+	oldPath := uint32(params[0])
+	oldPathLen := uint32(params[1])
+	dirFD := uint32(params[2])
+	newPath := uint32(params[3])
+	newPathLen := uint32(params[4])
+
+	mem := mod.Memory()
+
+	dir, ok := fsc.LookupFile(dirFD)
+	if !ok {
+		return ErrnoBadf // closed
+	} else if !dir.IsDir() {
+		return ErrnoNotdir
+	}
+
+	if oldPathLen == 0 || newPathLen == 0 {
+		return ErrnoInval
+	}
+
+	oldPathBuf, ok := mem.Read(oldPath, oldPathLen)
+	if !ok {
+		return ErrnoFault
+	}
+
+	newPathBuf, ok := mem.Read(newPath, newPathLen)
+	if !ok {
+		return ErrnoFault
+	}
+
+	if err := dir.FS.Symlink(
+		// Do not join old path since it's only resolved when dereference the link created here.
+		// And the dereference result depends on the opening directory's file descriptor at that point.
+		bufToStr(oldPathBuf, int(oldPathLen)),
+		pathutil.Join(dir.Name, bufToStr(newPathBuf, int(newPathLen))),
+	); err != nil {
+		return ToErrno(err)
+	}
+	return ErrnoSuccess
+}
+
+// bufToStr converts the given byte slice as string unsafely.
+func bufToStr(buf []byte, l int) string {
+	return *(*string)(unsafe.Pointer(&reflect.SliceHeader{ //nolint
+		Data: uintptr(unsafe.Pointer(&buf[0])),
+		Len:  l,
+		Cap:  l,
+	}))
+}
+
 // pathUnlinkFile is the WASI function named PathUnlinkFileName which unlinks a
 // file.
 //
diff --git a/internal/platform/open_file_windows.go b/internal/platform/open_file_windows.go
index 33763ae1..43189d29 100644
--- a/internal/platform/open_file_windows.go
+++ b/internal/platform/open_file_windows.go
@@ -8,17 +8,17 @@ import (
 )
 
 func OpenFile(name string, flag int, perm fs.FileMode) (*os.File, error) {
-	fd, err := windowsOpenFile(name, flag|syscall.O_CLOEXEC, uint32(perm))
+	fd, err := open(name, flag|syscall.O_CLOEXEC, uint32(perm))
 	if err == nil {
 		return os.NewFile(uintptr(fd), name), nil
 	}
-
 	// TODO: Set FILE_SHARE_DELETE for directory as well.
 	return os.OpenFile(name, flag, perm)
 }
 
-// lifted from ./go/src/syscall/syscall_windows.go. Modified to add FILE_SHARE_DELETE
-func windowsOpenFile(path string, mode int, perm uint32) (fd syscall.Handle, err error) {
+// The following is lifted from syscall_windows.go to add support for setting FILE_SHARE_DELETE.
+// https://github.com/golang/go/blob/go1.19.5/src/syscall/syscall_windows.go#L307-L375
+func open(path string, mode int, perm uint32) (fd syscall.Handle, err error) {
 	if len(path) == 0 {
 		return syscall.InvalidHandle, syscall.ERROR_FILE_NOT_FOUND
 	}
@@ -42,8 +42,7 @@ func windowsOpenFile(path string, mode int, perm uint32) (fd syscall.Handle, err
 		access &^= syscall.GENERIC_WRITE
 		access |= syscall.FILE_APPEND_DATA
 	}
-	sharemode := uint32(syscall.FILE_SHARE_READ | syscall.FILE_SHARE_WRITE |
-		syscall.FILE_SHARE_DELETE)
+	sharemode := uint32(syscall.FILE_SHARE_READ | syscall.FILE_SHARE_WRITE | syscall.FILE_SHARE_DELETE)
 	var sa *syscall.SecurityAttributes
 	if mode&syscall.O_CLOEXEC == 0 {
 		var _sa syscall.SecurityAttributes
@@ -64,7 +63,30 @@ func windowsOpenFile(path string, mode int, perm uint32) (fd syscall.Handle, err
 	default:
 		createmode = syscall.OPEN_EXISTING
 	}
-	h, e := syscall.CreateFile(
-		pathp, access, sharemode, sa, createmode, syscall.FILE_ATTRIBUTE_NORMAL, 0)
+	var attrs uint32 = syscall.FILE_ATTRIBUTE_NORMAL
+	if perm&syscall.S_IWRITE == 0 {
+		attrs = syscall.FILE_ATTRIBUTE_READONLY
+		if createmode == syscall.CREATE_ALWAYS {
+			// We have been asked to create a read-only file.
+			// If the file already exists, the semantics of
+			// the Unix open system call is to preserve the
+			// existing permissions. If we pass CREATE_ALWAYS
+			// and FILE_ATTRIBUTE_READONLY to CreateFile,
+			// and the file already exists, CreateFile will
+			// change the file permissions.
+			// Avoid that to preserve the Unix semantics.
+			h, e := syscall.CreateFile(pathp, access, sharemode, sa, syscall.TRUNCATE_EXISTING, syscall.FILE_ATTRIBUTE_NORMAL, 0)
+			switch e {
+			case syscall.ERROR_FILE_NOT_FOUND, syscall.ERROR_PATH_NOT_FOUND:
+				// File does not exist. These are the same
+				// errors as Errno.Is checks for ErrNotExist.
+				// Carry on to create the file.
+			default:
+				// Success or some different error.
+				return h, e
+			}
+		}
+	}
+	h, e := syscall.CreateFile(pathp, access, sharemode, sa, createmode, attrs, 0)
 	return h, e
 }
diff --git a/internal/platform/separator.go b/internal/platform/separator.go
new file mode 100644
index 00000000..a98328ef
--- /dev/null
+++ b/internal/platform/separator.go
@@ -0,0 +1,7 @@
+//go:build !windows
+
+package platform
+
+// SanitizeSeparator sanitizes the path separator in the given buffer.
+// This does nothing on the non-windows platforms.
+func SanitizeSeparator([]byte) {}
diff --git a/internal/platform/separator_windows.go b/internal/platform/separator_windows.go
new file mode 100644
index 00000000..afe1cfe6
--- /dev/null
+++ b/internal/platform/separator_windows.go
@@ -0,0 +1,10 @@
+package platform
+
+// SanitizeSeparator sanitizes the path separator in the given buffer.
+func SanitizeSeparator(in []byte) {
+	for i := range in {
+		if in[i] == '\\' {
+			in[i] = '/'
+		}
+	}
+}
diff --git a/internal/platform/stat.go b/internal/platform/stat.go
index eee7f686..dbdd60d9 100644
--- a/internal/platform/stat.go
+++ b/internal/platform/stat.go
@@ -1,16 +1,30 @@
 package platform
 
-import "os"
+import (
+	"io/fs"
+	"os"
+)
 
 // StatTimes returns platform-specific values if os.FileInfo Sys is available.
 // Otherwise, it returns the mod time for all values.
 func StatTimes(t os.FileInfo) (atimeNsec, mtimeNsec, ctimeNsec int64) {
 	if t.Sys() == nil { // possibly fake filesystem
-		return mtimes(t)
+		atimeNsec, mtimeNsec, ctimeNsec = mtimes(t)
+		return
 	}
 	return statTimes(t)
 }
 
+// Stat returns platform-specific values if os.FileInfo Sys is available.
+func Stat(f fs.File, t os.FileInfo) (atimeNsec, mtimeNsec, ctimeNsec int64, nlink uint64, err error) {
+	if t.Sys() == nil { // possibly fake filesystem
+		atimeNsec, mtimeNsec, ctimeNsec = mtimes(t)
+		nlink = 1
+		return
+	}
+	return stat(f, t)
+}
+
 // StatDeviceInode returns platform-specific values if os.FileInfo Sys is
 // available. Otherwise, it returns zero which makes file identity comparison
 // unsupported.
diff --git a/internal/platform/stat_bsd.go b/internal/platform/stat_bsd.go
index 7d5fae75..ded99682 100644
--- a/internal/platform/stat_bsd.go
+++ b/internal/platform/stat_bsd.go
@@ -3,6 +3,7 @@
 package platform
 
 import (
+	"io/fs"
 	"os"
 	"syscall"
 )
@@ -15,6 +16,14 @@ func statTimes(t os.FileInfo) (atimeNsec, mtimeNsec, ctimeNsec int64) {
 	return atime.Sec*1e9 + atime.Nsec, mtime.Sec*1e9 + mtime.Nsec, ctime.Sec*1e9 + ctime.Nsec
 }
 
+func stat(_ fs.File, t os.FileInfo) (atimeNsec, mtimeNsec, ctimeNsec int64, nlink uint64, err error) {
+	d := t.Sys().(*syscall.Stat_t)
+	atime := d.Atimespec
+	mtime := d.Mtimespec
+	ctime := d.Ctimespec
+	return atime.Sec*1e9 + atime.Nsec, mtime.Sec*1e9 + mtime.Nsec, ctime.Sec*1e9 + ctime.Nsec, uint64(d.Nlink), nil
+}
+
 func statDeviceInode(t os.FileInfo) (dev, inode uint64) {
 	d := t.Sys().(*syscall.Stat_t)
 	dev = uint64(d.Dev)
diff --git a/internal/platform/stat_linux.go b/internal/platform/stat_linux.go
index 8f0beb2c..5e07296b 100644
--- a/internal/platform/stat_linux.go
+++ b/internal/platform/stat_linux.go
@@ -6,6 +6,7 @@
 package platform
 
 import (
+	"io/fs"
 	"os"
 	"syscall"
 )
@@ -18,6 +19,14 @@ func statTimes(t os.FileInfo) (atimeNsec, mtimeNsec, ctimeNsec int64) {
 	return atime.Sec*1e9 + atime.Nsec, mtime.Sec*1e9 + mtime.Nsec, ctime.Sec*1e9 + ctime.Nsec
 }
 
+func stat(_ fs.File, t os.FileInfo) (atimeNsec, mtimeNsec, ctimeNsec int64, nlink uint64, err error) {
+	d := t.Sys().(*syscall.Stat_t)
+	atime := d.Atim
+	mtime := d.Mtim
+	ctime := d.Ctim
+	return atime.Sec*1e9 + atime.Nsec, mtime.Sec*1e9 + mtime.Nsec, ctime.Sec*1e9 + ctime.Nsec, uint64(d.Nlink), nil
+}
+
 func statDeviceInode(t os.FileInfo) (dev, inode uint64) {
 	d := t.Sys().(*syscall.Stat_t)
 	dev = d.Dev
diff --git a/internal/platform/stat_unsupported.go b/internal/platform/stat_unsupported.go
index 69635b5b..82351f60 100644
--- a/internal/platform/stat_unsupported.go
+++ b/internal/platform/stat_unsupported.go
@@ -2,10 +2,19 @@
 
 package platform
 
-import "os"
+import (
+	"io/fs"
+	"os"
+)
 
 func statTimes(t os.FileInfo) (atimeNsec, mtimeNsec, ctimeNsec int64) {
-	return mtimes(t)
+	atimeNsec, mtimeNsec, ctimeNsec = mtimes(t)
+	return
+}
+
+func stat(_ fs.File, t os.FileInfo) (atimeNsec, mtimeNsec, ctimeNsec int64, nlink uint64, err error) {
+	atimeNsec, mtimeNsec, ctimeNsec = mtimes(t)
+	return
 }
 
 func statDeviceInode(t os.FileInfo) (dev, inode uint64) {
diff --git a/internal/platform/stat_windows.go b/internal/platform/stat_windows.go
index a5dcb663..115935d8 100644
--- a/internal/platform/stat_windows.go
+++ b/internal/platform/stat_windows.go
@@ -3,6 +3,7 @@
 package platform
 
 import (
+	"io/fs"
 	"os"
 	"syscall"
 )
@@ -15,6 +16,48 @@ func statTimes(t os.FileInfo) (atimeNsec, mtimeNsec, ctimeNsec int64) {
 	return
 }
 
+// fdGetter is implemented by *os.File on Windows, but not a part of fs.File.
+// On the other hand, fs.File is implemented by the wrapped version of os.File,
+// therefore we define the interface here temporarily.
+//
+// This is only needed on Windows in order to nlink by making another raw system call
+// on the file handle.
+//
+// TODO: once we have our own File/FileInfo type, this shouldn't be needed.
+type fdGetter interface {
+	// Fd is the same as os.File Fd.
+	Fd() uintptr
+}
+
+func stat(f fs.File, t os.FileInfo) (atimeNsec, mtimeNsec, ctimeNsec int64, nlink uint64, err error) {
+	d := t.Sys().(*syscall.Win32FileAttributeData)
+	atimeNsec = d.LastAccessTime.Nanoseconds()
+	mtimeNsec = d.LastWriteTime.Nanoseconds()
+	ctimeNsec = d.CreationTime.Nanoseconds()
+
+	of, ok := f.(fdGetter)
+	if !ok {
+		return
+	}
+
+	handle := syscall.Handle(of.Fd())
+	var info syscall.ByHandleFileInformation
+	if err = syscall.GetFileInformationByHandle(handle, &info); err != nil {
+		// If the file descriptor is already closed, we have to re-open just like
+		// os.Stat does to allow the results on the closed files.
+		// https://github.com/golang/go/blob/go1.19.5/src/os/stat_windows.go#L93-L101
+		//
+		// TODO: once we have our File/Sta type, this shouldn't be necessary.
+		// But for now, ignore the error to pass the std library test for bad file descriptor.
+		// https://github.com/ziglang/zig/blob/master/lib/std/os/test.zig#L167-L170
+		if err == syscall.Errno(6) {
+			err = nil
+		}
+	}
+	nlink = uint64(info.NumberOfLinks)
+	return
+}
+
 func statDeviceInode(t os.FileInfo) (dev, inode uint64) {
 	// TODO: VolumeSerialNumber, FileIndexHigh and FileIndexLow are used in
 	// os.SameFile, but the fields aren't exported or accessible in os.FileInfo
diff --git a/internal/sysfs/dirfs.go b/internal/sysfs/dirfs.go
index 1f04891f..699d4934 100644
--- a/internal/sysfs/dirfs.go
+++ b/internal/sysfs/dirfs.go
@@ -62,6 +62,25 @@ func (d *dirFS) Rename(from, to string) error {
 	return platform.Rename(from, to)
 }
 
+// Readlink implements FS.Readlink
+func (d *dirFS) Readlink(path string, buf []byte) (n int, err error) {
+	n, err = syscall.Readlink(d.join(path), buf)
+	if err != nil {
+		err = unwrapOSError(err)
+		return
+	}
+
+	platform.SanitizeSeparator(buf[:n])
+	return
+}
+
+// Link implements FS.Link.
+func (d *dirFS) Link(oldName, newName string) (err error) {
+	err = os.Link(d.join(oldName), d.join(newName))
+	err = unwrapOSError(err)
+	return
+}
+
 // Rmdir implements FS.Rmdir
 func (d *dirFS) Rmdir(name string) error {
 	err := syscall.Rmdir(d.join(name))
@@ -74,6 +93,16 @@ func (d *dirFS) Unlink(name string) error {
 	return adjustUnlinkError(err)
 }
 
+// Symlink implements FS.Symlink
+func (d *dirFS) Symlink(oldName, link string) error {
+	// Note: do not resolve `oldName` relative to this dirFS. The link result is always resolved
+	// when dereference the `link` on its usage (e.g. readlink, read, etc).
+	// https://github.com/bytecodealliance/cap-std/blob/v1.0.4/cap-std/src/fs/dir.rs#L404-L409
+	err := os.Symlink(oldName, d.join(link))
+	err = unwrapOSError(err)
+	return err
+}
+
 // Utimes implements FS.Utimes
 func (d *dirFS) Utimes(name string, atimeNsec, mtimeNsec int64) error {
 	return syscall.UtimesNano(d.join(name), []syscall.Timespec{
diff --git a/internal/sysfs/sysfs.go b/internal/sysfs/sysfs.go
index 8231560b..724ba1d2 100644
--- a/internal/sysfs/sysfs.go
+++ b/internal/sysfs/sysfs.go
@@ -111,6 +111,48 @@ type FS interface {
 	//   - syscall.EISDIR: `path` exists, but is a directory.
 	Unlink(path string) error
 
+	// Link is similar to syscall.Link, except the path is relative to this
+	// file system. This creates "hard" link from oldPath to newPath, in contrast to soft link as in Symlink.
+	//
+	// # Errors
+	//
+	// The following errors are expected:
+	//   - syscall.EPERM: `oldPath` is invalid.
+	//   - syscall.ENOENT: `oldPath` doesn't exist.
+	//   - syscall.EISDIR: `newPath` exists, but is a directory.
+	Link(oldPath, newPath string) error
+
+	// Symlink is similar to syscall.Symlink, except the `oldPath` is relative to this
+	// file system. This creates "soft" link from oldPath to newPath,
+	// in contrast to hard link as in Link.
+	//
+	// # Errors
+	//
+	// The following errors are expected:
+	//   - syscall.EPERM: `oldPath` or `newPath` is invalid.
+	//   - syscall.EEXIST: `newPath` exists.
+	//
+	// # Note
+	//
+	//   - Only `newPath` is relative to this file system and `oldPath` is kept as-is.
+	//     That is because the link is only resolved relative to the directory when
+	//     dereferencing it (e.g. ReadLink).
+	//     See https://github.com/bytecodealliance/cap-std/blob/v1.0.4/cap-std/src/fs/dir.rs#L404-L409
+	//     for how others implement this.
+	Symlink(oldPath, linkName string) error
+
+	// Readlink is similar to syscall.Readlink, except the path is relative to this
+	// file system.
+	// # Errors
+	//
+	// The following errors are expected:
+	//   - syscall.EINVAL: `path` is invalid.
+	//
+	// # Notes
+	//   - On windows, its path separator is different from other platforms, but to provide consistent result to Wasm,
+	//     the implementation is supposed to sanitize the result with the regular "/" separator.
+	Readlink(path string, buf []byte) (n int, err error)
+
 	// Truncate is similar to syscall.Truncate, except the path is relative to
 	// this file system.
 	//
@@ -163,11 +205,13 @@ type file interface {
 	io.WriterAt // for pwrite
 	syncer
 	truncater
+	fder // for the number of links.
 }
 
 type (
 	syncer    interface{ Sync() error }
 	truncater interface{ Truncate(size int64) error }
+	fder      interface{ Fd() (fd uintptr) }
 )
 
 // ReaderAtOffset gets an io.Reader from a fs.File that reads from an offset,
diff --git a/internal/sysfs/unsupported.go b/internal/sysfs/unsupported.go
index a649bd9e..e7ee48c9 100644
--- a/internal/sysfs/unsupported.go
+++ b/internal/sysfs/unsupported.go
@@ -39,6 +39,21 @@ func (UnimplementedFS) Rmdir(path string) error {
 	return syscall.ENOSYS
 }
 
+// Readlink implements FS.Readlink
+func (UnimplementedFS) Readlink(string, []byte) (int, error) {
+	return 0, syscall.ENOSYS
+}
+
+// Link implements FS.Link
+func (UnimplementedFS) Link(_, _ string) error {
+	return syscall.ENOSYS
+}
+
+// Symlink implements FS.Symlink
+func (UnimplementedFS) Symlink(_, _ string) error {
+	return syscall.ENOSYS
+}
+
 // Unlink implements FS.Unlink
 func (UnimplementedFS) Unlink(path string) error {
 	return syscall.ENOSYS
diff --git a/internal/wasi_snapshot_preview1/errno.go b/internal/wasi_snapshot_preview1/errno.go
index bfac8a9a..652b4df0 100644
--- a/internal/wasi_snapshot_preview1/errno.go
+++ b/internal/wasi_snapshot_preview1/errno.go
@@ -267,6 +267,8 @@ var errnoToString = [...]string{
 // Errno.
 func ToErrno(err error) Errno {
 	switch {
+	case errors.Is(err, syscall.ENOTDIR):
+		return ErrnoNotdir
 	case errors.Is(err, syscall.EBADF), errors.Is(err, fs.ErrClosed):
 		return ErrnoBadf
 	case errors.Is(err, syscall.EINVAL), errors.Is(err, fs.ErrInvalid):
diff --git a/site/content/specs.md b/site/content/specs.md
index 27b1cdbd..a0a25c52 100644
--- a/site/content/specs.md
+++ b/site/content/specs.md
@@ -114,12 +114,12 @@ Notes:
 | path_create_directory   |   ✅    | Rust,TinyGo,Zig |
 | path_filestat_get       |   ✅    | Rust,TinyGo,Zig |
 | path_filestat_set_times |   ❌    |                 |
-| path_link               |   ❌    |                 |
+| path_link               |   ✅    |        Rust,Zig |
 | path_open               |   ✅    | Rust,TinyGo,Zig |
-| path_readlink           |   ❌    |                 |
+| path_readlink           |   ✅    |        Rust,Zig |
 | path_remove_directory   |   ✅    | Rust,TinyGo,Zig |
 | path_rename             |   ✅    | Rust,TinyGo,Zig |
-| path_symlink            |   ❌    |                 |
+| path_symlink            |   ✅    |        Rust,Zig |
 | path_unlink_file        |   ✅    | Rust,TinyGo,Zig |
 | poll_oneoff             |   ✅    | Rust,TinyGo,Zig |
 | proc_exit               |   ✅    | Rust,TinyGo,Zig |


Test patch:
diff --git a/imports/wasi_snapshot_preview1/fs_test.go b/imports/wasi_snapshot_preview1/fs_test.go
index 28e2ed21..1a783736 100644
--- a/imports/wasi_snapshot_preview1/fs_test.go
+++ b/imports/wasi_snapshot_preview1/fs_test.go
@@ -9,7 +9,6 @@ import (
 	"math"
 	"os"
 	"path"
-	"runtime"
 	"testing"
 	"time"
 
@@ -2893,13 +2892,91 @@ func Test_pathFilestatSetTimes(t *testing.T) {
 `, log)
 }
 
-// Test_pathLink only tests it is stubbed for GrainLang per #271
 func Test_pathLink(t *testing.T) {
-	log := requireErrnoNosys(t, PathLinkName, 0, 0, 0, 0, 0, 0, 0)
-	require.Equal(t, `
---> wasi_snapshot_preview1.path_link(old_fd=0,old_flags=,old_path=,new_fd=0,new_path=)
-<-- errno=ENOSYS
-`, log)
+	tmpDir := t.TempDir() // open before loop to ensure no locking problems.
+
+	const oldDirName = "my-old-dir"
+	mod, oldFd, log, r := requireOpenFile(t, tmpDir, oldDirName, nil, false)
+	defer r.Close(testCtx)
+
+	const newDirName = "my-new-dir/sub"
+	require.NoError(t, os.MkdirAll(path.Join(tmpDir, newDirName), 0o700))
+	fsc := mod.(*wasm.CallContext).Sys.FS()
+	newFd, err := fsc.OpenFile(fsc.RootFS(), newDirName, 0o600, 0)
+	require.NoError(t, err)
+
+	mem := mod.Memory()
+
+	const filename = "file"
+	err = os.WriteFile(path.Join(tmpDir, oldDirName, filename), []byte{1, 2, 3, 4}, 0o700)
+	require.NoError(t, err)
+
+	const fileNamePtr = 0xff
+	ok := mem.Write(fileNamePtr, []byte(filename))
+	require.True(t, ok)
+
+	const nonExistingFileNamePtr = 0xaa
+	const nonExistingFileName = "invalid-san"
+	ok = mem.Write(nonExistingFileNamePtr, []byte(nonExistingFileName))
+	require.True(t, ok)
+
+	const destinationNamePtr = 0xcc
+	const destinationName = "hard-linked"
+	ok = mem.Write(destinationNamePtr, []byte(destinationName))
+	require.True(t, ok)
+
+	destinationRealPath := path.Join(tmpDir, newDirName, destinationName)
+
+	t.Run("success", func(t *testing.T) {
+		requireErrno(t, ErrnoSuccess, mod, PathLinkName,
+			uint64(oldFd), 0, fileNamePtr, uint64(len(filename)),
+			uint64(newFd), destinationNamePtr, uint64(len(destinationName)))
+		require.Contains(t, log.String(), ErrnoName(ErrnoSuccess))
+
+		f, err := os.Open(destinationRealPath)
+		require.NoError(t, err)
+		defer func() {
+			require.NoError(t, f.Close())
+		}()
+		st, err := f.Stat()
+		require.NoError(t, err)
+		require.False(t, st.Mode()&os.ModeSymlink == os.ModeSymlink)
+
+		_, _, _, nlink, err := platform.Stat(f, st)
+		require.NoError(t, err)
+		require.Equal(t, uint64(2), nlink)
+	})
+
+	t.Run("errors", func(t *testing.T) {
+		for _, tc := range []struct {
+			errno                                              Errno
+			oldDirFd, newDirFd, oldPtr, newPtr, oldLen, newLen uint64
+		}{
+			{errno: ErrnoBadf, oldDirFd: 1000},
+			{errno: ErrnoBadf, oldDirFd: uint64(oldFd), newDirFd: 1000},
+			{errno: ErrnoNotdir, oldDirFd: uint64(oldFd), newDirFd: 1},
+			{errno: ErrnoNotdir, oldDirFd: 1, newDirFd: 1},
+			{errno: ErrnoNotdir, oldDirFd: 1, newDirFd: uint64(newFd)},
+			{errno: ErrnoFault, oldDirFd: uint64(oldFd), newDirFd: uint64(newFd), oldLen: math.MaxUint32},
+			{errno: ErrnoFault, oldDirFd: uint64(oldFd), newDirFd: uint64(newFd), newLen: math.MaxUint32},
+			{
+				errno: ErrnoFault, oldDirFd: uint64(oldFd), newDirFd: uint64(newFd),
+				oldPtr: math.MaxUint32, oldLen: 100, newLen: 100,
+			},
+			{
+				errno: ErrnoFault, oldDirFd: uint64(oldFd), newDirFd: uint64(newFd),
+				oldPtr: 1, oldLen: 100, newPtr: math.MaxUint32, newLen: 100,
+			},
+		} {
+			name := ErrnoName(tc.errno)
+			t.Run(name, func(t *testing.T) {
+				requireErrno(t, tc.errno, mod, PathLinkName,
+					tc.oldDirFd, 0, tc.oldPtr, tc.oldLen,
+					tc.newDirFd, tc.newPtr, tc.newLen)
+				require.Contains(t, log.String(), name)
+			})
+		}
+	})
 }
 
 func Test_pathOpen(t *testing.T) {
@@ -3327,13 +3404,107 @@ func Test_pathOpen_Errors(t *testing.T) {
 	}
 }
 
-// Test_pathReadlink only tests it is stubbed for GrainLang per #271
 func Test_pathReadlink(t *testing.T) {
-	log := requireErrnoNosys(t, PathReadlinkName, 0, 0, 0, 0, 0, 0)
-	require.Equal(t, `
---> wasi_snapshot_preview1.path_readlink(fd=0,path=,buf=0,buf_len=0,result.bufused=0)
-<-- errno=ENOSYS
-`, log)
+	tmpDir := t.TempDir() // open before loop to ensure no locking problems.
+
+	const topDirName = "top"
+	mod, topFd, log, r := requireOpenFile(t, tmpDir, topDirName, nil, false)
+	defer r.Close(testCtx)
+
+	const subDirName = "sub-dir"
+	require.NoError(t, os.Mkdir(path.Join(tmpDir, topDirName, subDirName), 0o700))
+
+	mem := mod.Memory()
+
+	const originalFileName = "top-original-file"
+	const destinationPathNamePtr = 0x77
+	const destinationPathName = "top-symlinked"
+	ok := mem.Write(destinationPathNamePtr, []byte(destinationPathName))
+	require.True(t, ok)
+
+	originalSubDirFileName := path.Join(subDirName, "subdir-original-file")
+	destinationSubDirFileName := path.Join(subDirName, "subdir-symlinked")
+	const destinationSubDirPathNamePtr = 0xcc
+	ok = mem.Write(destinationSubDirPathNamePtr, []byte(destinationSubDirFileName))
+	require.True(t, ok)
+
+	// Create original file and symlink to the destination.
+	originalRelativePath := path.Join(topDirName, originalFileName)
+	err := os.WriteFile(path.Join(tmpDir, originalRelativePath), []byte{4, 3, 2, 1}, 0o700)
+	require.NoError(t, err)
+	err = os.Symlink(originalRelativePath, path.Join(tmpDir, topDirName, destinationPathName))
+	require.NoError(t, err)
+	originalSubDirRelativePath := path.Join(topDirName, originalSubDirFileName)
+	err = os.WriteFile(path.Join(tmpDir, originalSubDirRelativePath), []byte{1, 2, 3, 4}, 0o700)
+	require.NoError(t, err)
+	err = os.Symlink(originalSubDirRelativePath, path.Join(tmpDir, topDirName, destinationSubDirFileName))
+	require.NoError(t, err)
+
+	t.Run("ok", func(t *testing.T) {
+		for _, tc := range []struct {
+			name    string
+			pathPtr uint64
+			pathLen uint64
+			exp     string
+		}{
+			{
+				name:    "top",
+				pathPtr: destinationPathNamePtr,
+				pathLen: uint64(len(destinationPathName)),
+				exp:     originalRelativePath,
+			},
+			{
+				name:    "subdir",
+				pathPtr: destinationSubDirPathNamePtr,
+				pathLen: uint64(len(destinationSubDirFileName)),
+				exp:     originalSubDirRelativePath,
+			},
+		} {
+			t.Run(tc.name, func(t *testing.T) {
+				const bufPtr, bufSize, resultBufUsedPtr = 0x100, 0xff, 0x200
+				requireErrno(t, ErrnoSuccess, mod, PathReadlinkName,
+					uint64(topFd),
+					tc.pathPtr, tc.pathLen,
+					bufPtr, bufSize, resultBufUsedPtr)
+				require.Contains(t, log.String(), ErrnoName(ErrnoSuccess))
+
+				size, ok := mem.ReadUint32Le(resultBufUsedPtr)
+				require.True(t, ok)
+				actual, ok := mem.Read(bufPtr, size)
+				require.True(t, ok)
+				require.Equal(t, tc.exp, string(actual))
+			})
+		}
+	})
+
+	t.Run("errors", func(t *testing.T) {
+		for _, tc := range []struct {
+			errno                                                     Errno
+			dirFd, pathPtr, pathLen, bufPtr, bufLen, resultBufUsedPtr uint64
+		}{
+			{errno: ErrnoInval},
+			{errno: ErrnoInval, pathLen: 100},
+			{errno: ErrnoInval, bufLen: 100},
+			{errno: ErrnoFault, dirFd: uint64(topFd), bufLen: 100, pathLen: 100, bufPtr: math.MaxUint32},
+			{errno: ErrnoFault, bufLen: 100, pathLen: 100, bufPtr: 50, pathPtr: math.MaxUint32},
+			{errno: ErrnoNotdir, bufLen: 100, pathLen: 100, bufPtr: 50, pathPtr: 50, dirFd: 1},
+			{errno: ErrnoBadf, bufLen: 100, pathLen: 100, bufPtr: 50, pathPtr: 50, dirFd: 1000},
+			{
+				errno:  ErrnoNoent,
+				bufLen: 100, bufPtr: 50,
+				pathPtr: destinationPathNamePtr, pathLen: uint64(len(destinationPathName)) - 1,
+				dirFd: uint64(topFd),
+			},
+		} {
+			name := ErrnoName(tc.errno)
+			t.Run(name, func(t *testing.T) {
+				requireErrno(t, tc.errno, mod, PathReadlinkName,
+					tc.dirFd, tc.pathPtr, tc.pathLen, tc.bufPtr,
+					tc.bufLen, tc.resultBufUsedPtr)
+				require.Contains(t, log.String(), name)
+			})
+		}
+	})
 }
 
 func Test_pathRemoveDirectory(t *testing.T) {
@@ -3453,11 +3624,11 @@ func Test_pathRemoveDirectory_Errors(t *testing.T) {
 			pathName:      file,
 			path:          0,
 			pathLen:       uint32(len(file)),
-			expectedErrno: errNotDir(),
+			expectedErrno: ErrnoNotdir,
 			expectedLog: fmt.Sprintf(`
 ==> wasi_snapshot_preview1.path_remove_directory(fd=3,path=file)
 <== errno=%s
-`, ErrnoName(errNotDir())),
+`, ErrnoName(ErrnoNotdir)),
 		},
 		{
 			name:          "dir not empty",
@@ -3486,21 +3657,76 @@ func Test_pathRemoveDirectory_Errors(t *testing.T) {
 	}
 }
 
-func errNotDir() Errno {
-	if runtime.GOOS == "windows" {
-		// As of Go 1.19, Windows maps syscall.ENOTDIR to syscall.ENOENT
-		return ErrnoNoent
-	}
-	return ErrnoNotdir
-}
+func Test_pathSymlink_errors(t *testing.T) {
+	tmpDir := t.TempDir() // open before loop to ensure no locking problems.
 
-// Test_pathSymlink only tests it is stubbed for GrainLang per #271
-func Test_pathSymlink(t *testing.T) {
-	log := requireErrnoNosys(t, PathSymlinkName, 0, 0, 0, 0, 0)
-	require.Equal(t, `
---> wasi_snapshot_preview1.path_symlink(old_path=,fd=0,new_path=)
-<-- errno=ENOSYS
-`, log)
+	const dirname = "my-dir"
+	mod, fd, log, r := requireOpenFile(t, tmpDir, dirname, nil, false)
+	defer r.Close(testCtx)
+
+	mem := mod.Memory()
+
+	const filename = "file"
+	err := os.WriteFile(path.Join(tmpDir, dirname, filename), []byte{1, 2, 3, 4}, 0o700)
+	require.NoError(t, err)
+
+	const fileNamePtr = 0xff
+	ok := mem.Write(fileNamePtr, []byte(filename))
+	require.True(t, ok)
+
+	const nonExistingFileNamePtr = 0xaa
+	const nonExistingFileName = "invalid-san"
+	ok = mem.Write(nonExistingFileNamePtr, []byte(nonExistingFileName))
+	require.True(t, ok)
+
+	const destinationNamePtr = 0xcc
+	const destinationName = "symlinked"
+	ok = mem.Write(destinationNamePtr, []byte(destinationName))
+	require.True(t, ok)
+
+	t.Run("success", func(t *testing.T) {
+		requireErrno(t, ErrnoSuccess, mod, PathSymlinkName,
+			fileNamePtr, uint64(len(filename)), uint64(fd), destinationNamePtr, uint64(len(destinationName)))
+		require.Contains(t, log.String(), ErrnoName(ErrnoSuccess))
+		st, err := os.Lstat(path.Join(tmpDir, dirname, destinationName))
+		require.NoError(t, err)
+		require.Equal(t, st.Mode()&os.ModeSymlink, os.ModeSymlink)
+	})
+
+	t.Run("errors", func(t *testing.T) {
+		for _, tc := range []struct {
+			errno                                 Errno
+			dirFd, oldPtr, newPtr, oldLen, newLen uint64
+		}{
+			{errno: ErrnoBadf, dirFd: 1000},
+			{errno: ErrnoNotdir, dirFd: 2},
+			// Length zero buffer is not valid.
+			{errno: ErrnoInval, dirFd: uint64(fd)},
+			{errno: ErrnoInval, oldLen: 100, dirFd: uint64(fd)},
+			{errno: ErrnoInval, newLen: 100, dirFd: uint64(fd)},
+			// Invalid pointer to the names.
+			{errno: ErrnoFault, oldPtr: math.MaxUint64, oldLen: 100, newLen: 100, dirFd: uint64(fd)},
+			{errno: ErrnoFault, newPtr: math.MaxUint64, oldLen: 100, newLen: 100, dirFd: uint64(fd)},
+			{errno: ErrnoFault, oldPtr: math.MaxUint64, newPtr: math.MaxUint64, oldLen: 100, newLen: 100, dirFd: uint64(fd)},
+			// Non-existing path as source.
+			{
+				errno: ErrnoInval, oldPtr: nonExistingFileNamePtr, oldLen: uint64(len(nonExistingFileName)),
+				newPtr: 0, newLen: 5, dirFd: uint64(fd),
+			},
+			// Linking to existing file.
+			{
+				errno: ErrnoExist, oldPtr: fileNamePtr, oldLen: uint64(len(filename)),
+				newPtr: fileNamePtr, newLen: uint64(len(filename)), dirFd: uint64(fd),
+			},
+		} {
+			name := ErrnoName(tc.errno)
+			t.Run(name, func(t *testing.T) {
+				requireErrno(t, tc.errno, mod, PathSymlinkName,
+					tc.oldPtr, tc.oldLen, tc.dirFd, tc.newPtr, tc.newLen)
+				require.Contains(t, log.String(), name)
+			})
+		}
+	})
 }
 
 func Test_pathRename(t *testing.T) {
diff --git a/internal/gojs/testdata/writefs/main.go b/internal/gojs/testdata/writefs/main.go
index 1b18f306..a114453d 100644
--- a/internal/gojs/testdata/writefs/main.go
+++ b/internal/gojs/testdata/writefs/main.go
@@ -107,10 +107,10 @@ func Main() {
 	}
 
 	// Ensure the times translated properly.
-	if stat, err := os.Stat(dir); err != nil {
+	if st, err := os.Stat(dir); err != nil {
 		log.Panicln("unexpected error", err)
 	} else {
-		atimeNsec, mtimeNsec, _ := statTimes(stat)
+		atimeNsec, mtimeNsec, _ := statTimes(st)
 		fmt.Println("times:", atimeNsec, mtimeNsec)
 
 		// statDeviceInode cannot be tested against real device values because
@@ -121,7 +121,7 @@ func Main() {
 		// platform.StatDeviceInode on windows, so we couldn't run that test
 		// in CI. For now, this only tests there is no compilation problem or
 		// runtime panic.
-		_, _ = statDeviceInode(stat)
+		_, _ = statDeviceInode(st)
 	}
 
 	// Test renaming a file, noting we can't verify error numbers as they
diff --git a/internal/platform/separator_test.go b/internal/platform/separator_test.go
new file mode 100644
index 00000000..5951eb7f
--- /dev/null
+++ b/internal/platform/separator_test.go
@@ -0,0 +1,14 @@
+package platform
+
+import (
+	"path/filepath"
+	"testing"
+
+	"github.com/tetratelabs/wazero/internal/testing/require"
+)
+
+func TestSanitizeSeparator(t *testing.T) {
+	orig := []byte(filepath.Join("a", "b", "c"))
+	SanitizeSeparator(orig)
+	require.Equal(t, "a/b/c", string(orig))
+}
diff --git a/internal/platform/stat_test.go b/internal/platform/stat_test.go
index f240450c..d25193dc 100644
--- a/internal/platform/stat_test.go
+++ b/internal/platform/stat_test.go
@@ -10,7 +10,7 @@ import (
 	"github.com/tetratelabs/wazero/internal/testing/require"
 )
 
-func Test_StatTimes(t *testing.T) {
+func Test_Stat(t *testing.T) {
 	tmpDir := t.TempDir()
 
 	file := path.Join(tmpDir, "file")
diff --git a/internal/sysfs/dirfs_test.go b/internal/sysfs/dirfs_test.go
index 4a0738a2..9470dffc 100644
--- a/internal/sysfs/dirfs_test.go
+++ b/internal/sysfs/dirfs_test.go
@@ -512,3 +512,85 @@ func Test_fdReaddir_opened_file_written(t *testing.T) {
 	require.Equal(t, 1, len(entries))
 	require.Equal(t, "my-file", entries[0].Name())
 }
+
+func TestDirFS_Link(t *testing.T) {
+	t.Parallel()
+
+	// Set up the test files
+	tmpDir := t.TempDir()
+	require.NoError(t, fstest.WriteTestFiles(tmpDir))
+
+	testFS := NewDirFS(tmpDir)
+
+	require.ErrorIs(t, testFS.Link("cat", ""), syscall.ENOENT)
+	require.ErrorIs(t, testFS.Link("sub/test.txt", "sub/test.txt"), syscall.EEXIST)
+	require.ErrorIs(t, testFS.Link("sub/test.txt", "."), syscall.EEXIST)
+	require.ErrorIs(t, testFS.Link("sub/test.txt", ""), syscall.EEXIST)
+	require.ErrorIs(t, testFS.Link("sub/test.txt", "/"), syscall.EEXIST)
+	require.NoError(t, testFS.Link("sub/test.txt", "foo"))
+}
+
+func TestDirFS_Symlink(t *testing.T) {
+	t.Parallel()
+
+	// Set up the test files
+	tmpDir := t.TempDir()
+	require.NoError(t, fstest.WriteTestFiles(tmpDir))
+
+	testFS := NewDirFS(tmpDir)
+
+	require.ErrorIs(t, testFS.Symlink("sub/test.txt", "sub/test.txt"), syscall.EEXIST)
+	// Non-existing old name is allowed.
+	require.NoError(t, testFS.Symlink("non-existing", "aa"))
+	require.NoError(t, testFS.Symlink("sub/", "symlinked-subdir"))
+
+	st, err := os.Lstat(pathutil.Join(tmpDir, "aa"))
+	require.NoError(t, err)
+	require.Equal(t, "aa", st.Name())
+	require.True(t, st.Mode()&fs.ModeSymlink > 0 && !st.IsDir())
+
+	st, err = os.Lstat(pathutil.Join(tmpDir, "symlinked-subdir"))
+	require.NoError(t, err)
+	require.Equal(t, "symlinked-subdir", st.Name())
+	require.True(t, st.Mode()&fs.ModeSymlink > 0)
+}
+
+func TestDirFS_Readlink(t *testing.T) {
+	// Set up the test files
+	tmpDir := t.TempDir()
+	require.NoError(t, fstest.WriteTestFiles(tmpDir))
+
+	testFS := NewDirFS(tmpDir)
+
+	testLinks := []struct {
+		old, dst string
+	}{
+		// Same dir.
+		{old: "animals.txt", dst: "symlinked-animals.txt"},
+		{old: "sub/test.txt", dst: "sub/symlinked-test.txt"},
+		// Parent to sub.
+		{old: "animals.txt", dst: "sub/symlinked-animals.txt"},
+		// Sub to parent.
+		{old: "sub/test.txt", dst: "symlinked-zoo.txt"},
+	}
+
+	buf := make([]byte, 200)
+	for _, tl := range testLinks {
+		err := os.Symlink(pathutil.Join(tl.old), pathutil.Join(tmpDir, tl.dst))
+		require.NoError(t, err)
+
+		n, err := testFS.Readlink(tl.dst, buf)
+		require.NoError(t, err)
+		require.Equal(t, tl.old, string(buf[:n]))
+		require.Equal(t, len(tl.old), n)
+	}
+
+	t.Run("errors", func(t *testing.T) {
+		_, err := testFS.Readlink("sub/test.txt", buf)
+		require.Error(t, err)
+		_, err = testFS.Readlink("", buf)
+		require.Error(t, err)
+		_, err = testFS.Readlink("animals.txt", buf)
+		require.Error(t, err)
+	})
+}