Skip to content

Let DriveManager own all filesystem and image mount objects #2306

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

Merged
merged 5 commits into from
Mar 6, 2023

Conversation

kcgen
Copy link
Member

@kcgen kcgen commented Feb 28, 2023

Previously, management of the mountable filesystems (CDROM ISOs, HDD FAT16, Floppy FAT12) and raw images (numbered HDD images and bootable floppy images) was spread across a handful of literal arrays:

  • imageDiskList[] array
  • diskSwap[] array
  • Drives[] array

This made it difficult to properly destruct and replace objects without double-free situations.

This commit moves all ownership inside the DriveManager class where they are all held as std::unique_ptr objects to guarantee that only one instance exists.

The other three literal arrays (imageDiskList, diskSwap, and Drives hold simple pointers into the drive manager's unique_ptr->get() content. This removes all management responsibility from the arrays.

Unfortunately this is a large commit because it's essentially all-or-nothing, as once the data types are switched, all the affected code needs to be switched, too.

@kcgen kcgen added the plumbing Issues related to low-level support functions and classes label Feb 28, 2023
@kcgen kcgen self-assigned this Feb 28, 2023
@kcgen
Copy link
Member Author

kcgen commented Feb 28, 2023

Needs extensive testing - try lots of mount types (CDROM images and ISOs, HDD images, Floppy images, directory and overlay mounts) mixing them, unmounting them, and so on. Ideally using UBSAN-enabled builds.

I'll post test results as I go.

@kcgen kcgen force-pushed the kc/image-leak-2 branch 2 times, most recently from fb7f94f to 16c6baf Compare February 28, 2023 22:07
@kcgen
Copy link
Member Author

kcgen commented Feb 28, 2023

With all the pointers now in std::arrays, I thought it would be nice to bound-checks all their accesses using the .at(index) instead of the direct [index] operator, but unfortunately PVS studio flagged them as unnecessary:

2023-02-28_15-44

The good news is that PVS Studio isn't foolishly recommending we forgo secure/safe code, because it only recommends the [] operator when prior bounds checks exist:

2023-02-28_15-43

So I'll back out most of these truly unnecessary .at() calls, and keep the remainder.

@kcgen kcgen force-pushed the kc/image-leak-2 branch from 16c6baf to fbf0e5c Compare March 1, 2023 01:12
@johnnovak
Copy link
Member

johnnovak commented Mar 1, 2023

@kcgen this will take me a few days, I'm busy with other stuff too. Maybe to get it fully reviewed by Monday is a safe assumption, and hopefully a bit earlier.

@kcgen kcgen force-pushed the kc/image-leak-2 branch from fbf0e5c to c408f79 Compare March 1, 2023 03:56
@kcgen
Copy link
Member Author

kcgen commented Mar 1, 2023

Zero rush! Needs plenty of testing in the meantime :-)
Thanks, @johnnovak.

Copy link
Member

@johnnovak johnnovak left a comment

Choose a reason for hiding this comment

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

That must have been a lot of work @kcgen; it made my eyes water a bit towards the end 😅 Looks alright; I guess it needs a lot of testing to make sure things have only improved and there's no regressions.

@kcgen kcgen force-pushed the kc/image-leak-2 branch from c408f79 to 31cd5f7 Compare March 5, 2023 07:14
kcgen added 4 commits March 5, 2023 16:41
Previously, management of the mountable filesystems (CDROM ISOs, HDD
FAT16, Floppy FAT12) and raw images (numbered HDD images and bootable
floppy images) was spread across a handful of literal arrays:

- `imageDiskList[]` array
- `diskSwap[]` array
- `Drives[]` array

Without a centralized managing these, it was difficult to clear /
destruct / delete them as they could often point to each-other, leading
to double-free situations.

This commit moves all ownership inside the `DriveManager` class where
they are all held as `std::unique_ptr` objects to guarantee that only
one instance exists.

The other three literal arrays (`imageDiskList`, `diskSwap`, and
`Drives` hold simple pointers into the drive manager's
`unique_ptr->get()` content.  This removes all management responsibility
from the arrays.

Unfortunately this is a large commit because it's essentially
all-or-nothing (once the data types are switched, all the affected code
needs to be switched, too).
Only the look ups without prior bounding logic were moved
to .at() access, as PVS studio rightly caught many as unecessary
as it duplicates logic in the code.
This commit should be functionally identical and only contain
1:1 variable renames.
This commit should be functionally identical and only contain
whitespace changes.
@kcgen kcgen force-pushed the kc/image-leak-2 branch from 31cd5f7 to 9bc025e Compare March 6, 2023 00:41
@kcgen
Copy link
Member Author

kcgen commented Mar 6, 2023

That must have been a lot of work @kcgen; it made my eyes water a bit towards the end 😅 Looks alright; I guess it needs a lot of testing to make sure things have only improved and there's no regressions.

Ready to merge; I've tested all the mount types with various combinations and there are at least no regressions.

After this PR ownership is centralized but runtime state is still held in those three external global arrays (as well as some state help in the drive manager).

The next phase of refactoring should eliminate thos3 external arrays by moving their state all inside the Drive Manager, so it becomes a single data and state 'registry' for all the mounts.

From that point, it should be easier manage the mounts via the CLI commands (if anyone has a desire to work on that.. of course, things are work OK as-is, so no rush!) 😅

@kcgen
Copy link
Member Author

kcgen commented Mar 6, 2023

Thanks for the detailed review, @johnnovak !

@kcgen kcgen force-pushed the kc/image-leak-2 branch from 9bc025e to 5b31956 Compare March 6, 2023 02:31
@kcgen kcgen merged commit fac2820 into main Mar 6, 2023
@kcgen kcgen deleted the kc/image-leak-2 branch March 16, 2023 19:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plumbing Issues related to low-level support functions and classes
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants