Comments (4)
Starting from Rust 1.78, debug builds now check for null
pointer in slice_from_raw_parts
method, so now it seems to crash on start here (null
pointer gets passed along with zero length for empty slices):
gdnative/gdnative-core/src/export/method.rs
Lines 320 to 322 in 639aae7
The rationale for considering null
pointers invalid for zero-length slices is that Option
monad is optimized on compiler level in a way that zero-values are considered as None
(so if zero-length slice is constructed from 0x0
/0x0
parts, it would be matched as None
instead of Some()
inside of Option
). It's perfectly fine to pass any non-null
bogus, e.g. 0x1
etc.
I'm currently using this a quick workaround:
let args = if num_args > 0 { std::slice::from_raw_parts(args, num_args as usize) } else { &[] };
...but it may be sub-optimal, as it adds a branching condition in a pretty hot path. So maybe there's a better way to introduce a validation for zero-length slices and prevent passing null
as args
.
from gdnative.
Iterating on the workaround, I've adjusted my copy of method.rs
to include the original code in release builds (and the workaround in debug builds). Not necessarily the best way to do it, but sharing in case it's helpful:
pub unsafe fn from_sys(num_args: libc::c_int, args: *mut *mut sys::godot_variant) -> Self {
let args = Self::safe_args(num_args, args);
let args = std::mem::transmute::<&[*mut sys::godot_variant], &[&Variant]>(args);
Self {
idx: 0,
args,
offset_index: 0,
}
}
/// Convert args to a slice in debug builds - avoids crash when using Rust 1.78 and beyond
#[doc(hidden)]
#[inline]
#[cfg(debug_assertions)]
unsafe fn safe_args(
num_args: libc::c_int,
args: *mut *mut sys::godot_variant,
) -> &'a [*mut sys::godot_variant] {
if num_args > 0 {
std::slice::from_raw_parts(args, num_args as usize)
} else {
&[]
}
}
/// Convert args to a slice in release builds - using more effecient code than the version above
#[doc(hidden)]
#[inline]
#[cfg(not(debug_assertions))]
unsafe fn safe_args(
num_args: libc::c_int,
args: *mut *mut sys::godot_variant,
) -> &'a [*mut sys::godot_variant] {
std::slice::from_raw_parts(args, num_args as usize)
}
from gdnative.
Can confirm that I'm experiencing this under Ubuntu 20.04 (Linux 64-bit) with Rust 0.78.0 and Godot 3.5.3.
Can also confirm that the workaround fixed the crash here - thanks so much for sharing it! :)
from gdnative.
the only way to avoid such a branch is by simply keeping it as a pointer, instead of turning it into a slice, but that would take some work.
from gdnative.
Related Issues (20)
- Property Hint don't work properly using godot-rust v0.11.2 with Godot 3.5.1 HOT 2
- The draw_rect() "width" argument has no effect when "filled" is "true" warning HOT 4
- "This godot-rust version is only compatible with Godot 3.4.x; detected version 3.5.1-stable (official)." HOT 4
- Debug builds don't work on web HOT 1
- Multi-threading on web HOT 1
- `gdnative-core` reports versions mismatches even with `custom-godot` enabled
- Support custom versions of GDNative headers
- Investigate no_std, improve core / std consistency HOT 6
- Export functions from rust to gdscript HOT 4
- Simplify the `InitHandle` API HOT 4
- `Ref::upcast` should not require `SafeAsRaw`
- Outdated GDNative API: `determinant` method in Transform2D is not implemented HOT 1
- Rust Analyzer breaks without "one-class-one-file" feature. Enable "one-class-one-file" by default. HOT 1
- Godot-rust is hard depending on fragile parts of the Godot API and panicking if anything changes. HOT 1
- Vector4i doesn't implement "into Variant" HOT 2
- Outdated links in error message HOT 3
- NixOs Development Environment HOT 1
- `serde_yaml` end of life HOT 1
- GDNativeCallbacks is not unsafe to implement, but the deprecated macros think it is
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gdnative.