Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.sys.mman.munlock
libc.src.sys.mman.munlockall
libc.src.sys.mman.munmap
libc.src.sys.mman.pkey_alloc
libc.src.sys.mman.pkey_free
libc.src.sys.mman.pkey_get
libc.src.sys.mman.pkey_mprotect
libc.src.sys.mman.pkey_set
libc.src.sys.mman.remap_file_pages
libc.src.sys.mman.posix_madvise
libc.src.sys.mman.shm_open
Expand Down
35 changes: 35 additions & 0 deletions libc/include/sys/mman.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,41 @@ functions:
arguments:
- type: void *
- type: size_t
- name: pkey_alloc
standards:
- Linux
return_type: int
arguments:
- type: unsigned int
- type: unsigned int
- name: pkey_free
standards:
- Linux
return_type: int
arguments:
- type: int
- name: pkey_get
standards:
- GNUExtensions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: gnu

return_type: int
arguments:
- type: int
- name: pkey_mprotect
standards:
- Linux
return_type: int
arguments:
- type: void *
- type: size_t
- type: int
- type: int
- name: pkey_set
standards:
- GNUExtensions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: gnu

return_type: int
arguments:
- type: int
- type: unsigned int
- name: posix_madvise
standards:
- POSIX
Expand Down
35 changes: 35 additions & 0 deletions libc/src/sys/mman/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,41 @@ add_entrypoint_object(
.${LIBC_TARGET_OS}.msync
)

add_entrypoint_object(
pkey_alloc
ALIAS
DEPENDS
.${LIBC_TARGET_OS}.pkey_alloc
)

add_entrypoint_object(
pkey_free
ALIAS
DEPENDS
.${LIBC_TARGET_OS}.pkey_free
)

add_entrypoint_object(
pkey_get
ALIAS
DEPENDS
.${LIBC_TARGET_OS}.pkey_get
)

add_entrypoint_object(
pkey_mprotect
ALIAS
DEPENDS
.${LIBC_TARGET_OS}.pkey_mprotect
)

add_entrypoint_object(
pkey_set
ALIAS
DEPENDS
.${LIBC_TARGET_OS}.pkey_set
)

add_entrypoint_object(
remap_file_pages
ALIAS
Expand Down
87 changes: 87 additions & 0 deletions libc/src/sys/mman/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
add_subdirectory(generic)
set(ARCH_SUBDIRECTORY generic)
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_ARCHITECTURE})
add_subdirectory(${LIBC_TARGET_ARCHITECTURE})
set(ARCH_SUBDIRECTORY ${LIBC_TARGET_ARCHITECTURE})
endif()

add_entrypoint_object(
madvise
SRCS
Expand Down Expand Up @@ -50,6 +57,17 @@ add_entrypoint_object(
libc.src.errno.errno
)

add_header_library(
mprotect_common
HDRS
mprotect_common.h
DEPENDS
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
libc.src.__support.error_or
)

add_entrypoint_object(
mprotect
SRCS
Expand All @@ -61,6 +79,7 @@ add_entrypoint_object(
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
.mprotect_common
)

add_entrypoint_object(
Expand Down Expand Up @@ -166,6 +185,74 @@ add_entrypoint_object(
libc.src.errno.errno
)

add_entrypoint_object(
pkey_alloc
SRCS
pkey_alloc.cpp
HDRS
../pkey_alloc.h
DEPENDS
libc.include.sys_mman
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)

add_entrypoint_object(
pkey_free
SRCS
pkey_free.cpp
HDRS
../pkey_free.h
DEPENDS
libc.include.sys_mman
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)

add_entrypoint_object(
pkey_get
SRCS
pkey_get.cpp
HDRS
../pkey_get.h
DEPENDS
libc.include.sys_mman
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
.${ARCH_SUBDIRECTORY}.pkey_common
)

add_entrypoint_object(
pkey_mprotect
SRCS
pkey_mprotect.cpp
HDRS
../pkey_mprotect.h
DEPENDS
libc.include.sys_mman
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
.mprotect_common
)

add_entrypoint_object(
pkey_set
SRCS
pkey_set.cpp
HDRS
../pkey_set.h
DEPENDS
libc.include.sys_mman
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
.${ARCH_SUBDIRECTORY}.pkey_common
)

add_entrypoint_object(
remap_file_pages
SRCS
Expand Down
9 changes: 9 additions & 0 deletions libc/src/sys/mman/linux/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
add_header_library(
pkey_common
HDRS
pkey_common.h
DEPENDS
libc.hdr.errno_macros
libc.src.__support.common
libc.src.__support.error_or
)
31 changes: 31 additions & 0 deletions libc/src/sys/mman/linux/generic/pkey_common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//===---------- Generic stub implementations for pkey functionality. ------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_SYS_MMAN_LINUX_GENERIC_PKEY_COMMON_H_
#define LLVM_SYS_MMAN_LINUX_GENERIC_PKEY_COMMON_H_

#include "hdr/errno_macros.h" // For ENOSYS
#include "src/__support/common.h"
#include "src/__support/error_or.h"

namespace LIBC_NAMESPACE_DECL {
namespace pkey_common {

LIBC_INLINE ErrorOr<int> pkey_get([[maybe_unused]] int pkey) {
return Error(ENOSYS);
}

LIBC_INLINE ErrorOr<int> pkey_set([[maybe_unused]] int pkey,
[[maybe_unused]] unsigned int access_rights) {
return Error(ENOSYS);
}

} // namespace pkey_common
} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_SYS_MMAN_LINUX_GENERIC_PKEY_COMMON_H_
18 changes: 7 additions & 11 deletions libc/src/sys/mman/linux/mprotect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,22 @@
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"

#include "src/__support/error_or.h"
#include "src/__support/libc_errno.h"
#include "src/__support/macros/config.h"
#include "src/sys/mman/linux/mprotect_common.h"
#include <sys/syscall.h> // For syscall numbers.

namespace LIBC_NAMESPACE_DECL {

// This function is currently linux only. It has to be refactored suitably if
// mprotect is to be supported on non-linux operating systems also.
LLVM_LIBC_FUNCTION(int, mprotect, (void *addr, size_t size, int prot)) {
int ret = LIBC_NAMESPACE::syscall_impl<int>(
SYS_mprotect, reinterpret_cast<long>(addr), size, prot);

// A negative return value indicates an error with the magnitude of the
// value being the error code.
if (ret < 0) {
libc_errno = -ret;
ErrorOr<int> result =
LIBC_NAMESPACE::mprotect_common::mprotect_impl(addr, size, prot);
if (!result.has_value()) {
libc_errno = result.error();
return -1;
}

return 0;
return result.value();
}

} // namespace LIBC_NAMESPACE_DECL
38 changes: 38 additions & 0 deletions libc/src/sys/mman/linux/mprotect_common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//===---------- Shared Linux implementation of POSIX mprotect. ------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
#include "src/__support/error_or.h"
#include "src/__support/libc_errno.h"
#include "src/__support/macros/attributes.h"
#include "src/__support/macros/config.h"
#include <sys/syscall.h> // For syscall numbers.

namespace LIBC_NAMESPACE_DECL {

namespace mprotect_common {

// This function is currently linux only. It has to be refactored suitably if
// mprotect is to be supported on non-linux operating systems also.
LIBC_INLINE ErrorOr<int> mprotect_impl(void *addr, size_t size, int prot) {
int ret = LIBC_NAMESPACE::syscall_impl<int>(
SYS_mprotect, reinterpret_cast<long>(addr), size, prot);

// A negative return value indicates an error with the magnitude of the
// value being the error code.
if (ret < 0) {
return Error(-ret);
}

return 0;
}

} // namespace mprotect_common

} // namespace LIBC_NAMESPACE_DECL
37 changes: 37 additions & 0 deletions libc/src/sys/mman/linux/pkey_alloc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//===---------- Linux implementation of the Linux pkey_alloc function -----===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/sys/mman/pkey_alloc.h"

#include "hdr/errno_macros.h" // For ENOSYS
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
#include "src/__support/libc_errno.h"
#include "src/__support/macros/config.h"

#include <sys/syscall.h> // For syscall numbers.

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(int, pkey_alloc,
(unsigned int flags, unsigned int access_rights)) {
#if !defined(SYS_pkey_alloc)
libc_errno = ENOSYS;
return -1;
Comment on lines +24 to +25
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

generally for syscall wrappers like this we don't bother with the ENOSYS and leave it as a compile time error when building the library.

#else
int ret =
LIBC_NAMESPACE::syscall_impl<int>(SYS_pkey_alloc, flags, access_rights);
if (ret < 0) {
libc_errno = -ret;
return -1;
}
return static_cast<int>(ret);
#endif
}

} // namespace LIBC_NAMESPACE_DECL
35 changes: 35 additions & 0 deletions libc/src/sys/mman/linux/pkey_free.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//===---------- Linux implementation of the Linux pkey_free function ------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/sys/mman/pkey_free.h"

#include "hdr/errno_macros.h" // For ENOSYS
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
#include "src/__support/libc_errno.h"
#include "src/__support/macros/config.h"

#include <sys/syscall.h> // For syscall numbers.

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(int, pkey_free, (int pkey)) {
#if !defined(SYS_pkey_free)
libc_errno = ENOSYS;
return -1;
#else
int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_pkey_free, pkey);
if (ret < 0) {
libc_errno = -ret;
return -1;
}
return 0;
#endif
}

} // namespace LIBC_NAMESPACE_DECL
Loading
Loading