-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[libc] add mremap #112804
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
Merged
[libc] add mremap #112804
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| //===---------- Linux implementation of the POSIX mremap 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/mremap.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 <linux/param.h> // For EXEC_PAGESIZE. | ||
| #include <stdarg.h> | ||
| #include <sys/syscall.h> // For syscall numbers. | ||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
|
|
||
| LLVM_LIBC_FUNCTION(void *, mremap, | ||
| (void *old_address, size_t old_size, size_t new_size, | ||
| int flags, ... /* void *new_address */)) { | ||
|
|
||
| long ret = 0; | ||
| void *new_address = nullptr; | ||
| if (flags & MREMAP_FIXED) { | ||
| va_list varargs; | ||
| va_start(varargs, flags); | ||
| new_address = va_arg(varargs, void *); | ||
| va_end(varargs); | ||
| } | ||
| ret = LIBC_NAMESPACE::syscall_impl<long>(SYS_mremap, old_address, old_size, | ||
| new_size, flags, new_address); | ||
|
|
||
| if (ret < 0 && ret > -EXEC_PAGESIZE) { | ||
| libc_errno = static_cast<int>(-ret); | ||
| return MAP_FAILED; | ||
| } | ||
|
|
||
| return reinterpret_cast<void *>(ret); | ||
| } | ||
|
|
||
| } // 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 mremap 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef LLVM_LIBC_SRC_SYS_MMAN_MREMAP_H | ||
| #define LLVM_LIBC_SRC_SYS_MMAN_MREMAP_H | ||
|
|
||
| #include "src/__support/macros/config.h" | ||
| #include <sys/mman.h> // For size_t and off_t | ||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
|
|
||
| void *mremap(void *old_address, size_t old_size, size_t new_size, int flags, | ||
| ... /* void *new_address */); | ||
|
|
||
| } // namespace LIBC_NAMESPACE_DECL | ||
|
|
||
| #endif // LLVM_LIBC_SRC_SYS_MMAN_MREMAP_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,68 @@ | ||
| //===-- Unittests for mremap ----------------------------------------------===// | ||
| // | ||
| // 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/mremap.h" | ||
| #include "src/sys/mman/munmap.h" | ||
| #include "test/UnitTest/ErrnoSetterMatcher.h" | ||
| #include "test/UnitTest/Test.h" | ||
|
|
||
| #include <sys/mman.h> | ||
|
|
||
| using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails; | ||
| using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds; | ||
|
|
||
| TEST(LlvmLibcMremapTest, NoError) { | ||
| size_t initial_size = 128; | ||
| size_t new_size = 256; | ||
| LIBC_NAMESPACE::libc_errno = 0; | ||
|
|
||
| // Allocate memory using mmap. | ||
| void *addr = | ||
| LIBC_NAMESPACE::mmap(nullptr, initial_size, PROT_READ | PROT_WRITE, | ||
| MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); | ||
| ASSERT_ERRNO_SUCCESS(); | ||
| EXPECT_NE(addr, MAP_FAILED); | ||
|
|
||
| int *array = reinterpret_cast<int *>(addr); | ||
| // Writing to the memory should not crash the test. | ||
| array[0] = 123; | ||
| EXPECT_EQ(array[0], 123); | ||
|
|
||
| // Re-map the memory using mremap with an increased size. | ||
| void *new_addr = | ||
| LIBC_NAMESPACE::mremap(addr, initial_size, new_size, MREMAP_MAYMOVE); | ||
| ASSERT_ERRNO_SUCCESS(); | ||
| EXPECT_NE(new_addr, MAP_FAILED); | ||
| EXPECT_EQ(reinterpret_cast<int *>(new_addr)[0], | ||
| 123); // Verify data is preserved. | ||
|
|
||
| // Clean up memory by unmapping it. | ||
| EXPECT_THAT(LIBC_NAMESPACE::munmap(new_addr, new_size), Succeeds()); | ||
| } | ||
|
|
||
| TEST(LlvmLibcMremapTest, Error_InvalidSize) { | ||
| size_t initial_size = 128; | ||
| LIBC_NAMESPACE::libc_errno = 0; | ||
|
|
||
| // Allocate memory using mmap. | ||
| void *addr = | ||
| LIBC_NAMESPACE::mmap(nullptr, initial_size, PROT_READ | PROT_WRITE, | ||
| MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); | ||
| ASSERT_ERRNO_SUCCESS(); | ||
| EXPECT_NE(addr, MAP_FAILED); | ||
|
|
||
| // Attempt to re-map the memory with an invalid new size (0). | ||
| void *new_addr = | ||
| LIBC_NAMESPACE::mremap(addr, initial_size, 0, MREMAP_MAYMOVE); | ||
| EXPECT_THAT(new_addr, Fails(EINVAL, MAP_FAILED)); | ||
|
|
||
| // Clean up the original mapping. | ||
| EXPECT_THAT(LIBC_NAMESPACE::munmap(addr, initial_size), Succeeds()); | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.