-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[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
Changes from 1 commit
50bf5e9
5a8891a
17d7626
cbc1c33
0caed2f
6cdfe53
db96b76
6a181ef
09f8671
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 | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -153,6 +153,19 @@ add_entrypoint_object( | |||||||||||||
| libc.src.errno.errno | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
| add_entrypoint_object( | ||||||||||||||
| remap_file_pages | ||||||||||||||
| SRCS | ||||||||||||||
| remap_file_pages.cpp.cpp | ||||||||||||||
| HDRS | ||||||||||||||
| ../remap_file_pages.h.h | ||||||||||||||
|
||||||||||||||
| remap_file_pages.cpp.cpp | |
| HDRS | |
| ../remap_file_pages.h.h | |
| remap_file_pages.cpp | |
| HDRS | |
| ../remap_file_pages.h |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,44 @@ | ||||||
| //===----- Linux implementation of the POSIX 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/sys/mman/remap_file_pages.h> | |
| #include "src/sys/mman/remap_file_pages.h" |
Outdated
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: since this is a linux specific function you don't need this comment.
Outdated
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.
there's only one possible syscall here, so you can move the syscall into this ifdef like this (though you may need to reformat the code):
| long syscall_number = SYS_remap_file_pages; | |
| int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_remap_file_pages, reinterpret_cast<long>(addr), size, prot, pgoff, flags); |
Outdated
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: needs ending newline
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,21 @@ | ||||||
| //===-- 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 <sys/mman.h> | ||||||
|
||||||
| #include <sys/mman.h> | |
| #include <stddef.h> |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -128,6 +128,23 @@ add_libc_unittest( | |||||||
| libc.test.UnitTest.ErrnoSetterMatcher | ||||||||
| ) | ||||||||
|
|
||||||||
| add_libc_unittest( | ||||||||
| remap_file_pages_test | ||||||||
| SUITE | ||||||||
| libc_sys_mman_unittests | ||||||||
| SRCS | ||||||||
| remap_file_pages_test.cpp | ||||||||
| DEPENDS | ||||||||
| libc.include.sys_mman | ||||||||
|
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.
Suggested change
|
||||||||
| libc.src.unistd.sysconf | ||||||||
| libc.test.UnitTest.Test | ||||||||
| libc.test.UnitTest.ErrnoSetterMatcher | ||||||||
| libc.src.sys.mman.remap_file_pages | ||||||||
| libc.src.errno.errno | ||||||||
| libc.src.sys.mman.mmap | ||||||||
| libc.src.sys.mman.munmap | ||||||||
| ) | ||||||||
|
|
||||||||
| add_libc_unittest( | ||||||||
| shm_test | ||||||||
| SUITE | ||||||||
|
|
||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| //===-- 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/sys/mman/mmap.h" | ||
| #include "src/sys/mman/munmap.h" | ||
| #include "src/sys/mman/remap_file_pages.h" | ||
| #include "test/UnitTest/ErrnoSetterMatcher.h" | ||
| #include "test/UnitTest/Test.h" | ||
| #include "src/unistd/sysconf.h" | ||
|
|
||
| #include <sys/mman.h> | ||
|
|
||
|
|
||
| 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)); | ||
|
|
||
| // 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_PRIVATE | MAP_ANONYMOUS, -1, 0); | ||
| ASSERT_ERRNO_SUCCESS(); | ||
| EXPECT_NE(addr, MAP_FAILED); | ||
|
|
||
| // Reset error number for the new function | ||
| LIBC_NAMESPACE::libc_errno = 0; | ||
|
||
|
|
||
| // Now try to remap the pages | ||
| EXPECT_THAT(LIBC_NAMESPACE::remap_file_pages(addr, page_size, PROT_READ, page_size, 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()); | ||
| } | ||
|
|
||
| TEST(LlvmLibcRemapFilePagesTest, ErrorInvalidFlags) { | ||
| size_t page_size = sysconf(_SC_PAGESIZE); | ||
| ASSERT_GT(page_size, size_t(0)); | ||
|
|
||
| void *addr = LIBC_NAMESPACE::mmap(nullptr, page_size, PROT_READ | PROT_WRITE, | ||
| MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | ||
| ASSERT_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()); | ||
| } | ||
|
|
||
| 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)); | ||
| } | ||
|
||
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: in other places it's capitalized as
Linux