Skip to content

Commit cb7a6f4

Browse files
committed
BUGFIX: variable size discrepancy with FFI for Rust enums
Rust enums use 8-bit number for representation of values where as c uses 32-bit number. This caused the issue in release mode. The compiler optimized these variables in release mode which must have caused over-bound writes thus causing the program crash. Fixes #73
1 parent 84b6c59 commit cb7a6f4

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

src/array.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ extern {
2626

2727
fn af_get_elements(out: MutAfArray, arr: AfArray) -> c_int;
2828

29-
fn af_get_type(out: *mut uint8_t, arr: AfArray) -> c_int;
29+
fn af_get_type(out: *mut c_int, arr: AfArray) -> c_int;
3030

3131
fn af_get_dims(dim0: *mut c_longlong, dim1: *mut c_longlong, dim2: *mut c_longlong,
3232
dim3: *mut c_longlong, arr: AfArray) -> c_int;
@@ -206,8 +206,8 @@ impl Array {
206206
/// Returns the Array data type
207207
pub fn get_type(&self) -> DType {
208208
unsafe {
209-
let mut ret_val: u8 = 0;
210-
let err_val = af_get_type(&mut ret_val as *mut uint8_t, self.handle as AfArray);
209+
let mut ret_val: i32 = 0;
210+
let err_val = af_get_type(&mut ret_val as *mut c_int, self.handle as AfArray);
211211
HANDLE_ERROR(AfError::from(err_val));
212212
DType::from(ret_val)
213213
}

src/defines.rs

+10
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ impl Error for AfError {
106106
}
107107

108108
/// Types of Array data type
109+
#[repr(C)]
109110
#[derive(Clone, Copy, Debug, PartialEq)]
110111
pub enum DType {
111112
/// 32 bit float
@@ -135,6 +136,7 @@ pub enum DType {
135136
}
136137

137138
/// Dictates the interpolation method to be used by a function
139+
#[repr(C)]
138140
#[derive(Clone, Copy, Debug, PartialEq)]
139141
pub enum InterpType {
140142
/// Nearest Neighbor interpolation method
@@ -148,6 +150,7 @@ pub enum InterpType {
148150
}
149151

150152
/// Helps determine how to pad kernels along borders
153+
#[repr(C)]
151154
#[derive(Clone, Copy, Debug, PartialEq)]
152155
pub enum BorderType {
153156
/// Pad using zeros
@@ -157,6 +160,7 @@ pub enum BorderType {
157160
}
158161

159162
/// Used by `regions` function to identify type of connectivity
163+
#[repr(C)]
160164
#[derive(Clone, Copy, Debug, PartialEq)]
161165
pub enum Connectivity {
162166
/// North-East-South-West (N-E-S-W) connectivity from given pixel/point
@@ -166,6 +170,7 @@ pub enum Connectivity {
166170
}
167171

168172
/// Helps determine the size of output of convolution
173+
#[repr(C)]
169174
#[derive(Clone, Copy, Debug, PartialEq)]
170175
pub enum ConvMode {
171176
/// Default convolution mode where output size is same as input size
@@ -175,6 +180,7 @@ pub enum ConvMode {
175180
}
176181

177182
/// Helps determine if convolution is in Spatial or Frequency domain
183+
#[repr(C)]
178184
#[derive(Clone, Copy, Debug, PartialEq)]
179185
pub enum ConvDomain {
180186
/// ArrayFire chooses whether the convolution will be in spatial domain or frequency domain
@@ -186,6 +192,7 @@ pub enum ConvDomain {
186192
}
187193

188194
/// Error metric used by `matchTemplate` function
195+
#[repr(C)]
189196
#[derive(Clone, Copy, Debug, PartialEq)]
190197
pub enum MatchType {
191198
/// Sum of Absolute Differences
@@ -209,6 +216,7 @@ pub enum MatchType {
209216
}
210217

211218
/// Identify the color space of given image(Array)
219+
#[repr(C)]
212220
#[derive(Clone, Copy, Debug, PartialEq)]
213221
pub enum ColorSpace {
214222
/// Grayscale color space
@@ -220,6 +228,7 @@ pub enum ColorSpace {
220228
}
221229

222230
/// Helps determine the type of a Matrix
231+
#[repr(C)]
223232
#[derive(Clone, Copy, Debug, PartialEq)]
224233
pub enum MatProp {
225234
/// Default (no-op)
@@ -248,6 +257,7 @@ pub enum MatProp {
248257

249258
/// Norm type
250259
#[allow(non_camel_case_types)]
260+
#[repr(C)]
251261
#[derive(Clone, Copy, Debug, PartialEq)]
252262
pub enum NormType {
253263
/// Treats input as a vector and return sum of absolute values

src/util.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,37 @@ impl From<i32> for AfError {
1111
}
1212
}
1313

14-
impl From<u8> for DType {
15-
fn from(t: u8) -> DType {
16-
assert!(DType::F32 as u8 <= t && t <= DType::U64 as u8);
14+
impl From<i32> for DType {
15+
fn from(t: i32) -> DType {
16+
assert!(DType::F32 as i32 <= t && t <= DType::U64 as i32);
1717
unsafe { mem::transmute(t) }
1818
}
1919
}
2020

21-
impl From<u8> for InterpType {
22-
fn from(t: u8) -> InterpType {
23-
assert!(InterpType::NEAREST as u8 <= t && t <= InterpType::CUBIC as u8);
21+
impl From<i32> for InterpType {
22+
fn from(t: i32) -> InterpType {
23+
assert!(InterpType::NEAREST as i32 <= t && t <= InterpType::CUBIC as i32);
2424
unsafe { mem::transmute(t) }
2525
}
2626
}
2727

28-
impl From<u8> for ConvMode {
29-
fn from(t: u8) -> ConvMode {
30-
assert!(ConvMode::DEFAULT as u8 <= t && t <= ConvMode::EXPAND as u8);
28+
impl From<i32> for ConvMode {
29+
fn from(t: i32) -> ConvMode {
30+
assert!(ConvMode::DEFAULT as i32 <= t && t <= ConvMode::EXPAND as i32);
3131
unsafe { mem::transmute(t) }
3232
}
3333
}
3434

35-
impl From<u8> for ConvDomain {
36-
fn from(t: u8) -> ConvDomain {
37-
assert!(ConvDomain::AUTO as u8 <= t && t <= ConvDomain::FREQUENCY as u8);
35+
impl From<i32> for ConvDomain {
36+
fn from(t: i32) -> ConvDomain {
37+
assert!(ConvDomain::AUTO as i32 <= t && t <= ConvDomain::FREQUENCY as i32);
3838
unsafe { mem::transmute(t) }
3939
}
4040
}
4141

42-
impl From<u8> for MatchType {
43-
fn from(t: u8) -> MatchType {
44-
assert!(MatchType::SAD as u8 <= t && t <= MatchType::SHD as u8);
42+
impl From<i32> for MatchType {
43+
fn from(t: i32) -> MatchType {
44+
assert!(MatchType::SAD as i32 <= t && t <= MatchType::SHD as i32);
4545
unsafe { mem::transmute(t) }
4646
}
4747
}

0 commit comments

Comments
 (0)