├── .README.tpl ├── .gitattributes ├── .github └── workflows │ └── main.yml ├── .gitignore ├── Cargo.toml ├── LICENSE ├── README.md ├── src ├── lazy_atomic_cell.rs ├── lib.rs ├── pthread_mutex.rs └── windows_mutex.rs └── tests └── global.rs /.README.tpl: -------------------------------------------------------------------------------- 1 | # `{{crate}}` 2 | 3 | {{readme}} 4 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # The README.md is programmatically generated with `cargo readme`. 2 | README.md -diff -merge 3 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: [ main ] 6 | pull_request: 7 | branches: [ main ] 8 | 9 | env: 10 | CARGO_TERM_COLOR: always 11 | 12 | jobs: 13 | test: 14 | strategy: 15 | matrix: 16 | os: ["ubuntu-latest", "windows-latest", "macos-latest"] 17 | runs-on: ${{ matrix.os }} 18 | steps: 19 | - uses: actions/checkout@v2 20 | - name: Build 21 | run: cargo build --verbose 22 | - name: Run tests 23 | run: cargo test --verbose 24 | 25 | readme: 26 | runs-on: ubuntu-latest 27 | steps: 28 | - uses: actions/checkout@v2 29 | - run: cargo install cargo-readme 30 | - run: cargo readme -t .README.tpl > README.md 31 | - run: | 32 | git diff --exit-code || { 33 | echo 34 | echo "============================================================" 35 | echo 36 | echo "README.md is not up to date! Run" 37 | echo 38 | echo " $ cargo readme -t .README.tpl > README.md" 39 | echo 40 | echo "to update it." 41 | exit 1 42 | } 43 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | Cargo.lock 3 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | authors = ["Nick Fitzgerald "] 3 | categories = ["memory-management", "development-tools::profiling"] 4 | description = "A shuffling allocator, randomizing heap object locations; useful for avoiding accidental cache locality during benchmarking, which can obscure performance evaluation." 5 | documentation = "https://docs.rs/shuffling-allocator" 6 | edition = "2018" 7 | license = "MPL-2.0" 8 | name = "shuffling-allocator" 9 | readme = "./README.md" 10 | repository = "https://github.com/fitzgen/shuffling-allocator" 11 | version = "1.1.2" 12 | 13 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 14 | 15 | [dependencies] 16 | rand = "0.8.2" 17 | cfg-if = "1.0.0" 18 | 19 | [target.'cfg(unix)'.dependencies.libc] 20 | default-features = false 21 | version = "0.2" 22 | 23 | [target.'cfg(target_os = "windows")'.dependencies.winapi] 24 | version = "0.3" 25 | features = ["synchapi"] 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Mozilla Public License Version 2.0 2 | ================================== 3 | 4 | 1. Definitions 5 | -------------- 6 | 7 | 1.1. "Contributor" 8 | means each individual or legal entity that creates, contributes to 9 | the creation of, or owns Covered Software. 10 | 11 | 1.2. "Contributor Version" 12 | means the combination of the Contributions of others (if any) used 13 | by a Contributor and that particular Contributor's Contribution. 14 | 15 | 1.3. "Contribution" 16 | means Covered Software of a particular Contributor. 17 | 18 | 1.4. "Covered Software" 19 | means Source Code Form to which the initial Contributor has attached 20 | the notice in Exhibit A, the Executable Form of such Source Code 21 | Form, and Modifications of such Source Code Form, in each case 22 | including portions thereof. 23 | 24 | 1.5. "Incompatible With Secondary Licenses" 25 | means 26 | 27 | (a) that the initial Contributor has attached the notice described 28 | in Exhibit B to the Covered Software; or 29 | 30 | (b) that the Covered Software was made available under the terms of 31 | version 1.1 or earlier of the License, but not also under the 32 | terms of a Secondary License. 33 | 34 | 1.6. "Executable Form" 35 | means any form of the work other than Source Code Form. 36 | 37 | 1.7. "Larger Work" 38 | means a work that combines Covered Software with other material, in 39 | a separate file or files, that is not Covered Software. 40 | 41 | 1.8. "License" 42 | means this document. 43 | 44 | 1.9. "Licensable" 45 | means having the right to grant, to the maximum extent possible, 46 | whether at the time of the initial grant or subsequently, any and 47 | all of the rights conveyed by this License. 48 | 49 | 1.10. "Modifications" 50 | means any of the following: 51 | 52 | (a) any file in Source Code Form that results from an addition to, 53 | deletion from, or modification of the contents of Covered 54 | Software; or 55 | 56 | (b) any new file in Source Code Form that contains any Covered 57 | Software. 58 | 59 | 1.11. "Patent Claims" of a Contributor 60 | means any patent claim(s), including without limitation, method, 61 | process, and apparatus claims, in any patent Licensable by such 62 | Contributor that would be infringed, but for the grant of the 63 | License, by the making, using, selling, offering for sale, having 64 | made, import, or transfer of either its Contributions or its 65 | Contributor Version. 66 | 67 | 1.12. "Secondary License" 68 | means either the GNU General Public License, Version 2.0, the GNU 69 | Lesser General Public License, Version 2.1, the GNU Affero General 70 | Public License, Version 3.0, or any later versions of those 71 | licenses. 72 | 73 | 1.13. "Source Code Form" 74 | means the form of the work preferred for making modifications. 75 | 76 | 1.14. "You" (or "Your") 77 | means an individual or a legal entity exercising rights under this 78 | License. For legal entities, "You" includes any entity that 79 | controls, is controlled by, or is under common control with You. For 80 | purposes of this definition, "control" means (a) the power, direct 81 | or indirect, to cause the direction or management of such entity, 82 | whether by contract or otherwise, or (b) ownership of more than 83 | fifty percent (50%) of the outstanding shares or beneficial 84 | ownership of such entity. 85 | 86 | 2. License Grants and Conditions 87 | -------------------------------- 88 | 89 | 2.1. Grants 90 | 91 | Each Contributor hereby grants You a world-wide, royalty-free, 92 | non-exclusive license: 93 | 94 | (a) under intellectual property rights (other than patent or trademark) 95 | Licensable by such Contributor to use, reproduce, make available, 96 | modify, display, perform, distribute, and otherwise exploit its 97 | Contributions, either on an unmodified basis, with Modifications, or 98 | as part of a Larger Work; and 99 | 100 | (b) under Patent Claims of such Contributor to make, use, sell, offer 101 | for sale, have made, import, and otherwise transfer either its 102 | Contributions or its Contributor Version. 103 | 104 | 2.2. Effective Date 105 | 106 | The licenses granted in Section 2.1 with respect to any Contribution 107 | become effective for each Contribution on the date the Contributor first 108 | distributes such Contribution. 109 | 110 | 2.3. Limitations on Grant Scope 111 | 112 | The licenses granted in this Section 2 are the only rights granted under 113 | this License. No additional rights or licenses will be implied from the 114 | distribution or licensing of Covered Software under this License. 115 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 116 | Contributor: 117 | 118 | (a) for any code that a Contributor has removed from Covered Software; 119 | or 120 | 121 | (b) for infringements caused by: (i) Your and any other third party's 122 | modifications of Covered Software, or (ii) the combination of its 123 | Contributions with other software (except as part of its Contributor 124 | Version); or 125 | 126 | (c) under Patent Claims infringed by Covered Software in the absence of 127 | its Contributions. 128 | 129 | This License does not grant any rights in the trademarks, service marks, 130 | or logos of any Contributor (except as may be necessary to comply with 131 | the notice requirements in Section 3.4). 132 | 133 | 2.4. Subsequent Licenses 134 | 135 | No Contributor makes additional grants as a result of Your choice to 136 | distribute the Covered Software under a subsequent version of this 137 | License (see Section 10.2) or under the terms of a Secondary License (if 138 | permitted under the terms of Section 3.3). 139 | 140 | 2.5. Representation 141 | 142 | Each Contributor represents that the Contributor believes its 143 | Contributions are its original creation(s) or it has sufficient rights 144 | to grant the rights to its Contributions conveyed by this License. 145 | 146 | 2.6. Fair Use 147 | 148 | This License is not intended to limit any rights You have under 149 | applicable copyright doctrines of fair use, fair dealing, or other 150 | equivalents. 151 | 152 | 2.7. Conditions 153 | 154 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 155 | in Section 2.1. 156 | 157 | 3. Responsibilities 158 | ------------------- 159 | 160 | 3.1. Distribution of Source Form 161 | 162 | All distribution of Covered Software in Source Code Form, including any 163 | Modifications that You create or to which You contribute, must be under 164 | the terms of this License. You must inform recipients that the Source 165 | Code Form of the Covered Software is governed by the terms of this 166 | License, and how they can obtain a copy of this License. You may not 167 | attempt to alter or restrict the recipients' rights in the Source Code 168 | Form. 169 | 170 | 3.2. Distribution of Executable Form 171 | 172 | If You distribute Covered Software in Executable Form then: 173 | 174 | (a) such Covered Software must also be made available in Source Code 175 | Form, as described in Section 3.1, and You must inform recipients of 176 | the Executable Form how they can obtain a copy of such Source Code 177 | Form by reasonable means in a timely manner, at a charge no more 178 | than the cost of distribution to the recipient; and 179 | 180 | (b) You may distribute such Executable Form under the terms of this 181 | License, or sublicense it under different terms, provided that the 182 | license for the Executable Form does not attempt to limit or alter 183 | the recipients' rights in the Source Code Form under this License. 184 | 185 | 3.3. Distribution of a Larger Work 186 | 187 | You may create and distribute a Larger Work under terms of Your choice, 188 | provided that You also comply with the requirements of this License for 189 | the Covered Software. If the Larger Work is a combination of Covered 190 | Software with a work governed by one or more Secondary Licenses, and the 191 | Covered Software is not Incompatible With Secondary Licenses, this 192 | License permits You to additionally distribute such Covered Software 193 | under the terms of such Secondary License(s), so that the recipient of 194 | the Larger Work may, at their option, further distribute the Covered 195 | Software under the terms of either this License or such Secondary 196 | License(s). 197 | 198 | 3.4. Notices 199 | 200 | You may not remove or alter the substance of any license notices 201 | (including copyright notices, patent notices, disclaimers of warranty, 202 | or limitations of liability) contained within the Source Code Form of 203 | the Covered Software, except that You may alter any license notices to 204 | the extent required to remedy known factual inaccuracies. 205 | 206 | 3.5. Application of Additional Terms 207 | 208 | You may choose to offer, and to charge a fee for, warranty, support, 209 | indemnity or liability obligations to one or more recipients of Covered 210 | Software. However, You may do so only on Your own behalf, and not on 211 | behalf of any Contributor. You must make it absolutely clear that any 212 | such warranty, support, indemnity, or liability obligation is offered by 213 | You alone, and You hereby agree to indemnify every Contributor for any 214 | liability incurred by such Contributor as a result of warranty, support, 215 | indemnity or liability terms You offer. You may include additional 216 | disclaimers of warranty and limitations of liability specific to any 217 | jurisdiction. 218 | 219 | 4. Inability to Comply Due to Statute or Regulation 220 | --------------------------------------------------- 221 | 222 | If it is impossible for You to comply with any of the terms of this 223 | License with respect to some or all of the Covered Software due to 224 | statute, judicial order, or regulation then You must: (a) comply with 225 | the terms of this License to the maximum extent possible; and (b) 226 | describe the limitations and the code they affect. Such description must 227 | be placed in a text file included with all distributions of the Covered 228 | Software under this License. Except to the extent prohibited by statute 229 | or regulation, such description must be sufficiently detailed for a 230 | recipient of ordinary skill to be able to understand it. 231 | 232 | 5. Termination 233 | -------------- 234 | 235 | 5.1. The rights granted under this License will terminate automatically 236 | if You fail to comply with any of its terms. However, if You become 237 | compliant, then the rights granted under this License from a particular 238 | Contributor are reinstated (a) provisionally, unless and until such 239 | Contributor explicitly and finally terminates Your grants, and (b) on an 240 | ongoing basis, if such Contributor fails to notify You of the 241 | non-compliance by some reasonable means prior to 60 days after You have 242 | come back into compliance. Moreover, Your grants from a particular 243 | Contributor are reinstated on an ongoing basis if such Contributor 244 | notifies You of the non-compliance by some reasonable means, this is the 245 | first time You have received notice of non-compliance with this License 246 | from such Contributor, and You become compliant prior to 30 days after 247 | Your receipt of the notice. 248 | 249 | 5.2. If You initiate litigation against any entity by asserting a patent 250 | infringement claim (excluding declaratory judgment actions, 251 | counter-claims, and cross-claims) alleging that a Contributor Version 252 | directly or indirectly infringes any patent, then the rights granted to 253 | You by any and all Contributors for the Covered Software under Section 254 | 2.1 of this License shall terminate. 255 | 256 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 257 | end user license agreements (excluding distributors and resellers) which 258 | have been validly granted by You or Your distributors under this License 259 | prior to termination shall survive termination. 260 | 261 | ************************************************************************ 262 | * * 263 | * 6. Disclaimer of Warranty * 264 | * ------------------------- * 265 | * * 266 | * Covered Software is provided under this License on an "as is" * 267 | * basis, without warranty of any kind, either expressed, implied, or * 268 | * statutory, including, without limitation, warranties that the * 269 | * Covered Software is free of defects, merchantable, fit for a * 270 | * particular purpose or non-infringing. The entire risk as to the * 271 | * quality and performance of the Covered Software is with You. * 272 | * Should any Covered Software prove defective in any respect, You * 273 | * (not any Contributor) assume the cost of any necessary servicing, * 274 | * repair, or correction. This disclaimer of warranty constitutes an * 275 | * essential part of this License. No use of any Covered Software is * 276 | * authorized under this License except under this disclaimer. * 277 | * * 278 | ************************************************************************ 279 | 280 | ************************************************************************ 281 | * * 282 | * 7. Limitation of Liability * 283 | * -------------------------- * 284 | * * 285 | * Under no circumstances and under no legal theory, whether tort * 286 | * (including negligence), contract, or otherwise, shall any * 287 | * Contributor, or anyone who distributes Covered Software as * 288 | * permitted above, be liable to You for any direct, indirect, * 289 | * special, incidental, or consequential damages of any character * 290 | * including, without limitation, damages for lost profits, loss of * 291 | * goodwill, work stoppage, computer failure or malfunction, or any * 292 | * and all other commercial damages or losses, even if such party * 293 | * shall have been informed of the possibility of such damages. This * 294 | * limitation of liability shall not apply to liability for death or * 295 | * personal injury resulting from such party's negligence to the * 296 | * extent applicable law prohibits such limitation. Some * 297 | * jurisdictions do not allow the exclusion or limitation of * 298 | * incidental or consequential damages, so this exclusion and * 299 | * limitation may not apply to You. * 300 | * * 301 | ************************************************************************ 302 | 303 | 8. Litigation 304 | ------------- 305 | 306 | Any litigation relating to this License may be brought only in the 307 | courts of a jurisdiction where the defendant maintains its principal 308 | place of business and such litigation shall be governed by laws of that 309 | jurisdiction, without reference to its conflict-of-law provisions. 310 | Nothing in this Section shall prevent a party's ability to bring 311 | cross-claims or counter-claims. 312 | 313 | 9. Miscellaneous 314 | ---------------- 315 | 316 | This License represents the complete agreement concerning the subject 317 | matter hereof. If any provision of this License is held to be 318 | unenforceable, such provision shall be reformed only to the extent 319 | necessary to make it enforceable. Any law or regulation which provides 320 | that the language of a contract shall be construed against the drafter 321 | shall not be used to construe this License against a Contributor. 322 | 323 | 10. Versions of the License 324 | --------------------------- 325 | 326 | 10.1. New Versions 327 | 328 | Mozilla Foundation is the license steward. Except as provided in Section 329 | 10.3, no one other than the license steward has the right to modify or 330 | publish new versions of this License. Each version will be given a 331 | distinguishing version number. 332 | 333 | 10.2. Effect of New Versions 334 | 335 | You may distribute the Covered Software under the terms of the version 336 | of the License under which You originally received the Covered Software, 337 | or under the terms of any subsequent version published by the license 338 | steward. 339 | 340 | 10.3. Modified Versions 341 | 342 | If you create software not governed by this License, and you want to 343 | create a new license for such software, you may create and use a 344 | modified version of this License if you rename the license and remove 345 | any references to the name of the license steward (except to note that 346 | such modified license differs from this License). 347 | 348 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 349 | Licenses 350 | 351 | If You choose to distribute Source Code Form that is Incompatible With 352 | Secondary Licenses under the terms of this version of the License, the 353 | notice described in Exhibit B of this License must be attached. 354 | 355 | Exhibit A - Source Code Form License Notice 356 | ------------------------------------------- 357 | 358 | This Source Code Form is subject to the terms of the Mozilla Public 359 | License, v. 2.0. If a copy of the MPL was not distributed with this 360 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 361 | 362 | If it is not possible or desirable to put the notice in a particular 363 | file, then You may include the notice in a location (such as a LICENSE 364 | file in a relevant directory) where a recipient would be likely to look 365 | for such a notice. 366 | 367 | You may add additional accurate notices of copyright ownership. 368 | 369 | Exhibit B - "Incompatible With Secondary Licenses" Notice 370 | --------------------------------------------------------- 371 | 372 | This Source Code Form is "Incompatible With Secondary Licenses", as 373 | defined by the Mozilla Public License, v. 2.0. 374 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # `shuffling-allocator` 2 | 3 | A shuffling allocator. 4 | 5 | This crate provides the `ShufflingAllocator` type, which wraps an existing 6 | allocator and shuffles the order of heap allocations it yields, effectively 7 | randomizing the placement of heap allocations. 8 | 9 | Randomizing the locations of heap allocations is useful for testing, 10 | benchmarking, and performance evaluation. It helps you separate the 11 | performance effects of a given code change from accidental heap object 12 | locality and the effects this may have on performance due to memory caches 13 | in the CPU. This is the use case that this crate focuses on. 14 | 15 | While randomizing the locations of heap allocations can also be used for 16 | defense-in-depth security, similar to ASLR, this crate is not written to 17 | support that use case. As a result, this crate may not be the right choice 18 | if your use case is the defense-in-depth security use case. Some trade offs 19 | and design decisions made in this crate's implementation might not be the 20 | choices you want for your use case. 21 | 22 | This crate is inspired by the allocator described in [*Stabilizer: 23 | Statistically Sound Performance Evaluation* by Curtsinger and 24 | Berger](https://people.cs.umass.edu/~emery/pubs/stabilizer-asplos13.pdf ). 25 | 26 | ## How Does It Work? 27 | 28 | An array of available objects for each size class is always 29 | maintained. Allocating a new object involves making the allocation, choosing 30 | a random index in the array, swapping the new allocation for `array[i]` and 31 | returning the swapped out value. Freeing an object is similar: choose a 32 | random index in the array, swap the pointer being freed with `array[i]`, and 33 | then use the underlying allocator to actually free the swapped out 34 | pointer. The larger the array in the shuffling layer, the closer to truly 35 | randomized heap allocations we get, but also the greater the 36 | overhead. Curtsinger and Berger found that arrays of size 256 gave good 37 | randomization for acceptable overhead, and that is also the array size that 38 | this crate uses. 39 | 40 | ## Example 41 | 42 | Wrap the system allocator in a `ShufflingAllocator`, randomizing the 43 | location of the system allocator's heap objects: 44 | 45 | ```rust 46 | use shuffling_allocator::ShufflingAllocator; 47 | use std::alloc::System; 48 | 49 | static SHUFFLED_SYSTEM_ALLOC: ShufflingAllocator = 50 | shuffling_allocator::wrap!(&System); 51 | ``` 52 | -------------------------------------------------------------------------------- /src/lazy_atomic_cell.rs: -------------------------------------------------------------------------------- 1 | use std::{ 2 | alloc::{handle_alloc_error, GlobalAlloc, Layout}, 3 | ptr, 4 | sync::atomic::{AtomicPtr, Ordering}, 5 | }; 6 | 7 | #[doc(hidden)] 8 | pub struct LazyAtomicCell 9 | where 10 | A: 'static + GlobalAlloc, 11 | { 12 | #[doc(hidden)] 13 | pub ptr: AtomicPtr, 14 | #[doc(hidden)] 15 | pub allocator: &'static A, 16 | } 17 | 18 | impl Drop for LazyAtomicCell 19 | where 20 | A: 'static + GlobalAlloc, 21 | { 22 | fn drop(&mut self) { 23 | let p = self.ptr.swap(ptr::null_mut(), Ordering::SeqCst); 24 | if p.is_null() { 25 | return; 26 | } 27 | unsafe { 28 | ptr::drop_in_place(p); 29 | self.allocator.dealloc(p.cast(), Layout::new::()); 30 | } 31 | } 32 | } 33 | 34 | impl LazyAtomicCell 35 | where 36 | A: 'static + GlobalAlloc, 37 | { 38 | /// Create a new `LazyAtomicCell`. 39 | pub fn new(allocator: &'static A) -> Self { 40 | LazyAtomicCell { 41 | ptr: AtomicPtr::new(ptr::null_mut()), 42 | allocator, 43 | } 44 | } 45 | 46 | /// Get the value if it already exists, or create it by calling `init`. 47 | pub fn get_or_create(&self, init: impl FnOnce() -> T) -> &T { 48 | let ptr = self.ptr.load(Ordering::SeqCst); 49 | if !ptr.is_null() { 50 | return unsafe { &*ptr }; 51 | } 52 | 53 | // Allocate space for our `T`. 54 | let layout = Layout::new::(); 55 | let new_ptr = unsafe { self.allocator.alloc(layout).cast::() }; 56 | if new_ptr.is_null() { 57 | handle_alloc_error(layout); 58 | } 59 | 60 | // Initialize our `T`. 61 | unsafe { 62 | ptr::write(new_ptr, init()); 63 | } 64 | 65 | // Attempt to initialize `self.ptr` with our newly allocated and 66 | // initialized `T`. We are racing against other threads to be the first 67 | // to initialize `self.ptr`. 68 | let existing_ptr = self 69 | .ptr 70 | .compare_and_swap(ptr::null_mut(), new_ptr, Ordering::SeqCst); 71 | if existing_ptr.is_null() { 72 | // We won the race! 73 | unsafe { &*new_ptr } 74 | } else { 75 | // We lost the race, so we have to remember to drop and deallocate 76 | // our now-unnecessary `State`. 77 | unsafe { 78 | ptr::drop_in_place(new_ptr); 79 | self.allocator.dealloc(new_ptr.cast(), layout); 80 | &*existing_ptr 81 | } 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | //! A shuffling allocator. 2 | //! 3 | //! This crate provides the `ShufflingAllocator` type, which wraps an existing 4 | //! allocator and shuffles the order of heap allocations it yields, effectively 5 | //! randomizing the placement of heap allocations. 6 | //! 7 | //! Randomizing the locations of heap allocations is useful for testing, 8 | //! benchmarking, and performance evaluation. It helps you separate the 9 | //! performance effects of a given code change from accidental heap object 10 | //! locality and the effects this may have on performance due to memory caches 11 | //! in the CPU. This is the use case that this crate focuses on. 12 | //! 13 | //! While randomizing the locations of heap allocations can also be used for 14 | //! defense-in-depth security, similar to ASLR, this crate is not written to 15 | //! support that use case. As a result, this crate may not be the right choice 16 | //! if your use case is the defense-in-depth security use case. Some trade offs 17 | //! and design decisions made in this crate's implementation might not be the 18 | //! choices you want for your use case. 19 | //! 20 | //! This crate is inspired by the allocator described in [*Stabilizer: 21 | //! Statistically Sound Performance Evaluation* by Curtsinger and 22 | //! Berger](https://people.cs.umass.edu/~emery/pubs/stabilizer-asplos13.pdf ). 23 | //! 24 | //! # How Does It Work? 25 | //! 26 | //! An array of available objects for each size class is always 27 | //! maintained. Allocating a new object involves making the allocation, choosing 28 | //! a random index in the array, swapping the new allocation for `array[i]` and 29 | //! returning the swapped out value. Freeing an object is similar: choose a 30 | //! random index in the array, swap the pointer being freed with `array[i]`, and 31 | //! then use the underlying allocator to actually free the swapped out 32 | //! pointer. The larger the array in the shuffling layer, the closer to truly 33 | //! randomized heap allocations we get, but also the greater the 34 | //! overhead. Curtsinger and Berger found that arrays of size 256 gave good 35 | //! randomization for acceptable overhead, and that is also the array size that 36 | //! this crate uses. 37 | //! 38 | //! # Example 39 | //! 40 | //! Wrap the system allocator in a `ShufflingAllocator`, randomizing the 41 | //! location of the system allocator's heap objects: 42 | //! 43 | //! ``` 44 | //! use shuffling_allocator::ShufflingAllocator; 45 | //! use std::alloc::System; 46 | //! 47 | //! static SHUFFLED_SYSTEM_ALLOC: ShufflingAllocator = 48 | //! shuffling_allocator::wrap!(&System); 49 | //! ``` 50 | 51 | #![deny(missing_docs)] 52 | 53 | mod lazy_atomic_cell; 54 | 55 | cfg_if::cfg_if! { 56 | if #[cfg(unix)] { 57 | mod pthread_mutex; 58 | use pthread_mutex::PthreadMutex as Mutex; 59 | } else if #[cfg(windows)] { 60 | mod windows_mutex; 61 | use windows_mutex::WindowsMutex as Mutex; 62 | } else { 63 | compile_error!("no mutex implementation for this platform"); 64 | } 65 | } 66 | 67 | // This is only public because we can't use type parameters in `const fn` yet, 68 | // so we can't implement `const fn ShufflingAllocator::new`. Don't use this! 69 | #[doc(hidden)] 70 | pub use lazy_atomic_cell::LazyAtomicCell; 71 | 72 | use mem::MaybeUninit; 73 | use rand::{rngs::StdRng, Rng, SeedableRng}; 74 | use std::{ 75 | alloc::{handle_alloc_error, GlobalAlloc, Layout}, 76 | mem, ptr, 77 | sync::atomic::{AtomicPtr, Ordering}, 78 | }; 79 | 80 | const SHUFFLING_ARRAY_SIZE: usize = 256; 81 | 82 | struct ShufflingArray 83 | where 84 | A: 'static + GlobalAlloc, 85 | { 86 | elems: [AtomicPtr; SHUFFLING_ARRAY_SIZE], 87 | size_class: usize, 88 | allocator: &'static A, 89 | } 90 | 91 | impl Drop for ShufflingArray 92 | where 93 | A: 'static + GlobalAlloc, 94 | { 95 | fn drop(&mut self) { 96 | let layout = 97 | unsafe { Layout::from_size_align_unchecked(self.size_class, mem::align_of::()) }; 98 | for el in &self.elems { 99 | let p = el.swap(ptr::null_mut(), Ordering::SeqCst); 100 | if !p.is_null() { 101 | unsafe { 102 | self.allocator.dealloc(p, layout); 103 | } 104 | } 105 | } 106 | } 107 | } 108 | 109 | impl ShufflingArray 110 | where 111 | A: 'static + GlobalAlloc, 112 | { 113 | fn new(size_class: usize, allocator: &'static A) -> Self { 114 | let elems = unsafe { 115 | let mut elems = MaybeUninit::<[AtomicPtr; 256]>::uninit(); 116 | let elems_ptr: *mut [AtomicPtr; 256] = elems.as_mut_ptr(); 117 | let elems_ptr: *mut AtomicPtr = elems_ptr.cast(); 118 | let layout = Layout::from_size_align_unchecked(size_class, mem::align_of::()); 119 | for i in 0..256 { 120 | let p = allocator.alloc(layout); 121 | if p.is_null() { 122 | handle_alloc_error(layout); 123 | } 124 | ptr::write(elems_ptr.offset(i), AtomicPtr::new(p)); 125 | } 126 | elems.assume_init() 127 | }; 128 | ShufflingArray { 129 | elems, 130 | size_class, 131 | allocator, 132 | } 133 | } 134 | 135 | /// Get the layout for this size class, aka the layout for elements within 136 | /// this shuffing array. 137 | fn elem_layout(&self) -> Layout { 138 | unsafe { 139 | debug_assert!( 140 | Layout::from_size_align(self.size_class, mem::align_of::()).is_ok() 141 | ); 142 | Layout::from_size_align_unchecked(self.size_class, mem::align_of::()) 143 | } 144 | } 145 | } 146 | 147 | struct SizeClasses([LazyAtomicCell>; NUM_SIZE_CLASSES]) 148 | where 149 | A: 'static + GlobalAlloc; 150 | 151 | struct SizeClassInfo { 152 | index: usize, 153 | size_class: usize, 154 | } 155 | 156 | #[rustfmt::skip] 157 | #[inline] 158 | fn size_class_info(size: usize) -> Option { 159 | let mut size_class = mem::size_of::(); 160 | let mut stride = mem::size_of::(); 161 | 162 | if size <= size_class { 163 | return Some(SizeClassInfo { index: 0, size_class }); 164 | } 165 | size_class += stride; 166 | if size <= size_class { 167 | return Some(SizeClassInfo { index: 1, size_class }); 168 | } 169 | size_class += stride; 170 | if size <= size_class { 171 | return Some(SizeClassInfo { index: 2, size_class }); 172 | } 173 | size_class += stride; 174 | if size <= size_class { 175 | return Some(SizeClassInfo { index: 3, size_class }); 176 | } 177 | size_class += stride; 178 | 179 | stride = stride * 2; 180 | 181 | if size <= size_class { 182 | return Some(SizeClassInfo { index: 4, size_class }); 183 | } 184 | size_class += stride; 185 | if size <= size_class { 186 | return Some(SizeClassInfo { index: 5, size_class }); 187 | } 188 | size_class += stride; 189 | if size <= size_class { 190 | return Some(SizeClassInfo { index: 6, size_class }); 191 | } 192 | size_class += stride; 193 | if size <= size_class { 194 | return Some(SizeClassInfo { index: 7, size_class }); 195 | } 196 | size_class += stride; 197 | 198 | stride = stride * 2; 199 | 200 | if size <= size_class { 201 | return Some(SizeClassInfo { index: 8, size_class }); 202 | } 203 | size_class += stride; 204 | if size <= size_class { 205 | return Some(SizeClassInfo { index: 9, size_class }); 206 | } 207 | size_class += stride; 208 | if size <= size_class { 209 | return Some(SizeClassInfo { index: 10, size_class }); 210 | } 211 | size_class += stride; 212 | if size <= size_class { 213 | return Some(SizeClassInfo { index: 11, size_class }); 214 | } 215 | size_class += stride; 216 | 217 | stride = stride * 2; 218 | 219 | if size <= size_class { 220 | return Some(SizeClassInfo { index: 12, size_class }); 221 | } 222 | size_class += stride; 223 | if size <= size_class { 224 | return Some(SizeClassInfo { index: 13, size_class }); 225 | } 226 | size_class += stride; 227 | if size <= size_class { 228 | return Some(SizeClassInfo { index: 14, size_class }); 229 | } 230 | size_class += stride; 231 | if size <= size_class { 232 | return Some(SizeClassInfo { index: 15, size_class }); 233 | } 234 | size_class += stride; 235 | 236 | stride = stride * 2; 237 | 238 | if size <= size_class { 239 | return Some(SizeClassInfo { index: 16, size_class }); 240 | } 241 | size_class += stride; 242 | if size <= size_class { 243 | return Some(SizeClassInfo { index: 17, size_class }); 244 | } 245 | size_class += stride; 246 | if size <= size_class { 247 | return Some(SizeClassInfo { index: 18, size_class }); 248 | } 249 | size_class += stride; 250 | if size <= size_class { 251 | return Some(SizeClassInfo { index: 19, size_class }); 252 | } 253 | size_class += stride; 254 | 255 | stride = stride * 2; 256 | 257 | if size <= size_class { 258 | return Some(SizeClassInfo { index: 20, size_class }); 259 | } 260 | size_class += stride; 261 | if size <= size_class { 262 | return Some(SizeClassInfo { index: 21, size_class }); 263 | } 264 | size_class += stride; 265 | if size <= size_class { 266 | return Some(SizeClassInfo { index: 22, size_class }); 267 | } 268 | size_class += stride; 269 | if size <= size_class { 270 | return Some(SizeClassInfo { index: 23, size_class }); 271 | } 272 | size_class += stride; 273 | 274 | stride = stride * 2; 275 | 276 | if size <= size_class { 277 | return Some(SizeClassInfo { index: 24, size_class }); 278 | } 279 | size_class += stride; 280 | if size <= size_class { 281 | return Some(SizeClassInfo { index: 25, size_class }); 282 | } 283 | size_class += stride; 284 | if size <= size_class { 285 | return Some(SizeClassInfo { index: 26, size_class }); 286 | } 287 | size_class += stride; 288 | if size <= size_class { 289 | return Some(SizeClassInfo { index: 27, size_class }); 290 | } 291 | size_class += stride; 292 | 293 | stride = stride * 2; 294 | 295 | if size <= size_class { 296 | return Some(SizeClassInfo { index: 28, size_class }); 297 | } 298 | size_class += stride; 299 | if size <= size_class { 300 | return Some(SizeClassInfo { index: 29, size_class }); 301 | } 302 | size_class += stride; 303 | if size <= size_class { 304 | return Some(SizeClassInfo { index: 30, size_class }); 305 | } 306 | size_class += stride; 307 | if size <= size_class { 308 | return Some(SizeClassInfo { index: 31, size_class }); 309 | } 310 | 311 | None 312 | } 313 | 314 | const NUM_SIZE_CLASSES: usize = 32; 315 | 316 | /// A shuffling allocator. 317 | /// 318 | /// Wraps an existing allocator and shuffles the order of heap allocations 319 | /// yielded. 320 | /// 321 | /// See [the crate-level documentation](./index.html) for more details. 322 | /// 323 | /// # Example 324 | /// 325 | /// ``` 326 | /// use shuffling_allocator::ShufflingAllocator; 327 | /// use std::alloc::System; 328 | /// 329 | /// static SHUFFLED_SYSTEM_ALLOC: ShufflingAllocator = shuffling_allocator::wrap!(&System); 330 | /// ``` 331 | pub struct ShufflingAllocator 332 | where 333 | A: 'static + GlobalAlloc, 334 | { 335 | // XXX: You shouldn't touch these fields! They need to be `pub` so that 336 | // `wrap!` works, but as soon as `const fn`s can have type parameters, these 337 | // will be private. 338 | #[doc(hidden)] 339 | pub inner: &'static A, 340 | #[doc(hidden)] 341 | pub state: LazyAtomicCell>, 342 | } 343 | 344 | #[doc(hidden)] 345 | pub struct State 346 | where 347 | A: 'static + GlobalAlloc, 348 | { 349 | rng: Mutex, 350 | size_classes: LazyAtomicCell>, 351 | } 352 | 353 | /// Wrap shuffling around an existing global allocator. 354 | /// 355 | /// # Example 356 | /// 357 | /// ``` 358 | /// use shuffling_allocator::ShufflingAllocator; 359 | /// use std::alloc::System; 360 | /// 361 | /// static SHUFFLED_SYSTEM_ALLOC: ShufflingAllocator = shuffling_allocator::wrap!(&System); 362 | /// ``` 363 | #[macro_export] 364 | macro_rules! wrap { 365 | ($inner:expr) => { 366 | $crate::ShufflingAllocator { 367 | inner: $inner, 368 | state: $crate::LazyAtomicCell { 369 | ptr: ::std::sync::atomic::AtomicPtr::new(::std::ptr::null_mut()), 370 | allocator: $inner, 371 | }, 372 | } 373 | }; 374 | } 375 | 376 | impl ShufflingAllocator 377 | where 378 | A: 'static + GlobalAlloc, 379 | { 380 | // XXX: this is disabled until we can have `const fn`s with type parameters. 381 | // 382 | // pub const fn new(inner: &'static A) -> Self { 383 | // ShufflingAllocator { 384 | // inner, 385 | // state: LazyAtomicCell::new(inner), 386 | // } 387 | // } 388 | 389 | #[inline] 390 | fn state(&self) -> &State { 391 | self.state.get_or_create(|| State { 392 | rng: Mutex::new(&self.inner, StdRng::from_entropy()), 393 | size_classes: LazyAtomicCell::new(self.inner), 394 | }) 395 | } 396 | 397 | #[inline] 398 | fn random_index(&self) -> usize { 399 | let mut rng = self.state().rng.lock(); 400 | rng.gen_range(0..SHUFFLING_ARRAY_SIZE) 401 | } 402 | 403 | #[inline] 404 | fn size_classes(&self) -> &SizeClasses { 405 | self.state().size_classes.get_or_create(|| { 406 | let mut classes = 407 | MaybeUninit::<[LazyAtomicCell>; NUM_SIZE_CLASSES]>::uninit(); 408 | unsafe { 409 | for i in 0..NUM_SIZE_CLASSES { 410 | ptr::write( 411 | classes 412 | .as_mut_ptr() 413 | .cast::>>() 414 | .offset(i as _), 415 | LazyAtomicCell::new(self.inner), 416 | ); 417 | } 418 | SizeClasses(classes.assume_init()) 419 | } 420 | }) 421 | } 422 | 423 | #[inline] 424 | fn shuffling_array(&self, size: usize) -> Option<&ShufflingArray> { 425 | let SizeClassInfo { index, size_class } = size_class_info(size)?; 426 | let size_classes = self.size_classes(); 427 | Some(size_classes.0[index].get_or_create(|| ShufflingArray::new(size_class, self.inner))) 428 | } 429 | } 430 | 431 | unsafe impl GlobalAlloc for ShufflingAllocator 432 | where 433 | A: GlobalAlloc, 434 | { 435 | #[inline] 436 | unsafe fn alloc(&self, layout: std::alloc::Layout) -> *mut u8 { 437 | // We only support shuffling reasonably aligned allocations. 438 | if layout.align() > mem::align_of::() { 439 | return self.inner.alloc(layout); 440 | } 441 | 442 | match self.shuffling_array(layout.size()) { 443 | // We don't have a shuffling array for this size (it must be fairly 444 | // big) so just use the inner allocator. 445 | None => self.inner.alloc(layout), 446 | 447 | // Choose a random entry from the shuffle array to return, refilling 448 | // the entry with a new pointer from the inner allocator. 449 | Some(array) => { 450 | let replacement_ptr = self.inner.alloc(array.elem_layout()); 451 | if replacement_ptr.is_null() { 452 | return ptr::null_mut(); 453 | } 454 | 455 | let index = self.random_index(); 456 | array.elems[index].swap(replacement_ptr, Ordering::SeqCst) 457 | } 458 | } 459 | } 460 | 461 | #[inline] 462 | unsafe fn dealloc(&self, ptr: *mut u8, layout: std::alloc::Layout) { 463 | if ptr.is_null() { 464 | return; 465 | } 466 | 467 | if layout.align() > mem::align_of::() { 468 | self.inner.dealloc(ptr, layout); 469 | return; 470 | } 471 | 472 | match self.shuffling_array(layout.size()) { 473 | // No size class for this layout, use the inner allocator directly. 474 | None => self.inner.dealloc(ptr, layout), 475 | 476 | // Choose a random entry in the shuffle array to swap this pointer 477 | // with, and then deallocate the old entry. 478 | Some(array) => { 479 | let index = self.random_index(); 480 | let old_ptr = array.elems[index].swap(ptr, Ordering::SeqCst); 481 | self.inner.dealloc(old_ptr, array.elem_layout()); 482 | } 483 | } 484 | } 485 | } 486 | -------------------------------------------------------------------------------- /src/pthread_mutex.rs: -------------------------------------------------------------------------------- 1 | use std::cell::UnsafeCell; 2 | use std::ops::{Deref, DerefMut}; 3 | use std::{ 4 | alloc::{handle_alloc_error, GlobalAlloc, Layout}, 5 | mem::MaybeUninit, 6 | ptr, 7 | }; 8 | 9 | pub(crate) struct PthreadMutex 10 | where 11 | A: 'static + GlobalAlloc, 12 | { 13 | inner: *mut libc::pthread_mutex_t, 14 | value: UnsafeCell, 15 | allocator: &'static A, 16 | } 17 | 18 | impl Drop for PthreadMutex 19 | where 20 | A: 'static + GlobalAlloc, 21 | { 22 | fn drop(&mut self) { 23 | unsafe { 24 | let retcode = libc::pthread_mutex_destroy(self.inner); 25 | assert_eq!(retcode, 0); 26 | 27 | let layout = Layout::new::(); 28 | self.allocator.dealloc(self.inner.cast(), layout); 29 | } 30 | } 31 | } 32 | 33 | impl PthreadMutex 34 | where 35 | A: 'static + GlobalAlloc, 36 | { 37 | pub fn new(allocator: &'static A, value: T) -> Self { 38 | let layout = Layout::new::(); 39 | let inner: *mut libc::pthread_mutex_t = unsafe { allocator.alloc(layout).cast() }; 40 | if inner.is_null() { 41 | handle_alloc_error(layout); 42 | } 43 | 44 | unsafe { 45 | ptr::write(inner, libc::PTHREAD_MUTEX_INITIALIZER); 46 | } 47 | 48 | // Ensure the mutex is of type PTHREAD_MUTEX_NORMAL so that we 49 | // deadlock on re-entrancy, rather than trigger undefined behavior. 50 | // 51 | // For details, see the comment inside `std`: 52 | // https://github.com/rust-lang/rust/blob/c9b52100/library/std/src/sys/unix/mutex.rs#L30-L59 53 | unsafe { 54 | let mut attr = MaybeUninit::::uninit(); 55 | let attr_ptr = attr.as_mut_ptr(); 56 | 57 | let retcode = libc::pthread_mutexattr_init(attr_ptr); 58 | assert_eq!(retcode, 0); 59 | 60 | let retcode = libc::pthread_mutexattr_settype(attr_ptr, libc::PTHREAD_MUTEX_NORMAL); 61 | assert_eq!(retcode, 0); 62 | 63 | let retcode = libc::pthread_mutex_init(inner, attr_ptr); 64 | assert_eq!(retcode, 0); 65 | } 66 | 67 | PthreadMutex { 68 | inner, 69 | value: UnsafeCell::new(value), 70 | allocator, 71 | } 72 | } 73 | 74 | pub fn lock(&self) -> PthreadLockGuard { 75 | let retcode = unsafe { libc::pthread_mutex_lock(self.inner) }; 76 | assert_eq!(retcode, 0); 77 | PthreadLockGuard { mutex: self } 78 | } 79 | } 80 | 81 | pub struct PthreadLockGuard<'a, A, T> 82 | where 83 | A: 'static + GlobalAlloc, 84 | { 85 | mutex: &'a PthreadMutex, 86 | } 87 | 88 | impl<'a, A, T> Drop for PthreadLockGuard<'a, A, T> 89 | where 90 | A: 'static + GlobalAlloc, 91 | { 92 | fn drop(&mut self) { 93 | let retcode = unsafe { libc::pthread_mutex_unlock(self.mutex.inner) }; 94 | assert_eq!(retcode, 0); 95 | } 96 | } 97 | 98 | impl<'a, A, T> Deref for PthreadLockGuard<'a, A, T> 99 | where 100 | A: 'static + GlobalAlloc, 101 | { 102 | type Target = T; 103 | 104 | fn deref(&self) -> &T { 105 | unsafe { &*self.mutex.value.get() } 106 | } 107 | } 108 | 109 | impl<'a, A, T> DerefMut for PthreadLockGuard<'a, A, T> 110 | where 111 | A: 'static + GlobalAlloc, 112 | { 113 | fn deref_mut(&mut self) -> &mut T { 114 | unsafe { &mut *self.mutex.value.get() } 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /src/windows_mutex.rs: -------------------------------------------------------------------------------- 1 | use std::cell::UnsafeCell; 2 | use std::ops::{Deref, DerefMut}; 3 | use std::{ 4 | alloc::{handle_alloc_error, GlobalAlloc, Layout}, 5 | ptr, 6 | }; 7 | use winapi::um::synchapi::{ 8 | AcquireSRWLockExclusive, ReleaseSRWLockExclusive, SRWLOCK, SRWLOCK_INIT, 9 | }; 10 | 11 | pub(crate) struct WindowsMutex 12 | where 13 | A: 'static + GlobalAlloc, 14 | { 15 | inner: *mut SRWLOCK, 16 | value: UnsafeCell, 17 | allocator: &'static A, 18 | } 19 | 20 | impl Drop for WindowsMutex 21 | where 22 | A: 'static + GlobalAlloc, 23 | { 24 | fn drop(&mut self) { 25 | unsafe { 26 | let layout = Layout::new::(); 27 | self.allocator.dealloc(self.inner.cast(), layout); 28 | } 29 | } 30 | } 31 | 32 | impl WindowsMutex 33 | where 34 | A: 'static + GlobalAlloc, 35 | { 36 | pub fn new(allocator: &'static A, value: T) -> Self { 37 | let layout = Layout::new::(); 38 | let inner: *mut SRWLOCK = unsafe { allocator.alloc(layout).cast() }; 39 | if inner.is_null() { 40 | handle_alloc_error(layout); 41 | } 42 | 43 | unsafe { 44 | ptr::write(inner, SRWLOCK_INIT); 45 | } 46 | 47 | WindowsMutex { 48 | inner, 49 | value: UnsafeCell::new(value), 50 | allocator, 51 | } 52 | } 53 | 54 | pub fn lock(&self) -> WindowsLockGuard { 55 | unsafe { 56 | AcquireSRWLockExclusive(self.inner); 57 | } 58 | WindowsLockGuard { mutex: self } 59 | } 60 | } 61 | 62 | pub struct WindowsLockGuard<'a, A, T> 63 | where 64 | A: 'static + GlobalAlloc, 65 | { 66 | mutex: &'a WindowsMutex, 67 | } 68 | 69 | impl<'a, A, T> Drop for WindowsLockGuard<'a, A, T> 70 | where 71 | A: 'static + GlobalAlloc, 72 | { 73 | fn drop(&mut self) { 74 | unsafe { 75 | ReleaseSRWLockExclusive(self.mutex.inner); 76 | } 77 | } 78 | } 79 | 80 | impl<'a, A, T> Deref for WindowsLockGuard<'a, A, T> 81 | where 82 | A: 'static + GlobalAlloc, 83 | { 84 | type Target = T; 85 | 86 | fn deref(&self) -> &T { 87 | unsafe { &*self.mutex.value.get() } 88 | } 89 | } 90 | 91 | impl<'a, A, T> DerefMut for WindowsLockGuard<'a, A, T> 92 | where 93 | A: 'static + GlobalAlloc, 94 | { 95 | fn deref_mut(&mut self) -> &mut T { 96 | unsafe { &mut *self.mutex.value.get() } 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /tests/global.rs: -------------------------------------------------------------------------------- 1 | use shuffling_allocator::ShufflingAllocator; 2 | use std::alloc::System; 3 | use std::collections::HashMap; 4 | use std::thread; 5 | 6 | #[global_allocator] 7 | static A: ShufflingAllocator = shuffling_allocator::wrap!(&System); 8 | 9 | #[test] 10 | fn foo() { 11 | println!("hello"); 12 | } 13 | 14 | #[test] 15 | fn map() { 16 | let mut m = HashMap::new(); 17 | m.insert(1, 2); 18 | m.insert(5, 3); 19 | drop(m); 20 | } 21 | 22 | #[test] 23 | fn strings() { 24 | format!("foo, bar, {}", "baz"); 25 | } 26 | 27 | #[test] 28 | fn threads() { 29 | assert!(thread::spawn(|| panic!()).join().is_err()); 30 | } 31 | 32 | #[test] 33 | fn test_larger_than_word_alignment() { 34 | use std::mem; 35 | 36 | // Align to 32 bytes. 37 | #[repr(align(32))] 38 | struct Align32(u8); 39 | 40 | assert_eq!(mem::align_of::(), 32); 41 | 42 | for _ in 0..100 { 43 | let b = Box::new(Align32(42)); 44 | 45 | let p = Box::into_raw(b); 46 | assert_eq!(p as usize % 32, 0, "{:p} should be aligned to 32", p); 47 | 48 | unsafe { 49 | let b = Box::from_raw(p); 50 | assert_eq!(b.0, 42); 51 | } 52 | } 53 | } 54 | 55 | #[test] 56 | fn many_small_allocs() { 57 | let boxes = (0..1024).map(|i| Box::new(i)).collect::>(); 58 | drop(boxes); 59 | } 60 | --------------------------------------------------------------------------------