1 /*************************************************************************
3 * Copyright 2016 Realm Inc.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 **************************************************************************/
19 #ifndef REALM_UTIL_FILE_MAPPER_HPP
20 #define REALM_UTIL_FILE_MAPPER_HPP
22 #include <realm/util/file.hpp>
23 #include <realm/utilities.hpp>
24 #include <realm/util/thread.hpp>
25 #include <realm/util/encrypted_file_mapping.hpp>
30 void* mmap(FileDesc fd, size_t size, File::AccessMode access, size_t offset, const char* encryption_key);
31 void munmap(void* addr, size_t size) noexcept;
32 void* mremap(FileDesc fd, size_t file_offset, void* old_addr, size_t old_size, File::AccessMode a, size_t new_size,
33 const char* encryption_key);
34 void msync(FileDesc fd, void* addr, size_t size);
36 // A function which may be given to encryption_read_barrier. If present, the read barrier is a
37 // a barrier for a full array. If absent, the read barrier is a barrier only for the address
38 // range give as argument. If the barrier is for a full array, it will read the array header
39 // and determine the address range from the header.
40 using HeaderToSize = size_t (*)(const char* addr);
41 class EncryptedFileMapping;
43 #if REALM_ENABLE_ENCRYPTION
46 // This variant allows the caller to obtain direct access to the encrypted file mapping
47 // for optimization purposes.
48 void* mmap(FileDesc fd, size_t size, File::AccessMode access, size_t offset, const char* encryption_key,
49 EncryptedFileMapping*& mapping);
51 void do_encryption_read_barrier(const void* addr, size_t size, HeaderToSize header_to_size,
52 EncryptedFileMapping* mapping);
54 void do_encryption_write_barrier(const void* addr, size_t size, EncryptedFileMapping* mapping);
56 void inline encryption_read_barrier(const void* addr, size_t size, EncryptedFileMapping* mapping,
57 HeaderToSize header_to_size = nullptr)
60 do_encryption_read_barrier(addr, size, header_to_size, mapping);
63 void inline encryption_write_barrier(const void* addr, size_t size, EncryptedFileMapping* mapping)
66 do_encryption_write_barrier(addr, size, mapping);
70 extern util::Mutex& mapping_mutex;
72 inline void do_encryption_read_barrier(const void* addr, size_t size, HeaderToSize header_to_size,
73 EncryptedFileMapping* mapping)
75 UniqueLock lock(mapping_mutex, defer_lock_tag());
76 mapping->read_barrier(addr, size, lock, header_to_size);
79 inline void do_encryption_write_barrier(const void* addr, size_t size, EncryptedFileMapping* mapping)
81 LockGuard lock(mapping_mutex);
82 mapping->write_barrier(addr, size);
87 void inline encryption_read_barrier(const void*, size_t, EncryptedFileMapping*, HeaderToSize header_to_size = nullptr)
89 static_cast<void>(header_to_size);
91 void inline encryption_write_barrier(const void*, size_t)
94 void inline encryption_write_barrier(const void*, size_t, EncryptedFileMapping*)
99 // helpers for encrypted Maps
100 template <typename T>
101 void encryption_read_barrier(File::Map<T>& map, size_t index, size_t num_elements = 1)
103 T* addr = map.get_addr();
104 encryption_read_barrier(addr + index, sizeof(T) * num_elements, map.get_encrypted_mapping());
107 template <typename T>
108 void encryption_write_barrier(File::Map<T>& map, size_t index, size_t num_elements = 1)
110 T* addr = map.get_addr();
111 encryption_write_barrier(addr + index, sizeof(T) * num_elements, map.get_encrypted_mapping());
114 File::SizeType encrypted_size_to_data_size(File::SizeType size) noexcept;
115 File::SizeType data_size_to_encrypted_size(File::SizeType size) noexcept;
117 size_t round_up_to_page_size(size_t size) noexcept;