-
Notifications
You must be signed in to change notification settings - Fork 175
feat(starboard): Add prctl wrapper for VMA naming #7308
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
Open
sherryzy
wants to merge
1
commit into
youtube:main
Choose a base branch
from
sherryzy:wrap_prtcl
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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,153 @@ | ||
| // Copyright 2025 The Cobalt Authors. All Rights Reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include "starboard/shared/modular/starboard_layer_posix_prctl_abi_wrappers.h" | ||
|
|
||
| #include <errno.h> | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
| #include <sys/prctl.h> | ||
| #include <unistd.h> | ||
|
|
||
| #include "starboard/common/log.h" | ||
| #include "testing/gtest/include/gtest/gtest.h" | ||
|
|
||
| // From third_party/musl/include/sys/prctl.h | ||
| #ifndef PR_SET_VMA | ||
| #define PR_SET_VMA 0x53564d41 | ||
| #endif | ||
| #ifndef PR_SET_VMA_ANON_NAME | ||
| #define PR_SET_VMA_ANON_NAME 0 | ||
| #endif | ||
|
|
||
| namespace { | ||
|
|
||
| const char kVmaName[] = "TestVmaName"; | ||
|
|
||
| // Checks /proc/self/maps to see if the memory mapping containing |addr| has | ||
| // the specified |name|. This is the expected outcome when the kernel supports | ||
| // PR_SET_VMA_ANON_NAME. | ||
| bool VmaIsNamed(void* addr, const char* name) { | ||
| FILE* fp = fopen("/proc/self/maps", "r"); | ||
| if (!fp) { | ||
| // If we can't open /proc/self/maps, we can't verify. | ||
| // Assume it's not named and let the fallback check proceed. | ||
| return false; | ||
| } | ||
|
|
||
| char line[1024]; | ||
| bool found = false; | ||
| unsigned long target_addr = (unsigned long)addr; | ||
|
|
||
| while (fgets(line, sizeof(line), fp)) { | ||
| unsigned long start, end; | ||
| if (sscanf(line, "%lx-%lx", &start, &end) != 2) { | ||
| continue; | ||
| } | ||
| if (target_addr >= start && target_addr < end) { | ||
| if (strstr(line, name)) { | ||
| found = true; | ||
| } | ||
| // We found the mapping containing our address, so we can stop. | ||
| // If it's not named here, it's not named. | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| fclose(fp); | ||
| return found; | ||
| } | ||
|
|
||
| // Checks for the existence and content of the fallback file. This is the | ||
| // expected outcome when the kernel does NOT support PR_SET_VMA_ANON_NAME. | ||
| bool FallbackFileIsCorrect(unsigned long start, | ||
| unsigned long size, | ||
| const char* name) { | ||
| char file_path[256]; | ||
| snprintf(file_path, sizeof(file_path), "/tmp/cobalt_vma_tags_%d.txt", | ||
| getpid()); | ||
|
|
||
| FILE* fp = fopen(file_path, "r"); | ||
| if (!fp) { | ||
| return false; | ||
| } | ||
|
|
||
| char line[1024]; | ||
| bool found = false; | ||
| while (fgets(line, sizeof(line), fp)) { | ||
| unsigned long file_start, file_end; | ||
| char file_name[256]; | ||
| // The format is "0x%lx 0x%lx %s\n" | ||
| if (sscanf(line, "0x%lx 0x%lx %s", &file_start, &file_end, file_name) == | ||
| 3) { | ||
| if (file_start == start && file_end == start + size && | ||
| strcmp(file_name, name) == 0) { | ||
| found = true; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fclose(fp); | ||
| return found; | ||
| } | ||
|
|
||
| // This test verifies that __abi_wrap_prctl with PR_SET_VMA and | ||
| // PR_SET_VMA_ANON_NAME correctly names a VMA region, either by calling the | ||
| // underlying prctl syscall or by using the fallback mechanism of writing to a | ||
| // file. | ||
| TEST(PosixPrctlTest, SetVmaAnonName) { | ||
| const size_t kMapSize = 4096; | ||
| void* p = malloc(kMapSize); | ||
| ASSERT_NE(p, nullptr); | ||
|
|
||
| // Ensure we have a clean state by deleting any leftover fallback file. | ||
| char file_path[256]; | ||
| snprintf(file_path, sizeof(file_path), "/tmp/cobalt_vma_tags_%d.txt", | ||
| getpid()); | ||
| unlink(file_path); | ||
|
|
||
| int result = | ||
| __abi_wrap_prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, (unsigned long)p, | ||
| kMapSize, (unsigned long)kVmaName); | ||
|
|
||
| // The wrapper should return 0 on success, for both the prctl call and the | ||
| // fallback. | ||
| EXPECT_EQ(result, 0); | ||
|
|
||
| bool vma_is_named = VmaIsNamed(p, kVmaName); | ||
| bool fallback_file_is_correct = | ||
| FallbackFileIsCorrect((unsigned long)p, kMapSize, kVmaName); | ||
|
|
||
| // We expect one of the two mechanisms to have worked. | ||
| EXPECT_TRUE(vma_is_named || fallback_file_is_correct) | ||
| << "VMA name was not set via prctl and fallback file was not created or " | ||
| "is incorrect."; | ||
|
|
||
| if (vma_is_named) { | ||
| SB_LOG(INFO) << "VMA naming with PR_SET_VMA_ANON_NAME is supported by the " | ||
| "kernel."; | ||
| } | ||
| if (fallback_file_is_correct) { | ||
| SB_LOG(INFO) << "VMA naming with PR_SET_VMA_ANON_NAME is NOT supported by " | ||
| "the kernel, fallback was used."; | ||
| } | ||
|
|
||
| free(p); | ||
| // Clean up the fallback file if it was created. | ||
| unlink(file_path); | ||
| } | ||
|
|
||
| } // namespace |
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
37 changes: 37 additions & 0 deletions
37
starboard/shared/modular/cobalt_layer_posix_prctl_abi_wrappers.cc
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,37 @@ | ||
| // Copyright 2025 The Cobalt Authors. All Rights Reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include <stdarg.h> | ||
| #include <sys/prctl.h> | ||
|
|
||
| extern "C" { | ||
|
|
||
| int __abi_wrap_prctl(int option, | ||
| unsigned long arg2, | ||
| unsigned long arg3, | ||
| unsigned long arg4, | ||
| unsigned long arg5); | ||
|
|
||
| int prctl(int option, ...) { | ||
| va_list args; | ||
| va_start(args, option); | ||
| long arg2 = va_arg(args, long); | ||
| long arg3 = va_arg(args, long); | ||
| long arg4 = va_arg(args, long); | ||
| long arg5 = va_arg(args, long); | ||
| va_end(args); | ||
| return __abi_wrap_prctl(option, arg2, arg3, arg4, arg5); | ||
| } | ||
|
|
||
| } // extern "C" |
Oops, something went wrong.
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.