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

[Merged] Fix UB related to engine enum layout #326

Closed
wants to merge 1 commit into from

Conversation

lilizoey
Copy link
Member

EngineEnums should be represented as 32-bit values, but also should be passed as i64 to/from godot in function parameters/return values.

This fixes that by having engine enums not implement GodotFfi but instead only implements GodotFuncMarshal.

To support this change, GodotFuncMarshal has been refactored:

  • Via now represents only the intermediate type, and must implement GodotFfi
  • Add error-types for when conversion fails
  • Add explicit conversion methods to/from Via, we can't just use TryFrom here since not all types we wanna use have the proper TryFrom conversions. like f32 and f64

With this change, a minor change was made to the class generator, such that all arguments are first converted to their Via type, before a pointer to them is made. And return pointers are now dereferenced to the Via type before they're converted to the proper type.

This means that GodotFuncMarshal is now the type that represents whether something can be passed as an argument/return value to a ptrcall.

AsArg was also removed and its method moved into GodotFfi as that simplifies codegen. (we can just always call GodotFfi::as_arg_ptr to create the pointer, rather than treating classes and other types differently). In the future we can try splitting up GodotFfi more, but i do think that AsArg belonged more in godot_ffi anyway.

@lilizoey lilizoey added bug c: ffi Low-level components and interaction with GDExtension API labels Jun 30, 2023
@lilizoey lilizoey marked this pull request as draft June 30, 2023 19:11
@lilizoey
Copy link
Member Author

oh i accidentally included #323, i'll remove that in a bit

@GodotRust
Copy link

API docs are being generated and will be shortly available at: https://godot-rust.github.io/docs/gdext/pr-326

@lilizoey lilizoey changed the title Fix UB related to engine enum layou Fix UB related to engine enum layout Jun 30, 2023
Copy link
Member

@Bromeon Bromeon 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! Nice if we can get rid of AsArg 🙂

godot-codegen/src/class_generator.rs Outdated Show resolved Hide resolved
godot-ffi/src/godot_ffi.rs Outdated Show resolved Hide resolved
godot-ffi/src/godot_ffi.rs Outdated Show resolved Hide resolved
godot-ffi/src/godot_ffi.rs Outdated Show resolved Hide resolved
@lilizoey
Copy link
Member Author

lilizoey commented Jul 1, 2023

bors try

bors bot added a commit that referenced this pull request Jul 1, 2023
- Move `AsArg` into `GodotFfi`
- Refactor `GodotFuncMarshal`
@bors
Copy link
Contributor

bors bot commented Jul 1, 2023

try

Build succeeded!

The publicly hosted instance of bors-ng is deprecated and will go away soon.

If you want to self-host your own instance, instructions are here.
For more help, visit the forum.

If you want to switch to GitHub's built-in merge queue, visit their help page.

@lilizoey lilizoey marked this pull request as ready for review July 1, 2023 18:09
@lilizoey lilizoey requested a review from Bromeon July 1, 2023 18:10
Copy link
Member

@Bromeon Bromeon left a comment

Choose a reason for hiding this comment

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

Thanks!

bors r+

Comment on lines +605 to +607
// Some places Godot will pass along 64-bit numbers that are intended to be interpreted as unsigned
// integers despite Godot integers being signed by default.
impl_godot_marshalling!(u64 as i64; lossy);
Copy link
Member

Choose a reason for hiding this comment

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

Regarding this, I will open an issue, so we don't forget to remove it from being passed via #[func].

bors bot added a commit that referenced this pull request Jul 2, 2023
326: Fix UB related to engine enum layout r=Bromeon a=lilizoey

`EngineEnum`s should be represented as 32-bit values, but also should be passed as i64 to/from godot in function parameters/return values.

This fixes that by having engine enums not implement `GodotFfi` but instead only implements `GodotFuncMarshal`.

To support this change, `GodotFuncMarshal` has been refactored:
- `Via` now represents only the intermediate type, and must implement `GodotFfi`
- Add error-types for when conversion fails
- Add explicit conversion methods to/from `Via`, we can't just use `TryFrom` here since not all types we wanna use have the proper `TryFrom` conversions. like f32 and f64

With this change, a minor change was made to the class generator, such that all arguments are first converted to their `Via` type, before a pointer to them is made. And return pointers are now dereferenced to the `Via` type before they're converted to the proper type. 

This means that `GodotFuncMarshal` is now the type that represents whether something can be passed as an argument/return value to a ptrcall.

`AsArg` was also removed and its method moved into `GodotFfi` as that simplifies codegen. (we can just always call `GodotFfi::as_arg_ptr` to create the pointer, rather than treating classes and other types differently). In the future we can try splitting up `GodotFfi` more, but i do think that `AsArg` belonged more in `godot_ffi` anyway.

Co-authored-by: Lili Zoey <lili.andersen@nrk.no>
@bors
Copy link
Contributor

bors bot commented Jul 2, 2023

Build succeeded!

The publicly hosted instance of bors-ng is deprecated and will go away soon.

If you want to self-host your own instance, instructions are here.
For more help, visit the forum.

If you want to switch to GitHub's built-in merge queue, visit their help page.

@Bromeon
Copy link
Member

Bromeon commented Jul 2, 2023

What is wrong with merging lately? Merge conflicts after merging?

Maybe time to look into #255 soon...

@Bromeon Bromeon closed this Jul 2, 2023
@Bromeon Bromeon changed the title Fix UB related to engine enum layout [Merged] Fix UB related to engine enum layout Jul 2, 2023
@Bromeon Bromeon added the ub Undefined behavior label Jul 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug c: ffi Low-level components and interaction with GDExtension API ub Undefined behavior
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants