-
Notifications
You must be signed in to change notification settings - Fork 13.7k
[libc] Add Linux mman extension remap_file_pages. #110307
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
Merged
SchrodingerZhu
merged 9 commits into
llvm:main
from
AlyElashram:users/AlyElashram/linux-mman-extension-remap-file-pages-addition
Oct 14, 2024
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
50bf5e9
- Create remap_file_pages wrapper for the linux sys call.
AlyElashram 5a8891a
fixup! - Create remap_file_pages wrapper for the linux sys call. - Ad…
AlyElashram 17d7626
fixup! - Create remap_file_pages wrapper for the linux sys call. - Ad…
AlyElashram cbc1c33
Remove extra file extensions
AlyElashram 0caed2f
- Remove extra file extensions
AlyElashram 6cdfe53
- Patch Successful Test
AlyElashram db96b76
- Add test file creation
AlyElashram 6a181ef
Change Test file names to be more indicative of the test
AlyElashram 09f8671
Add Systat to header inclusion in unittest CMakeLists
AlyElashram File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
//===------- Linux implementation of the remap_file_pages 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/remap_file_pages.h" | ||
|
||
#include "src/__support/OSUtil/syscall.h" // For internal syscall function. | ||
#include "src/__support/common.h" | ||
#include "src/__support/macros/config.h" | ||
#include "src/errno/libc_errno.h" | ||
#include <sys/syscall.h> // For syscall numbers. | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
LLVM_LIBC_FUNCTION(int, remap_file_pages, | ||
(void *addr, size_t size, int prot, size_t pgoff, | ||
int flags)) { | ||
#ifdef SYS_remap_file_pages | ||
int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_remap_file_pages, | ||
reinterpret_cast<long>(addr), | ||
size, prot, pgoff, flags); | ||
#else | ||
#error "remap_file_pages syscall is not available." | ||
#endif | ||
|
||
// A negative return value indicates an error with the magnitude of the | ||
// value being the error code. | ||
if (ret < 0) { | ||
libc_errno = -ret; | ||
return -1; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
} // namespace LIBC_NAMESPACE_DECL |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//===-- Implementation header for remap_file_pages function -----*- C++ -*-===// | ||
// | ||
// 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_LIBC_SRC_SYS_MMAN_REMAP_FILE_PAGES_H | ||
#define LLVM_LIBC_SRC_SYS_MMAN_REMAP_FILE_PAGES_H | ||
|
||
#include "src/__support/macros/config.h" | ||
#include <stddef.h> | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
int remap_file_pages(void *addr, size_t size, int prot, size_t pgoff, | ||
int flags); | ||
|
||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SRC_SYS_MMAN_REMAP_FILE_PAGES_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
//===-- Unittests for remap_file_pages ------------------------------------===// | ||
// | ||
// 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/errno/libc_errno.h" | ||
#include "src/fcntl/open.h" | ||
#include "src/sys/mman/mmap.h" | ||
#include "src/sys/mman/munmap.h" | ||
#include "src/sys/mman/remap_file_pages.h" | ||
#include "src/unistd/close.h" | ||
#include "src/unistd/sysconf.h" | ||
#include "test/UnitTest/ErrnoSetterMatcher.h" | ||
#include "test/UnitTest/Test.h" | ||
|
||
#include <sys/mman.h> | ||
#include <sys/stat.h> // For S_IRWXU | ||
|
||
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails; | ||
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds; | ||
|
||
TEST(LlvmLibcRemapFilePagesTest, NoError) { | ||
size_t page_size = sysconf(_SC_PAGE_SIZE); | ||
ASSERT_GT(page_size, size_t(0)); | ||
|
||
// Create a file-backed mapping | ||
constexpr const char *file_name = "remap_file_pages.test.noerror"; | ||
auto test_file = libc_make_test_file_path(file_name); | ||
int fd = LIBC_NAMESPACE::open(test_file, O_RDWR | O_CREAT, S_IRWXU); | ||
ASSERT_GT(fd, 0); | ||
|
||
// First, allocate some memory using mmap | ||
size_t alloc_size = 2 * page_size; | ||
LIBC_NAMESPACE::libc_errno = 0; | ||
void *addr = LIBC_NAMESPACE::mmap(nullptr, alloc_size, PROT_READ | PROT_WRITE, | ||
MAP_SHARED, fd, 0); | ||
ASSERT_ERRNO_SUCCESS(); | ||
EXPECT_NE(addr, MAP_FAILED); | ||
|
||
// Now try to remap the pages | ||
EXPECT_THAT(LIBC_NAMESPACE::remap_file_pages(addr, page_size, 0, 1, 0), | ||
Succeeds()); | ||
|
||
// Reset error number for the new function | ||
LIBC_NAMESPACE::libc_errno = 0; | ||
|
||
// Clean up | ||
EXPECT_THAT(LIBC_NAMESPACE::munmap(addr, alloc_size), Succeeds()); | ||
EXPECT_THAT(LIBC_NAMESPACE::close(fd), Succeeds()); | ||
} | ||
|
||
TEST(LlvmLibcRemapFilePagesTest, ErrorInvalidFlags) { | ||
size_t page_size = sysconf(_SC_PAGE_SIZE); | ||
ASSERT_GT(page_size, size_t(0)); | ||
|
||
// Create a file-backed mapping | ||
constexpr const char *file_name = "remap_file_pages.test.error"; | ||
auto test_file = libc_make_test_file_path(file_name); | ||
int fd = LIBC_NAMESPACE::open(test_file, O_RDWR | O_CREAT, S_IRWXU); | ||
ASSERT_GT(fd, 0); | ||
|
||
// First, allocate some memory using mmap | ||
size_t alloc_size = 2 * page_size; | ||
LIBC_NAMESPACE::libc_errno = 0; | ||
void *addr = LIBC_NAMESPACE::mmap(nullptr, alloc_size, PROT_READ | PROT_WRITE, | ||
MAP_SHARED, fd, 0); | ||
ASSERT_ERRNO_SUCCESS(); | ||
EXPECT_NE(addr, MAP_FAILED); | ||
|
||
// Try to remap pages with an invalid flag MAP_PRIVATE | ||
EXPECT_THAT(LIBC_NAMESPACE::remap_file_pages(addr, page_size, PROT_READ, 0, | ||
MAP_PRIVATE), | ||
Fails(EINVAL)); | ||
|
||
// Clean up | ||
EXPECT_THAT(LIBC_NAMESPACE::munmap(addr, page_size), Succeeds()); | ||
EXPECT_THAT(LIBC_NAMESPACE::close(fd), Succeeds()); | ||
} | ||
|
||
TEST(LlvmLibcRemapFilePagesTest, ErrorInvalidAddress) { | ||
size_t page_size = sysconf(_SC_PAGESIZE); | ||
ASSERT_GT(page_size, size_t(0)); | ||
|
||
// Use an address that we haven't mapped | ||
void *invalid_addr = reinterpret_cast<void *>(0x12345000); | ||
|
||
EXPECT_THAT(LIBC_NAMESPACE::remap_file_pages(invalid_addr, page_size, | ||
PROT_READ, 0, 0), | ||
Fails(EINVAL)); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.