| 1604 | } |
| 1605 | |
| 1606 | int _mmap_js(size_t length, |
| 1607 | int prot, |
| 1608 | int flags, |
| 1609 | int fd, |
| 1610 | off_t offset, |
| 1611 | int* allocated, |
| 1612 | void** addr) { |
| 1613 | // PROT_EXEC is not supported (although we pretend to support the absence of |
| 1614 | // PROT_READ or PROT_WRITE). |
| 1615 | if ((prot & PROT_EXEC)) { |
| 1616 | return -EPERM; |
| 1617 | } |
| 1618 | |
| 1619 | if (!length) { |
| 1620 | return -EINVAL; |
| 1621 | } |
| 1622 | |
| 1623 | // One of MAP_PRIVATE, MAP_SHARED, or MAP_SHARED_VALIDATE must be used. |
| 1624 | int mapType = flags & MAP_TYPE; |
| 1625 | if (mapType != MAP_PRIVATE && mapType != MAP_SHARED && |
| 1626 | mapType != MAP_SHARED_VALIDATE) { |
| 1627 | return -EINVAL; |
| 1628 | } |
| 1629 | |
| 1630 | if (mapType == MAP_SHARED_VALIDATE) { |
| 1631 | WASMFS_UNREACHABLE("TODO: MAP_SHARED_VALIDATE"); |
| 1632 | } |
| 1633 | |
| 1634 | auto openFile = wasmFS.getFileTable().locked().getEntry(fd); |
| 1635 | if (!openFile) { |
| 1636 | return -EBADF; |
| 1637 | } |
| 1638 | |
| 1639 | std::shared_ptr<DataFile> file; |
| 1640 | |
| 1641 | // Keep the open file info locked only for as long as we need that. |
| 1642 | { |
| 1643 | auto lockedOpenFile = openFile->locked(); |
| 1644 | |
| 1645 | // Check permissions. We always need read permissions, since we need to read |
| 1646 | // the data in the file to map it. |
| 1647 | if ((lockedOpenFile.getFlags() & O_ACCMODE) == O_WRONLY) { |
| 1648 | return -EACCES; |
| 1649 | } |
| 1650 | |
| 1651 | // According to the POSIX spec it is possible to write to a file opened in |
| 1652 | // read-only mode with MAP_PRIVATE flag, as all modifications will be |
| 1653 | // visible only in the memory of the current process. |
| 1654 | if ((prot & PROT_WRITE) != 0 && mapType != MAP_PRIVATE && |
| 1655 | (lockedOpenFile.getFlags() & O_ACCMODE) != O_RDWR) { |
| 1656 | return -EACCES; |
| 1657 | } |
| 1658 | |
| 1659 | file = lockedOpenFile.getFile()->dynCast<DataFile>(); |
| 1660 | } |
| 1661 | |
| 1662 | if (!file) { |
| 1663 | return -ENODEV; |
no test coverage detected