Skip to content

Commit

Permalink
Fix Rust 1.78 crash due to new slice.get_unchecked behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
AngheloAlf committed May 22, 2024
1 parent d3669aa commit f5c65d0
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 25 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
with two operands. Otherwise the `rd` register will be omitted if it is
`$ra` and will be used explicitly if it isn't `$ra`. Defaults to `False`.

### Fixed

- Fix crash in Rust bindings for to Rust 1.78.
- This happens because `slice.get_unchecked` now performs OoB checks in debug
builds, which is is triggered when trying to dereference unsized C arrays.

## [1.10.0] - 2024-04-22

### Added
Expand Down
135 changes: 110 additions & 25 deletions rust/src/registers_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ impl registers_enum::registers::GprO32 {
pub fn descriptor(&self) -> &RegisterDescriptor {
let reg_value: u32 = (*self).into();

unsafe { RabbitizerRegister_GprO32_Descriptors.get_unchecked(reg_value as usize) }
unsafe {
&*RabbitizerRegister_GprO32_Descriptors
.as_ptr()
.offset(reg_value as isize)
}
}
}

Expand All @@ -128,7 +132,11 @@ impl registers_enum::registers::GprN32 {
pub fn descriptor(&self) -> &RegisterDescriptor {
let reg_value: u32 = (*self).into();

unsafe { RabbitizerRegister_GprN32_Descriptors.get_unchecked(reg_value as usize) }
unsafe {
&*RabbitizerRegister_GprN32_Descriptors
.as_ptr()
.offset(reg_value as isize)
}
}
}

Expand All @@ -146,7 +154,11 @@ impl registers_enum::registers::Cop0 {
pub fn descriptor(&self) -> &RegisterDescriptor {
let reg_value: u32 = (*self).into();

unsafe { RabbitizerRegister_Cop0_Descriptors.get_unchecked(reg_value as usize) }
unsafe {
&*RabbitizerRegister_Cop0_Descriptors
.as_ptr()
.offset(reg_value as isize)
}
}
}

Expand All @@ -164,7 +176,11 @@ impl registers_enum::registers::Cop1O32 {
pub fn descriptor(&self) -> &RegisterDescriptor {
let reg_value: u32 = (*self).into();

unsafe { RabbitizerRegister_Cop1O32_Descriptors.get_unchecked(reg_value as usize) }
unsafe {
&*RabbitizerRegister_Cop1O32_Descriptors
.as_ptr()
.offset(reg_value as isize)
}
}
}

Expand All @@ -182,7 +198,11 @@ impl registers_enum::registers::Cop1N32 {
pub fn descriptor(&self) -> &RegisterDescriptor {
let reg_value: u32 = (*self).into();

unsafe { RabbitizerRegister_Cop1N32_Descriptors.get_unchecked(reg_value as usize) }
unsafe {
&*RabbitizerRegister_Cop1N32_Descriptors
.as_ptr()
.offset(reg_value as isize)
}
}
}

Expand All @@ -200,7 +220,11 @@ impl registers_enum::registers::Cop1N64 {
pub fn descriptor(&self) -> &RegisterDescriptor {
let reg_value: u32 = (*self).into();

unsafe { RabbitizerRegister_Cop1N64_Descriptors.get_unchecked(reg_value as usize) }
unsafe {
&*RabbitizerRegister_Cop1N64_Descriptors
.as_ptr()
.offset(reg_value as isize)
}
}
}

Expand All @@ -218,7 +242,11 @@ impl registers_enum::registers::Cop1Control {
pub fn descriptor(&self) -> &RegisterDescriptor {
let reg_value: u32 = (*self).into();

unsafe { RabbitizerRegister_Cop1Control_Descriptors.get_unchecked(reg_value as usize) }
unsafe {
&*RabbitizerRegister_Cop1Control_Descriptors
.as_ptr()
.offset(reg_value as isize)
}
}
}

Expand All @@ -236,7 +264,11 @@ impl registers_enum::registers::Cop2 {
pub fn descriptor(&self) -> &RegisterDescriptor {
let reg_value: u32 = (*self).into();

unsafe { RabbitizerRegister_Cop2_Descriptors.get_unchecked(reg_value as usize) }
unsafe {
&*RabbitizerRegister_Cop2_Descriptors
.as_ptr()
.offset(reg_value as isize)
}
}
}

Expand All @@ -254,7 +286,11 @@ impl registers_enum::registers::RspGpr {
pub fn descriptor(&self) -> &RegisterDescriptor {
let reg_value: u32 = (*self).into();

unsafe { RabbitizerRegister_RspGpr_Descriptors.get_unchecked(reg_value as usize) }
unsafe {
&*RabbitizerRegister_RspGpr_Descriptors
.as_ptr()
.offset(reg_value as isize)
}
}
}

Expand All @@ -272,7 +308,11 @@ impl registers_enum::registers::RspCop0 {
pub fn descriptor(&self) -> &RegisterDescriptor {
let reg_value: u32 = (*self).into();

unsafe { RabbitizerRegister_RspCop0_Descriptors.get_unchecked(reg_value as usize) }
unsafe {
&*RabbitizerRegister_RspCop0_Descriptors
.as_ptr()
.offset(reg_value as isize)
}
}
}

Expand All @@ -290,7 +330,11 @@ impl registers_enum::registers::RspCop2 {
pub fn descriptor(&self) -> &RegisterDescriptor {
let reg_value: u32 = (*self).into();

unsafe { RabbitizerRegister_RspCop2_Descriptors.get_unchecked(reg_value as usize) }
unsafe {
&*RabbitizerRegister_RspCop2_Descriptors
.as_ptr()
.offset(reg_value as isize)
}
}
}

Expand All @@ -308,7 +352,11 @@ impl registers_enum::registers::RspCop2Control {
pub fn descriptor(&self) -> &RegisterDescriptor {
let reg_value: u32 = (*self).into();

unsafe { RabbitizerRegister_RspCop2Control_Descriptors.get_unchecked(reg_value as usize) }
unsafe {
&*RabbitizerRegister_RspCop2Control_Descriptors
.as_ptr()
.offset(reg_value as isize)
}
}
}

Expand All @@ -326,7 +374,11 @@ impl registers_enum::registers::RspVector {
pub fn descriptor(&self) -> &RegisterDescriptor {
let reg_value: u32 = (*self).into();

unsafe { RabbitizerRegister_RspVector_Descriptors.get_unchecked(reg_value as usize) }
unsafe {
&*RabbitizerRegister_RspVector_Descriptors
.as_ptr()
.offset(reg_value as isize)
}
}
}

Expand All @@ -344,7 +396,11 @@ impl registers_enum::registers::R4000AllegrexS {
pub fn descriptor(&self) -> &RegisterDescriptor {
let reg_value: u32 = (*self).into();

unsafe { RabbitizerRegister_R4000AllegrexS_Descriptors.get_unchecked(reg_value as usize) }
unsafe {
&*RabbitizerRegister_R4000AllegrexS_Descriptors
.as_ptr()
.offset(reg_value as isize)
}
}
}

Expand All @@ -364,7 +420,11 @@ impl registers_enum::registers::R4000AllegrexV2D {
pub fn descriptor(&self) -> &RegisterDescriptor {
let reg_value: u32 = (*self).into();

unsafe { RabbitizerRegister_R4000AllegrexV2D_Descriptors.get_unchecked(reg_value as usize) }
unsafe {
&*RabbitizerRegister_R4000AllegrexV2D_Descriptors
.as_ptr()
.offset(reg_value as isize)
}
}
}

Expand All @@ -384,7 +444,11 @@ impl registers_enum::registers::R4000AllegrexV3D {
pub fn descriptor(&self) -> &RegisterDescriptor {
let reg_value: u32 = (*self).into();

unsafe { RabbitizerRegister_R4000AllegrexV3D_Descriptors.get_unchecked(reg_value as usize) }
unsafe {
&*RabbitizerRegister_R4000AllegrexV3D_Descriptors
.as_ptr()
.offset(reg_value as isize)
}
}
}

Expand All @@ -404,7 +468,11 @@ impl registers_enum::registers::R4000AllegrexV4D {
pub fn descriptor(&self) -> &RegisterDescriptor {
let reg_value: u32 = (*self).into();

unsafe { RabbitizerRegister_R4000AllegrexV4D_Descriptors.get_unchecked(reg_value as usize) }
unsafe {
&*RabbitizerRegister_R4000AllegrexV4D_Descriptors
.as_ptr()
.offset(reg_value as isize)
}
}
}

Expand All @@ -425,7 +493,9 @@ impl registers_enum::registers::R4000AllegrexM2x2 {
let reg_value: u32 = (*self).into();

unsafe {
RabbitizerRegister_R4000AllegrexM2x2_Descriptors.get_unchecked(reg_value as usize)
&*RabbitizerRegister_R4000AllegrexM2x2_Descriptors
.as_ptr()
.offset(reg_value as isize)
}
}
}
Expand All @@ -447,7 +517,9 @@ impl registers_enum::registers::R4000AllegrexM3x3 {
let reg_value: u32 = (*self).into();

unsafe {
RabbitizerRegister_R4000AllegrexM3x3_Descriptors.get_unchecked(reg_value as usize)
&*RabbitizerRegister_R4000AllegrexM3x3_Descriptors
.as_ptr()
.offset(reg_value as isize)
}
}
}
Expand All @@ -469,7 +541,9 @@ impl registers_enum::registers::R4000AllegrexM4x4 {
let reg_value: u32 = (*self).into();

unsafe {
RabbitizerRegister_R4000AllegrexM4x4_Descriptors.get_unchecked(reg_value as usize)
&*RabbitizerRegister_R4000AllegrexM4x4_Descriptors
.as_ptr()
.offset(reg_value as isize)
}
}
}
Expand All @@ -491,8 +565,9 @@ impl registers_enum::registers::R4000AllegrexVfpuControl {
let reg_value: u32 = (*self).into();

unsafe {
RabbitizerRegister_R4000AllegrexVfpuControl_Descriptors
.get_unchecked(reg_value as usize)
&*RabbitizerRegister_R4000AllegrexVfpuControl_Descriptors
.as_ptr()
.offset(reg_value as isize)
}
}
}
Expand All @@ -514,7 +589,9 @@ impl registers_enum::registers::R4000AllegrexVConstant {
let reg_value: u32 = (*self).into();

unsafe {
RabbitizerRegister_R4000AllegrexVConstant_Descriptors.get_unchecked(reg_value as usize)
&*RabbitizerRegister_R4000AllegrexVConstant_Descriptors
.as_ptr()
.offset(reg_value as isize)
}
}
}
Expand All @@ -533,7 +610,11 @@ impl registers_enum::registers::R5900VF {
pub fn descriptor(&self) -> &RegisterDescriptor {
let reg_value: u32 = (*self).into();

unsafe { RabbitizerRegister_R5900VF_Descriptors.get_unchecked(reg_value as usize) }
unsafe {
&*RabbitizerRegister_R5900VF_Descriptors
.as_ptr()
.offset(reg_value as isize)
}
}
}

Expand All @@ -549,6 +630,10 @@ impl registers_enum::registers::R5900VI {
pub fn descriptor(&self) -> &RegisterDescriptor {
let reg_value: u32 = (*self).into();

unsafe { RabbitizerRegister_R5900VI_Descriptors.get_unchecked(reg_value as usize) }
unsafe {
&*RabbitizerRegister_R5900VI_Descriptors
.as_ptr()
.offset(reg_value as isize)
}
}
}

0 comments on commit f5c65d0

Please sign in to comment.