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
10 changes: 10 additions & 0 deletions cpp2rust/compat/errno.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.

#include_next <errno.h>

#undef errno

int *cpp2rust_errno(void);

#define errno (*cpp2rust_errno())
15 changes: 15 additions & 0 deletions libcc2rs/src/compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ unsafe extern "C" {
#[cfg(target_os = "macos")]
#[link_name = "malloc_size"]
fn platform_malloc_size(ptr: *const c_void) -> usize;

#[cfg(target_os = "linux")]
#[link_name = "__errno_location"]
fn platform_errno_location() -> *mut i32;

#[cfg(target_os = "macos")]
#[link_name = "__error"]
fn platform_errno_location() -> *mut i32;
}

/// # Safety
Expand All @@ -28,3 +36,10 @@ pub unsafe fn malloc_usable_size(ptr: *mut c_void) -> usize {
unsafe { platform_malloc_size(ptr as *const c_void) }
}
}

/// # Safety
///
/// Invokes the platform specific errno.
pub unsafe fn cpp2rust_errno() -> *mut i32 {
unsafe { platform_errno_location() }
}
13 changes: 13 additions & 0 deletions rules/errno/ir_unsafe.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"f1": {
"body": [
{
"text": "libcc2rs::cpp2rust_errno()"
}
],
"return_type": {
"type": "*mut i32",
"is_unsafe_pointer": true
}
}
}
6 changes: 6 additions & 0 deletions rules/errno/src.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.

#include <errno.h>

int *f1(void) { return cpp2rust_errno(); }
6 changes: 6 additions & 0 deletions rules/errno/tgt_unsafe.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.

unsafe fn f1() -> *mut i32 {
libcc2rs::cpp2rust_errno()
}
2 changes: 2 additions & 0 deletions rules/src/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ pub mod cstring_tgt_unsafe;
pub mod deque_tgt_refcount;
#[path = r#"../deque/tgt_unsafe.rs"#]
pub mod deque_tgt_unsafe;
#[path = r#"../errno/tgt_unsafe.rs"#]
pub mod errno_tgt_unsafe;
#[path = r#"../fstream/tgt_refcount.rs"#]
pub mod fstream_tgt_refcount;
#[path = r#"../fstream/tgt_unsafe.rs"#]
Expand Down
40 changes: 40 additions & 0 deletions tests/unit/errno.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// no-compile: refcount
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static void test_errno(void) {
errno = 0;
assert(errno == 0);
errno = 42;
assert(errno == 42);
int saved = errno;
assert(saved == 42);
errno = 0;
}

static void test_errno_preserved_across_strdup(void) {
errno = 99;
char *d = strdup("hello");
assert(d != NULL);
assert(errno == 99);
free(d);
errno = 0;
}

static void test_errno_from_fseek(void) {
errno = 0;
int r = fseek(stdin, 0, SEEK_SET);
assert(r == -1);
assert(errno == ESPIPE);
errno = 0;
}

int main(void) {
test_errno();
test_errno_preserved_across_strdup();
test_errno_from_fseek();
return 0;
}
55 changes: 55 additions & 0 deletions tests/unit/out/unsafe/errno.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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 unsafe fn test_errno_0() {
(*libcc2rs::cpp2rust_errno()) = 0;
assert!(((((*libcc2rs::cpp2rust_errno()) == (0)) as i32) != 0));
(*libcc2rs::cpp2rust_errno()) = 42;
assert!(((((*libcc2rs::cpp2rust_errno()) == (42)) as i32) != 0));
let mut saved: i32 = (*libcc2rs::cpp2rust_errno());
assert!(((((saved) == (42)) as i32) != 0));
(*libcc2rs::cpp2rust_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;
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::cpp2rust_errno()) = 0;
}
pub unsafe fn test_errno_from_fseek_2() {
(*libcc2rs::cpp2rust_errno()) = 0;
let mut r: i32 = if (match 0 {
0 => (*libcc2rs::cin_unsafe()).seek(std::io::SeekFrom::Start(0_i64 as u64)),
1 => (*libcc2rs::cin_unsafe()).seek(std::io::SeekFrom::Current(0_i64)),
2 => (*libcc2rs::cin_unsafe()).seek(std::io::SeekFrom::End(0_i64)),
_ => Err(std::io::Error::other("unsupported whence for fseek.")),
})
.is_ok()
{
0
} else {
-1
};
assert!(((((r) == (-1_i32)) as i32) != 0));
assert!(((((*libcc2rs::cpp2rust_errno()) == (29)) as i32) != 0));
(*libcc2rs::cpp2rust_errno()) = 0;
}
pub fn main() {
unsafe {
std::process::exit(main_0() as i32);
}
}
unsafe fn main_0() -> i32 {
(unsafe { test_errno_0() });
(unsafe { test_errno_preserved_across_strdup_1() });
(unsafe { test_errno_from_fseek_2() });
return 0;
}
Loading