@@ -1162,10 +1162,11 @@ impl Architecture for CoreArchitecture {
1162
1162
& mut result as * mut _ ,
1163
1163
& mut count as * mut _ ,
1164
1164
) {
1165
- let vec = Vec :: < BNInstructionTextToken > :: from_raw_parts ( result, count , count)
1165
+ let vec = slice :: from_raw_parts ( result, count)
1166
1166
. iter ( )
1167
- . map ( |x| InstructionTextToken :: from_raw ( x) )
1167
+ . map ( |x| InstructionTextToken :: from_raw ( x) . to_owned ( ) )
1168
1168
. collect ( ) ;
1169
+ BNFreeInstructionText ( result, count) ;
1169
1170
Some ( ( consumed, vec) )
1170
1171
} else {
1171
1172
None
@@ -1810,27 +1811,25 @@ where
1810
1811
let data = unsafe { slice:: from_raw_parts ( data, * len) } ;
1811
1812
let result = unsafe { & mut * result } ;
1812
1813
1813
- match custom_arch. instruction_text ( data, addr) {
1814
- Some ( ( res_size, mut res_tokens) ) => {
1815
- unsafe {
1816
- // TODO: Can't use into_raw_parts as it's unstable so we do this instead...
1817
- let r_ptr = res_tokens. as_mut_ptr ( ) ;
1818
- let r_count = res_tokens. len ( ) ;
1819
- mem:: forget ( res_tokens) ;
1820
-
1821
- * result = & mut ( * r_ptr) . 0 ;
1822
- * count = r_count;
1823
- * len = res_size;
1824
- }
1825
- true
1826
- }
1827
- None => false ,
1814
+ let Some ( ( res_size, res_tokens) ) = custom_arch. instruction_text ( data, addr) else {
1815
+ return false ;
1816
+ } ;
1817
+
1818
+ let res_tokens: Box < [ _ ] > = res_tokens. into_boxed_slice ( ) ;
1819
+ unsafe {
1820
+ let res_tokens = Box :: leak ( res_tokens) ;
1821
+ let r_ptr = res_tokens. as_mut_ptr ( ) ;
1822
+ let r_count = res_tokens. len ( ) ;
1823
+
1824
+ * result = & mut ( * r_ptr) . 0 ;
1825
+ * count = r_count;
1826
+ * len = res_size;
1828
1827
}
1828
+ true
1829
1829
}
1830
1830
1831
1831
extern "C" fn cb_free_instruction_text ( tokens : * mut BNInstructionTextToken , count : usize ) {
1832
- let _tokens =
1833
- unsafe { Vec :: from_raw_parts ( tokens as * mut InstructionTextToken , count, count) } ;
1832
+ let _tokens = unsafe { Box :: from_raw ( ptr:: slice_from_raw_parts_mut ( tokens, count) ) } ;
1834
1833
}
1835
1834
1836
1835
extern "C" fn cb_instruction_llil < A > (
@@ -1930,15 +1929,7 @@ where
1930
1929
if len == 0 {
1931
1930
ptr:: null_mut ( )
1932
1931
} else {
1933
- let mut res = Vec :: with_capacity ( len + 1 ) ;
1934
-
1935
- res. push ( len as u32 ) ;
1936
-
1937
- for i in items {
1938
- res. push ( i) ;
1939
- }
1940
-
1941
- assert ! ( res. len( ) == len + 1 ) ;
1932
+ let mut res: Box < [ _ ] > = [ len as u32 ] . into_iter ( ) . chain ( items) . collect ( ) ;
1942
1933
1943
1934
let raw = res. as_mut_ptr ( ) ;
1944
1935
mem:: forget ( res) ;
@@ -2279,7 +2270,8 @@ where
2279
2270
unsafe {
2280
2271
let actual_start = regs. offset ( -1 ) ;
2281
2272
let len = * actual_start + 1 ;
2282
- let _regs = Vec :: from_raw_parts ( actual_start, len as usize , len as usize ) ;
2273
+ let regs_ptr = ptr:: slice_from_raw_parts_mut ( actual_start, len. try_into ( ) . unwrap ( ) ) ;
2274
+ let _regs = Box :: from_raw ( regs_ptr) ;
2283
2275
}
2284
2276
}
2285
2277
@@ -2419,28 +2411,25 @@ where
2419
2411
{
2420
2412
let custom_arch = unsafe { & * ( ctxt as * mut A ) } ;
2421
2413
2422
- if let Some ( intrinsic) = custom_arch. intrinsic_from_id ( intrinsic) {
2423
- let inputs = intrinsic. inputs ( ) ;
2424
- let mut res = Vec :: with_capacity ( inputs. len ( ) ) ;
2425
- for input in inputs {
2426
- res. push ( unsafe { Ref :: into_raw ( input) } . into_raw ( ) ) ;
2427
- }
2428
-
2429
- unsafe {
2430
- * count = res. len ( ) ;
2431
- if res. is_empty ( ) {
2432
- ptr:: null_mut ( )
2433
- } else {
2434
- let raw = res. as_mut_ptr ( ) ;
2435
- mem:: forget ( res) ;
2436
- raw
2437
- }
2438
- }
2439
- } else {
2414
+ let Some ( intrinsic) = custom_arch. intrinsic_from_id ( intrinsic) else {
2440
2415
unsafe {
2441
2416
* count = 0 ;
2442
2417
}
2443
- ptr:: null_mut ( )
2418
+ return ptr:: null_mut ( ) ;
2419
+ } ;
2420
+
2421
+ let inputs = intrinsic. inputs ( ) ;
2422
+ let mut res: Box < [ _ ] > = inputs. into_iter ( ) . map ( |input| input. into_raw ( ) ) . collect ( ) ;
2423
+
2424
+ unsafe {
2425
+ * count = res. len ( ) ;
2426
+ if res. is_empty ( ) {
2427
+ ptr:: null_mut ( )
2428
+ } else {
2429
+ let raw = res. as_mut_ptr ( ) ;
2430
+ mem:: forget ( res) ;
2431
+ raw
2432
+ }
2444
2433
}
2445
2434
}
2446
2435
@@ -2452,8 +2441,8 @@ where
2452
2441
2453
2442
if !nt. is_null ( ) {
2454
2443
unsafe {
2455
- let list = Vec :: from_raw_parts ( nt, count, count ) ;
2456
- for nt in list {
2444
+ let name_and_types = Box :: from_raw ( ptr :: slice_from_raw_parts_mut ( nt, count) ) ;
2445
+ for nt in name_and_types . into_iter ( ) {
2457
2446
BnString :: from_raw ( nt. name ) ;
2458
2447
}
2459
2448
}
@@ -2472,10 +2461,7 @@ where
2472
2461
2473
2462
if let Some ( intrinsic) = custom_arch. intrinsic_from_id ( intrinsic) {
2474
2463
let inputs = intrinsic. outputs ( ) ;
2475
- let mut res = Vec :: with_capacity ( inputs. len ( ) ) ;
2476
- for input in inputs. iter ( ) {
2477
- res. push ( input. as_ref ( ) . into ( ) ) ;
2478
- }
2464
+ let mut res: Box < [ _ ] > = inputs. iter ( ) . map ( |input| input. as_ref ( ) . into ( ) ) . collect ( ) ;
2479
2465
2480
2466
unsafe {
2481
2467
* count = res. len ( ) ;
@@ -2504,9 +2490,7 @@ where
2504
2490
{
2505
2491
let _custom_arch = unsafe { & * ( ctxt as * mut A ) } ;
2506
2492
if !tl. is_null ( ) {
2507
- unsafe {
2508
- let _list = Vec :: from_raw_parts ( tl, count, count) ;
2509
- }
2493
+ let _type_list = unsafe { Box :: from_raw ( ptr:: slice_from_raw_parts_mut ( tl, count) ) } ;
2510
2494
}
2511
2495
}
2512
2496
0 commit comments