-
Notifications
You must be signed in to change notification settings - Fork 131
SUPPORT_DIRECT_MALLOC and SUPPORT_REMOVE_MALLOC_LEGACY #372
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
SUPPORT_DIRECT_MALLOC and SUPPORT_REMOVE_MALLOC_LEGACY #372
Conversation
|
As I mentioned in another PR comment, I don't like the one-off deallocation but since it exists in ten thousand places in the code, I have just learned to live with it instead of trying to get rid of it. If this can be proven to catch all cases and not break anything anywhere, I'm perfectly fine to be done with the one-off deallocation, which has been, by and large, a royal pain in the neck. |
|
I'll keep you posted, its too early to say. It is possible to apply this PR (build so libc malloc/free is used directly), then fire up magic under valgrind and do some things and then shutdown without any reported issues. It looks like the static code analysis worked well enough to immediately spot all the locations I was looking for first time. I will take any pointers to ZIP projects and scripts to run to exercise things more thoroughly, I can run under valgrind/address-sanitizer/gdb over several days to find issues (or gain confidence). |
|
If that were easy I would have created a comprehensive CI by now. |
|
Uri Shaked might know something about the magic layout video for TinyTapeout. |
|
Merge Status: could be merged, but not expected to be merged, review this status in 2 months It is expected that bugs found from it will be the subject of new/separate PRs, but it may depend on the bug. |
|
1cad152 updated with 1st order issue CodeQL static analyser maybe not have spotted. |
1cad152 to
884485c
Compare
|
Rebase onto 8.3.545 no other changes MacOS still builds successful but fails with Abort during runtime test. Maybe this is indicative of a double-free on exit |
This supports three build modes: No additional -D options, default legacy mode -DSUPPORT_DIRECT_MALLOC, magic will use direct calls to libc malloc/free and will leave in place the symbols now renamed as mallocMagicLegacy() freeMagicLegacy() and callocMagicLegacy(). -DSUPPORT_DIRECT_MALLOC -DSUPPORT_REMOVE_MALLOC_LEGACY as above but will remove the three legacy functions from the binary to provide assurance they can not be used. The system malloc is thread-safe the legacy magic malloc has a global deferred free pointer and the mmap() allocate has a free-list that is not thread-safe making use of free not thread-safe. This could of course be improved with the use of atomic_compare_and_exchange operations but for what gain ? Then there is the additional function call overhead (of the indirection) and a few tests/branches inserted into a commonly used code paths around memory allocation, it hides the call site of the malloc/free usage from the compiler which maybe have special optimization cases. The existing malloc/free makes static code analysis around memory allocation more problematic, also use of runtime analysers will operate better with a fail-fast to bad memory usage.
…gacy malloc Just in case I also modify the allocation to ensure it was also performed via exactly the same method.
…REMOVE_MALLOC_LEGACY ./configure # If you are brave, enable with your favourite editor after ./configure sed -e 's/^#FEATURE_FLAGS /FEATURE_FLAGS /' -i defs.mak make make install
freeMagic1 series 2nd order find
884485c to
fb846ef
Compare
|
@RTimothyEdwards Looking to remove the draft status on this, opening this for release. Just need feedback this doesn't break the world for MacOS. Recap:
@d-m-bailey Then run it normally and see if it crashes on startup or exit. |
|
Ok maybe the issue is due to:
f173c0e#diff-8e0a99e32bba3bde3fac26382922326aa9403c7df62c359713e2ac034fca9243R182 Related LLVM/MacOS bug also: |
Originally the use of a weak symbol was to provide a fallback for non-inline supporting compilers. However all compilers (we care about) support inline keyword (which was not known at the original time of the work). Furthermore GCC have already worked through the solution and make it easy to implement. The use of __GNUC_STDC_INLINE__ pattern in this way manages the fallback and emits a hard symbol this can be tested with: CFLAGS="-g" ./configure; make; nm */lib*.o | grep freeMagic1 CFLAGS="-O3" ./configure; make; nm */lib*.o | grep freeMagic1 A hard 'T' symbol is emitted (to provide fallback) with all builds, but in the -O3 all usage is inlined. So an individual file can decide to inline or not at the occasion (compile time options) allows.
|
Showing all green now, so maybe it was some interaction with building for DSO and weak symbols on MacOS. So this is ready when everyone else is... |
The idiom created is like so to allow potential instrumentation/debugging possibilities around specific sites if it is found specific memory bugs need chasing, maybe due to the scoping choice of this patch (the location of the init() and end() sites) is found to be incorrect.
It also marks with a unique grep'able term 'freeMagic1' to facilitate later cleanup/removal of the idiom, or inspection for optimization at a later date.
The '1' in the naming related to the deferred-by-one more than it being version 1.
There is some overlapping usage so the variable naming ended up as
mm1andmm1_where a 2nd variable was needed.While this patch seeks to not modify magic default configuration (you must opt-in via manual editing of defs.mak after ./configure) it is designed to allow community testing, bug hunting and reporting to take place.
PLEASE DO NOT ENABLE THESE FEATURE FLAGS FOR PRODUCTION USE, only evaluation and bug hunting.
TODO: The tcl
versioncommand (and the startup banner) needs indicate which feature flags are enable for the specific binary being run.--
Methodology summary:
Changes applied in commit order (except for last commit).
Use CodeQL to find use-after-free sites.
Introduce freeMagic1 idiom at each callsite, with an assessment of scope (placement of init/end).
Create the last commit in the sequence based on the changes.
Comment taken from the lead commit: