-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[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
base: main
Are you sure you want to change the base?
Changes from all commits
6eb5a45
7147a2a
50f264e
e67b0c5
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 |
---|---|---|
@@ -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_ | ||
#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; | ||
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. use |
||
return Error(ENOSYS); | ||
} | ||
|
||
LIBC_INLINE ErrorOr<int> pkey_set(int pkey, unsigned int access_rights) { | ||
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. use |
||
(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; | ||
#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); | ||
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. Can this entrypoint dependency be broken up? |
||
} | ||
|
||
#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 |
Uh oh!
There was an error while loading. Please reload this page.