Skip to content

Use global base_alloc in proxy lib #263

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

Closed
wants to merge 3 commits into from

Conversation

igchor
Copy link
Member

@igchor igchor commented Feb 26, 2024

Replace linear allocator with global base alloc. This PR also makes linear allocator obsolete and hence fixes: #260

After these changes, it is possible to destroy Proxy_pool and OS provider in proxy lib.

During debugging I found that there is still one or two allocations coming from dlopen that are not free, but they are bigger than any allocation class size that we use hence it doesn't trigger any asserts.

@igchor igchor force-pushed the only_global_ba_alloc branch 4 times, most recently from 77cf244 to a1bd5c4 Compare February 28, 2024 00:15
global base alloc free function now does not require size param
- make sure base_alloc dtor is only called once by adding a macro
  to skip the dtor (it's set in proxy lib).

  Previously, since base_alloc was linked with both libumf and
  proxy_lib, it was called twice and the first time was BEFORE
  the libumf dtor (hence some allocations were still unfreed).
@igchor igchor force-pushed the only_global_ba_alloc branch from a1bd5c4 to c0c35c4 Compare February 28, 2024 00:30
@igchor igchor marked this pull request as ready for review February 28, 2024 00:50
@igchor igchor requested a review from a team as a code owner February 28, 2024 00:50
@igchor igchor requested a review from ldorau February 28, 2024 00:50
@@ -33,7 +35,8 @@ void *ba_os_alloc(size_t size) {

void ba_os_free(void *ptr, size_t size) {
int ret = munmap(ptr, size);
assert(ret == 0);
// TODO: this might fail in proxy_lib
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is applied here and reverted in the next commit. Remove it at all, please.

@@ -35,8 +37,7 @@ void *ba_os_alloc(size_t size) {

void ba_os_free(void *ptr, size_t size) {
int ret = munmap(ptr, size);
// TODO: this might fail in proxy_lib
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change was applied in the previous commit and is reverted here. Remove it at all, please.

umfPoolDestroy(Proxy_pool);
Proxy_pool = NULL;

umfMemoryProviderDestroy(OS_memory_provider);
OS_memory_provider = NULL;

umf_ba_destroy_global();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change was applied in the previous commit and is reverted here. Remove it at all, please.

umfMemoryProviderDestroy(OS_memory_provider);
OS_memory_provider = NULL;

umf_ba_destroy_global();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is applied here and reverted in the next commit. Remove it at all, please.

Copy link
Contributor

@ldorau ldorau Feb 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean adding umf_ba_destroy_global();

@@ -93,13 +93,12 @@ void proxy_lib_create_common(void) {
}

void proxy_lib_destroy_common(void) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundant empty line

return user_ptr;
}

static void *get_original_alloc(void *user_ptr, size_t *total_size,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment what this function does, please.


void *user_ptr;
if (alignment <= sizeof(size_t)) {
user_ptr = (void *)((uintptr_t)ptr + sizeof(size_t));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment why it is aligned to alignment, please.

#include "proxy_lib.h"
#include "utils_common.h"
#include "utils_concurrency.h"

/*
* The UMF proxy library uses two memory allocators:
* 1) the "LEAK" internal linear base allocator based on the anonymous mapped
* memory that will NOT be destroyed (with API ba_leak_*()).
* 1) base_alloc for bootstrapping
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

base allocator

}

if (Proxy_pool) {
if (!was_called_from_umfPool && Proxy_pool) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Important) What if an app wants to realloc a pointer from the base allocator when Proxy_pool already exists?
How can you distinguish which allocator a pointer comes from?

}

if (Proxy_pool) {
if (!was_called_from_umfPool && Proxy_pool) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Important) What if an app wants to free a pointer from the base allocator when Proxy_pool already exists?
How can you distinguish which allocator a pointer comes from?

Copy link
Contributor

@ldorau ldorau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

General comment:
This PR merges 4 independent changes:

  1. Fixing a problem with freeing memory in proxy lib (make sure base_alloc dtor is only called once by adding a macro to skip the dtor (it's set in proxy lib).)
  2. Improvements in the global base allocator (adding metadata with stored size, alloc_usable_size() and removing size from free())
  3. Replacing the linear base allocator with the global base allocator in the proxy library
  4. Removing the linear base allocator from the repo.

IMHO this PR should be split into 4 separate smaller pull requests.

Copy link
Contributor

@ldorau ldorau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$ LD_PRELOAD=./lib/libumf_proxy.so ctest --output-on-failure

ends with Segmentation fault:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7833bf5 in __GI_getenv (name=0x7ffff694eedc "GNUTLS_NO_IMPLICIT_INIT") at ./stdlib/getenv.c:75
75            for (ep = __environ; *ep != NULL; ++ep)

@bratpiorka
Copy link
Contributor

@igchor please split this change into multiple PRs

@igchor
Copy link
Member Author

igchor commented Feb 28, 2024

@igchor please split this change into multiple PRs

Ok, here's the first one: #272

@igchor igchor closed this Feb 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The umf-base_alloc_linear test fails sporadically on the assert on Windows
3 participants