Skip to content

Commit

Permalink
Fix leaks in test/correctness/memoize.cpp (#7705)
Browse files Browse the repository at this point in the history
* Fix leaks caused by self-referential parameter constraints

* Add comment

* Add missing overrides

* Fix reported leaks in memoize test

by explicitly releasing the shared runtime at the end of the test

* Use const refs for non-mutated args

* Hopefully fix for windows

* Fix for 32-bit pointers

* Don't use _aligned_malloc

It requires _aligned_free, which the runtime aint gonna do

* Fix other memoize test

* Use runtime built-in malloc/free

On windows mixing and matching mallocs and frees doesn't work well.

* Fix comment

---------

Co-authored-by: Steven Johnson <srj@google.com>
  • Loading branch information
abadams and steven-johnson committed Aug 5, 2023
1 parent f39576f commit c254043
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
29 changes: 22 additions & 7 deletions test/correctness/memoize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,22 @@ extern "C" HALIDE_EXPORT_SYMBOL int computed_eviction_key(int a) {
}
HalideExtern_1(int, computed_eviction_key, int);

void simple_free(JITUserContext *user_context, void *ptr) {
free(ptr);
}
void *(*default_malloc)(JITUserContext *, size_t);
void (*default_free)(JITUserContext *, void *);

void *flakey_malloc(JITUserContext * /* user_context */, size_t x) {
// A flaky allocator that wraps the built-in runtime one.
void *flaky_malloc(JITUserContext *user_context, size_t x) {
if ((rand() % 4) == 0) {
return nullptr;
} else {
return malloc(x);
return default_malloc(user_context, x);
}
}

void simple_free(JITUserContext *user_context, void *ptr) {
return default_free(user_context, ptr);
}

bool error_occured = false;
void record_error(JITUserContext *user_context, const char *msg) {
error_occured = true;
Expand Down Expand Up @@ -584,6 +588,15 @@ int main(int argc, char **argv) {
return 0;
} else {
// Test out of memory handling.

// Get the runtime's malloc and free. We need to use the ones
// in the runtime to ensure the matching free is called when
// we release all the runtimes at the end.
JITUserContext ctx;
Internal::JITSharedRuntime::populate_jit_handlers(&ctx, JITHandlers{});
default_malloc = ctx.handlers.custom_malloc;
default_free = ctx.handlers.custom_free;

Param<float> val;

Func count_calls;
Expand All @@ -600,7 +613,7 @@ int main(int argc, char **argv) {

Pipeline pipe(g);
pipe.jit_handlers().custom_error = record_error;
pipe.jit_handlers().custom_malloc = flakey_malloc;
pipe.jit_handlers().custom_malloc = flaky_malloc;
pipe.jit_handlers().custom_free = simple_free;

int total_errors = 0;
Expand Down Expand Up @@ -644,7 +657,8 @@ int main(int argc, char **argv) {
}
}

printf("In 100 attempts with flakey malloc, %d errors and %d full completions occured.\n", total_errors, completed);
printf("In 100 attempts with flaky malloc, %d errors and %d full completions occured.\n",
total_errors, completed);
}

{
Expand Down Expand Up @@ -733,6 +747,7 @@ int main(int argc, char **argv) {

assert(call_count == 8);
}
Internal::JITSharedRuntime::release_all();

printf("Success!\n");
return 0;
Expand Down
2 changes: 2 additions & 0 deletions test/correctness/memoize_cloned.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ int main(int argc, char **argv) {
return 1;
}

Internal::JITSharedRuntime::release_all();

printf("Success!\n");
return 0;
}

0 comments on commit c254043

Please sign in to comment.