From 4910525e11c2aa9a7fa559ddecf1a96c243d9356 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Sat, 19 Sep 2026 19:30:33 +0100 Subject: [PATCH 1/3] Add rules for operator new and delete --- rules/new/src.cpp | 12 ++++++++++++ rules/new/tgt_refcount.rs | 20 ++++++++++++++++++++ rules/new/tgt_unsafe.rs | 18 ++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 rules/new/src.cpp create mode 100644 rules/new/tgt_refcount.rs create mode 100644 rules/new/tgt_unsafe.rs diff --git a/rules/new/src.cpp b/rules/new/src.cpp new file mode 100644 index 000000000..ca62a01af --- /dev/null +++ b/rules/new/src.cpp @@ -0,0 +1,12 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +#include + +void *f1(std::size_t a0) { return ::operator new(a0); } + +void f2(void *a0) { return ::operator delete(a0); } + +void *f3(std::size_t a0) { return ::operator new[](a0); } + +void f4(void *a0) { return ::operator delete[](a0); } diff --git a/rules/new/tgt_refcount.rs b/rules/new/tgt_refcount.rs new file mode 100644 index 000000000..e430854b5 --- /dev/null +++ b/rules/new/tgt_refcount.rs @@ -0,0 +1,20 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +use libcc2rs::*; + +fn f1(a0: usize) -> AnyPtr { + libcc2rs::malloc_refcount(a0) +} + +fn f2(a0: AnyPtr) { + libcc2rs::free_refcount(a0) +} + +fn f3(a0: usize) -> AnyPtr { + libcc2rs::malloc_refcount(a0) +} + +fn f4(a0: AnyPtr) { + libcc2rs::free_refcount(a0) +} diff --git a/rules/new/tgt_unsafe.rs b/rules/new/tgt_unsafe.rs new file mode 100644 index 000000000..9b86683ec --- /dev/null +++ b/rules/new/tgt_unsafe.rs @@ -0,0 +1,18 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +unsafe fn f1(a0: usize) -> *mut ::libc::c_void { + libcc2rs::malloc_unsafe(a0) +} + +unsafe fn f2(a0: *mut ::libc::c_void) { + libcc2rs::free_unsafe(a0) +} + +unsafe fn f3(a0: usize) -> *mut ::libc::c_void { + libcc2rs::malloc_unsafe(a0) +} + +unsafe fn f4(a0: *mut ::libc::c_void) { + libcc2rs::free_unsafe(a0) +} From 42535c74e4cf6872ed9436107959df1b1dc0e3e7 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Sat, 19 Sep 2026 19:45:38 +0100 Subject: [PATCH 2/3] Add operator new/delete test --- tests/unit/operator_new_delete.cpp | 17 ++++++++++ .../unit/out/refcount/operator_new_delete.rs | 34 +++++++++++++++++++ tests/unit/out/unsafe/operator_new_delete.rs | 29 ++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 tests/unit/operator_new_delete.cpp create mode 100644 tests/unit/out/refcount/operator_new_delete.rs create mode 100644 tests/unit/out/unsafe/operator_new_delete.rs diff --git a/tests/unit/operator_new_delete.cpp b/tests/unit/operator_new_delete.cpp new file mode 100644 index 000000000..da244a4f4 --- /dev/null +++ b/tests/unit/operator_new_delete.cpp @@ -0,0 +1,17 @@ +#include +#include + +int main() { + auto a = static_cast(::operator new(sizeof(int))); + *a = 42; + assert(*a == 42); + ::operator delete(a); + + auto arr = static_cast(::operator new[](sizeof(int) * 2)); + arr[0] = 0; + arr[1] = 1; + assert(arr[0] + arr[1] == 1); + ::operator delete[](arr); + + return 0; +} diff --git a/tests/unit/out/refcount/operator_new_delete.rs b/tests/unit/out/refcount/operator_new_delete.rs new file mode 100644 index 000000000..0170b75e6 --- /dev/null +++ b/tests/unit/out/refcount/operator_new_delete.rs @@ -0,0 +1,34 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let a: Value> = Rc::new(RefCell::new( + libcc2rs::malloc_refcount(::std::mem::size_of::()).reinterpret_cast::(), + )); + (*a.borrow()).write(42); + assert!((((*a.borrow()).read()) == 42)); + libcc2rs::free_refcount(((*a.borrow()).clone() as Ptr).to_any()); + let arr: Value> = Rc::new(RefCell::new( + libcc2rs::malloc_refcount((::std::mem::size_of::() as usize).wrapping_mul(2_usize)) + .reinterpret_cast::(), + )); + (*arr.borrow()).offset((0) as isize).write(0); + (*arr.borrow()).offset((1) as isize).write(1); + assert!( + ((((*arr.borrow()).offset((0) as isize).read()) + + ((*arr.borrow()).offset((1) as isize).read())) + == 1) + ); + libcc2rs::free_refcount(((*arr.borrow()).clone() as Ptr).to_any()); + return 0; +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/unsafe/operator_new_delete.rs b/tests/unit/out/unsafe/operator_new_delete.rs new file mode 100644 index 000000000..05006ca0f --- /dev/null +++ b/tests/unit/out/unsafe/operator_new_delete.rs @@ -0,0 +1,29 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut a: *mut i32 = (libcc2rs::malloc_unsafe(::std::mem::size_of::()) as *mut i32); + (*a) = 42; + assert!(((*a) == (42))); + libcc2rs::free_unsafe((a as *mut i32 as *mut ::libc::c_void)); + let mut arr: *mut i32 = + (libcc2rs::malloc_unsafe((::std::mem::size_of::() as usize).wrapping_mul(2_usize)) + as *mut i32); + (*arr.offset((0) as isize)) = 0; + (*arr.offset((1) as isize)) = 1; + assert!((((*arr.offset((0) as isize)) + (*arr.offset((1) as isize))) == (1))); + libcc2rs::free_unsafe((arr as *mut i32 as *mut ::libc::c_void)); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() {} From 881d67055b2056c35eb3e8881e5a39135e00fb90 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Sat, 19 Sep 2026 20:38:05 +0100 Subject: [PATCH 3/3] clang-format --- tests/unit/operator_new_delete.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/operator_new_delete.cpp b/tests/unit/operator_new_delete.cpp index da244a4f4..ac2a00e01 100644 --- a/tests/unit/operator_new_delete.cpp +++ b/tests/unit/operator_new_delete.cpp @@ -2,12 +2,12 @@ #include int main() { - auto a = static_cast(::operator new(sizeof(int))); + auto a = static_cast(::operator new(sizeof(int))); *a = 42; assert(*a == 42); ::operator delete(a); - auto arr = static_cast(::operator new[](sizeof(int) * 2)); + auto arr = static_cast(::operator new[](sizeof(int) * 2)); arr[0] = 0; arr[1] = 1; assert(arr[0] + arr[1] == 1);