Skip to content

Conversation

jhshannon17
Copy link
Contributor

As detailed in this issue, in order to allow for vendor extensions to the standard, raw session/object handles need to be accessible outside of the crate. Creating new methods marked unsafe was the preferred way to allow this extension.

  • adds new_from_raw method to ObjectHandle to allow creation
  • adds raw_handle to ObjectHandle to get the underlying handle
  • adds raw_handle to Session to get the underlying handle

@hug-dev hug-dev linked an issue Oct 15, 2025 that may be closed by this pull request
@hug-dev hug-dev self-requested a review October 15, 2025 07:54
hug-dev
hug-dev previously approved these changes Oct 15, 2025
Copy link
Member

@hug-dev hug-dev left a comment

Choose a reason for hiding this comment

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

Looks nice! Thank you!

Was wondering if you could add a small test using those if that would make sense?

@jhshannon17
Copy link
Contributor Author

Looks nice! Thank you!

Was wondering if you could add a small test using those if that would make sense?

Absolutely!

Added a test case that resembles what I'm doing and exercises the new functions. Made small changes to the common test mod.

@jhshannon17
Copy link
Contributor Author

Happy to move/reformat anything if it aligns more with convention.

Copy link
Member

@hug-dev hug-dev left a comment

Choose a reason for hiding this comment

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

Thanks for the added test! It will be a very nice example to anyone wanting to do similar things 👍

@jhshannon17 jhshannon17 force-pushed the feature/pub-unsafe-handles branch from 7c0839c to 5de9fbf Compare October 15, 2025 18:30
@hug-dev
Copy link
Member

hug-dev commented Oct 16, 2025

If you want to make it easier for now and get the PR merged, you could just add a simple "smoke" test where we try to clone an existing ObjectHandle from its raw handle. Then we can think about how making your more complex and useful example in a separate one?

@wiktor-k
Copy link
Collaborator

Folks, is there a reason getting the raw handle is considered unsafe?

As far as I saw in Rust, usually it's the constructor functions that are unsafe (since the compiler cannot check the input for validity, e.g. CStr::from_bytes_with_nul_unchecked) but the accessor functions are not unsafe since it's assumed that the check for validity has already been done elsewhere (e.g. Cstr::as_ptr).

(As a side-note, quite a lot of structures already have inner accessors via AsRef<INNER_TYPE> and friends).

Thanks for your input!

@hug-dev
Copy link
Member

hug-dev commented Oct 16, 2025

is there a reason getting the raw handle is considered unsafe?

I thought about the same when reading as well! I also agree that it should be ok to make them safe as getting the value should not be a safety issue per se!

@wiktor-k
Copy link
Collaborator

is there a reason getting the raw handle is considered unsafe?

I thought about the same when reading as well! I also agree that it should be ok to make them safe as getting the value should not be a safety issue per se!

Great to hear. I think just extending the visibility of handle from pub(crate) to pub would be sufficient.

Just as a curiosity, Rust docs for CStr::as_ptr also contain a warning about pointer misuse:

WARNING
The returned pointer is read-only; writing to it (including passing it to C code that writes to it) causes undefined behavior.
It is your responsibility to make sure that the underlying memory is not freed too early. For example, the following code will cause undefined behavior when ptr is used inside the unsafe block:

and even though that function is not marked unsafe. I think we could add documentation similar to this as well and consider the job well done 😃

@jhshannon17
Copy link
Contributor Author

jhshannon17 commented Oct 16, 2025

I'm very happy to remove the new handle accessors and just change the existing ones to pub.

Are there any other feelings for the ObjectHandle new?

@wiktor-k
Copy link
Collaborator

Are there any other feelings for the ObjectHandle new?

new_from_raw is good 👌 I wonder if we could/should replace all calls to new within the library to new_from_raw because that's basically what it does... not sure how that change would propagate.

FWIW I'm okay with what's in here already, just wondering if there are more "low hanging fruit" to be harvested ;)

@jhshannon17
Copy link
Contributor Author

Are there any other feelings for the ObjectHandle new?

new_from_raw is good 👌 I wonder if we could/should replace all calls to new within the library to new_from_raw because that's basically what it does... not sure how that change would propagate.

FWIW I'm okay with what's in here already, just wondering if there are more "low hanging fruit" to be harvested ;)

Happy to harvest whatever is helpful - there are a number of places where new is currently being used that would introduce new unsafe blocks (mechanism/hkdf.rs, mechanism/kbkdf.rs) is that fine?

The other places it's used are just outside unsafe blocks so small to change.

@jhshannon17 jhshannon17 force-pushed the feature/pub-unsafe-handles branch 2 times, most recently from 3340e30 to bfe4634 Compare October 20, 2025 17:23
@jhshannon17
Copy link
Contributor Author

If you want to make it easier for now and get the PR merged, you could just add a simple "smoke" test where we try to clone an existing ObjectHandle from its raw handle. Then we can think about how making your more complex and useful example in a separate one?

Did this for now just to exercise the new ObjectHandle::new_from_raw.

I wonder if we could/should replace all calls to new within the library to new_from_raw because that's basically what it does... not sure how that change would propagate.

And still happy to make these changes if the two new unsafe blocks are fine.

Copy link
Member

@hug-dev hug-dev left a comment

Choose a reason for hiding this comment

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

Thank you for going through the comments! For me it's good as is. We can change all calls from new_from_raw to new in a later PR for all handle types.

The CI is failing, maybe #318 should be merged before?

@jhshannon17
Copy link
Contributor Author

The CI is failing, maybe #318 should be merged before?

Gotcha, will rebase when available.

@hug-dev
Copy link
Member

hug-dev commented Oct 22, 2025

You can try now!

…new_from_raw

Signed-off-by: Jack Shannon <jhshannon17@gmail.com>
@jhshannon17 jhshannon17 force-pushed the feature/pub-unsafe-handles branch from bfe4634 to a99ad88 Compare October 22, 2025 16:29
@jhshannon17
Copy link
Contributor Author

pushed!

@wiktor-k wiktor-k merged commit a884179 into parallaxsecond:main Oct 23, 2025
44 checks passed
@wiktor-k
Copy link
Collaborator

Happy to have this finally merged 🥳

Thanks for your work @jhshannon17 ! 🙇

@jhshannon17
Copy link
Contributor Author

Excellent, thank you all!

@jhshannon17 jhshannon17 deleted the feature/pub-unsafe-handles branch October 23, 2025 14:00
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.

Consider opening visibility on ObjectHandle and SessionHandle

4 participants