diff --git a/rules/arpa_inet/tgt_refcount.rs b/rules/arpa_inet/tgt_refcount.rs new file mode 100644 index 00000000..90a0e52e --- /dev/null +++ b/rules/arpa_inet/tgt_refcount.rs @@ -0,0 +1,63 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +use libcc2rs::*; + +fn f5(a0: i32, a1: Ptr, a2: AnyPtr) -> i32 { + if a0 == libc::AF_INET { + match a1.to_rust_string().parse::() { + Ok(__ip) => { + a2.reinterpret_cast::() + .with_slice_mut(4, |__s| __s.copy_from_slice(&__ip.octets())); + 1 + } + Err(_) => 0, + } + } else if a0 == libc::AF_INET6 { + match a1.to_rust_string().parse::() { + Ok(__ip) => { + a2.reinterpret_cast::() + .with_slice_mut(16, |__s| __s.copy_from_slice(&__ip.octets())); + 1 + } + Err(_) => 0, + } + } else { + libcc2rs::cpp2rust_errno().write(::libc::EAFNOSUPPORT); + -1 + } +} + +fn f6(a0: i32, a1: AnyPtr, a2: Ptr, a3: u32) -> Ptr { + let __text = if a0 == libc::AF_INET { + let __b: [u8; 4] = a1 + .reinterpret_cast::() + .with_slice(4, |__s| __s.try_into().unwrap()); + Some(std::net::Ipv4Addr::from(__b).to_string()) + } else if a0 == libc::AF_INET6 { + let __b: [u8; 16] = a1 + .reinterpret_cast::() + .with_slice(16, |__s| __s.try_into().unwrap()); + Some(std::net::Ipv6Addr::from(__b).to_string()) + } else { + None + }; + match __text { + Some(__s) if (__s.len() as u32) < a3 => { + let __n = __s.len(); + a2.with_slice_mut(__n + 1, |__sl| { + __sl[..__n].copy_from_slice(__s.as_bytes()); + __sl[__n] = 0; + }); + a2.clone() + } + Some(_) => { + libcc2rs::cpp2rust_errno().write(::libc::ENOSPC); + Ptr::null() + } + None => { + libcc2rs::cpp2rust_errno().write(::libc::EAFNOSUPPORT); + Ptr::null() + } + } +} diff --git a/rules/src/modules.rs b/rules/src/modules.rs index f91da1da..c0913d7d 100644 --- a/rules/src/modules.rs +++ b/rules/src/modules.rs @@ -2,6 +2,8 @@ pub mod algorithm_tgt_refcount; #[path = r#"../algorithm/tgt_unsafe.rs"#] pub mod algorithm_tgt_unsafe; +#[path = r#"../arpa_inet/tgt_refcount.rs"#] +pub mod arpa_inet_tgt_refcount; #[path = r#"../arpa_inet/tgt_unsafe.rs"#] pub mod arpa_inet_tgt_unsafe; #[path = r#"../array/tgt_refcount.rs"#] diff --git a/tests/unit/inet_pton_ntop.c b/tests/unit/inet_pton_ntop.c new file mode 100644 index 00000000..757c65ae --- /dev/null +++ b/tests/unit/inet_pton_ntop.c @@ -0,0 +1,24 @@ +#include +#include +#include + +int main(void) { + unsigned char buf[16]; + assert(inet_pton(AF_INET, "1.2.3.4", buf) == 1); + assert(buf[0] == 1 && buf[1] == 2 && buf[2] == 3 && buf[3] == 4); + assert(inet_pton(AF_INET, "999.1.1.1", buf) == 0); + assert(inet_pton(AF_INET, "not an ip", buf) == 0); + assert(inet_pton(AF_INET6, "::1", buf) == 1); + assert(buf[0] == 0 && buf[15] == 1); + assert(inet_pton(AF_INET6, "2001:db8::5", buf) == 1); + assert(buf[0] == 0x20 && buf[1] == 0x01 && buf[15] == 5); + + char text[64]; + unsigned char four[4] = {10, 0, 0, 1}; + assert(strcmp(inet_ntop(AF_INET, four, text, sizeof(text)), "10.0.0.1") == 0); + unsigned char sixteen[16] = {0}; + sixteen[15] = 1; + assert(strcmp(inet_ntop(AF_INET6, sixteen, text, sizeof(text)), "::1") == 0); + assert(inet_ntop(AF_INET, four, text, 4) == 0); + return 0; +} diff --git a/tests/unit/out/refcount/inet_pton_ntop.rs b/tests/unit/out/refcount/inet_pton_ntop.rs new file mode 100644 index 00000000..2394ba92 --- /dev/null +++ b/tests/unit/out/refcount/inet_pton_ntop.rs @@ -0,0 +1,384 @@ +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() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let buf: Value> = Rc::new(RefCell::new( + (0..16).map(|_| ::default()).collect::>(), + )); + assert!( + (((if libc::AF_INET == libc::AF_INET { + match Ptr::from_string_literal(b"1.2.3.4") + .to_rust_string() + .parse::() + { + Ok(__ip) => { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(4, |__s| __s.copy_from_slice(&__ip.octets())); + 1 + } + Err(_) => 0, + } + } else if libc::AF_INET == libc::AF_INET6 { + match Ptr::from_string_literal(b"1.2.3.4") + .to_rust_string() + .parse::() + { + Ok(__ip) => { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(16, |__s| __s.copy_from_slice(&__ip.octets())); + 1 + } + Err(_) => 0, + } + } else { + libcc2rs::cpp2rust_errno().write(::libc::EAFNOSUPPORT); + -1 + } == 1) as i32) + != 0) + ); + assert!( + ((((((((((((((*buf.borrow())[(0) as usize] as i32) == 1) as i32) != 0) + && (((((*buf.borrow())[(1) as usize] as i32) == 2) as i32) != 0)) + as i32) + != 0) + && (((((*buf.borrow())[(2) as usize] as i32) == 3) as i32) != 0)) as i32) + != 0) + && (((((*buf.borrow())[(3) as usize] as i32) == 4) as i32) != 0)) as i32) + != 0) + ); + assert!( + (((if libc::AF_INET == libc::AF_INET { + match Ptr::from_string_literal(b"999.1.1.1") + .to_rust_string() + .parse::() + { + Ok(__ip) => { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(4, |__s| __s.copy_from_slice(&__ip.octets())); + 1 + } + Err(_) => 0, + } + } else if libc::AF_INET == libc::AF_INET6 { + match Ptr::from_string_literal(b"999.1.1.1") + .to_rust_string() + .parse::() + { + Ok(__ip) => { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(16, |__s| __s.copy_from_slice(&__ip.octets())); + 1 + } + Err(_) => 0, + } + } else { + libcc2rs::cpp2rust_errno().write(::libc::EAFNOSUPPORT); + -1 + } == 0) as i32) + != 0) + ); + assert!( + (((if libc::AF_INET == libc::AF_INET { + match Ptr::from_string_literal(b"not an ip") + .to_rust_string() + .parse::() + { + Ok(__ip) => { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(4, |__s| __s.copy_from_slice(&__ip.octets())); + 1 + } + Err(_) => 0, + } + } else if libc::AF_INET == libc::AF_INET6 { + match Ptr::from_string_literal(b"not an ip") + .to_rust_string() + .parse::() + { + Ok(__ip) => { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(16, |__s| __s.copy_from_slice(&__ip.octets())); + 1 + } + Err(_) => 0, + } + } else { + libcc2rs::cpp2rust_errno().write(::libc::EAFNOSUPPORT); + -1 + } == 0) as i32) + != 0) + ); + assert!( + (((if libc::AF_INET6 == libc::AF_INET { + match Ptr::from_string_literal(b"::1") + .to_rust_string() + .parse::() + { + Ok(__ip) => { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(4, |__s| __s.copy_from_slice(&__ip.octets())); + 1 + } + Err(_) => 0, + } + } else if libc::AF_INET6 == libc::AF_INET6 { + match Ptr::from_string_literal(b"::1") + .to_rust_string() + .parse::() + { + Ok(__ip) => { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(16, |__s| __s.copy_from_slice(&__ip.octets())); + 1 + } + Err(_) => 0, + } + } else { + libcc2rs::cpp2rust_errno().write(::libc::EAFNOSUPPORT); + -1 + } == 1) as i32) + != 0) + ); + assert!( + ((((((((*buf.borrow())[(0) as usize] as i32) == 0) as i32) != 0) + && (((((*buf.borrow())[(15) as usize] as i32) == 1) as i32) != 0)) as i32) + != 0) + ); + assert!( + (((if libc::AF_INET6 == libc::AF_INET { + match Ptr::from_string_literal(b"2001:db8::5") + .to_rust_string() + .parse::() + { + Ok(__ip) => { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(4, |__s| __s.copy_from_slice(&__ip.octets())); + 1 + } + Err(_) => 0, + } + } else if libc::AF_INET6 == libc::AF_INET6 { + match Ptr::from_string_literal(b"2001:db8::5") + .to_rust_string() + .parse::() + { + Ok(__ip) => { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(16, |__s| __s.copy_from_slice(&__ip.octets())); + 1 + } + Err(_) => 0, + } + } else { + libcc2rs::cpp2rust_errno().write(::libc::EAFNOSUPPORT); + -1 + } == 1) as i32) + != 0) + ); + assert!( + (((((((((((*buf.borrow())[(0) as usize] as i32) == 32) as i32) != 0) + && (((((*buf.borrow())[(1) as usize] as i32) == 1) as i32) != 0)) as i32) + != 0) + && (((((*buf.borrow())[(15) as usize] as i32) == 5) as i32) != 0)) as i32) + != 0) + ); + let text: Value> = Rc::new(RefCell::new( + (0..64).map(|_| ::default()).collect::>(), + )); + let four: Value> = Rc::new(RefCell::new(Box::new([10_u8, 0_u8, 0_u8, 1_u8]))); + assert!( + ((({ + let mut __it1 = { + let __text = if libc::AF_INET == libc::AF_INET { + let __b: [u8; 4] = ((four.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice(4, |__s| __s.try_into().unwrap()); + Some(std::net::Ipv4Addr::from(__b).to_string()) + } else if libc::AF_INET == libc::AF_INET6 { + let __b: [u8; 16] = ((four.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice(16, |__s| __s.try_into().unwrap()); + Some(std::net::Ipv6Addr::from(__b).to_string()) + } else { + None + }; + match __text { + Some(__s) + if (__s.len() as u32) < (::std::mem::size_of::<[u8; 64]>() as u32) => + { + let __n = __s.len(); + (text.as_pointer() as Ptr).with_slice_mut(__n + 1, |__sl| { + __sl[..__n].copy_from_slice(__s.as_bytes()); + __sl[__n] = 0; + }); + (text.as_pointer() as Ptr).clone() + } + Some(_) => { + libcc2rs::cpp2rust_errno().write(::libc::ENOSPC); + Ptr::null() + } + None => { + libcc2rs::cpp2rust_errno().write(::libc::EAFNOSUPPORT); + Ptr::null() + } + } + } + .to_c_string_iterator(); + let mut __it2 = Ptr::from_string_literal(b"10.0.0.1").to_c_string_iterator(); + loop { + let __c1 = __it1.next(); + let __c2 = __it2.next(); + if __c1 != __c2 { + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); + } + if __c1.is_none() { + break 0; + } + } + } == 0) as i32) + != 0) + ); + let sixteen: Value> = Rc::new(RefCell::new(Box::new([ + 0_u8, + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ]))); + (*sixteen.borrow_mut())[(15) as usize] = 1_u8; + assert!( + ((({ + let mut __it1 = { + let __text = if libc::AF_INET6 == libc::AF_INET { + let __b: [u8; 4] = ((sixteen.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice(4, |__s| __s.try_into().unwrap()); + Some(std::net::Ipv4Addr::from(__b).to_string()) + } else if libc::AF_INET6 == libc::AF_INET6 { + let __b: [u8; 16] = ((sixteen.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice(16, |__s| __s.try_into().unwrap()); + Some(std::net::Ipv6Addr::from(__b).to_string()) + } else { + None + }; + match __text { + Some(__s) + if (__s.len() as u32) < (::std::mem::size_of::<[u8; 64]>() as u32) => + { + let __n = __s.len(); + (text.as_pointer() as Ptr).with_slice_mut(__n + 1, |__sl| { + __sl[..__n].copy_from_slice(__s.as_bytes()); + __sl[__n] = 0; + }); + (text.as_pointer() as Ptr).clone() + } + Some(_) => { + libcc2rs::cpp2rust_errno().write(::libc::ENOSPC); + Ptr::null() + } + None => { + libcc2rs::cpp2rust_errno().write(::libc::EAFNOSUPPORT); + Ptr::null() + } + } + } + .to_c_string_iterator(); + let mut __it2 = Ptr::from_string_literal(b"::1").to_c_string_iterator(); + loop { + let __c1 = __it1.next(); + let __c2 = __it2.next(); + if __c1 != __c2 { + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); + } + if __c1.is_none() { + break 0; + } + } + } == 0) as i32) + != 0) + ); + assert!( + (((({ + let __text = if libc::AF_INET == libc::AF_INET { + let __b: [u8; 4] = ((four.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice(4, |__s| __s.try_into().unwrap()); + Some(std::net::Ipv4Addr::from(__b).to_string()) + } else if libc::AF_INET == libc::AF_INET6 { + let __b: [u8; 16] = ((four.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice(16, |__s| __s.try_into().unwrap()); + Some(std::net::Ipv6Addr::from(__b).to_string()) + } else { + None + }; + match __text { + Some(__s) if (__s.len() as u32) < 4_u32 => { + let __n = __s.len(); + (text.as_pointer() as Ptr).with_slice_mut(__n + 1, |__sl| { + __sl[..__n].copy_from_slice(__s.as_bytes()); + __sl[__n] = 0; + }); + (text.as_pointer() as Ptr).clone() + } + Some(_) => { + libcc2rs::cpp2rust_errno().write(::libc::ENOSPC); + Ptr::null() + } + None => { + libcc2rs::cpp2rust_errno().write(::libc::EAFNOSUPPORT); + Ptr::null() + } + } + }) + .is_null()) as i32) + != 0) + ); + return 0; +} diff --git a/tests/unit/out/unsafe/inet_pton_ntop.rs b/tests/unit/out/unsafe/inet_pton_ntop.rs new file mode 100644 index 00000000..2f11f229 --- /dev/null +++ b/tests/unit/out/unsafe/inet_pton_ntop.rs @@ -0,0 +1,174 @@ +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 { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut buf: [u8; 16] = [0_u8; 16]; + assert!( + (((({ + unsafe extern "C" { + fn inet_pton(af: i32, src: *const libc::c_char, dst: *mut ::libc::c_void) -> i32; + } + inet_pton( + libc::AF_INET, + (c"1.2.3.4".as_ptr().cast_mut()).cast_const(), + (buf.as_mut_ptr() as *mut u8 as *mut ::libc::c_void), + ) + }) == (1)) as i32) + != 0) + ); + assert!( + (((((((((((((buf[(0) as usize] as i32) == (1)) as i32) != 0) + && ((((buf[(1) as usize] as i32) == (2)) as i32) != 0)) as i32) + != 0) + && ((((buf[(2) as usize] as i32) == (3)) as i32) != 0)) as i32) + != 0) + && ((((buf[(3) as usize] as i32) == (4)) as i32) != 0)) as i32) + != 0) + ); + assert!( + (((({ + unsafe extern "C" { + fn inet_pton(af: i32, src: *const libc::c_char, dst: *mut ::libc::c_void) -> i32; + } + inet_pton( + libc::AF_INET, + (c"999.1.1.1".as_ptr().cast_mut()).cast_const(), + (buf.as_mut_ptr() as *mut u8 as *mut ::libc::c_void), + ) + }) == (0)) as i32) + != 0) + ); + assert!( + (((({ + unsafe extern "C" { + fn inet_pton(af: i32, src: *const libc::c_char, dst: *mut ::libc::c_void) -> i32; + } + inet_pton( + libc::AF_INET, + (c"not an ip".as_ptr().cast_mut()).cast_const(), + (buf.as_mut_ptr() as *mut u8 as *mut ::libc::c_void), + ) + }) == (0)) as i32) + != 0) + ); + assert!( + (((({ + unsafe extern "C" { + fn inet_pton(af: i32, src: *const libc::c_char, dst: *mut ::libc::c_void) -> i32; + } + inet_pton( + libc::AF_INET6, + (c"::1".as_ptr().cast_mut()).cast_const(), + (buf.as_mut_ptr() as *mut u8 as *mut ::libc::c_void), + ) + }) == (1)) as i32) + != 0) + ); + assert!( + (((((((buf[(0) as usize] as i32) == (0)) as i32) != 0) + && ((((buf[(15) as usize] as i32) == (1)) as i32) != 0)) as i32) + != 0) + ); + assert!( + (((({ + unsafe extern "C" { + fn inet_pton(af: i32, src: *const libc::c_char, dst: *mut ::libc::c_void) -> i32; + } + inet_pton( + libc::AF_INET6, + (c"2001:db8::5".as_ptr().cast_mut()).cast_const(), + (buf.as_mut_ptr() as *mut u8 as *mut ::libc::c_void), + ) + }) == (1)) as i32) + != 0) + ); + assert!( + ((((((((((buf[(0) as usize] as i32) == (32)) as i32) != 0) + && ((((buf[(1) as usize] as i32) == (1)) as i32) != 0)) as i32) + != 0) + && ((((buf[(15) as usize] as i32) == (5)) as i32) != 0)) as i32) + != 0) + ); + let mut text: [libc::c_char; 64] = [(0 as libc::c_char); 64]; + let mut four: [u8; 4] = [10_u8, 0_u8, 0_u8, 1_u8]; + assert!( + ((((libc::strcmp( + { + unsafe extern "C" { + fn inet_ntop( + af: i32, + src: *const ::libc::c_void, + dst: *mut libc::c_char, + size: u32, + ) -> *const libc::c_char; + } + inet_ntop( + libc::AF_INET, + (four.as_mut_ptr() as *const u8 as *const ::libc::c_void), + text.as_mut_ptr(), + (::std::mem::size_of::<[libc::c_char; 64]>() as u32), + ) + }, + (c"10.0.0.1".as_ptr().cast_mut()).cast_const() + )) == (0)) as i32) + != 0) + ); + let mut sixteen: [u8; 16] = [ + 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, + 0_u8, + ]; + sixteen[(15) as usize] = 1_u8; + assert!( + ((((libc::strcmp( + { + unsafe extern "C" { + fn inet_ntop( + af: i32, + src: *const ::libc::c_void, + dst: *mut libc::c_char, + size: u32, + ) -> *const libc::c_char; + } + inet_ntop( + libc::AF_INET6, + (sixteen.as_mut_ptr() as *const u8 as *const ::libc::c_void), + text.as_mut_ptr(), + (::std::mem::size_of::<[libc::c_char; 64]>() as u32), + ) + }, + (c"::1".as_ptr().cast_mut()).cast_const() + )) == (0)) as i32) + != 0) + ); + assert!( + (((({ + unsafe extern "C" { + fn inet_ntop( + af: i32, + src: *const ::libc::c_void, + dst: *mut libc::c_char, + size: u32, + ) -> *const libc::c_char; + } + inet_ntop( + libc::AF_INET, + (four.as_mut_ptr() as *const u8 as *const ::libc::c_void), + text.as_mut_ptr(), + 4_u32, + ) + }) + .is_null()) as i32) + != 0) + ); + return 0; +}