github.com/wasilibs/wazerox@v0.0.0-20240124024944-4923be63ab5f/internal/wasip1/errno.go (about)

     1  package wasip1
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/wasilibs/wazerox/experimental/sys"
     7  )
     8  
     9  // Errno is neither uint16 nor an alias for parity with wasm.ValueType.
    10  type Errno = uint32
    11  
    12  // ErrnoName returns the POSIX error code name, except ErrnoSuccess, which is
    13  // not an error. e.g. Errno2big -> "E2BIG"
    14  func ErrnoName(errno uint32) string {
    15  	if int(errno) < len(errnoToString) {
    16  		return errnoToString[errno]
    17  	}
    18  	return fmt.Sprintf("errno(%d)", errno)
    19  }
    20  
    21  // Note: Below prefers POSIX symbol names over WASI ones, even if the docs are from WASI.
    22  // See https://linux.die.net/man/3/errno
    23  // See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#variants-1
    24  const (
    25  	// ErrnoSuccess No error occurred. System call completed successfully.
    26  	ErrnoSuccess Errno = iota
    27  	// Errno2big Argument list too long.
    28  	Errno2big
    29  	// ErrnoAcces Permission denied.
    30  	ErrnoAcces
    31  	// ErrnoAddrinuse Address in use.
    32  	ErrnoAddrinuse
    33  	// ErrnoAddrnotavail Address not available.
    34  	ErrnoAddrnotavail
    35  	// ErrnoAfnosupport Address family not supported.
    36  	ErrnoAfnosupport
    37  	// ErrnoAgain Resource unavailable, or operation would block.
    38  	ErrnoAgain
    39  	// ErrnoAlready Connection already in progress.
    40  	ErrnoAlready
    41  	// ErrnoBadf Bad file descriptor.
    42  	ErrnoBadf
    43  	// ErrnoBadmsg Bad message.
    44  	ErrnoBadmsg
    45  	// ErrnoBusy Device or resource busy.
    46  	ErrnoBusy
    47  	// ErrnoCanceled Operation canceled.
    48  	ErrnoCanceled
    49  	// ErrnoChild No child processes.
    50  	ErrnoChild
    51  	// ErrnoConnaborted Connection aborted.
    52  	ErrnoConnaborted
    53  	// ErrnoConnrefused Connection refused.
    54  	ErrnoConnrefused
    55  	// ErrnoConnreset Connection reset.
    56  	ErrnoConnreset
    57  	// ErrnoDeadlk Resource deadlock would occur.
    58  	ErrnoDeadlk
    59  	// ErrnoDestaddrreq Destination address required.
    60  	ErrnoDestaddrreq
    61  	// ErrnoDom Mathematics argument out of domain of function.
    62  	ErrnoDom
    63  	// ErrnoDquot Reserved.
    64  	ErrnoDquot
    65  	// ErrnoExist File exists.
    66  	ErrnoExist
    67  	// ErrnoFault Bad address.
    68  	ErrnoFault
    69  	// ErrnoFbig File too large.
    70  	ErrnoFbig
    71  	// ErrnoHostunreach Host is unreachable.
    72  	ErrnoHostunreach
    73  	// ErrnoIdrm Identifier removed.
    74  	ErrnoIdrm
    75  	// ErrnoIlseq Illegal byte sequence.
    76  	ErrnoIlseq
    77  	// ErrnoInprogress Operation in progress.
    78  	ErrnoInprogress
    79  	// ErrnoIntr Interrupted function.
    80  	ErrnoIntr
    81  	// ErrnoInval Invalid argument.
    82  	ErrnoInval
    83  	// ErrnoIo I/O error.
    84  	ErrnoIo
    85  	// ErrnoIsconn Socket is connected.
    86  	ErrnoIsconn
    87  	// ErrnoIsdir Is a directory.
    88  	ErrnoIsdir
    89  	// ErrnoLoop Too many levels of symbolic links.
    90  	ErrnoLoop
    91  	// ErrnoMfile File descriptor value too large.
    92  	ErrnoMfile
    93  	// ErrnoMlink Too many links.
    94  	ErrnoMlink
    95  	// ErrnoMsgsize Message too large.
    96  	ErrnoMsgsize
    97  	// ErrnoMultihop Reserved.
    98  	ErrnoMultihop
    99  	// ErrnoNametoolong Filename too long.
   100  	ErrnoNametoolong
   101  	// ErrnoNetdown Network is down.
   102  	ErrnoNetdown
   103  	// ErrnoNetreset Connection aborted by network.
   104  	ErrnoNetreset
   105  	// ErrnoNetunreach Network unreachable.
   106  	ErrnoNetunreach
   107  	// ErrnoNfile Too many files open in system.
   108  	ErrnoNfile
   109  	// ErrnoNobufs No buffer space available.
   110  	ErrnoNobufs
   111  	// ErrnoNodev No such device.
   112  	ErrnoNodev
   113  	// ErrnoNoent No such file or directory.
   114  	ErrnoNoent
   115  	// ErrnoNoexec Executable file format error.
   116  	ErrnoNoexec
   117  	// ErrnoNolck No locks available.
   118  	ErrnoNolck
   119  	// ErrnoNolink Reserved.
   120  	ErrnoNolink
   121  	// ErrnoNomem Not enough space.
   122  	ErrnoNomem
   123  	// ErrnoNomsg No message of the desired type.
   124  	ErrnoNomsg
   125  	// ErrnoNoprotoopt No message of the desired type.
   126  	ErrnoNoprotoopt
   127  	// ErrnoNospc No space left on device.
   128  	ErrnoNospc
   129  	// ErrnoNosys function not supported.
   130  	ErrnoNosys
   131  	// ErrnoNotconn The socket is not connected.
   132  	ErrnoNotconn
   133  	// ErrnoNotdir Not a directory or a symbolic link to a directory.
   134  	ErrnoNotdir
   135  	// ErrnoNotempty Directory not empty.
   136  	ErrnoNotempty
   137  	// ErrnoNotrecoverable State not recoverable.
   138  	ErrnoNotrecoverable
   139  	// ErrnoNotsock Not a socket.
   140  	ErrnoNotsock
   141  	// ErrnoNotsup Not supported, or operation not supported on socket.
   142  	ErrnoNotsup
   143  	// ErrnoNotty Inappropriate I/O control operation.
   144  	ErrnoNotty
   145  	// ErrnoNxio No such device or address.
   146  	ErrnoNxio
   147  	// ErrnoOverflow Value too large to be stored in data type.
   148  	ErrnoOverflow
   149  	// ErrnoOwnerdead Previous owner died.
   150  	ErrnoOwnerdead
   151  	// ErrnoPerm Operation not permitted.
   152  	ErrnoPerm
   153  	// ErrnoPipe Broken pipe.
   154  	ErrnoPipe
   155  	// ErrnoProto Protocol error.
   156  	ErrnoProto
   157  	// ErrnoProtonosupport Protocol error.
   158  	ErrnoProtonosupport
   159  	// ErrnoPrototype Protocol wrong type for socket.
   160  	ErrnoPrototype
   161  	// ErrnoRange Result too large.
   162  	ErrnoRange
   163  	// ErrnoRofs Read-only file system.
   164  	ErrnoRofs
   165  	// ErrnoSpipe Invalid seek.
   166  	ErrnoSpipe
   167  	// ErrnoSrch No such process.
   168  	ErrnoSrch
   169  	// ErrnoStale Reserved.
   170  	ErrnoStale
   171  	// ErrnoTimedout Connection timed out.
   172  	ErrnoTimedout
   173  	// ErrnoTxtbsy Text file busy.
   174  	ErrnoTxtbsy
   175  	// ErrnoXdev Cross-device link.
   176  	ErrnoXdev
   177  
   178  	// Note: ErrnoNotcapable was removed by WASI maintainers.
   179  	// See https://github.com/WebAssembly/wasi-libc/pull/294
   180  )
   181  
   182  var errnoToString = [...]string{
   183  	"ESUCCESS",
   184  	"E2BIG",
   185  	"EACCES",
   186  	"EADDRINUSE",
   187  	"EADDRNOTAVAIL",
   188  	"EAFNOSUPPORT",
   189  	"EAGAIN",
   190  	"EALREADY",
   191  	"EBADF",
   192  	"EBADMSG",
   193  	"EBUSY",
   194  	"ECANCELED",
   195  	"ECHILD",
   196  	"ECONNABORTED",
   197  	"ECONNREFUSED",
   198  	"ECONNRESET",
   199  	"EDEADLK",
   200  	"EDESTADDRREQ",
   201  	"EDOM",
   202  	"EDQUOT",
   203  	"EEXIST",
   204  	"EFAULT",
   205  	"EFBIG",
   206  	"EHOSTUNREACH",
   207  	"EIDRM",
   208  	"EILSEQ",
   209  	"EINPROGRESS",
   210  	"EINTR",
   211  	"EINVAL",
   212  	"EIO",
   213  	"EISCONN",
   214  	"EISDIR",
   215  	"ELOOP",
   216  	"EMFILE",
   217  	"EMLINK",
   218  	"EMSGSIZE",
   219  	"EMULTIHOP",
   220  	"ENAMETOOLONG",
   221  	"ENETDOWN",
   222  	"ENETRESET",
   223  	"ENETUNREACH",
   224  	"ENFILE",
   225  	"ENOBUFS",
   226  	"ENODEV",
   227  	"ENOENT",
   228  	"ENOEXEC",
   229  	"ENOLCK",
   230  	"ENOLINK",
   231  	"ENOMEM",
   232  	"ENOMSG",
   233  	"ENOPROTOOPT",
   234  	"ENOSPC",
   235  	"ENOSYS",
   236  	"ENOTCONN",
   237  	"ENOTDIR",
   238  	"ENOTEMPTY",
   239  	"ENOTRECOVERABLE",
   240  	"ENOTSOCK",
   241  	"ENOTSUP",
   242  	"ENOTTY",
   243  	"ENXIO",
   244  	"EOVERFLOW",
   245  	"EOWNERDEAD",
   246  	"EPERM",
   247  	"EPIPE",
   248  	"EPROTO",
   249  	"EPROTONOSUPPORT",
   250  	"EPROTOTYPE",
   251  	"ERANGE",
   252  	"EROFS",
   253  	"ESPIPE",
   254  	"ESRCH",
   255  	"ESTALE",
   256  	"ETIMEDOUT",
   257  	"ETXTBSY",
   258  	"EXDEV",
   259  	"ENOTCAPABLE",
   260  }
   261  
   262  // ToErrno coerces the error to a WASI Errno.
   263  //
   264  // Note: Coercion isn't centralized in sys.FSContext because ABI use different
   265  // error codes. For example, wasi-filesystem and GOOS=js don't map to these
   266  // Errno.
   267  func ToErrno(errno sys.Errno) Errno {
   268  	switch errno {
   269  	case 0:
   270  		return ErrnoSuccess
   271  	case sys.EACCES:
   272  		return ErrnoAcces
   273  	case sys.EAGAIN:
   274  		return ErrnoAgain
   275  	case sys.EBADF:
   276  		return ErrnoBadf
   277  	case sys.EEXIST:
   278  		return ErrnoExist
   279  	case sys.EFAULT:
   280  		return ErrnoFault
   281  	case sys.EINTR:
   282  		return ErrnoIntr
   283  	case sys.EINVAL:
   284  		return ErrnoInval
   285  	case sys.EIO:
   286  		return ErrnoIo
   287  	case sys.EISDIR:
   288  		return ErrnoIsdir
   289  	case sys.ELOOP:
   290  		return ErrnoLoop
   291  	case sys.ENAMETOOLONG:
   292  		return ErrnoNametoolong
   293  	case sys.ENOENT:
   294  		return ErrnoNoent
   295  	case sys.ENOSYS:
   296  		return ErrnoNosys
   297  	case sys.ENOTDIR:
   298  		return ErrnoNotdir
   299  	case sys.ERANGE:
   300  		return ErrnoRange
   301  	case sys.ENOTEMPTY:
   302  		return ErrnoNotempty
   303  	case sys.ENOTSOCK:
   304  		return ErrnoNotsock
   305  	case sys.ENOTSUP:
   306  		return ErrnoNotsup
   307  	case sys.EPERM:
   308  		return ErrnoPerm
   309  	case sys.EROFS:
   310  		return ErrnoRofs
   311  	default:
   312  		return ErrnoIo
   313  	}
   314  }