Serenity Operating System
1/*
2 * Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <LibTest/TestCase.h>
8#include <errno.h>
9#include <fcntl.h>
10#include <signal.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <sys/mman.h>
15#include <unistd.h>
16
17static u8* shared_ptr = nullptr;
18
19static void shared_non_empty_inode_vmobject_sync_signal_handler(int)
20{
21 auto rc = msync(shared_ptr, 0x1000, MS_ASYNC);
22 EXPECT(rc == 0);
23 rc = munmap(shared_ptr, 0x1000);
24 EXPECT(rc == 0);
25 exit(0);
26}
27
28TEST_CASE(shared_non_empty_inode_vmobject_sync)
29{
30 {
31 struct sigaction new_action {
32 { shared_non_empty_inode_vmobject_sync_signal_handler }, 0, 0
33 };
34 int rc = sigaction(SIGBUS, &new_action, nullptr);
35 VERIFY(rc == 0);
36 }
37 u8 buf[0x1000];
38 memset(buf, 0, sizeof(buf));
39 int fd = open("/tmp/shared_non_empty_msync_test", O_RDWR | O_CREAT, 0644);
40 VERIFY(fd >= 0);
41 auto rc = write(fd, buf, sizeof(buf));
42 VERIFY(rc == sizeof(buf));
43 shared_ptr = (u8*)mmap(nullptr, 0x2000, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 0);
44 EXPECT(shared_ptr != MAP_FAILED);
45 rc = msync(shared_ptr, 0x2000, MS_ASYNC);
46 EXPECT(rc == 0);
47 shared_ptr[0x1001] = 0x1;
48 VERIFY_NOT_REACHED();
49}