File: mmap_wrapper.cpp

package info (click to toggle)
mir 2.20.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 20,636 kB
  • sloc: cpp: 174,574; xml: 13,422; ansic: 8,221; python: 1,337; sh: 874; makefile: 216; javascript: 37
file content (94 lines) | stat: -rw-r--r-- 3,024 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * Copyright © Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 or 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/* As suggested by umockdev, _FILE_OFFSET_BITS breaks our open() interposing,
 * as it results in a (platform dependent!) subset of {__open64, __open64_2,
 * __open, __open_2} being defined (not just declared) in the header, causing
 * the build to fail with duplicate definitions.
 */

#include "mir_test_framework/mmap_wrapper.h"
#include "mir_test_framework/interposer_helper.h"

#include <boost/throw_exception.hpp>
#include <dlfcn.h>

namespace mtf = mir_test_framework;

namespace
{
using MmapInterposer = mtf::InterposerHandlers<void*, void *, size_t, int, int, int, off_t>;
using MunmapInterposer = mtf::InterposerHandlers<int, void*, size_t>;
}

mtf::MmapHandlerHandle mtf::add_mmap_handler(MmapHandler handler)
{
    return MmapInterposer::add(std::move(handler));
}

void* mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
{
    if (auto val = MmapInterposer::run(addr, length, prot, flags, fd, offset))
    {
        return *val;
    }

    static void* (*real_mmap)(void *addr, size_t length, int prot, int flags, int fd, off_t offset);

    // Where mmap64 is defined, even if off_t == off64_t on 64-bit platforms, this is still appropriate.
    if (!real_mmap)
    {
        *(void **)(&real_mmap) = dlsym(RTLD_NEXT, "mmap64");
    }

    // Empirically, mmap64 is NOT defined everywhere, but then this is appropriate
    if (!real_mmap)
    {
        *(void **)(&real_mmap) = dlsym(RTLD_NEXT, __func__);
    }

    if (!real_mmap)
    {
        using namespace std::literals::string_literals;
        // Oops! What has gone on here?!
        BOOST_THROW_EXCEPTION((std::runtime_error{"Failed to find mmap() symbol: "s + dlerror()}));
    }
    return (*real_mmap)(addr, length, prot, flags, fd, offset);
}

mtf::MunmapHandlerHandle mtf::add_munmap_handler(MunmapHandler handler)
{
    return MunmapInterposer::add(std::move(handler));
}

int munmap(void *addr, size_t length)
{
    if (auto val = MunmapInterposer::run(addr, length))
    {
        return *val;
    }

    int (*real_munmap)(void *addr, size_t length);
    *(void **)(&real_munmap) = dlsym(RTLD_NEXT, "munmap");

    if (!real_munmap)
    {
        using namespace std::literals::string_literals;
        // Oops! What has gone on here?!
        BOOST_THROW_EXCEPTION((std::runtime_error{"Failed to find munmap() symbol: "s + dlerror()}));
    }
    return (*real_munmap)(addr, length);
}