Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libcc2rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ edition = "2024"

[dependencies]
libcc2rs-macros = { path = "../libcc2rs-macros", version = "0.1.0" }
libc = "0.2"
37 changes: 37 additions & 0 deletions libcc2rs/src/alloc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.

/// # Safety
///
/// Same contract as C's `malloc`.
pub unsafe fn malloc_unsafe(a0: u64) -> *mut libc::c_void {
unsafe { libc::malloc(a0 as libc::size_t) }
}

/// # Safety
///
/// Same contract as C's `free`.
pub unsafe fn free_unsafe(a0: *mut libc::c_void) {
unsafe { libc::free(a0) }
}

/// # Safety
///
/// Same contract as C's `realloc`.
pub unsafe fn realloc_unsafe(a0: *mut libc::c_void, a1: u64) -> *mut libc::c_void {
unsafe { libc::realloc(a0, a1 as libc::size_t) }
}

/// # Safety
///
/// Same contract as C's `calloc`.
pub unsafe fn calloc_unsafe(a0: u64, a1: u64) -> *mut libc::c_void {
unsafe { libc::calloc(a0 as libc::size_t, a1 as libc::size_t) }
}

/// # Safety
///
/// Same contract as C's `strdup`.
pub unsafe fn strdup_unsafe(a0: *const u8) -> *mut u8 {
unsafe { libc::strdup(a0 as *const libc::c_char) as *mut u8 }
}
3 changes: 3 additions & 0 deletions libcc2rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ pub use rules::*;
mod io;
pub use io::*;

mod alloc;
pub use alloc::*;

mod iterators;
pub use iterators::*;

Expand Down
16 changes: 8 additions & 8 deletions rules/cstdlib/ir_unsafe.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"f2": {
"body": [
{
"text": "libc::free("
"text": "libcc2rs::free_unsafe("
},
{
"placeholder": {
Expand All @@ -31,7 +31,7 @@
"f3": {
"body": [
{
"text": "libc::malloc("
"text": "libcc2rs::malloc_unsafe("
},
{
"placeholder": {
Expand All @@ -40,7 +40,7 @@
}
},
{
"text": " as ::libc::size_t)"
"text": ")"
}
],
"params": {
Expand All @@ -56,7 +56,7 @@
"f4": {
"body": [
{
"text": "libc::realloc("
"text": "libcc2rs::realloc_unsafe("
},
{
"placeholder": {
Expand All @@ -74,7 +74,7 @@
}
},
{
"text": " as ::libc::size_t)"
"text": ")"
}
],
"params": {
Expand All @@ -94,7 +94,7 @@
"f5": {
"body": [
{
"text": "libc::calloc("
"text": "libcc2rs::calloc_unsafe("
},
{
"placeholder": {
Expand All @@ -103,7 +103,7 @@
}
},
{
"text": " as ::libc::size_t, "
"text": ", "
},
{
"placeholder": {
Expand All @@ -112,7 +112,7 @@
}
},
{
"text": " as ::libc::size_t)"
"text": ")"
}
],
"params": {
Expand Down
8 changes: 4 additions & 4 deletions rules/cstdlib/tgt_unsafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ unsafe fn f1() {
}

unsafe fn f2(a0: *mut ::libc::c_void) {
libc::free(a0)
libcc2rs::free_unsafe(a0)
}

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

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

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

unsafe fn f6(a0: *const u8) -> *mut u8 {
Expand Down
4 changes: 2 additions & 2 deletions rules/cstring/ir_unsafe.json
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@
"f15": {
"body": [
{
"text": "libc::strdup("
"text": "libcc2rs::strdup_unsafe("
},
{
"placeholder": {
Expand All @@ -292,7 +292,7 @@
}
},
{
"text": " as *const i8) as *mut u8"
"text": ")"
}
],
"params": {
Expand Down
2 changes: 1 addition & 1 deletion rules/cstring/tgt_unsafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ unsafe fn f14(a0: *mut u8, a1: i32) -> *mut u8 {
}

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

unsafe fn f16(a0: *const u8, a1: *const u8) -> u64 {
Expand Down
68 changes: 40 additions & 28 deletions tests/unit/malloc_realloc_free.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,49 @@
#include <assert.h>
#include <stdlib.h>

int main() {
int *p = (int *)malloc(sizeof(int));
*p = 42;
assert(*p == 42);
free(p);
#define ALLOC_TESTS(MALLOC, FREE, REALLOC, CALLOC) \
do { \
int *p = (int *)MALLOC(sizeof(int)); \
*p = 42; \
assert(*p == 42); \
FREE(p); \
\
int *arr = (int *)MALLOC(4 * sizeof(int)); \
for (int i = 0; i < 4; i++) { \
arr[i] = i * 10; \
} \
assert(arr[0] == 0); \
assert(arr[3] == 30); \
FREE(arr); \
\
int *grow = (int *)MALLOC(2 * sizeof(int)); \
grow[0] = 1; \
grow[1] = 2; \
grow = (int *)REALLOC(grow, 4 * sizeof(int)); \
grow[2] = 3; \
grow[3] = 4; \
assert(grow[0] == 1); \
assert(grow[1] == 2); \
assert(grow[2] == 3); \
assert(grow[3] == 4); \
FREE(grow); \
\
int *zeros = (int *)CALLOC(4, sizeof(int)); \
for (int i = 0; i < 4; i++) { \
assert(zeros[i] == 0); \
} \
FREE(zeros); \
} while (0)

int *arr = (int *)malloc(4 * sizeof(int));
for (int i = 0; i < 4; i++) {
arr[i] = i * 10;
}
assert(arr[0] == 0);
assert(arr[3] == 30);
free(arr);
int main() {
ALLOC_TESTS(malloc, free, realloc, calloc);

int *grow = (int *)malloc(2 * sizeof(int));
grow[0] = 1;
grow[1] = 2;
grow = (int *)realloc(grow, 4 * sizeof(int));
grow[2] = 3;
grow[3] = 4;
assert(grow[0] == 1);
assert(grow[1] == 2);
assert(grow[2] == 3);
assert(grow[3] == 4);
free(grow);
void *(*pmalloc)(size_t) = malloc;
void (*pfree)(void *) = free;
void *(*prealloc)(void *, size_t) = realloc;
void *(*pcalloc)(size_t, size_t) = calloc;

int *zeros = (int *)calloc(4, sizeof(int));
for (int i = 0; i < 4; i++) {
assert(zeros[i] == 0);
}
free(zeros);
ALLOC_TESTS(pmalloc, pfree, prealloc, pcalloc);

return 0;
}
12 changes: 6 additions & 6 deletions tests/unit/out/unsafe/cstring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,30 +267,30 @@ pub unsafe fn test_strrchr_9() {
);
}
pub unsafe fn test_strdup_10() {
let mut d: *mut u8 = libc::strdup(b"hello\0".as_ptr() as *const i8) as *mut u8;
let mut d: *mut u8 = libcc2rs::strdup_unsafe(b"hello\0".as_ptr());
assert!(!((d).is_null()));
assert!(
((libc::strcmp(
(d).cast_const() as *const i8,
b"hello\0".as_ptr() as *const i8
)) == (0))
);
libc::free((d as *mut u8 as *mut ::libc::c_void));
libcc2rs::free_unsafe((d as *mut u8 as *mut ::libc::c_void));
let mut p: *const u8 = b"world\0".as_ptr();
let mut buf: [u8; 4] = [('a' as u8), ('b' as u8), ('c' as u8), ('\0' as u8)];
let mut d2: *mut u8 = libc::strdup(p as *const i8) as *mut u8;
let mut d2: *mut u8 = libcc2rs::strdup_unsafe(p);
assert!(!((d2).is_null()));
assert!(((libc::strcmp((d2).cast_const() as *const i8, p as *const i8)) == (0)));
libc::free((d2 as *mut u8 as *mut ::libc::c_void));
let mut d3: *mut u8 = libc::strdup((buf.as_mut_ptr()).cast_const() as *const i8) as *mut u8;
libcc2rs::free_unsafe((d2 as *mut u8 as *mut ::libc::c_void));
let mut d3: *mut u8 = libcc2rs::strdup_unsafe((buf.as_mut_ptr()).cast_const());
assert!(!((d3).is_null()));
assert!(
((libc::strcmp(
(d3).cast_const() as *const i8,
(buf.as_mut_ptr()).cast_const() as *const i8
)) == (0))
);
libc::free((d3 as *mut u8 as *mut ::libc::c_void));
libcc2rs::free_unsafe((d3 as *mut u8 as *mut ::libc::c_void));
}
pub unsafe fn test_strcspn_11() {
assert!(
Expand Down
5 changes: 2 additions & 3 deletions tests/unit/out/unsafe/errno.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ pub unsafe fn test_errno_0() {
}
pub unsafe fn test_errno_preserved_across_strdup_1() {
(*libcc2rs::cpp2rust_errno()) = 99;
let mut d: *mut u8 =
libc::strdup((b"hello\0".as_ptr().cast_mut()).cast_const() as *const i8) as *mut u8;
let mut d: *mut u8 = libcc2rs::strdup_unsafe((b"hello\0".as_ptr().cast_mut()).cast_const());
assert!((((!((d).is_null())) as i32) != 0));
assert!(((((*libcc2rs::cpp2rust_errno()) == (99)) as i32) != 0));
libc::free((d as *mut u8 as *mut ::libc::c_void));
libcc2rs::free_unsafe((d as *mut u8 as *mut ::libc::c_void));
(*libcc2rs::cpp2rust_errno()) = 0;
}
pub unsafe fn test_errno_from_fseek_2() {
Expand Down
Loading
Loading