From 68f2081497d733dc3dc292bb69c01d39b9db56d1 Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Thu, 30 Aug 2007 20:39:53 +0000 Subject: [PATCH] Little test for set_area_protection()'s B_READ_AREA -> B_WRITE_AREA behavior. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@22123 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/tests/system/kernel/Jamfile | 4 + .../kernel/set_area_protection_test1.cpp | 116 ++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 src/tests/system/kernel/set_area_protection_test1.cpp diff --git a/src/tests/system/kernel/Jamfile b/src/tests/system/kernel/Jamfile index 5ab6eb468a..fa6e3f58eb 100644 --- a/src/tests/system/kernel/Jamfile +++ b/src/tests/system/kernel/Jamfile @@ -44,6 +44,10 @@ SetSupportedPlatformsForTarget sigint_bug113_test : $(HAIKU_BEOS_COMPATIBLE_PLATFORMS) ; SimpleTest sigint_bug113_test : sigint_bug113_test.cpp ; +SetSupportedPlatformsForTarget set_area_protection_test1 + : $(HAIKU_BEOS_COMPATIBLE_PLATFORMS) ; +SimpleTest set_area_protection_test1 : set_area_protection_test1.cpp ; + SubInclude HAIKU_TOP src tests system kernel cache ; #SubInclude HAIKU_TOP src tests system kernel disk_device_manager ; diff --git a/src/tests/system/kernel/set_area_protection_test1.cpp b/src/tests/system/kernel/set_area_protection_test1.cpp new file mode 100644 index 0000000000..336b18b4dc --- /dev/null +++ b/src/tests/system/kernel/set_area_protection_test1.cpp @@ -0,0 +1,116 @@ +#include +#include +#include +#include +#include + +#include + +static area_id +create_test_area(const char* name, int** address, uint32 protection) +{ + area_id area = create_area(name, (void**)address, B_ANY_ADDRESS, + B_PAGE_SIZE, B_NO_LOCK, protection); + if (area < 0) { + fprintf(stderr, "Error: Failed to create area %s: %s\n", name, + strerror(area)); + exit(1); + } + + return area; +} + + +static area_id +clone_test_area(const char* name, int** address, uint32 protection, + area_id source) +{ + area_id area = clone_area(name, (void**)address, B_ANY_ADDRESS, + protection, source); + if (area < 0) { + fprintf(stderr, "Error: Failed to clone area %s: %s\n", name, + strerror(area)); + exit(1); + } + + return area; +} + + +int +main() +{ + // allocate read-only areas + const int kAreaCount = 3; + area_id areas[kAreaCount]; + int* areaAddresses[kAreaCount]; + areas[0] = create_test_area("area0", &areaAddresses[0], B_READ_AREA); + areas[1] = create_test_area("area1", &areaAddresses[1], B_READ_AREA); + areas[2] = create_test_area("area2", &areaAddresses[2], B_READ_AREA); + + int* area2CloneAddress; + /*area_id area2Clone =*/ clone_test_area("area2clone", &area2CloneAddress, + B_READ_AREA | B_WRITE_AREA, areas[2]); + + for (int i = 0; i < kAreaCount; i++) + printf("parent: areas[%d]: %ld, %p\n", i, areas[i], areaAddresses[i]); + + // fork() + pid_t pid = fork(); + if (pid < 0) { + fprintf(stderr, "Error: Failed to fork(): %s\n", strerror(errno)); + exit(1); + } + + if (pid == 0) { + // child + + // get the IDs of the copied areas + area_id parentAreas[kAreaCount]; + for (int i = 0; i < kAreaCount; i++) { + parentAreas[i] = areas[i]; + areas[i] = area_for(areaAddresses[i]); + } + + for (int i = 0; i < kAreaCount; i++) { + printf("child: areas[%d]: %ld, %p\n", i, areas[i], + areaAddresses[i]); + } + + // clone area 1 + delete_area(areas[1]); + areas[1] = clone_test_area("child:area1", &areaAddresses[1], + B_READ_AREA, parentAreas[1]); + + // clone area 2 + delete_area(areas[2]); + areas[2] = clone_test_area("child:area2", &areaAddresses[2], + B_READ_AREA, parentAreas[2]); + + snooze(400000); + + for (int i = 0; i < kAreaCount; i++) + printf("child: area[%d] contains: %d\n", i, *areaAddresses[i]); + + } else { + // parent + + snooze(200000); + + for (int i = 0; i < kAreaCount; i++) { + status_t error = set_area_protection(areas[i], + B_READ_AREA | B_WRITE_AREA); + if (error == B_OK) { + *areaAddresses[i] = pid; + } else { + fprintf(stderr, "parent: Error: set_area_protection(areas[%d]) " + "failed: %s\n", i, strerror(error)); + } + } + + status_t result; + wait_for_thread(pid, &result); + } + + return 0; +}