Merge branch 'dev' into dev-slice

This commit is contained in:
Daan Leijen 2024-12-19 11:50:42 -08:00
commit b6019897c1
2 changed files with 17 additions and 15 deletions

View file

@ -1,9 +1,9 @@
# Windows Override # Windows Override
<span id="override_on_windows">Dynamically overriding on mimalloc on Windows</span> <span id="override_on_windows">Dynamically overriding on mimalloc on Windows</span>
is robust and has the particular advantage to be able to redirect all malloc/free calls that go through is robust and has the particular advantage to be able to redirect all malloc/free calls
the (dynamic) C runtime allocator, including those from other DLL's or libraries. that go through the (dynamic) C runtime allocator, including those from other DLL's or
As it intercepts all allocation calls on a low level, it can be used reliably libraries. As it intercepts all allocation calls on a low level, it can be used reliably
on large programs that include other 3rd party components. on large programs that include other 3rd party components.
There are four requirements to make the overriding work well: There are four requirements to make the overriding work well:
@ -11,8 +11,8 @@ There are four requirements to make the overriding work well:
2. Link your program explicitly with the `mimalloc-override.lib` export library for 2. Link your program explicitly with the `mimalloc-override.lib` export library for
the `mimalloc-override.dll` -- which contains all mimalloc functionality. the `mimalloc-override.dll` -- which contains all mimalloc functionality.
To ensure the `mimalloc-override.dll` is actually loaded at run-time it is easiest to insert some To ensure the `mimalloc-override.dll` is actually loaded at run-time it is easiest
call to the mimalloc API in the `main` function, like `mi_version()` to insert some call to the mimalloc API in the `main` function, like `mi_version()`
(or use the `/include:mi_version` switch on the linker, or (or use the `/include:mi_version` switch on the linker, or
use `#pragma comment(linker, "/include:mi_version")` in some source file). use `#pragma comment(linker, "/include:mi_version")` in some source file).
See the `mimalloc-override-test` project for an example on how to use this. See the `mimalloc-override-test` project for an example on how to use this.
@ -32,7 +32,8 @@ is also recommended to also override the `new`/`delete` operations (by including
a single(!) source file in your project). a single(!) source file in your project).
The environment variable `MIMALLOC_DISABLE_REDIRECT=1` can be used to disable dynamic The environment variable `MIMALLOC_DISABLE_REDIRECT=1` can be used to disable dynamic
overriding at run-time. Use `MIMALLOC_VERBOSE=1` to check if mimalloc was successfully redirected. overriding at run-time. Use `MIMALLOC_VERBOSE=1` to check if mimalloc was successfully
redirected.
### Other Platforms ### Other Platforms
@ -49,7 +50,7 @@ need a specific `mimalloc-redirect.dll`:
`mimalloc-override.lib` export library. `mimalloc-override.lib` export library.
2. Now separately build `mimalloc-override.dll` in `arm64ec` mode and _overwrite_ your 2. Now separately build `mimalloc-override.dll` in `arm64ec` mode and _overwrite_ your
previous (x64) `mimalloc-override.dll` -- the loader can handle the mix of arm64ec previous (x64) `mimalloc-override.dll` -- the loader can handle the mix of arm64ec
and x64 code. Now use `mimalloc-redirect-arm64ec.dll` in this case to match your and x64 code. Now use `mimalloc-redirect-arm64ec.dll` to match your new
arm64ec `mimalloc-override.dll`. The main program stays as is and can be fully x64 arm64ec `mimalloc-override.dll`. The main program stays as is and can be fully x64
or contain more arm64ec modules. At runtime, the arm64ec `mimalloc-override.dll` will or contain more arm64ec modules. At runtime, the arm64ec `mimalloc-override.dll` will
run with native arm64 instructions while the rest of the program runs emulated x64. run with native arm64 instructions while the rest of the program runs emulated x64.
@ -59,12 +60,12 @@ need a specific `mimalloc-redirect.dll`:
### Minject ### Minject
We cannot always re-link an executable with `mimalloc-override.dll`, and similarly, we cannot always We cannot always re-link an executable with `mimalloc-override.dll`, and similarly, we
ensure that the DLL comes first in the import table of the final executable. cannot always ensure that the DLL comes first in the import table of the final executable.
In many cases though we can patch existing executables without any recompilation In many cases though we can patch existing executables without any recompilation
if they are linked with the dynamic C runtime (`ucrtbase.dll`) -- just put the `mimalloc-override.dll` if they are linked with the dynamic C runtime (`ucrtbase.dll`) -- just put the
into the import table (and put `mimalloc-redirect.dll` in the same folder) `mimalloc-override.dll` into the import table (and put `mimalloc-redirect.dll` in the same
Such patching can be done for example with [CFF Explorer](https://ntcore.com/?page_id=388). directory) Such patching can be done for example with [CFF Explorer](https://ntcore.com/?page_id=388).
The `minject` program can also do this from the command line The `minject` program can also do this from the command line
Use `minject --help` for options: Use `minject --help` for options:

View file

@ -50,9 +50,7 @@ int main() {
mi_stats_reset(); // ignore earlier allocations mi_stats_reset(); // ignore earlier allocations
various_tests(); various_tests();
test_mixed1(); test_mixed1();
const char* ptr = ::_Getdays();
free((void*)ptr);
//test_std_string(); //test_std_string();
//test_thread_local(); //test_thread_local();
// heap_thread_free_huge(); // heap_thread_free_huge();
@ -115,6 +113,9 @@ static void various_tests() {
t = new (tbuf) Test(42); t = new (tbuf) Test(42);
t->~Test(); t->~Test();
delete[] tbuf; delete[] tbuf;
const char* ptr = ::_Getdays(); // test _base overrid
free((void*)ptr);
} }
class Static { class Static {