-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[libc] Implement pkey_alloc/free/get/set/mprotect for x86_64 linux #162362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
6eb5a45
7147a2a
50f264e
e67b0c5
30b1d22
c1f6650
1780027
ee0b312
75b4ab6
45324d7
b37cb4b
2211955
0704290
becf3db
df5632d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
| 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 | ||
|
||
| return_type: int | ||
| arguments: | ||
| - type: int | ||
| - type: unsigned int | ||
| - name: posix_madvise | ||
| standards: | ||
| - POSIX | ||
|
|
||
| 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 | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| //===---------- 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_ | ||
jtstogel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #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(int pkey) { | ||
| (void)pkey; | ||
jtstogel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return Error(ENOSYS); | ||
| } | ||
|
|
||
| LIBC_INLINE ErrorOr<int> pkey_set(int pkey, unsigned int access_rights) { | ||
jtstogel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| (void)pkey; | ||
| (void)access_rights; | ||
| return Error(ENOSYS); | ||
| } | ||
|
|
||
| } // namespace pkey_common | ||
| } // namespace LIBC_NAMESPACE_DECL | ||
|
|
||
| #endif // LLVM_SYS_MMAN_LINUX_GENERIC_PKEY_COMMON_H_ | ||
| 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. generally for syscall wrappers like this we don't bother with the |
||
| #else | ||
| int ret = | ||
| LIBC_NAMESPACE::syscall_impl<int>(SYS_pkey_alloc, flags, access_rights); | ||
| if (ret < 0) { | ||
| libc_errno = static_cast<int>(-ret); | ||
| return -1; | ||
| } | ||
| return static_cast<int>(ret); | ||
| #endif | ||
| } | ||
|
|
||
| } // namespace LIBC_NAMESPACE_DECL | ||
| 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 = static_cast<int>(-ret); | ||
| return -1; | ||
| } | ||
| return 0; | ||
| #endif | ||
| } | ||
|
|
||
| } // namespace LIBC_NAMESPACE_DECL |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| //===---------- Linux implementation of the Linux pkey_mprotect 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_get.h" | ||
|
|
||
| #include "hdr/errno_macros.h" // For ENOSYS | ||
| #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/__support/macros/properties/architectures.h" | ||
|
|
||
| #if defined(LIBC_TARGET_ARCH_IS_X86_64) | ||
| #include "src/sys/mman/linux/x86_64/pkey_common.h" | ||
| #else | ||
| #include "src/sys/mman/linux/generic/pkey_common.h" | ||
| #endif | ||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
|
|
||
| LLVM_LIBC_FUNCTION(int, pkey_get, (int pkey)) { | ||
| ErrorOr<int> ret = LIBC_NAMESPACE::pkey_common::pkey_get(pkey); | ||
| if (!ret.has_value()) { | ||
| libc_errno = ret.error(); | ||
| return -1; | ||
| } | ||
| return ret.value(); | ||
| } | ||
|
|
||
| } // namespace LIBC_NAMESPACE_DECL |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| //===---------- Linux implementation of the Linux pkey_mprotect 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_mprotect.h" | ||
|
|
||
| #include "hdr/errno_macros.h" // For ENOSYS | ||
| #include "hdr/types/size_t.h" | ||
| #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 "src/sys/mman/mprotect.h" | ||
|
|
||
| #include <sys/syscall.h> // For syscall numbers. | ||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
|
|
||
| LLVM_LIBC_FUNCTION(int, pkey_mprotect, | ||
| (void *addr, size_t len, int prot, int pkey)) { | ||
| // Fall back to mprotect if pkey is -1 | ||
| // to maintain compatibility with kernel versions that don't support pkey. | ||
| if (pkey == -1) { | ||
| return LIBC_NAMESPACE::mprotect(addr, len, prot); | ||
|
||
| } | ||
|
|
||
| #if !defined(SYS_pkey_mprotect) | ||
| libc_errno = ENOSYS; | ||
| return -1; | ||
| #else | ||
| int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_pkey_mprotect, addr, len, | ||
| prot, pkey); | ||
| if (ret < 0) { | ||
| libc_errno = -ret; | ||
| return -1; | ||
| } | ||
| return 0; | ||
| #endif | ||
| } | ||
|
|
||
| } // namespace LIBC_NAMESPACE_DECL | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: gnu