Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test mmap #80

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions test/test.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ extern "C" {
#include <math.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <string.h>
Expand Down Expand Up @@ -61,6 +62,17 @@ public:

return !getVersionFunction ? 0 : std::atof(((const char*(*)())getVersionFunction)());
}

static void* AllocatePage() {
void *memory = mmap(nullptr, 4096, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);

if (memory == MAP_FAILED)
throw std::runtime_error(std::string("mmap() failed: ") + strerror(errno));

return memory;
}

static bool DeallocatePage(void *memory) { return munmap(memory, 4096) == 0; }
};

class LibDL {
Expand Down
2 changes: 2 additions & 0 deletions test/testExe.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ int main(int argc, const char *argv[])
if (!test::LibC::WithRandom([](std::size_t random) { std::cout << " Random: " << random << std::endl; }))
std::cout << " Random functions unsupported" << std::endl;

REQUIRE(test::LibC::DeallocatePage(test::LibC::AllocatePage()));

REQUIRE(test::LibC::WithTruncatedFile([](ino_t inode, mode_t mode, uid_t uid, gid_t gid, off_t size, long long m_sec) {
std::cout << " WithFile {inode: " << inode << ", mode: " << std::oct << mode << std::dec << ", uid: " << uid << ", gid: " << gid << ", size: " << size << ", modification time: " << m_sec << "}" << std::endl;
REQUIRE(size == 1);
Expand Down
2 changes: 2 additions & 0 deletions test/testSo.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ extern "C" int runSO(int argc, const char *argv[])
if (!test::LibC::WithRandom([](std::size_t random) { std::cout << " Random: " << random << std::endl; }))
std::cout << " Random functions unsupported" << std::endl;

REQUIRE(test::LibC::DeallocatePage(test::LibC::AllocatePage()));

REQUIRE(test::LibC::WithTruncatedFile([](ino_t inode, mode_t mode, uid_t uid, gid_t gid, off_t size, long long m_sec) {
std::cout << " WithFile {inode: " << inode << ", mode: " << std::oct << mode << std::dec << ", uid: " << uid << ", gid: " << gid << ", size: " << size << ", modification time: " << m_sec << "}" << std::endl;
REQUIRE(size == 1);
Expand Down
Loading