Page MenuHomeFreeBSD

Clean up memory management in pmcstat
ClosedPublic

Authored by stevek on Sep 22 2016, 6:10 AM.
Tags
None
Referenced Files
Unknown Object (File)
Jan 12 2025, 3:58 PM
Unknown Object (File)
Jan 10 2025, 12:30 AM
Unknown Object (File)
Jan 9 2025, 9:47 AM
Unknown Object (File)
Jan 9 2025, 6:32 AM
Unknown Object (File)
Dec 9 2024, 11:55 AM
Unknown Object (File)
Dec 7 2024, 2:02 AM
Unknown Object (File)
Sep 24 2024, 6:41 PM
Unknown Object (File)
Sep 18 2024, 3:29 AM

Details

Summary

Calls to malloc need to test the returned pointer to ensure it is non-NULL.
Ensure all allocated memory is cleaned up in pmcstat_cleanup().
Memory calculation for default value of args.pa_kernel was one more than
necessary.

Test Plan

Run on x86 installs to ensure no runtime issues occur.

Diff Detail

Repository
rS FreeBSD src repository - subversion
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

stevek retitled this revision from to Clean up memory management in pmcstat.
stevek updated this object.
stevek edited the test plan for this revision. (Show Details)

Generally looks fine. See in-line comments.

usr.sbin/pmcstat/pmcstat.c
201 ↗(On Diff #20604)

It looks like this will result in ev being removed from the list twice (here and line 214).

816 ↗(On Diff #20604)

If you're going to check this, you should probably also check the ev_spec strdup() call a few lines up.

usr.sbin/pmcstat/pmcstat.c
201 ↗(On Diff #20604)

Yes, I would just leave this as a STAILQ_FOREACH_SAFE(). I think the real fix is moving the calls to free() and STAILQ_REMOVE() out of the PMC_ID_INVALID conditional.

218 ↗(On Diff #20604)

FWIW, calling free on global vars during a clean shutdown isn't really that important. If you know you are going to exit() then it's going to get cleaned up anyway. I suppose this might appease valgrind, but it actually increases runtime and CPU cycles used.

usr.sbin/pmcstat/pmcstat.c
201 ↗(On Diff #20604)

Yes, I'm guessing it was an accidental paste from vim when I had deleted the line after re-structuring the code.

218 ↗(On Diff #20604)

True. The question is do we want to forego the cleanup of memory and just do the pmc_stop/pmc_release only, since all the cases that seem to call this function, we are getting out.

Or perhaps only do the cleanup if one is building for leak detection? (Is there a somewhat standard define for that? Like there is for lint?)

816 ↗(On Diff #20604)

Good point.

stevek edited edge metadata.

Just remove freeing memory in pmcstat_cleanup, since we only clean up
when we are exiting anyway.

Add NULL checks for all malloc and strdup returns.

gnn edited edge metadata.

LGTM

This revision is now accepted and ready to land.Oct 7 2016, 7:39 PM
This revision was automatically updated to reflect the committed changes.