1//===-- Unittests for shm_open/shm_unlink ---------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "src/__support/OSUtil/syscall.h"
10#include "src/sys/mman/mmap.h"
11#include "src/sys/mman/munmap.h"
12#include "src/sys/mman/shm_open.h"
13#include "src/sys/mman/shm_unlink.h"
14#include "src/unistd/close.h"
15#include "src/unistd/ftruncate.h"
16#include "test/UnitTest/ErrnoSetterMatcher.h"
17#include "test/UnitTest/LibcTest.h"
18#include <asm-generic/fcntl.h>
19#include <sys/syscall.h>
20
21using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
22// since shm_open/shm_unlink are wrappers around open/unlink, we only focus on
23// testing basic cases and name conversions.
24
25TEST(LlvmLibcShmTest, Basic) {
26 const char *name = "/test_shm_open";
27 int fd;
28 ASSERT_THAT(fd = LIBC_NAMESPACE::shm_open(name, O_CREAT | O_RDWR, 0666),
29 returns(GE(0)).with_errno(EQ(0)));
30
31 // check that FD_CLOEXEC is set by default.
32 // TODO: use fcntl when implemented.
33 // https://github.com/llvm/llvm-project/issues/84968
34 long flag = LIBC_NAMESPACE::syscall_impl(SYS_fcntl, arg1: fd, F_GETFD);
35 ASSERT_GE(static_cast<int>(flag), 0);
36 EXPECT_NE(static_cast<int>(flag) & FD_CLOEXEC, 0);
37
38 // allocate space using ftruncate
39 ASSERT_THAT(LIBC_NAMESPACE::ftruncate(fd, 4096), Succeeds());
40 // map the shared memory
41 void *addr = LIBC_NAMESPACE::mmap(addr: nullptr, size: 4096, PROT_READ | PROT_WRITE,
42 MAP_SHARED, fd, offset: 0);
43 ASSERT_NE(addr, MAP_FAILED);
44 // just write random data to the shared memory
45 char data[] = "Despite its name, LLVM has little to do with traditional "
46 "virtual machines.";
47 for (size_t i = 0; i < sizeof(data); ++i)
48 static_cast<char *>(addr)[i] = data[i];
49
50 // close fd does not affect the mapping
51 ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds());
52 for (size_t i = 0; i < sizeof(data); ++i)
53 EXPECT_EQ(static_cast<char *>(addr)[i], data[i]);
54
55 // unmap the shared memory
56 ASSERT_THAT(LIBC_NAMESPACE::munmap(addr, 4096), Succeeds());
57 // remove the shared memory
58 ASSERT_THAT(LIBC_NAMESPACE::shm_unlink(name), Succeeds());
59}
60
61TEST(LlvmLibcShmTest, NameConversion) {
62 const char *name = "////test_shm_open";
63 int fd;
64 ASSERT_THAT(fd = LIBC_NAMESPACE::shm_open(name, O_CREAT | O_RDWR, 0666),
65 returns(GE(0)).with_errno(EQ(0)));
66 ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds());
67 ASSERT_THAT(LIBC_NAMESPACE::shm_unlink(name), Succeeds());
68
69 ASSERT_THAT(LIBC_NAMESPACE::shm_open("/123/123", O_CREAT | O_RDWR, 0666),
70 Fails(EINVAL));
71
72 ASSERT_THAT(LIBC_NAMESPACE::shm_open("/.", O_CREAT | O_RDWR, 0666),
73 Fails(EINVAL));
74
75 ASSERT_THAT(LIBC_NAMESPACE::shm_open("/..", O_CREAT | O_RDWR, 0666),
76 Fails(EINVAL));
77}
78

source code of libc/test/src/sys/mman/linux/shm_test.cpp