[libc-commits] [libc] [libc][sys/shm] Implement shmat syscall wrapper on Linux. (PR #239428)

Alexey Samsonov via libc-commits libc-commits at lists.llvm.org
Thu Nov 5 16:04:55 PST 2026


https://github.com/vonosmas created https://github.com/llvm/llvm-project/pull/239428

shmat is a POSIX function (see [docs](https://pubs.opengroup.org/onlinepubs/9799919799/functions/shmat.html#)) that attaches the shared memory segment to the address space of a calling process.

On Linux, it can be implemented as a simple wrapper around SYS_shmat syscall.

Provide the implementation, and define related macro `SHMLBA` (used for rounding) and `SHM_FAILED` (to signal failed operation) to the `<sys/shm.h>` header.

>From 5200e94c2f51ed4c085d88424f9133f447433513 Mon Sep 17 00:00:00 2001
From: Alexey Samsonov <vonosmas at gmail.com>
Date: Thu, 5 Nov 2026 23:33:57 +0000
Subject: [PATCH 1/3] [libc][sys/shm] Implement shmat syscall wrapper on Linux.

---
 libc/config/linux/aarch64/entrypoints.txt     |  1 +
 libc/config/linux/riscv/entrypoints.txt       |  1 +
 libc/config/linux/x86_64/entrypoints.txt      |  1 +
 libc/hdr/CMakeLists.txt                       |  9 ++
 libc/hdr/sys_shm_macros.h                     | 28 ++++++
 libc/include/llvm-libc-macros/CMakeLists.txt  |  2 +
 .../llvm-libc-macros/linux/sys-shm-macros.h   |  8 ++
 libc/include/sys/shm.yaml                     | 12 +++
 .../linux/syscall_wrappers/CMakeLists.txt     | 12 +++
 .../OSUtil/linux/syscall_wrappers/shmat.h     | 33 +++++++
 libc/src/sys/shm/CMakeLists.txt               |  7 ++
 libc/src/sys/shm/linux/CMakeLists.txt         | 15 +++
 libc/src/sys/shm/linux/shmat.cpp              | 34 +++++++
 libc/src/sys/shm/shmat.h                      | 25 +++++
 libc/test/src/sys/shm/linux/CMakeLists.txt    | 17 ++++
 libc/test/src/sys/shm/linux/shmat_test.cpp    | 92 +++++++++++++++++++
 16 files changed, 297 insertions(+)
 create mode 100644 libc/hdr/sys_shm_macros.h
 create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/shmat.h
 create mode 100644 libc/src/sys/shm/linux/shmat.cpp
 create mode 100644 libc/src/sys/shm/shmat.h
 create mode 100644 libc/test/src/sys/shm/linux/shmat_test.cpp

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index a2e9908423b9fa..b5e6cac1b72d8b 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -345,6 +345,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.sys.sendfile.sendfile
 
     # sys/shm.h entrypoints
+    libc.src.sys.shm.shmat
     libc.src.sys.shm.shmget
 
     # sys/socket.h entrypoints
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index ef482356b604d8..458c695e3aa480 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -383,6 +383,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.sys.sendfile.sendfile
 
     # sys/shm.h entrypoints
+    libc.src.sys.shm.shmat
     libc.src.sys.shm.shmget
 
     # sys/socket.h entrypoints
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 7b8b0292176bfc..25b165d484ce8d 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -388,6 +388,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.sys.sendfile.sendfile
 
     # sys/shm.h entrypoints
+    libc.src.sys.shm.shmat
     libc.src.sys.shm.shmget
 
     # sys/socket.h entrypoints
diff --git a/libc/hdr/CMakeLists.txt b/libc/hdr/CMakeLists.txt
index 1f5bdcf3610976..a268987668fae3 100644
--- a/libc/hdr/CMakeLists.txt
+++ b/libc/hdr/CMakeLists.txt
@@ -261,6 +261,15 @@ add_proxy_header_library(
     libc.include.llvm-libc-macros.sys_sem_macros
 )
 
+add_proxy_header_library(
+  sys_shm_macros
+  HDRS
+    sys_shm_macros.h
+  FULL_BUILD_DEPENDS
+    libc.include.llvm-libc-macros.sys_shm_macros
+    libc.include.stdint
+)
+
 add_header_library(sys_socket_overlay HDRS sys_socket_overlay.h)
 add_proxy_header_library(
   sys_socket_macros
diff --git a/libc/hdr/sys_shm_macros.h b/libc/hdr/sys_shm_macros.h
new file mode 100644
index 00000000000000..225aa698d5093b
--- /dev/null
+++ b/libc/hdr/sys_shm_macros.h
@@ -0,0 +1,28 @@
+//===-- Definition of macros from sys/shm.h -------------------------------===//
+//
+// 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_HDR_SYS_SHM_MACROS_H
+#define LLVM_LIBC_HDR_SYS_SHM_MACROS_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-macros/sys-shm-macros.h"
+
+#else // Overlay mode
+
+#include <stdint.h>
+#include <sys/shm.h>
+
+// Older system sys/shm.h headers may not define POSIX.1-2024 SHM_FAILED yet.
+#ifndef SHM_FAILED
+#define SHM_FAILED (reinterpret_cast<void *>(static_cast<intptr_t>(-1)))
+#endif // SHM_FAILED
+
+#endif // LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_SYS_SHM_MACROS_H
diff --git a/libc/include/llvm-libc-macros/CMakeLists.txt b/libc/include/llvm-libc-macros/CMakeLists.txt
index ab585b96125c50..c10fa399b82883 100644
--- a/libc/include/llvm-libc-macros/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/CMakeLists.txt
@@ -326,6 +326,8 @@ add_macro_header(
   sys_shm_macros
   HDR
     sys-shm-macros.h
+  DEPENDS
+    .stdint_macros
 )
 
 add_macro_header(
diff --git a/libc/include/llvm-libc-macros/linux/sys-shm-macros.h b/libc/include/llvm-libc-macros/linux/sys-shm-macros.h
index fbf4a2d65892eb..55f98da9301caa 100644
--- a/libc/include/llvm-libc-macros/linux/sys-shm-macros.h
+++ b/libc/include/llvm-libc-macros/linux/sys-shm-macros.h
@@ -14,10 +14,18 @@
 #ifndef LLVM_LIBC_MACROS_LINUX_SYS_SHM_MACROS_H
 #define LLVM_LIBC_MACROS_LINUX_SYS_SHM_MACROS_H
 
+#include "../../__llvm-libc-common.h"
+#include "../stdint-macros.h"
+
 // shmat flags
 #define SHM_RDONLY 010000
 #define SHM_RND 020000
 #define SHM_REMAP 040000
 #define SHM_EXEC 0100000
 
+#define SHMLBA 4096
+#define SHM_FAILED                                                             \
+  __LLVM_LIBC_CAST(reinterpret_cast, void *,                                   \
+                   __LLVM_LIBC_CAST(static_cast, intptr_t, -1))
+
 #endif // LLVM_LIBC_MACROS_LINUX_SYS_SHM_MACROS_H
diff --git a/libc/include/sys/shm.yaml b/libc/include/sys/shm.yaml
index 481adeb8b911ae..b48d11b7e652f7 100644
--- a/libc/include/sys/shm.yaml
+++ b/libc/include/sys/shm.yaml
@@ -8,6 +8,10 @@ macros:
     macro_header: sys-shm-macros.h
   - macro_name: SHM_RND
     macro_header: sys-shm-macros.h
+  - macro_name: SHMLBA
+    macro_header: sys-shm-macros.h
+  - macro_name: SHM_FAILED
+    macro_header: sys-shm-macros.h
   - macro_name: SHM_REMAP
     macro_header: sys-shm-macros.h
     standards:
@@ -25,6 +29,14 @@ types:
 enums: []
 objects: []
 functions:
+  - name: shmat
+    standards:
+      - posix
+    return_type: void *
+    arguments:
+      - type: int
+      - type: const void *
+      - type: int
   - name: shmget
     standards:
       - posix
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index c2db5f35eddda3..b7d6242c9e9a63 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -222,6 +222,18 @@ add_header_library(
     libc.src.__support.macros.config
 )
 
+add_header_library(
+  shmat
+  HDRS
+    shmat.h
+  DEPENDS
+    libc.include.sys_syscall
+    libc.src.__support.OSUtil.osutil
+    libc.src.__support.common
+    libc.src.__support.error_or
+    libc.src.__support.macros.config
+)
+
 add_header_library(
   shmget
   HDRS
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/shmat.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/shmat.h
new file mode 100644
index 00000000000000..2060adcce22eea
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/shmat.h
@@ -0,0 +1,33 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Syscall wrapper for shmat.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SHMAT_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SHMAT_H
+
+#include "src/__support/OSUtil/linux/syscall.h" // For syscall_checked
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/config.h"
+#include <sys/syscall.h> // For syscall numbers
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+LIBC_INLINE ErrorOr<void *> shmat(int shmid, const void *shmaddr, int shmflg) {
+  return syscall_checked<void *>(SYS_shmat, shmid, shmaddr, shmflg);
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SHMAT_H
diff --git a/libc/src/sys/shm/CMakeLists.txt b/libc/src/sys/shm/CMakeLists.txt
index ce780f671d1ed2..0b8fecade5d29b 100644
--- a/libc/src/sys/shm/CMakeLists.txt
+++ b/libc/src/sys/shm/CMakeLists.txt
@@ -2,6 +2,13 @@ if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
   add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
 endif()
 
+add_entrypoint_object(
+  shmat
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.shmat
+)
+
 add_entrypoint_object(
   shmget
   ALIAS
diff --git a/libc/src/sys/shm/linux/CMakeLists.txt b/libc/src/sys/shm/linux/CMakeLists.txt
index 5828ad75c8c737..1ed864d2c743fd 100644
--- a/libc/src/sys/shm/linux/CMakeLists.txt
+++ b/libc/src/sys/shm/linux/CMakeLists.txt
@@ -1,3 +1,18 @@
+add_entrypoint_object(
+  shmat
+  SRCS
+    shmat.cpp
+  HDRS
+    ../shmat.h
+  DEPENDS
+    libc.hdr.sys_shm_macros
+    libc.src.__support.OSUtil.linux.syscall_wrappers.shmat
+    libc.src.__support.common
+    libc.src.__support.libc_errno
+    libc.src.__support.macros.config
+    libc.src.errno.errno
+)
+
 add_entrypoint_object(
   shmget
   SRCS
diff --git a/libc/src/sys/shm/linux/shmat.cpp b/libc/src/sys/shm/linux/shmat.cpp
new file mode 100644
index 00000000000000..b3a2b7a35523d6
--- /dev/null
+++ b/libc/src/sys/shm/linux/shmat.cpp
@@ -0,0 +1,34 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Linux implementation of shmat.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/sys/shm/shmat.h"
+
+#include "hdr/sys_shm_macros.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/shmat.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(void *, shmat,
+                   (int shmid, const void *shmaddr, int shmflg)) {
+  ErrorOr<void *> ret = linux_syscalls::shmat(shmid, shmaddr, shmflg);
+  if (!ret) {
+    libc_errno = ret.error();
+    return SHM_FAILED;
+  }
+  return ret.value();
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/sys/shm/shmat.h b/libc/src/sys/shm/shmat.h
new file mode 100644
index 00000000000000..6faa7ee5503a8e
--- /dev/null
+++ b/libc/src/sys/shm/shmat.h
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for shmat.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SYS_SHM_SHMAT_H
+#define LLVM_LIBC_SRC_SYS_SHM_SHMAT_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+void *shmat(int shmid, const void *shmaddr, int shmflg);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SYS_SHM_SHMAT_H
diff --git a/libc/test/src/sys/shm/linux/CMakeLists.txt b/libc/test/src/sys/shm/linux/CMakeLists.txt
index 3dfb8bf9f2c745..67b2d007917d99 100644
--- a/libc/test/src/sys/shm/linux/CMakeLists.txt
+++ b/libc/test/src/sys/shm/linux/CMakeLists.txt
@@ -1,5 +1,22 @@
 add_libc_testsuite(libc-sys-shm-tests)
 
+add_libc_test(
+  shmat_test
+  SUITE
+    libc-sys-shm-tests
+  SRCS
+    shmat_test.cpp
+  DEPENDS
+    libc.hdr.errno_macros
+    libc.hdr.sys_ipc_macros
+    libc.hdr.sys_shm_macros
+    libc.hdr.sys_stat_macros
+    libc.src.sys.shm.shmat
+    libc.src.sys.shm.shmget
+    libc.test.UnitTest.ErrnoCheckingTest
+    libc.test.UnitTest.ErrnoSetterMatcher
+)
+
 add_libc_test(
   shmget_test
   SUITE
diff --git a/libc/test/src/sys/shm/linux/shmat_test.cpp b/libc/test/src/sys/shm/linux/shmat_test.cpp
new file mode 100644
index 00000000000000..1394df8dde8ae8
--- /dev/null
+++ b/libc/test/src/sys/shm/linux/shmat_test.cpp
@@ -0,0 +1,92 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for shmat.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "hdr/sys_ipc_macros.h"
+#include "hdr/sys_shm_macros.h"
+#include "hdr/sys_stat_macros.h"
+#include "src/sys/shm/shmat.h"
+#include "src/sys/shm/shmget.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
+using LlvmLibcShmAtTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcShmAtTest, AttachReadWriteAndReadOnly) {
+  int shmid;
+  ASSERT_THAT(shmid = LIBC_NAMESPACE::shmget(
+                  IPC_PRIVATE, 256, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR),
+              returns(GE(0)).with_errno(EQ(0)));
+
+  // Attach the newly created segment for read-write access.
+  void *rw_addr = LIBC_NAMESPACE::shmat(shmid, nullptr, 0);
+  ASSERT_ERRNO_SUCCESS();
+  ASSERT_NE(rw_addr, SHM_FAILED);
+
+  // Newly created System V shared memory segments are zero-initialized.
+  int *rw_val = static_cast<int *>(rw_addr);
+  EXPECT_EQ(*rw_val, 0);
+  *rw_val = 42;
+
+  // Attach the same segment a second time with SHM_RDONLY and verify that
+  // the modification is visible through the second mapping.
+  void *ro_addr = LIBC_NAMESPACE::shmat(shmid, nullptr, SHM_RDONLY);
+  ASSERT_ERRNO_SUCCESS();
+  ASSERT_NE(ro_addr, SHM_FAILED);
+  EXPECT_NE(ro_addr, rw_addr);
+  EXPECT_EQ(*static_cast<const int *>(ro_addr), 42);
+
+  // TODO: Detach mappings with shmdt and remove the segment with shmctl
+  // once shmdt and shmctl are implemented.
+}
+
+TEST_F(LlvmLibcShmAtTest, UnalignedAddrWithoutShmRndFails) {
+  int shmid;
+  ASSERT_THAT(shmid = LIBC_NAMESPACE::shmget(
+                  IPC_PRIVATE, 256, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR),
+              returns(GE(0)).with_errno(EQ(0)));
+
+  void *addr = LIBC_NAMESPACE::shmat(shmid, nullptr, 0);
+  ASSERT_ERRNO_SUCCESS();
+  ASSERT_NE(addr, SHM_FAILED);
+
+  // Unaligned shmaddr without SHM_RND must fail with EINVAL.
+  EXPECT_THAT(
+      LIBC_NAMESPACE::shmat(shmid, static_cast<const char *>(addr) + 1, 0),
+      Fails(EINVAL, SHM_FAILED));
+}
+
+TEST_F(LlvmLibcShmAtTest, UnalignedAddrWithShmRndRoundsDown) {
+  int shmid;
+  ASSERT_THAT(shmid = LIBC_NAMESPACE::shmget(
+                  IPC_PRIVATE, 256, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR),
+              returns(GE(0)).with_errno(EQ(0)));
+
+  void *addr = LIBC_NAMESPACE::shmat(shmid, nullptr, 0);
+  ASSERT_ERRNO_SUCCESS();
+  ASSERT_NE(addr, SHM_FAILED);
+
+  // With SHM_RND, the unaligned address (addr + 1) is rounded down to the
+  // nearest multiple of SHMLBA (addr). SHM_REMAP replaces the existing mapping
+  // at addr since shmdt is not yet used to detach it first.
+  void *rounded_addr = LIBC_NAMESPACE::shmat(
+      shmid, static_cast<const char *>(addr) + 1, SHM_RND | SHM_REMAP);
+  ASSERT_ERRNO_SUCCESS();
+  EXPECT_EQ(rounded_addr, addr);
+}
+
+TEST_F(LlvmLibcShmAtTest, InvalidShmidReturnsEinval) {
+  EXPECT_THAT(LIBC_NAMESPACE::shmat(-1, nullptr, 0), Fails(EINVAL, SHM_FAILED));
+}

>From 8d50981797e25109f1d3a3ccf7dfa4a7d0cee7ef Mon Sep 17 00:00:00 2001
From: Alexey Samsonov <vonosmas at gmail.com>
Date: Thu, 5 Nov 2026 23:53:12 +0000
Subject: [PATCH 2/3] fixups

---
 libc/hdr/sys_shm_macros.h                  |  7 ++++++-
 libc/test/src/sys/shm/linux/CMakeLists.txt |  1 +
 libc/test/src/sys/shm/linux/shmat_test.cpp | 13 ++++++++-----
 3 files changed, 15 insertions(+), 6 deletions(-)

diff --git a/libc/hdr/sys_shm_macros.h b/libc/hdr/sys_shm_macros.h
index 225aa698d5093b..3a2dcd80a39e96 100644
--- a/libc/hdr/sys_shm_macros.h
+++ b/libc/hdr/sys_shm_macros.h
@@ -1,10 +1,15 @@
-//===-- Definition of macros from sys/shm.h -------------------------------===//
+//===----------------------------------------------------------------------===//
 //
 // 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
 //
 //===----------------------------------------------------------------------===//
+///
+/// \file
+/// Definition of macros from sys/shm.h.
+///
+//===----------------------------------------------------------------------===//
 
 #ifndef LLVM_LIBC_HDR_SYS_SHM_MACROS_H
 #define LLVM_LIBC_HDR_SYS_SHM_MACROS_H
diff --git a/libc/test/src/sys/shm/linux/CMakeLists.txt b/libc/test/src/sys/shm/linux/CMakeLists.txt
index 67b2d007917d99..4eea9d59bc171f 100644
--- a/libc/test/src/sys/shm/linux/CMakeLists.txt
+++ b/libc/test/src/sys/shm/linux/CMakeLists.txt
@@ -8,6 +8,7 @@ add_libc_test(
     shmat_test.cpp
   DEPENDS
     libc.hdr.errno_macros
+    libc.hdr.stdint_proxy
     libc.hdr.sys_ipc_macros
     libc.hdr.sys_shm_macros
     libc.hdr.sys_stat_macros
diff --git a/libc/test/src/sys/shm/linux/shmat_test.cpp b/libc/test/src/sys/shm/linux/shmat_test.cpp
index 1394df8dde8ae8..ba6fe5f959d236 100644
--- a/libc/test/src/sys/shm/linux/shmat_test.cpp
+++ b/libc/test/src/sys/shm/linux/shmat_test.cpp
@@ -12,6 +12,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "hdr/errno_macros.h"
+#include "hdr/stdint_proxy.h"
 #include "hdr/sys_ipc_macros.h"
 #include "hdr/sys_shm_macros.h"
 #include "hdr/sys_stat_macros.h"
@@ -62,10 +63,11 @@ TEST_F(LlvmLibcShmAtTest, UnalignedAddrWithoutShmRndFails) {
   ASSERT_ERRNO_SUCCESS();
   ASSERT_NE(addr, SHM_FAILED);
 
-  // Unaligned shmaddr without SHM_RND must fail with EINVAL.
-  EXPECT_THAT(
-      LIBC_NAMESPACE::shmat(shmid, static_cast<const char *>(addr) + 1, 0),
-      Fails(EINVAL, SHM_FAILED));
+  // Unaligned shmaddr without SHM_RND must fail with EINVAL (even with
+  // SHM_REMAP allowing replacement of the existing mapping at addr).
+  EXPECT_THAT(LIBC_NAMESPACE::shmat(shmid, static_cast<const char *>(addr) + 1,
+                                    SHM_REMAP),
+              Fails(EINVAL, SHM_FAILED));
 }
 
 TEST_F(LlvmLibcShmAtTest, UnalignedAddrWithShmRndRoundsDown) {
@@ -77,13 +79,14 @@ TEST_F(LlvmLibcShmAtTest, UnalignedAddrWithShmRndRoundsDown) {
   void *addr = LIBC_NAMESPACE::shmat(shmid, nullptr, 0);
   ASSERT_ERRNO_SUCCESS();
   ASSERT_NE(addr, SHM_FAILED);
-
+  
   // With SHM_RND, the unaligned address (addr + 1) is rounded down to the
   // nearest multiple of SHMLBA (addr). SHM_REMAP replaces the existing mapping
   // at addr since shmdt is not yet used to detach it first.
   void *rounded_addr = LIBC_NAMESPACE::shmat(
       shmid, static_cast<const char *>(addr) + 1, SHM_RND | SHM_REMAP);
   ASSERT_ERRNO_SUCCESS();
+  EXPECT_EQ(reinterpret_cast<uintptr_t>(rounded_addr) % SHMLBA, uintptr_t(0));
   EXPECT_EQ(rounded_addr, addr);
 }
 

>From 3fb94f8cfda258e53b4e904e21c96d61148b7768 Mon Sep 17 00:00:00 2001
From: Alexey Samsonov <vonosmas at gmail.com>
Date: Fri, 6 Nov 2026 00:01:16 +0000
Subject: [PATCH 3/3] few more fixups

---
 libc/hdr/CMakeLists.txt                    | 1 -
 libc/test/src/sys/shm/linux/shmat_test.cpp | 8 +++++++-
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/libc/hdr/CMakeLists.txt b/libc/hdr/CMakeLists.txt
index a268987668fae3..08c9bbe0a74c94 100644
--- a/libc/hdr/CMakeLists.txt
+++ b/libc/hdr/CMakeLists.txt
@@ -267,7 +267,6 @@ add_proxy_header_library(
     sys_shm_macros.h
   FULL_BUILD_DEPENDS
     libc.include.llvm-libc-macros.sys_shm_macros
-    libc.include.stdint
 )
 
 add_header_library(sys_socket_overlay HDRS sys_socket_overlay.h)
diff --git a/libc/test/src/sys/shm/linux/shmat_test.cpp b/libc/test/src/sys/shm/linux/shmat_test.cpp
index ba6fe5f959d236..e192e2c39beb30 100644
--- a/libc/test/src/sys/shm/linux/shmat_test.cpp
+++ b/libc/test/src/sys/shm/linux/shmat_test.cpp
@@ -68,6 +68,9 @@ TEST_F(LlvmLibcShmAtTest, UnalignedAddrWithoutShmRndFails) {
   EXPECT_THAT(LIBC_NAMESPACE::shmat(shmid, static_cast<const char *>(addr) + 1,
                                     SHM_REMAP),
               Fails(EINVAL, SHM_FAILED));
+
+  // TODO: Detach mapping with shmdt and remove the segment with shmctl
+  // once shmdt and shmctl are implemented.
 }
 
 TEST_F(LlvmLibcShmAtTest, UnalignedAddrWithShmRndRoundsDown) {
@@ -79,7 +82,7 @@ TEST_F(LlvmLibcShmAtTest, UnalignedAddrWithShmRndRoundsDown) {
   void *addr = LIBC_NAMESPACE::shmat(shmid, nullptr, 0);
   ASSERT_ERRNO_SUCCESS();
   ASSERT_NE(addr, SHM_FAILED);
-  
+
   // With SHM_RND, the unaligned address (addr + 1) is rounded down to the
   // nearest multiple of SHMLBA (addr). SHM_REMAP replaces the existing mapping
   // at addr since shmdt is not yet used to detach it first.
@@ -88,6 +91,9 @@ TEST_F(LlvmLibcShmAtTest, UnalignedAddrWithShmRndRoundsDown) {
   ASSERT_ERRNO_SUCCESS();
   EXPECT_EQ(reinterpret_cast<uintptr_t>(rounded_addr) % SHMLBA, uintptr_t(0));
   EXPECT_EQ(rounded_addr, addr);
+
+  // TODO: Detach mapping with shmdt and remove the segment with shmctl
+  // once shmdt and shmctl are implemented.
 }
 
 TEST_F(LlvmLibcShmAtTest, InvalidShmidReturnsEinval) {



More information about the libc-commits mailing list