Overview
Comment: | OFSecureData: Add support for preallocating memory
This is useful to allocate secure memory before enabling a sandbox that |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
0487a81ced4c39c74b6dc995a806a9b5 |
User & Date: | js on 2018-11-04 20:11:39 |
Other Links: | manifest | tags |
Context
2018-11-04
| ||
20:28 | ofhash: Use OFSandbox's new unveiling check-in: 0fd8ab483c user: js tags: trunk | |
20:11 | OFSecureData: Add support for preallocating memory check-in: 0487a81ced user: js tags: trunk | |
18:40 | Sandbox ofdns check-in: 605634184d user: js tags: trunk | |
Changes
Modified src/OFSecureData.h from [bc7ed72611] to [f530918539].
︙ | ︙ | |||
39 40 41 42 43 44 45 46 47 48 49 50 51 52 | /*! * @brief Whether OFSecureData is secure, meaning preventing the data from * being swapped out is supported. */ + (bool)isSecure; /*! * @brief Creates a new, autoreleased OFSecureData with count items of item * size 1, all set to zero. * * @param count The number of zero items the OFSecureData should contain * @return A new, autoreleased OFSecureData */ | > > > > > > > > > > > > > > | 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 | /*! * @brief Whether OFSecureData is secure, meaning preventing the data from * being swapped out is supported. */ + (bool)isSecure; /*! * @brief Preallocates the specified number of bytes. * * This is useful to allocate secure memory before enabling a sandbox that does * not allow it anymore. * * @note This may only be called once per thread! * @note Preallocated memory is only available for OFSecureData that is smaller * than a single page! * * @param size The number of bytes to preallocate */ + (void)preallocateMemoryWithSize: (size_t)size; /*! * @brief Creates a new, autoreleased OFSecureData with count items of item * size 1, all set to zero. * * @param count The number of zero items the OFSecureData should contain * @return A new, autoreleased OFSecureData */ |
︙ | ︙ |
Modified src/OFSecureData.m from [431c3bcd83] to [520efa2118].
︙ | ︙ | |||
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | void *map; unsigned char *page; }; #if defined(OF_HAVE_COMPILER_TLS) static thread_local struct page *firstPage = NULL; static thread_local struct page *lastPage = NULL; #elif defined(OF_HAVE_THREADS) static of_tlskey_t firstPageKey, lastPageKey; #else static struct page *firstPage = NULL; static struct page *lastPage = NULL; #endif static void * mapPages(size_t numPages) { size_t pageSize = [OFSystemInfo pageSize]; void *pointer; | > > > > > | 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | void *map; unsigned char *page; }; #if defined(OF_HAVE_COMPILER_TLS) static thread_local struct page *firstPage = NULL; static thread_local struct page *lastPage = NULL; static thread_local struct page **preallocatedPages = NULL; static thread_local size_t numPreallocatedPages = 0; #elif defined(OF_HAVE_THREADS) static of_tlskey_t firstPageKey, lastPageKey; static of_tlskey_t preallocatedPagesKey, numPreallocatedPagesKey; #else static struct page *firstPage = NULL; static struct page *lastPage = NULL; static struct page **preallocatedPages = NULL; static size_t numPreallocatedPages = 0; #endif static void * mapPages(size_t numPages) { size_t pageSize = [OFSystemInfo pageSize]; void *pointer; |
︙ | ︙ | |||
95 96 97 98 99 100 101 | munmap(pointer, numPages * pageSize); #else free(pointer); #endif } static struct page * | | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | munmap(pointer, numPages * pageSize); #else free(pointer); #endif } static struct page * addPage(bool allowPreallocated) { size_t pageSize = [OFSystemInfo pageSize]; size_t mapSize = OF_ROUND_UP_POW2(8, pageSize / CHUNK_SIZE) / 8; struct page *page; #if !defined(OF_HAVE_COMPILER_TLS) && defined(OF_HAVE_THREADS) struct page *lastPage; #endif if (allowPreallocated) { #if !defined(OF_HAVE_COMPILER_TLS) && defined(OF_HAVE_THREADS) uintptr_t numPreallocatedPages = (uintptr_t)of_tlskey_get(numPreallocatedPagesKey); #endif if (numPreallocatedPages > 0) { #if !defined(OF_HAVE_COMPILER_TLS) && defined(OF_HAVE_THREADS) struct page **preallocatedPages = of_tlskey_get(preallocatedPagesKey); #endif numPreallocatedPages--; #if !defined(OF_HAVE_COMPILER_TLS) && defined(OF_HAVE_THREADS) OF_ENSURE(of_tlskey_set(numPreallocatedPagesKey, (void *)numPreallocatedPages)); #endif page = preallocatedPages[numPreallocatedPages]; if (numPreallocatedPages == 0) { free(preallocatedPages); preallocatedPages = NULL; #if !defined(OF_HAVE_COMPILER_TLS) && defined(OF_HAVE_THREADS) OF_ENSURE(of_tlskey_set(preallocatedPagesKey, preallocatedPages)); #endif } return page; } } if ((page = malloc(sizeof(*page))) == NULL) @throw [OFOutOfMemoryException exceptionWithRequestedSize: sizeof(*page)]; if ((page->map = calloc(1, mapSize)) == NULL) @throw [OFOutOfMemoryException |
︙ | ︙ | |||
227 228 229 230 231 232 233 | @implementation OFSecureData #if !defined(OF_HAVE_COMPILER_TLS) && defined(OF_HAVE_THREADS) + (void)initialize { if (self != [OFSecureData class]) return; | | > > | 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | @implementation OFSecureData #if !defined(OF_HAVE_COMPILER_TLS) && defined(OF_HAVE_THREADS) + (void)initialize { if (self != [OFSecureData class]) return; if (!of_tlskey_new(&firstPageKey) || !of_tlskey_new(&lastPageKey) || !of_tlskey_new(&preallocatedPagesKey) || !of_tlskey_new(&numPreallocatedPagesKey)) @throw [OFInitializationFailedException exceptionWithClass: self]; } #endif + (bool)isSecure { |
︙ | ︙ | |||
264 265 266 267 268 269 270 271 272 273 274 275 276 277 | munmap(pointer, pageSize); return isSecure; #else return false; #endif } + (instancetype)dataWithCount: (size_t)count { return [[[self alloc] initWithCount: count] autorelease]; } + (instancetype)dataWithItemSize: (size_t)itemSize | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | munmap(pointer, pageSize); return isSecure; #else return false; #endif } + (void)preallocateMemoryWithSize: (size_t)size { size_t pageSize = [OFSystemInfo pageSize]; size_t numPages = OF_ROUND_UP_POW2(pageSize, size) / pageSize; #if !defined(OF_HAVE_COMPILER_TLS) && defined(OF_HAVE_THREADS) struct page **preallocatedPages = of_tlskey_get(preallocatedPagesKey); size_t numPreallocatedPages; #endif if (preallocatedPages != NULL) @throw [OFInvalidArgumentException exception]; preallocatedPages = calloc(numPages, sizeof(struct page)); if (preallocatedPages == NULL) @throw [OFOutOfMemoryException exceptionWithRequestedSize: numPages * sizeof(struct page)]; #if !defined(OF_HAVE_COMPILER_TLS) && defined(OF_HAVE_THREADS) of_tlskey_set(preallocatedPagesKey, preallocatedPages); #endif for (size_t i = 0; i < numPages; i++) preallocatedPages[i] = addPage(false); numPreallocatedPages = numPages; #if !defined(OF_HAVE_COMPILER_TLS) && defined(OF_HAVE_THREADS) of_tlskey_set(numPreallocatedPagesKey, (void *)(uintptr_t)numPreallocatedPages); #endif } + (instancetype)dataWithCount: (size_t)count { return [[[self alloc] initWithCount: count] autorelease]; } + (instancetype)dataWithItemSize: (size_t)itemSize |
︙ | ︙ | |||
340 341 342 343 344 345 346 | if (_items != NULL) { _page = page; break; } } if (_items == NULL) { | | | 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 | if (_items != NULL) { _page = page; break; } } if (_items == NULL) { _page = addPage(true); _items = allocateMemory(_page, count * itemSize); if (_items == NULL) @throw [OFOutOfMemoryException exceptionWithRequestedSize: count * itemSize]; |
︙ | ︙ |