From fba84f08b854b61ac65e44d2269667847fd10bcd Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Mon, 19 Apr 2010 20:15:32 +0000 Subject: [PATCH] Small test program that shows Haiku doesn't deal correctly with resizing mmap()ed files. Also reproduces #5473. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@36359 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/tests/system/kernel/Jamfile | 2 + src/tests/system/kernel/mmap_resize_test.cpp | 61 ++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 src/tests/system/kernel/mmap_resize_test.cpp diff --git a/src/tests/system/kernel/Jamfile b/src/tests/system/kernel/Jamfile index 3e50fc40aa..545e4c38f7 100644 --- a/src/tests/system/kernel/Jamfile +++ b/src/tests/system/kernel/Jamfile @@ -47,6 +47,8 @@ SimpleTest port_wakeup_test_7 : port_wakeup_test_7.cpp ; SimpleTest port_wakeup_test_8 : port_wakeup_test_8.cpp ; SimpleTest port_wakeup_test_9 : port_wakeup_test_9.cpp ; +SimpleTest mmap_resize_test : mmap_resize_test.cpp ; + SimpleTest reserved_areas_test : reserved_areas_test.cpp ; SimpleTest select_check : select_check.cpp ; diff --git a/src/tests/system/kernel/mmap_resize_test.cpp b/src/tests/system/kernel/mmap_resize_test.cpp new file mode 100644 index 0000000000..965cb93c72 --- /dev/null +++ b/src/tests/system/kernel/mmap_resize_test.cpp @@ -0,0 +1,61 @@ +/* + * Copyright 2010, Ingo Weinhold, ingo_weinhold@gmx.de. + * Distributed under the terms of the MIT License. + */ + + +#include +#include +#include +#include +#include +#include +#include + + +int +main() +{ + const char* fileName = "/tmp/mmap-resize-test-file"; + + // create file + printf("creating file...\n"); + int fd = open(fileName, O_CREAT | O_RDWR | O_TRUNC, 0644); + if (fd < 0) { + fprintf(stderr, "Failed to open \"%s\": %s\n", fileName, + strerror(errno)); + exit(1); + } + + // write half a page + printf("writing data to file...\n"); + char buffer[2048]; + memset(buffer, 0xdd, sizeof(buffer)); + if (write(fd, buffer, sizeof(buffer)) != (ssize_t)sizeof(buffer)) { + fprintf(stderr, "Failed to write to file!\n"); + exit(1); + } + + // map the page + printf("mapping file...\n"); + void* address = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (address == MAP_FAILED) { + fprintf(stderr, "Failed to map the file: %s\n", strerror(errno)); + exit(1); + } + + // truncate the file + printf("truncating file...\n"); + if (ftruncate(fd, 0) < 0) { + fprintf(stderr, "Failed to truncate the file: %s\n", strerror(errno)); + exit(1); + } + + // touch data + printf("touching no longer mapped data (should fail with SIGBUS)...\n"); + *(int*)address = 42; + + printf("Shouldn't get here!!!\n"); + + return 0; +}