2
Copyright (C) 1999-2007 The Botan Project. All rights reserved.
4
Redistribution and use in source and binary forms, for any use, with or without
5
modification, is permitted provided that the following conditions are met:
7
1. Redistributions of source code must retain the above copyright notice, this
8
list of conditions, and the following disclaimer.
10
2. Redistributions in binary form must reproduce the above copyright notice,
11
this list of conditions, and the following disclaimer in the documentation
12
and/or other materials provided with the distribution.
14
THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) "AS IS" AND ANY EXPRESS OR IMPLIED
15
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
18
IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTOR(S) BE LIABLE FOR ANY DIRECT,
19
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
23
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
namespace QCA { // WRAPNS_LINE
28
/*************************************************
29
* Memory Mapping Allocator Source File *
30
* (C) 1999-2007 The Botan Project *
31
*************************************************/
34
#include <botan/mmap_mem.h>
35
namespace QCA { // WRAPNS_LINE
38
namespace QCA { // WRAPNS_LINE
41
#define _XOPEN_SOURCE 500
44
#ifndef _XOPEN_SOURCE_EXTENDED
45
#define _XOPEN_SOURCE_EXTENDED 1
49
#include <sys/types.h>
50
namespace QCA { // WRAPNS_LINE
53
namespace QCA { // WRAPNS_LINE
56
namespace QCA { // WRAPNS_LINE
59
namespace QCA { // WRAPNS_LINE
62
namespace QCA { // WRAPNS_LINE
65
namespace QCA { // WRAPNS_LINE
75
/*************************************************
76
* MemoryMapping_Allocator Exception *
77
*************************************************/
78
class MemoryMapping_Failed : public Exception
81
MemoryMapping_Failed(const std::string& msg) :
82
Exception("MemoryMapping_Allocator: " + msg) {}
87
/*************************************************
88
* Memory Map a File into Memory *
89
*************************************************/
90
void* MemoryMapping_Allocator::alloc_block(u32bit n)
95
int get_fd() const { return fd; }
96
const std::string path() const { return filepath; }
98
TemporaryFile(const std::string& base)
100
const std::string path = base + "XXXXXX";
102
filepath = new char[path.length() + 1];
103
std::strcpy(filepath, path.c_str());
105
mode_t old_umask = umask(077);
106
fd = mkstemp(filepath);
113
if(fd != -1 && close(fd) == -1)
114
throw MemoryMapping_Failed("Could not close file");
121
TemporaryFile file("/tmp/botan_");
123
if(file.get_fd() == -1)
124
throw MemoryMapping_Failed("Could not create file");
126
if(unlink(file.path().c_str()))
127
throw MemoryMapping_Failed("Could not unlink file " + file.path());
129
lseek(file.get_fd(), n-1, SEEK_SET);
130
if(write(file.get_fd(), "\0", 1) != 1)
131
throw MemoryMapping_Failed("Could not write to file");
133
void* ptr = mmap(0, n, PROT_READ | PROT_WRITE, MAP_SHARED,
136
if(ptr == (void*)MAP_FAILED)
137
throw MemoryMapping_Failed("Could not map file");
142
/*************************************************
143
* Remove a Memory Mapping *
144
*************************************************/
145
void MemoryMapping_Allocator::dealloc_block(void* ptr, u32bit n)
148
#ifdef MLOCK_NOT_VOID_PTR
149
# define MLOCK_TYPE_CAST (char *)
151
# define MLOCK_TYPE_CAST
154
const u32bit OVERWRITE_PASSES = 12;
155
const byte PATTERNS[] = { 0x00, 0xFF, 0xAA, 0x55, 0x73, 0x8C, 0x5F, 0xA0,
156
0x6E, 0x91, 0x30, 0xCF, 0xD3, 0x2C, 0xAC, 0x53 };
158
for(u32bit j = 0; j != OVERWRITE_PASSES; j++)
160
std::memset(ptr, PATTERNS[j % sizeof(PATTERNS)], n);
161
if(msync(MLOCK_TYPE_CAST ptr, n, MS_SYNC))
162
throw MemoryMapping_Failed("Sync operation failed");
164
std::memset(ptr, 0, n);
165
if(msync(MLOCK_TYPE_CAST ptr, n, MS_SYNC))
166
throw MemoryMapping_Failed("Sync operation failed");
168
if(munmap(MLOCK_TYPE_CAST ptr, n))
169
throw MemoryMapping_Failed("Could not unmap file");