aboutsummaryrefslogtreecommitdiffstats
path: root/src/alloc/alloc_mmap
diff options
context:
space:
mode:
authorlloyd <[email protected]>2010-06-17 21:48:55 +0000
committerlloyd <[email protected]>2010-06-17 21:48:55 +0000
commitc06b260b3328c5ce4be44c4f1a88feb55ee3dbc4 (patch)
tree41b05df5982b5b2e8a23b55972263d2172d6a9fd /src/alloc/alloc_mmap
parent0eecae9f21172c0a74ad62acaf77148c94a25be7 (diff)
parent3dde5683f69b9cb9f558bfb18087ce35fbbec78a (diff)
propagate from branch 'net.randombit.botan' (head 294e2082ce9231d6165276e2f2a4153a0116aca3)
to branch 'net.randombit.botan.c++0x' (head 0b695fad10f924601e07b009fcd781191fafcb28)
Diffstat (limited to 'src/alloc/alloc_mmap')
-rw-r--r--src/alloc/alloc_mmap/mmap_mem.cpp27
-rw-r--r--src/alloc/alloc_mmap/mmap_mem.h7
2 files changed, 22 insertions, 12 deletions
diff --git a/src/alloc/alloc_mmap/mmap_mem.cpp b/src/alloc/alloc_mmap/mmap_mem.cpp
index 4a7019ae7..a2059a6ea 100644
--- a/src/alloc/alloc_mmap/mmap_mem.cpp
+++ b/src/alloc/alloc_mmap/mmap_mem.cpp
@@ -6,6 +6,7 @@
*/
#include <botan/internal/mmap_mem.h>
+#include <vector>
#include <cstring>
#include <sys/types.h>
@@ -44,29 +45,38 @@ void* MemoryMapping_Allocator::alloc_block(u32bit n)
{
public:
int get_fd() const { return fd; }
- const std::string path() const { return filepath; }
TemporaryFile(const std::string& base)
{
- const std::string path = base + "XXXXXX";
+ const std::string mkstemp_template = base + "XXXXXX";
- filepath = new char[path.length() + 1];
- std::strcpy(filepath, path.c_str());
+ std::vector<char> filepath(mkstemp_template.begin(),
+ mkstemp_template.end());
+ filepath.push_back(0); // add terminating NULL
mode_t old_umask = ::umask(077);
- fd = ::mkstemp(filepath);
+ fd = ::mkstemp(&filepath[0]);
::umask(old_umask);
+
+ if(fd == -1)
+ throw MemoryMapping_Failed("Temporary file allocation failed");
+
+ if(::unlink(&filepath[0]) != 0)
+ throw MemoryMapping_Failed("Could not unlink temporary file");
}
~TemporaryFile()
{
- delete[] filepath;
+ /*
+ * We can safely close here, because post-mmap the file
+ * will continue to exist until the mmap is unmapped from
+ * our address space upon deallocation.
+ */
if(fd != -1 && ::close(fd) == -1)
throw MemoryMapping_Failed("Could not close file");
}
private:
int fd;
- char* filepath;
};
TemporaryFile file("/tmp/botan_");
@@ -74,9 +84,6 @@ void* MemoryMapping_Allocator::alloc_block(u32bit n)
if(file.get_fd() == -1)
throw MemoryMapping_Failed("Could not create file");
- if(::unlink(file.path().c_str()))
- throw MemoryMapping_Failed("Could not unlink file '" + file.path() + "'");
-
if(::lseek(file.get_fd(), n-1, SEEK_SET) < 0)
throw MemoryMapping_Failed("Could not seek file");
diff --git a/src/alloc/alloc_mmap/mmap_mem.h b/src/alloc/alloc_mmap/mmap_mem.h
index 14caf5db1..890658ebc 100644
--- a/src/alloc/alloc_mmap/mmap_mem.h
+++ b/src/alloc/alloc_mmap/mmap_mem.h
@@ -12,8 +12,11 @@
namespace Botan {
-/*
-* Memory Mapping Allocator
+/**
+* Allocator that uses memory maps backed by disk. We zeroize the map
+* upon deallocation. If swap occurs, the VM will swap to the shared
+* file backing rather than to a swap device, which means we know where
+* it is and can zap it later.
*/
class MemoryMapping_Allocator : public Pooling_Allocator
{