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

Refactoring system interface #15

Merged
merged 11 commits into from
Nov 6, 2020

Conversation

raoulstrackx
Copy link
Contributor

With the arrival of SGXv2, enclaves can dynamically add and remove memory pages. In SGXv1 this was not supported. Instead of adding the required changes for SGXv2 to this crate, the choice was made to factor out the sys interface. Now users of dlmalloc can simply implement the System trait. This allows further changes to this trait implementation (e.g., for SGXv2 support) without impacting this crate.

cc: @jethrogb

src/dlmalloc.rs Outdated Show resolved Hide resolved
Copy link

@jethrogb jethrogb left a comment

Choose a reason for hiding this comment

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

In addition, please change all existing platforms to the new API.

src/lib.rs Outdated Show resolved Hide resolved
src/dlmalloc.rs Outdated Show resolved Hide resolved
src/dlmalloc.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
@raoulstrackx raoulstrackx force-pushed the raoul/interface branch 2 times, most recently from 432a542 to 356d1ea Compare October 14, 2020 14:30
src/lib.rs Outdated Show resolved Hide resolved
src/dlmalloc.rs Outdated Show resolved Hide resolved
src/dlmalloc.rs Outdated Show resolved Hide resolved
tests/smoke.rs Outdated Show resolved Hide resolved
tests/smoke.rs Outdated Show resolved Hide resolved
src/global.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/global.rs Outdated Show resolved Hide resolved
src/dlmalloc.rs Outdated Show resolved Hide resolved
@alexcrichton
Copy link
Owner

Thanks for the PR! I'm not sure though that this is something I want to commit to in this crate. I'd prefer to not have a trait that exposes the underlying required operations since that's difficult to change over time. If sgx is changing rapidly then it may be best for a separate allocator other than this crate (although maybe based on this one?) to be used in libstd.

@raoulstrackx
Copy link
Contributor Author

The main purpose of the PR is not because SGX may change (again) in the future. It's because there is a dependency inversion; implementing the system allocator for SGX (and other targets) relies on information provided by libstd.
Going over all commits since the initial release of the crate, none of them changed the System interface. I think that is as expected as the crate is based on the dlmalloc C implementation that in itself is also very stable.

Copy link
Owner

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

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

Sorry it's been a hectic week, but that makes sense if the embedding has application-specific knowledge about how to implement the allocator. I've left some comments throughout.

src/dlmalloc.rs Outdated Show resolved Hide resolved
src/dlmalloc.rs Outdated Show resolved Hide resolved
src/dlmalloc.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated
/// Instances of this type are used to allocate blocks of memory. For best
/// results only use one of these. Currently doesn't implement `Drop` to release
/// lingering memory back to the OS. That may happen eventually though!
#[cfg(any(target_os = "linux", target_arch = "wasm32", target_os = "macos"))]
Copy link
Owner

Choose a reason for hiding this comment

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

This is similar to my comment above, but I would prefer that this structure not have a conditional definition.

Choose a reason for hiding this comment

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

It only conditionally sets the type parameter default. I don't see how you could have a default (in order not to break existing users) but also have platforms that don't have an in-crate implementation without conditional compilation.

Copy link
Owner

Choose a reason for hiding this comment

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

Platforms which don't have an implementation in this crate would default to just saying "allocation failed"

Copy link
Owner

Choose a reason for hiding this comment

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

I think this #[cfg] can be removed?

tests/smoke.rs Outdated Show resolved Hide resolved
@raoulstrackx
Copy link
Contributor Author

No worries about the timing @alexcrichton, thanks for changing your point of view on this. I'll address your comments as soon as possible.

src/dlmalloc.rs Outdated Show resolved Hide resolved
src/dlmalloc.rs Outdated Show resolved Hide resolved
src/dlmalloc.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated
/// Instances of this type are used to allocate blocks of memory. For best
/// results only use one of these. Currently doesn't implement `Drop` to release
/// lingering memory back to the OS. That may happen eventually though!
#[cfg(any(target_os = "linux", target_arch = "wasm32", target_os = "macos"))]
Copy link
Owner

Choose a reason for hiding this comment

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

Platforms which don't have an implementation in this crate would default to just saying "allocation failed"

@raoulstrackx
Copy link
Contributor Author

I don't see what exactly a default implementation of System that always returns an error would contribute. Isn't this just replacing a compile-time error with a runtime error? Agreed removing the conditional compilation would make the code more readable, but currently that is contained within the struct Dlmalloc. Note that such conditional compilation already needs to exist to select the correct sys module.

Copy link
Owner

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

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

I personally find that dealing with #[cfg] soup is much less fun and flexible than having APIs available that simply return errors. It's not great if you're not expecting an error but the error comes out really fast so long as you use basically anything. Overall I think the user experience is improved with fallible runtime methods rather than not-provided-at-compile-time methods.

src/dlmalloc.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
tests/smoke.rs Outdated Show resolved Hide resolved
@raoulstrackx raoulstrackx force-pushed the raoul/interface branch 2 times, most recently from 8b0a8ee to c798baf Compare November 2, 2020 09:21
src/lib.rs Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated
/// Instances of this type are used to allocate blocks of memory. For best
/// results only use one of these. Currently doesn't implement `Drop` to release
/// lingering memory back to the OS. That may happen eventually though!
#[cfg(any(target_os = "linux", target_arch = "wasm32", target_os = "macos"))]
Copy link
Owner

Choose a reason for hiding this comment

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

I think this #[cfg] can be removed?

src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated

impl<S> Dlmalloc<S> {
/// Creates a new instance of an allocator
pub const fn new_with_platform(sys_allocator: S) -> Dlmalloc<S> {
Copy link
Owner

Choose a reason for hiding this comment

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

I think this may want to be new_with_system since the name of the trait is "System"?

Copy link
Owner

Choose a reason for hiding this comment

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

This can also be lumped in with the impl block below I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately, that results in trait bounds other than Sized on const fn parameters are unstable. I'm assuming you'd want to avoid a const_fn crate attribute?

src/dlmalloc.rs Outdated Show resolved Hide resolved
src/dlmalloc.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated
@@ -29,15 +30,54 @@ mod dlmalloc;
#[cfg(all(feature = "global", not(test)))]
mod global;

/// A platform interface
pub unsafe trait System: Send {
Copy link
Owner

Choose a reason for hiding this comment

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

To bikeshed a bit, the documentation and wording here are used somewhat interchangeably (e.g. this trait's documentation says "platform" where the trait says "system"). Perhaps the trait could be Allocator and the default struct could be System (to mirror std::alloc a bit).

src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
@alexcrichton
Copy link
Owner

Ok this looks good to me, but I think CI is failing?

@jethrogb
Copy link

jethrogb commented Nov 4, 2020

CI is failing on master too

@raoulstrackx raoulstrackx force-pushed the raoul/interface branch 2 times, most recently from 944418c to b1f9c2f Compare November 5, 2020 15:55
@alexcrichton alexcrichton merged commit fbad771 into alexcrichton:master Nov 6, 2020
@alexcrichton
Copy link
Owner

Thanks again!

@raoulstrackx
Copy link
Contributor Author

Thanks for merging!

m-ou-se added a commit to m-ou-se/rust that referenced this pull request Nov 17, 2020
…Simulacrum

Upgrade dlmalloc to version 0.2

In preparation of adding dynamic memory management support for SGXv2-enabled platforms, the dlmalloc crate has been refactored. More specifically, support has been added to implement platform specification outside of the dlmalloc crate. (see alexcrichton/dlmalloc-rs#15)

This PR upgrades dlmalloc to version 0.2 for the `wasm` and `sgx` targets.

As the dlmalloc changes have received a positive review, but have not been merged yet, this PR contains a commit to prevent tidy from aborting CI prematurely.

cc: `@jethrogb`
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.

3 participants