Skip to content

Commit 175fa73

Browse files
committed
Move malloc,free,realloc,calloc,strdup in libcc2rs
1 parent 60deba8 commit 175fa73

10 files changed

Lines changed: 94 additions & 48 deletions

File tree

libcc2rs/src/alloc.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
use std::ffi::{c_char, c_void};
5+
6+
unsafe extern "C" {
7+
fn malloc(size: usize) -> *mut c_void;
8+
fn free(ptr: *mut c_void);
9+
fn realloc(ptr: *mut c_void, size: usize) -> *mut c_void;
10+
fn calloc(nmemb: usize, size: usize) -> *mut c_void;
11+
fn strdup(s: *const c_char) -> *mut c_char;
12+
}
13+
14+
/// # Safety
15+
///
16+
/// Same contract as C's `malloc`.
17+
pub unsafe fn malloc_unsafe(a0: u64) -> *mut c_void {
18+
unsafe { malloc(a0 as usize) }
19+
}
20+
21+
/// # Safety
22+
///
23+
/// Same contract as C's `free`.
24+
pub unsafe fn free_unsafe(a0: *mut c_void) {
25+
unsafe { free(a0) }
26+
}
27+
28+
/// # Safety
29+
///
30+
/// Same contract as C's `realloc`.
31+
pub unsafe fn realloc_unsafe(a0: *mut c_void, a1: u64) -> *mut c_void {
32+
unsafe { realloc(a0, a1 as usize) }
33+
}
34+
35+
/// # Safety
36+
///
37+
/// Same contract as C's `calloc`.
38+
pub unsafe fn calloc_unsafe(a0: u64, a1: u64) -> *mut c_void {
39+
unsafe { calloc(a0 as usize, a1 as usize) }
40+
}
41+
42+
/// # Safety
43+
///
44+
/// Same contract as C's `strdup`.
45+
pub unsafe fn strdup_unsafe(a0: *const u8) -> *mut u8 {
46+
unsafe { strdup(a0 as *const c_char) as *mut u8 }
47+
}

libcc2rs/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ pub use rules::*;
2222
mod io;
2323
pub use io::*;
2424

25+
mod alloc;
26+
pub use alloc::*;
27+
2528
mod iterators;
2629
pub use iterators::*;
2730

rules/cstdlib/ir_unsafe.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"f2": {
1010
"body": [
1111
{
12-
"text": "libc::free("
12+
"text": "libcc2rs::free_unsafe("
1313
},
1414
{
1515
"placeholder": {
@@ -31,7 +31,7 @@
3131
"f3": {
3232
"body": [
3333
{
34-
"text": "libc::malloc("
34+
"text": "libcc2rs::malloc_unsafe("
3535
},
3636
{
3737
"placeholder": {
@@ -40,7 +40,7 @@
4040
}
4141
},
4242
{
43-
"text": " as ::libc::size_t)"
43+
"text": ")"
4444
}
4545
],
4646
"params": {
@@ -56,7 +56,7 @@
5656
"f4": {
5757
"body": [
5858
{
59-
"text": "libc::realloc("
59+
"text": "libcc2rs::realloc_unsafe("
6060
},
6161
{
6262
"placeholder": {
@@ -74,7 +74,7 @@
7474
}
7575
},
7676
{
77-
"text": " as ::libc::size_t)"
77+
"text": ")"
7878
}
7979
],
8080
"params": {
@@ -94,7 +94,7 @@
9494
"f5": {
9595
"body": [
9696
{
97-
"text": "libc::calloc("
97+
"text": "libcc2rs::calloc_unsafe("
9898
},
9999
{
100100
"placeholder": {
@@ -103,7 +103,7 @@
103103
}
104104
},
105105
{
106-
"text": " as ::libc::size_t, "
106+
"text": ", "
107107
},
108108
{
109109
"placeholder": {
@@ -112,7 +112,7 @@
112112
}
113113
},
114114
{
115-
"text": " as ::libc::size_t)"
115+
"text": ")"
116116
}
117117
],
118118
"params": {

rules/cstdlib/tgt_unsafe.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ unsafe fn f1() {
66
}
77

88
unsafe fn f2(a0: *mut ::libc::c_void) {
9-
libc::free(a0)
9+
libcc2rs::free_unsafe(a0)
1010
}
1111

1212
unsafe fn f3(a0: u64) -> *mut ::libc::c_void {
13-
libc::malloc(a0 as ::libc::size_t)
13+
libcc2rs::malloc_unsafe(a0)
1414
}
1515

1616
unsafe fn f4(a0: *mut ::libc::c_void, a1: u64) -> *mut ::libc::c_void {
17-
libc::realloc(a0, a1 as ::libc::size_t)
17+
libcc2rs::realloc_unsafe(a0, a1)
1818
}
1919

2020
unsafe fn f5(a0: u64, a1: u64) -> *mut ::libc::c_void {
21-
libc::calloc(a0 as ::libc::size_t, a1 as ::libc::size_t)
21+
libcc2rs::calloc_unsafe(a0, a1)
2222
}
2323

2424
unsafe fn f6(a0: *const u8) -> *mut u8 {

rules/cstring/ir_unsafe.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@
283283
"f15": {
284284
"body": [
285285
{
286-
"text": "libc::strdup("
286+
"text": "libcc2rs::strdup_unsafe("
287287
},
288288
{
289289
"placeholder": {
@@ -292,7 +292,7 @@
292292
}
293293
},
294294
{
295-
"text": " as *const i8) as *mut u8"
295+
"text": ")"
296296
}
297297
],
298298
"params": {

rules/cstring/tgt_unsafe.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ unsafe fn f14(a0: *mut u8, a1: i32) -> *mut u8 {
7777
}
7878

7979
unsafe fn f15(a0: *const u8) -> *mut u8 {
80-
libc::strdup(a0 as *const i8) as *mut u8
80+
libcc2rs::strdup_unsafe(a0)
8181
}
8282

8383
unsafe fn f16(a0: *const u8, a1: *const u8) -> u64 {

tests/unit/out/unsafe/cstring.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,30 +267,30 @@ pub unsafe fn test_strrchr_9() {
267267
);
268268
}
269269
pub unsafe fn test_strdup_10() {
270-
let mut d: *mut u8 = libc::strdup(b"hello\0".as_ptr() as *const i8) as *mut u8;
270+
let mut d: *mut u8 = libcc2rs::strdup_unsafe(b"hello\0".as_ptr());
271271
assert!(!((d).is_null()));
272272
assert!(
273273
((libc::strcmp(
274274
(d).cast_const() as *const i8,
275275
b"hello\0".as_ptr() as *const i8
276276
)) == (0))
277277
);
278-
libc::free((d as *mut u8 as *mut ::libc::c_void));
278+
libcc2rs::free_unsafe((d as *mut u8 as *mut ::libc::c_void));
279279
let mut p: *const u8 = b"world\0".as_ptr();
280280
let mut buf: [u8; 4] = [('a' as u8), ('b' as u8), ('c' as u8), ('\0' as u8)];
281-
let mut d2: *mut u8 = libc::strdup(p as *const i8) as *mut u8;
281+
let mut d2: *mut u8 = libcc2rs::strdup_unsafe(p);
282282
assert!(!((d2).is_null()));
283283
assert!(((libc::strcmp((d2).cast_const() as *const i8, p as *const i8)) == (0)));
284-
libc::free((d2 as *mut u8 as *mut ::libc::c_void));
285-
let mut d3: *mut u8 = libc::strdup((buf.as_mut_ptr()).cast_const() as *const i8) as *mut u8;
284+
libcc2rs::free_unsafe((d2 as *mut u8 as *mut ::libc::c_void));
285+
let mut d3: *mut u8 = libcc2rs::strdup_unsafe((buf.as_mut_ptr()).cast_const());
286286
assert!(!((d3).is_null()));
287287
assert!(
288288
((libc::strcmp(
289289
(d3).cast_const() as *const i8,
290290
(buf.as_mut_ptr()).cast_const() as *const i8
291291
)) == (0))
292292
);
293-
libc::free((d3 as *mut u8 as *mut ::libc::c_void));
293+
libcc2rs::free_unsafe((d3 as *mut u8 as *mut ::libc::c_void));
294294
}
295295
pub unsafe fn test_strcspn_11() {
296296
assert!(

tests/unit/out/unsafe/errno.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@ pub unsafe fn test_errno_0() {
1717
}
1818
pub unsafe fn test_errno_preserved_across_strdup_1() {
1919
(*libcc2rs::cpp2rust_errno()) = 99;
20-
let mut d: *mut u8 =
21-
libc::strdup((b"hello\0".as_ptr().cast_mut()).cast_const() as *const i8) as *mut u8;
20+
let mut d: *mut u8 = libcc2rs::strdup_unsafe((b"hello\0".as_ptr().cast_mut()).cast_const());
2221
assert!((((!((d).is_null())) as i32) != 0));
2322
assert!(((((*libcc2rs::cpp2rust_errno()) == (99)) as i32) != 0));
24-
libc::free((d as *mut u8 as *mut ::libc::c_void));
23+
libcc2rs::free_unsafe((d as *mut u8 as *mut ::libc::c_void));
2524
(*libcc2rs::cpp2rust_errno()) = 0;
2625
}
2726
pub unsafe fn test_errno_from_fseek_2() {

tests/unit/out/unsafe/malloc_realloc_free.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,46 +13,44 @@ pub fn main() {
1313
}
1414
unsafe fn main_0() -> i32 {
1515
let mut p: *mut i32 =
16-
(libc::malloc(::std::mem::size_of::<i32>() as u64 as ::libc::size_t) as *mut i32);
16+
(libcc2rs::malloc_unsafe(::std::mem::size_of::<i32>() as u64) as *mut i32);
1717
(*p) = 42;
1818
assert!(((((*p) == (42)) as i32) != 0));
19-
libc::free((p as *mut i32 as *mut ::libc::c_void));
20-
let mut arr: *mut i32 = (libc::malloc(
21-
(4_u64).wrapping_mul(::std::mem::size_of::<i32>() as u64 as u64) as ::libc::size_t,
22-
) as *mut i32);
19+
libcc2rs::free_unsafe((p as *mut i32 as *mut ::libc::c_void));
20+
let mut arr: *mut i32 =
21+
(libcc2rs::malloc_unsafe((4_u64).wrapping_mul(::std::mem::size_of::<i32>() as u64 as u64))
22+
as *mut i32);
2323
let mut i: i32 = 0;
2424
'loop_: while ((((i) < (4)) as i32) != 0) {
2525
(*arr.offset((i) as isize)) = ((i) * (10));
2626
i.postfix_inc();
2727
}
2828
assert!(((((*arr.offset((0) as isize)) == (0)) as i32) != 0));
2929
assert!(((((*arr.offset((3) as isize)) == (30)) as i32) != 0));
30-
libc::free((arr as *mut i32 as *mut ::libc::c_void));
31-
let mut grow: *mut i32 = (libc::malloc(
32-
(2_u64).wrapping_mul(::std::mem::size_of::<i32>() as u64 as u64) as ::libc::size_t,
33-
) as *mut i32);
30+
libcc2rs::free_unsafe((arr as *mut i32 as *mut ::libc::c_void));
31+
let mut grow: *mut i32 =
32+
(libcc2rs::malloc_unsafe((2_u64).wrapping_mul(::std::mem::size_of::<i32>() as u64 as u64))
33+
as *mut i32);
3434
(*grow.offset((0) as isize)) = 1;
3535
(*grow.offset((1) as isize)) = 2;
36-
grow = (libc::realloc(
36+
grow = (libcc2rs::realloc_unsafe(
3737
(grow as *mut i32 as *mut ::libc::c_void),
38-
(4_u64).wrapping_mul(::std::mem::size_of::<i32>() as u64 as u64) as ::libc::size_t,
38+
(4_u64).wrapping_mul(::std::mem::size_of::<i32>() as u64 as u64),
3939
) as *mut i32);
4040
(*grow.offset((2) as isize)) = 3;
4141
(*grow.offset((3) as isize)) = 4;
4242
assert!(((((*grow.offset((0) as isize)) == (1)) as i32) != 0));
4343
assert!(((((*grow.offset((1) as isize)) == (2)) as i32) != 0));
4444
assert!(((((*grow.offset((2) as isize)) == (3)) as i32) != 0));
4545
assert!(((((*grow.offset((3) as isize)) == (4)) as i32) != 0));
46-
libc::free((grow as *mut i32 as *mut ::libc::c_void));
47-
let mut zeros: *mut i32 = (libc::calloc(
48-
4_u64 as ::libc::size_t,
49-
::std::mem::size_of::<i32>() as u64 as ::libc::size_t,
50-
) as *mut i32);
46+
libcc2rs::free_unsafe((grow as *mut i32 as *mut ::libc::c_void));
47+
let mut zeros: *mut i32 =
48+
(libcc2rs::calloc_unsafe(4_u64, ::std::mem::size_of::<i32>() as u64) as *mut i32);
5149
let mut i: i32 = 0;
5250
'loop_: while ((((i) < (4)) as i32) != 0) {
5351
assert!(((((*zeros.offset((i) as isize)) == (0)) as i32) != 0));
5452
i.postfix_inc();
5553
}
56-
libc::free((zeros as *mut i32 as *mut ::libc::c_void));
54+
libcc2rs::free_unsafe((zeros as *mut i32 as *mut ::libc::c_void));
5755
return 0;
5856
}

tests/unit/out/unsafe/string_h.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,7 @@ pub unsafe fn test_strrchr_9() {
338338
);
339339
}
340340
pub unsafe fn test_strdup_10() {
341-
let mut d: *mut u8 =
342-
libc::strdup((b"hello\0".as_ptr().cast_mut()).cast_const() as *const i8) as *mut u8;
341+
let mut d: *mut u8 = libcc2rs::strdup_unsafe((b"hello\0".as_ptr().cast_mut()).cast_const());
343342
assert!((((!((d).is_null())) as i32) != 0));
344343
assert!(
345344
((((libc::strcmp(
@@ -348,21 +347,21 @@ pub unsafe fn test_strdup_10() {
348347
)) == (0)) as i32)
349348
!= 0)
350349
);
351-
libc::free((d as *mut u8 as *mut ::libc::c_void));
350+
libcc2rs::free_unsafe((d as *mut u8 as *mut ::libc::c_void));
352351
let mut p: *const u8 = (b"world\0".as_ptr().cast_mut()).cast_const();
353352
let mut buf: [u8; 4] = [
354353
(('a' as i32) as u8),
355354
(('b' as i32) as u8),
356355
(('c' as i32) as u8),
357356
(('\0' as i32) as u8),
358357
];
359-
let mut d2: *mut u8 = libc::strdup(p as *const i8) as *mut u8;
358+
let mut d2: *mut u8 = libcc2rs::strdup_unsafe(p);
360359
assert!((((!((d2).is_null())) as i32) != 0));
361360
assert!(
362361
((((libc::strcmp((d2).cast_const() as *const i8, p as *const i8)) == (0)) as i32) != 0)
363362
);
364-
libc::free((d2 as *mut u8 as *mut ::libc::c_void));
365-
let mut d3: *mut u8 = libc::strdup((buf.as_mut_ptr()).cast_const() as *const i8) as *mut u8;
363+
libcc2rs::free_unsafe((d2 as *mut u8 as *mut ::libc::c_void));
364+
let mut d3: *mut u8 = libcc2rs::strdup_unsafe((buf.as_mut_ptr()).cast_const());
366365
assert!((((!((d3).is_null())) as i32) != 0));
367366
assert!(
368367
((((libc::strcmp(
@@ -371,7 +370,7 @@ pub unsafe fn test_strdup_10() {
371370
)) == (0)) as i32)
372371
!= 0)
373372
);
374-
libc::free((d3 as *mut u8 as *mut ::libc::c_void));
373+
libcc2rs::free_unsafe((d3 as *mut u8 as *mut ::libc::c_void));
375374
}
376375
pub unsafe fn test_strcspn_11() {
377376
assert!(

0 commit comments

Comments
 (0)