Skip to content
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

platform:fix possible double free for FreeRTOS #98

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

ajaybhargav
Copy link

FreeRTOS implementation of platform_thread_destroy free the pointer passed to the function and later the same pointer is freed again at mqttclient/mqttclient.c#L959

This will cause possible double free for FreeRTOS implementation.

@SL-RU
Copy link

SL-RU commented Feb 23, 2024

This is not correct and causes memory leak. vTaskDelete frees only pointer in field thread thread which is allocated in by xTaskCreate and platform_memory_free(thread) releases pointer to thread structure which allocated by thread = platform_memory_alloc(sizeof(platform_thread_t));

@ajaybhargav
Copy link
Author

ajaybhargav commented Feb 23, 2024

but platform_memory_free is called again after destroy here

platform_thread_destroy(thread_to_be_destoried);
platform_memory_free(thread_to_be_destoried);

So thread storage variable seems to be freed twice.

@SL-RU
Copy link

SL-RU commented Feb 23, 2024

Oh, indeed. There refactoring is required. Allocation is done inside the platform and free is outside. It is responsibility of platform_thread_destroy to free structure because it is allocated there.

So we need to remove free of thread structure in mqttclient and add free inside destroy in other ports

@ajaybhargav
Copy link
Author

yes, either way, the two free has to be prevented. and yes ideally platform code needs to take care of allocation and free. Shall I make necessary change and push again?

@ajaybhargav
Copy link
Author

however, if you look at other implementations e.g. TencentOS, thread variable is not being freed. so other platforms are to modified too.

void platform_thread_destroy(platform_thread_t* thread)
{
if (NULL != thread)
tos_task_destroy(&(thread->thread));
platform_memory_free(&(thread->thread));
platform_memory_free(&(thread->thread.stk_size));
}

@SL-RU
Copy link

SL-RU commented Feb 23, 2024

Yes, let's do it in the right way without creating new tangling. Where variable is allocated, there it needs to be freed

This patch fix the issue where platform_thread_destroy do not actually
delete the thread created, only memory free was done.

Signed-off-by: Ajay Bhargav <[email protected]>
@ajaybhargav
Copy link
Author

Another issue found in Tencent-OS implementation, stk_base needed to be freed not stk_size. Please look into the new patches

@@ -60,7 +60,8 @@ void platform_thread_destroy(platform_thread_t* thread)
if (NULL != thread)
tos_task_destroy(&(thread->thread));
platform_memory_free(&(thread->thread));
Copy link

@SL-RU SL-RU Feb 23, 2024

Choose a reason for hiding this comment

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

&(thread->thread) is equal to thread. This line is need to be removed. There are only two allocations: thread and stack

Copy link
Author

Choose a reason for hiding this comment

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

Yes, so the order need to be changed, after thread memory is free, it is not advisable to access thread structure.

In tencentos implementation of platform_thread_destroy, stack size is
getting freed where as stack base is suppose to be freed.

platform_thread_t structure varaible is also not freed. this patch fix
both the issues

Signed-off-by: Ajay Bhargav <[email protected]>
@ajaybhargav
Copy link
Author

Updated, please review.

Copy link

@SL-RU SL-RU left a comment

Choose a reason for hiding this comment

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

Now bugs are fixed

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.

2 participants