4 ms·
That conflicts. Suppose you want to allocate 24 bytes and process them using SIMD instructions. You call `aligned_alloc(24, 16)` to get a 24 byte allocation a
by volta83 6y ago
That conflicts.
Suppose you want to allocate 24 bytes and process them using SIMD instructions.
You call `aligned_alloc(24, 16)` to get a 24 byte allocation allocated to a 16 byte boundary.
Since the OS doesn't give the allocator 24 bytes, but whole memory pages, _it is valid_ for your program to read 32 bytes starting at the pointer returned, since that will never read out of the memory page in which this allocation resides.
This means that you can actually read the 24 bytes using 1 AVX instruction to read 32 bytes at once into a SIMD register, and do stuff with it (masking the uninitialized memory, etc.).
From those 32 bytes that you read, only the first 24 bytes might have been touched. That's a perfectly normal thing (e.g. you ask the allocator for 24 bytes, and it gives you a pointer to a 32 byte allocation, such that the last 8 bytes will never be touched by anyone).
- jeffbee 6y agoBut this thing works page-by-page.
- chrisrohlf 6y agoCorrect, this technique works on a page by page basis. It's the only way to do it with userfaultfd as the technique relies entirely on the first page fault being a read and not a write. Im working on a feature that returns an allocation that straddles 2 pages to catch reads of structure paddings but it will come at the cost of an additional 4k of memory and/or possibly returning unaligned memory.
- volta83 6y agoSo it only errors if you try to access a page in which no byte has been initialized ? That is, if a program writes only to the 0th byte of a page, this tool won't warn on it reading from the 42nd byte ?
- chrisrohlf 6y agoNo it will still catch it if you access the 42nd byte with a read, but not if you write to any other byte first. Thats just a limitation of page faults and the information provided by userfaultfd.