Comments (5)
Ah this is a bit tricky: I now designed the zero interface, like mi_calloc
, mi_zalloc
, to only zero out the requested size, and for mi_rezalloc
to only zero out the any additional requested size (if reallocating to a larger size).
A change we could make is to zero out always up to the useable size instead of requested size. I think that would be a good idea.
_It seems to me that the rezalloc feature requires to store the requested size or at least to zero out the implicit allocated range form requested size to allocated size in all allocations. _
It does do that already, zero out any additional space if reallocating to a larger area. Of course, the existing data should not zero out.
from mimalloc.
It's tricky indeed! Unfortunately, a change limited to the 'z' and 'c' allocation variants will not solve the problem. An example:
Initial allocation of 6 bytes by mi_malloc()
(with debug initialization simulating an undefined state):
*p=xD0D0D0D0D0D0D0D0D0D0
The application copies its C string of length 5 plus a null byte as termination, i.e. it defines the state of the requested memory completely:
*p=x48656C6C6F00D0D0
("Hello")
Another part of the application wants to append a sixth character to the C string and ensure termination by using mi_rezalloc(p, 7)
. The current implementation of _mi_realloc_zero()
recognizes that the assigned block is large enough and delivers it unchanged. The application appends another character but no termination, since it used the initialization of the additional memory with zero. We end up with an unterminated C string with funny effects up to segmentation fault.
*p=x48656C6C6F21D0D0
("Hello!ÐÐ...")
Can the implementation of mi_rezalloc()
prevent this?
I don't see any way to do this without mi_rezalloc()
knowing the size of the original allocation or initializing the extra memory available in all allocations (not just the 'z' and 'c' variants) to zero.
Note that even if the old block is copied to a larger new block, the undefined memory area is copied as well, since only the block size is known. The extension of the zero initialization by one word in the area of the old block is overwritten, so that the extended initialization remains ineffective! (see master commit 7b4f359; alloc.c; lines 336 and 339)
On possible solution without performance losses if the feature is not used would be the requirement to pass the originally requested size resp. count in mi_rezalloc()
resp. mi_recalloc()
. The application needs to know these values anyway in order to use the memory correctly, so why not make this missing information available? Aren't there even suggestions to call mi_free()
itself with the requested size to allow better debug/consistency checks?
from mimalloc.
Ah, nice example. The way it should be I think is that (a) calloc
, zalloc
always zero out all the useable memory. rezalloc
and recalloc
are only well-defined for a previous calloc,zalloc,rezalloc,recalloc
, but not if the previous memory was alloc'd with malloc
. That would fix this I think. Still, we cannot enforce this and perhaps we should do away with the rezalloc
variantcs...
from mimalloc.
> ...and perhaps we should do away with the rezalloc variantcs...
👍
from mimalloc.
removed rezalloc and variants in the latest dev
branch. kept mi_recalloc as a Unix variant for convenience.
from mimalloc.
Related Issues (20)
- Call mi_heap_destroy from another thread
- getting an assertion error in Koka
- Feature Request: Support AArch64 MTE
- Feedback on mimalloc 2.X
- mi_is_in_heap_region asserting in debug HOT 2
- Override malloc in dynamically loaded dll on Windows
- Mimalloc attempts to allocate 33 MB shortly after thread start, prints spurious warning when it fails
- Any way to know how much memory was freed but not yet purged?
- The performance of mimalloc is unexpectedly lower than that of the system's built-in allocator. HOT 3
- Use MADV_FREE in FreeBSD HOT 4
- mimalloc.cmake has hard-coded paths
- Shutdown issues using heap bound to arena, `mi_collect`
- What interface is redirected _expand_base
- mi_malloc didn't return NULL while no memory available
- Multiple debug print statements and hugepages mapped HOT 1
- When will release the next version?
- Why is `mimalloc-redirect` closed-source? HOT 1
- Unused variable from heap alloc code
- Crash on start on OSX with v2.0.9 HOT 1
- "mimalloc-obj" is not in any export set
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mimalloc.