Skip to content

Fix memory leak in sd_lldp_stop() - #11163

Closed
filbranden wants to merge 2 commits into
systemd:masterfrom
filbranden:lldpleak1
Closed

filbranden wants to merge 2 commits into
systemd:masterfrom
filbranden:lldpleak1

Conversation

@filbranden

Copy link
Copy Markdown
Contributor

Fix memory leak in sd_lldp_stop()

The root cause of the issue was calling hashmap_remove() from function lldp_neighbor_unlink(), which was registered as destructor of the Hashmap and would get called from hashmap_clear(). But this would potentially shuffle the elements in the hashmap, therefore potentially missing destruction of some of the elements.

Fix this by using a separate lldp_neighbor_destroy() function that cleans up an sd_lldp_neighbor entry but does not remove it from the hashmap and use it exclusively as a destructor for the Hashmap.

Tested:

  • valgrind --leak-check=full build/test-lldp (including test case test_multiple_neighbors_sorted(), which uncovered this memory leak and would have shown the problem if it was not fixed.)
  • ninja -C build/ test, all tests passed.

Comment thread src/libsystemd-network/lldp-neighbor.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hmm, should this be sd_lldp_neighbor_unref()? Otherwise, if sd_lldp_neighbor objects are referenced and sd_lldp is freed, then sd_lldp_neighbor objects are also freed even if they are referenced.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

So I added assert(n->n_ref <= 0); here, in the assumption that sd_lldp_stop() will only be called once all references to all sd_lldp_neighbors will have been unref'd already...

(Is that really a valid assumption? The assert() will validate it, but potentially with a crash...)

(Calling sd_lldp_neighbor_unref() doesn't work, since typically n_ref is zero here, so the ref/unref logic is meant for all references outside the one from the LLDP itself.)

Not really sure what you mean by your other comment ("reference counter of sd_lldp_neighbor objects seems to be borked, ...), can you please expand on that?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hmm? Sorry, I may misunderstand your point or code, but I guess with your PR the following code may not work, as the ref counters of neighbors are not 1 or 0.

n = sd_lldp_get_neighbors(lldp, &neighbors);
(void) sd_lldp_stop(lldp);
for (i = 0; i < n; i++)
        sd_lldp_neighbor_unref(neighbors[i]);
free(neighbors);

@yuwata

yuwata commented Dec 15, 2018

Copy link
Copy Markdown
Member

BTW, reference counter of sd_lldp_neighbor objects seems to be borked, at least very storage... It is not necessary to use DEFINE_TRIVIAL_REF_UNREF_FUNC() macro or friends to define sd_lldp_neighbor_ref() or _unref(), but...

@yuwata yuwata added the reviewed/needs-rework 🔨 PR has been reviewed and needs another round of reworks label Dec 15, 2018
@filbranden

Copy link
Copy Markdown
Contributor Author

I'll have to rebase this one... but I'll wait until #11172 is in first, then we have time to have further discussion on this one... Thanks!

The root cause of the issue was calling hashmap_remove() from function
lldp_neighbor_unlink(), which was registered as destructor of the Hashmap
and would get called from hashmap_clear(). But this would potentially
shuffle the elements in the hashmap, therefore potentially missing
destruction of some of the elements.

Fix this by using a separate lldp_neighbor_destroy() function that
cleans up an sd_lldp_neighbor entry but does not remove it from the
hashmap and use it exclusively as a destructor for the Hashmap.

Tested: Ran test case that exposed this (test_multiple_neighbors_sorted)
under Valgrind and confirmed memory leaks were no longer reported.
…ghbors

In particular, check that the order of the results is consistent.

This test coverage will be useful in order to refactor the compare_func
used while sorting the results.

When introduced, this test also uncovered a memory leak in sd_lldp_stop(),
which was then fixed by a separate commit using a specialized function
as destructor of the LLDP Hashmap.

Tested:
  $ ninja -C build/ test
  $ valgrind --leak-check=full build/test-lldp
@keszybz

keszybz commented Dec 16, 2018

Copy link
Copy Markdown
Member

Hmm, defining a special cleanup function just to be called from hashmap_clear is pretty ugly. I'd prefer to have a scheme where we just call the normal unref function but in a safe way. What about changing hashmap_clear to use hashmap_steal_first_key_and_value if either of the cleanup functions (free_key and free_value) are non-NULL and not equal to free? When hashmap_steal_first_key_and_value is used, it is OK if the hashmap is reshuffled during freeing.

Using the "safe" public API will be slower then using the internal API. It would be great to quantify this. My hope is that the cost is not significant enough to matter.

@filbranden

Copy link
Copy Markdown
Contributor Author

I was actually thinking in the other direction... Having --debug-extra=hashmap make hashmap_clear() set a flag indicating it's destroying it and have the other operations assert that the flag is not set if debugging is on...

Either approach is fine with me, let me know which you prefer and I can try to come up with a PR for it.

Cheers,
Filipe

@keszybz

keszybz commented Dec 17, 2018

Copy link
Copy Markdown
Member

Such a check could be useful, but I think it's orthogonal to the issue at hand: if it is implemented, it'd apply equally well to either of the solutions (the one as implemented in the patch and my alternative proposal).

@poettering

Copy link
Copy Markdown
Member

Yeah, I too would say we should make hashmap_clear() safe to the destroy callback unregistering from the same hashmap. That sounds like a generally useful change to me, as this is likely to explode elsewhere too.

static void lldp_neighbor_destroy(sd_lldp_neighbor *n) {
assert(n);
assert(n->lldp);
assert(n->n_ref <= 0);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this is assumption is wrong. it should be OK for callers to keep a ref on a neighbor object, and it should stay around then, though in unlinked state

@poettering

Copy link
Copy Markdown
Member

I prepped an alternative to this in #11191, that incorporates @filbranden's test case. ptal!

@filbranden

Copy link
Copy Markdown
Contributor Author

Let's go with #11191 instead... I'll take a look at it shortly.

@filbranden filbranden closed this Dec 18, 2018
@filbranden
filbranden deleted the lldpleak1 branch December 18, 2018 01:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lldp reviewed/needs-rework 🔨 PR has been reviewed and needs another round of reworks

Development

Successfully merging this pull request may close these issues.

4 participants