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

Fix data races for _lf_count_payload_allocations and _lf_count_token_allocations #313

Merged
merged 5 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions core/lf_token.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@
* Functions supporting token types. See lf_token.h for docs.
*/

#if !defined NDEBUG
/**
* Counter used to issue a warning if memory is
* allocated for message payloads and never freed.
*/
int _lf_count_payload_allocations;
int _lf_count_token_allocations;
#endif

#include <stdbool.h>
#include <assert.h>
Expand Down Expand Up @@ -72,6 +74,9 @@ static hashset_t _lf_token_recycling_bin = NULL;
*/
static hashset_t _lf_token_templates = NULL;

// Forward declarations
static lf_token_t* _lf_writable_copy_locked(lf_port_base_t* port);

////////////////////////////////////////////////////////////////////
//// Functions that users may call.

Expand All @@ -80,6 +85,13 @@ lf_token_t* lf_new_token(void* port_or_action, void* val, size_t len) {
}

lf_token_t* lf_writable_copy(lf_port_base_t* port) {
lf_critical_section_enter(port->source_reactor->environment);
lf_token_t* token = _lf_writable_copy_locked(port);
lf_critical_section_exit(port->source_reactor->environment);
return token;
}

static lf_token_t* _lf_writable_copy_locked(lf_port_base_t* port) {
assert(port != NULL);

lf_token_t* token = port->tmplt.token;
Expand Down Expand Up @@ -115,7 +127,11 @@ lf_token_t* lf_writable_copy(lf_port_base_t* port) {
LF_PRINT_DEBUG("lf_writable_copy: Allocated memory for payload (token value): %p", copy);

// Count allocations to issue a warning if this is never freed.
#if !defined NDEBUG
LF_ASSERT(!lf_critical_section_enter(GLOBAL_ENVIRONMENT), "Could not enter critical section");
_lf_count_payload_allocations++;
LF_ASSERT(!lf_critical_section_exit(GLOBAL_ENVIRONMENT), "Could not exit critical section");
#endif

// Create a new, dynamically allocated token.
lf_token_t* result = _lf_new_token((token_type_t*)port, copy, token->length);
Expand All @@ -133,7 +149,11 @@ lf_token_t* lf_writable_copy(lf_port_base_t* port) {
void _lf_free_token_value(lf_token_t* token) {
if (token->value != NULL) {
// Count frees to issue a warning if this is never freed.
#if !defined NDEBUG
LF_ASSERT(!lf_critical_section_enter(GLOBAL_ENVIRONMENT), "Could not enter critical section");
_lf_count_payload_allocations--;
LF_ASSERT(!lf_critical_section_exit(GLOBAL_ENVIRONMENT), "Could not exit critical section");
#endif
// Free the value field (the payload).
LF_PRINT_DEBUG("_lf_free_token_value: Freeing allocated memory for payload (token value): %p",
token->value);
Expand Down Expand Up @@ -161,9 +181,7 @@ token_freed _lf_free_token(lf_token_t* token) {
// Tokens that are created at the start of execution and associated with
// output ports or actions persist until they are overwritten.
// Need to acquire a mutex to access the recycle bin.
if (lf_critical_section_enter(GLOBAL_ENVIRONMENT) != 0) {
lf_print_error_and_exit("Could not enter critical section");
}
LF_ASSERT(!lf_critical_section_enter(GLOBAL_ENVIRONMENT), "Could not enter critical section");
if (_lf_token_recycling_bin == NULL) {
_lf_token_recycling_bin = hashset_create(4); // Initial size is 16.
if (_lf_token_recycling_bin == NULL) {
Expand All @@ -182,10 +200,10 @@ token_freed _lf_free_token(lf_token_t* token) {
LF_PRINT_DEBUG("_lf_free_token: Freeing allocated memory for token: %p", token);
free(token);
}
if(lf_critical_section_exit(GLOBAL_ENVIRONMENT) != 0) {
lf_print_error_and_exit("Could not leave critical section");
}
#if !defined NDEBUG
_lf_count_token_allocations--;
#endif
LF_ASSERT(!lf_critical_section_exit(GLOBAL_ENVIRONMENT), "Could not exit critical section");
result &= TOKEN_FREED;

return result;
Expand All @@ -206,6 +224,12 @@ lf_token_t* _lf_new_token(token_type_t* type, void* value, size_t length) {
}
free(iterator);
}

// Count the token allocation to catch memory leaks.
#if !defined NDEBUG
_lf_count_token_allocations++;
#endif

if(lf_critical_section_exit(GLOBAL_ENVIRONMENT) != 0) {
lf_print_error_and_exit("Could not leave critical section");
}
Expand Down Expand Up @@ -276,7 +300,11 @@ lf_token_t* _lf_initialize_token_with_value(token_template_t* tmplt, void* value
lf_token_t* result = _lf_get_token(tmplt);
result->value = value;
// Count allocations to issue a warning if this is never freed.
#if !defined NDEBUG
LF_ASSERT(!lf_critical_section_enter(GLOBAL_ENVIRONMENT), "Could not enter critical section");
_lf_count_payload_allocations++;
LF_ASSERT(!lf_critical_section_exit(GLOBAL_ENVIRONMENT), "Could not exit critical section");
#endif
result->length = length;
return result;
}
Expand Down
6 changes: 6 additions & 0 deletions core/reactor_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ extern watchdog_t* _lf_watchdogs;
// Global variable defined in tag.c:
extern instant_t start_time;

#if !defined NDEBUG
// Global variable defined in lf_token.c:
extern int _lf_count_payload_allocations;
#endif

/**
* Indicator of whether to wait for physical time to match logical time.
Expand Down Expand Up @@ -1725,8 +1727,10 @@ int process_args(int argc, const char* argv[]) {
* `_lf_initialize_trigger_objects` function
*/
void initialize_global(void) {
#if !defined NDEBUG
_lf_count_payload_allocations = 0;
_lf_count_token_allocations = 0;
#endif

environment_t *envs;
int num_envs = _lf_get_environments(&envs);
Expand Down Expand Up @@ -1816,6 +1820,7 @@ void termination(void) {
// Skip most cleanup on abnormal termination.
if (_lf_normal_termination) {
_lf_free_all_tokens(); // Must be done before freeing reactors.
#if !defined NDEBUG
// Issue a warning if a memory leak has been detected.
if (_lf_count_payload_allocations > 0) {
lf_print_warning("Memory allocated for messages has not been freed.");
Expand All @@ -1825,6 +1830,7 @@ void termination(void) {
lf_print_warning("Memory allocated for tokens has not been freed!");
lf_print_warning("Number of unfreed tokens: %d.", _lf_count_token_allocations);
}
#endif
#if !defined(LF_SINGLE_THREADED)
for (int i = 0; i < _lf_watchdog_count; i++) {
if (_lf_watchdogs[i].base->reactor_mutex != NULL) {
Expand Down
Loading