Skip to content

[DRAFT] Wrap prtcl sandbox#10012

Open
Awallky wants to merge 4 commits intoyoutube:mainfrom
Awallky:wrap_prtcl_sandbox
Open

[DRAFT] Wrap prtcl sandbox#10012
Awallky wants to merge 4 commits intoyoutube:mainfrom
Awallky:wrap_prtcl_sandbox

Conversation

@Awallky
Copy link
Copy Markdown
Contributor

@Awallky Awallky commented Apr 14, 2026

Bug: 443798603

sherryzy and others added 3 commits April 10, 2026 22:10
This change introduces a wrapper for the `prctl` system call to support naming Virtual Memory Areas (VMAs) using `PR_SET_VMA_ANON_NAME`.

When the underlying kernel does not support this feature, the wrapper provides a fallback mechanism that writes the VMA information to a temporary file (`/tmp/cobalt_vma_tags_<pid>.txt`). This allows for memory debugging and attribution even on systems without the latest kernel features.

A new NPLB test, `PosixPrctlTest.SetVmaAnonName`, has been added to verify both the direct `prctl` call and the fallback mechanism.

The `prctl` wrapper is now used in various parts of the codebase, including the partition allocator and persistent memory allocator, under the `IS_STARBOARD` build flag.

Bug: 443798603
This change introduces a wrapper for the `prctl` system call to support naming Virtual Memory Areas (VMAs) using `PR_SET_VMA_ANON_NAME`.

When the underlying kernel does not support this feature, the wrapper provides a fallback mechanism that writes the VMA information to a temporary file (`/tmp/cobalt_vma_tags_<pid>.txt`). This allows for memory debugging and attribution even on systems without the latest kernel features.

A new NPLB test, `PosixPrctlTest.SetVmaAnonName`, has been added to verify both the direct `prctl` call and the fallback mechanism.

The `prctl` wrapper is now used in various parts of the codebase, including the partition allocator and persistent memory allocator, under the `IS_STARBOARD` build flag.

Bug: 443798603
@Awallky Awallky marked this pull request as ready for review April 14, 2026 00:05
@Awallky Awallky requested review from a team as code owners April 14, 2026 00:05
@Awallky Awallky requested a review from y4vor April 14, 2026 00:05
@Awallky Awallky changed the title Wrap prtcl sandbox [DRAFT] Wrap prtcl sandbox Apr 14, 2026
@github-actions
Copy link
Copy Markdown
Contributor

🤖 Gemini Suggested Commit Message


starboard: Wrap prctl with VMA naming support

The prctl ABI wrapper for Starboard is updated to correctly handle variadic
arguments for prctl calls. This change primarily introduces support for
PR_SET_VMA with PR_SET_VMA_ANON_NAME, which allows naming of anonymous
memory regions for improved debugging and profiling on Starboard.

A fallback mechanism is implemented for platforms where the kernel does not
natively support PR_SET_VMA_ANON_NAME. In such cases, VMA naming information
is recorded in a temporary file. Additionally, the 'atexit' symbol is now
exported for use by the ELF loader, and comprehensive NPLB tests are added
to verify the new functionality and thread safety.

Bug: 443798603

💡 Pro Tips for a Better Commit Message:

  1. Influence the Result: Want to change the output? You can write custom prompts or instructions directly in the Pull Request description. The model uses that text to generate the message.
  2. Re-run the Generator: Post a comment with: /generate-commit-message

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request implements support for PR_SET_VMA and PR_SET_VMA_ANON_NAME on the Starboard platform, updating memory management components and the prctl ABI wrapper to include a file-based fallback mechanism for VMA naming. Feedback identifies a potential undefined behavior risk when handling NULL pointers in snprintf, suggests improved error checking for system calls like snprintf and write, and recommends using mmap instead of malloc in tests to ensure reliable VMA tagging verification.

Comment on lines +132 to +133
int len = snprintf(buf, sizeof(buf), "0x%lx 0x%lx %s\n", arg3,
arg3 + arg4, reinterpret_cast<const char*>(arg5));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The arg5 parameter for PR_SET_VMA_ANON_NAME can be NULL to clear a previously set name. Passing a NULL pointer to snprintf with the %s format specifier is undefined behavior and can lead to crashes on some platforms. A check should be added to handle this case safely.

        int len = snprintf(buf, sizeof(buf), "0x%lx 0x%lx %s\n", arg3,
                           arg3 + arg4, arg5 ? reinterpret_cast<const char*>(arg5) : "");

Comment on lines +110 to +111
snprintf(path_template, sizeof(path_template), "%s/%s_XXXXXX.txt",
GetTempDir(), kVmaTagsFileNamePrefix);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The return value of snprintf should be checked to ensure the path was not truncated. If truncation occurs, the XXXXXX placeholder required by mkstemps might be incomplete, causing the file creation to fail.

      int written = snprintf(path_template, sizeof(path_template), "%s/%s_XXXXXX.txt",
                             GetTempDir(), kVmaTagsFileNamePrefix);
      if (written < 0 || written >= static_cast<int>(sizeof(path_template))) {
        return -1;
      }

if (len > 0) {
size_t write_len =
std::min(static_cast<size_t>(len), sizeof(buf) - 1);
write(fd, buf, write_len);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The return value of write is ignored. It is recommended to check the return value to handle potential errors and to avoid compiler warnings about unused results.

          if (write(fd, buf, write_len) < 0) {
            close(fd);
            return -1;
          }

#include <string.h>
#include <sys/prctl.h>
#include <sys/resource.h>
#include <sys/types.h>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Include <sys/mman.h> to provide definitions for mmap and munmap, which are better suited for testing PR_SET_VMA than malloc.

#include <sys/mman.h>
#include <sys/types.h>

// or by using the fallback mechanism of writing to a file.
TEST(PosixPrctlTest, SetVmaAnonName) {
const size_t kMapSize = 4096;
void* p = malloc(kMapSize);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

PR_SET_VMA is designed to work with anonymous memory mappings. Memory from malloc might not be a distinct anonymous mapping (e.g., it could be part of the heap's brk segment), which can lead to prctl returning EINVAL. Using mmap ensures a valid target for the test.

  void* p = mmap(nullptr, kMapSize, PROT_READ | PROT_WRITE,
                 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  ASSERT_NE(p, MAP_FAILED);

"the kernel, fallback was used.";
}

free(p);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Use munmap to release memory allocated with mmap.

  munmap(p, kMapSize);

… bugs.

- Fix unsafe variadic implementation in prctl() by using a switch to safely
  extract arguments from va_list.
- Integrate robust VMA tags fallback logic into __abi_wrap_prctl.
- Use a self-contained fallback in posix_prctl_test.cc to ensure correctness
  across all build configurations.
- Use std::mutex for thread safety in the VMA tags fallback.
- Modernize code by replacing all C-style casts with static_cast and
  reinterpret_cast, adhering to the Chromium C++ Style Guide.
- Add PosixPrctlVariadicTest to verify variadic handling and thread safety.
@Awallky Awallky force-pushed the wrap_prtcl_sandbox branch from aceec3f to 2653447 Compare April 14, 2026 20:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants