Skip to content

Commit 3dea7ec

Browse files
committed
Add stdlib rules
1 parent 24c1c62 commit 3dea7ec

7 files changed

Lines changed: 227 additions & 0 deletions

File tree

rules/cstdlib/ir_unsafe.json

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,118 @@
9090
"type": "*mut ::libc::c_void",
9191
"is_unsafe_pointer": true
9292
}
93+
},
94+
"f5": {
95+
"body": [
96+
{
97+
"text": "libc::calloc("
98+
},
99+
{
100+
"placeholder": {
101+
"arg": 0,
102+
"access": "read"
103+
}
104+
},
105+
{
106+
"text": " as ::libc::size_t, "
107+
},
108+
{
109+
"placeholder": {
110+
"arg": 1,
111+
"access": "read"
112+
}
113+
},
114+
{
115+
"text": " as ::libc::size_t)"
116+
}
117+
],
118+
"params": {
119+
"a0": {
120+
"type": "u64"
121+
},
122+
"a1": {
123+
"type": "u64"
124+
}
125+
},
126+
"return_type": {
127+
"type": "*mut ::libc::c_void",
128+
"is_unsafe_pointer": true
129+
}
130+
},
131+
"f6": {
132+
"body": [
133+
{
134+
"text": "libc::getenv("
135+
},
136+
{
137+
"placeholder": {
138+
"arg": 0,
139+
"access": "read"
140+
}
141+
},
142+
{
143+
"text": " as *const i8) as *mut u8"
144+
}
145+
],
146+
"params": {
147+
"a0": {
148+
"type": "*const u8",
149+
"is_unsafe_pointer": true
150+
}
151+
},
152+
"return_type": {
153+
"type": "*mut u8",
154+
"is_unsafe_pointer": true
155+
}
156+
},
157+
"f7": {
158+
"body": [
159+
{
160+
"text": "libc::setenv("
161+
},
162+
{
163+
"placeholder": {
164+
"arg": 0,
165+
"access": "read"
166+
}
167+
},
168+
{
169+
"text": " as *const i8, "
170+
},
171+
{
172+
"placeholder": {
173+
"arg": 1,
174+
"access": "read"
175+
}
176+
},
177+
{
178+
"text": " as *const i8, "
179+
},
180+
{
181+
"placeholder": {
182+
"arg": 2,
183+
"access": "read"
184+
}
185+
},
186+
{
187+
"text": ")"
188+
}
189+
],
190+
"params": {
191+
"a0": {
192+
"type": "*const u8",
193+
"is_unsafe_pointer": true
194+
},
195+
"a1": {
196+
"type": "*const u8",
197+
"is_unsafe_pointer": true
198+
},
199+
"a2": {
200+
"type": "i32"
201+
}
202+
},
203+
"return_type": {
204+
"type": "i32"
205+
}
93206
}
94207
}

rules/cstdlib/src.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,11 @@ void f2(void *a0) { return free(a0); }
1010
void *f3(size_t a0) { return malloc(a0); }
1111

1212
void *f4(void *a0, size_t a1) { return realloc(a0, a1); }
13+
14+
void *f5(size_t nmemb, size_t size) { return calloc(nmemb, size); }
15+
16+
char *f6(const char *name) { return getenv(name); }
17+
18+
int f7(const char *name, const char *value, int overwrite) {
19+
return setenv(name, value, overwrite);
20+
}

rules/cstdlib/tgt_unsafe.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,15 @@ unsafe fn f3(a0: u64) -> *mut ::libc::c_void {
1616
unsafe fn f4(a0: *mut ::libc::c_void, a1: u64) -> *mut ::libc::c_void {
1717
libc::realloc(a0, a1 as ::libc::size_t)
1818
}
19+
20+
unsafe fn f5(a0: u64, a1: u64) -> *mut ::libc::c_void {
21+
libc::calloc(a0 as ::libc::size_t, a1 as ::libc::size_t)
22+
}
23+
24+
unsafe fn f6(a0: *const u8) -> *mut u8 {
25+
libc::getenv(a0 as *const i8) as *mut u8
26+
}
27+
28+
unsafe fn f7(a0: *const u8, a1: *const u8, a2: i32) -> i32 {
29+
libc::setenv(a0 as *const i8, a1 as *const i8, a2)
30+
}

tests/unit/malloc_realloc_free.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,11 @@ int main() {
2828
assert(grow[3] == 4);
2929
free(grow);
3030

31+
int *zeros = (int *)calloc(4, sizeof(int));
32+
for (int i = 0; i < 4; i++) {
33+
assert(zeros[i] == 0);
34+
}
35+
free(zeros);
36+
3137
return 0;
3238
}

tests/unit/out/unsafe/malloc_realloc_free.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,15 @@ unsafe fn main_0() -> i32 {
4444
assert!(((((*grow.offset((2) as isize)) == (3)) as i32) != 0));
4545
assert!(((((*grow.offset((3) as isize)) == (4)) as i32) != 0));
4646
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);
51+
let mut i: i32 = 0;
52+
'loop_: while ((((i) < (4)) as i32) != 0) {
53+
assert!(((((*zeros.offset((i) as isize)) == (0)) as i32) != 0));
54+
i.postfix_inc();
55+
}
56+
libc::free((zeros as *mut i32 as *mut ::libc::c_void));
4757
return 0;
4858
}

tests/unit/out/unsafe/stdlib_h.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
8+
use std::rc::Rc;
9+
pub unsafe fn test_setenv_getenv_0() {
10+
assert!(
11+
((((libc::setenv(
12+
(b"CPP2RUST_TEST_VAR\0".as_ptr().cast_mut()).cast_const() as *const i8,
13+
(b"test_value\0".as_ptr().cast_mut()).cast_const() as *const i8,
14+
1
15+
)) == (0)) as i32)
16+
!= 0)
17+
);
18+
let mut v: *const u8 =
19+
(libc::getenv((b"CPP2RUST_TEST_VAR\0".as_ptr().cast_mut()).cast_const() as *const i8)
20+
as *mut u8)
21+
.cast_const();
22+
assert!((((!((v).is_null())) as i32) != 0));
23+
assert!(
24+
((((libc::strcmp(
25+
v as *const i8,
26+
(b"test_value\0".as_ptr().cast_mut()).cast_const() as *const i8
27+
)) == (0)) as i32)
28+
!= 0)
29+
);
30+
assert!(
31+
((((libc::setenv(
32+
(b"CPP2RUST_TEST_VAR\0".as_ptr().cast_mut()).cast_const() as *const i8,
33+
(b"replaced\0".as_ptr().cast_mut()).cast_const() as *const i8,
34+
1
35+
)) == (0)) as i32)
36+
!= 0)
37+
);
38+
v = (libc::getenv((b"CPP2RUST_TEST_VAR\0".as_ptr().cast_mut()).cast_const() as *const i8)
39+
as *mut u8)
40+
.cast_const();
41+
assert!((((!((v).is_null())) as i32) != 0));
42+
assert!(
43+
((((libc::strcmp(
44+
v as *const i8,
45+
(b"replaced\0".as_ptr().cast_mut()).cast_const() as *const i8
46+
)) == (0)) as i32)
47+
!= 0)
48+
);
49+
}
50+
pub fn main() {
51+
unsafe {
52+
std::process::exit(main_0() as i32);
53+
}
54+
}
55+
unsafe fn main_0() -> i32 {
56+
(unsafe { test_setenv_getenv_0() });
57+
return 0;
58+
}

tests/unit/stdlib_h.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// no-compile: refcount
2+
#include <assert.h>
3+
#include <stdlib.h>
4+
#include <string.h>
5+
6+
static void test_setenv_getenv(void) {
7+
assert(setenv("CPP2RUST_TEST_VAR", "test_value", 1) == 0);
8+
const char *v = getenv("CPP2RUST_TEST_VAR");
9+
assert(v != NULL);
10+
assert(strcmp(v, "test_value") == 0);
11+
assert(setenv("CPP2RUST_TEST_VAR", "replaced", 1) == 0);
12+
v = getenv("CPP2RUST_TEST_VAR");
13+
assert(v != NULL);
14+
assert(strcmp(v, "replaced") == 0);
15+
}
16+
17+
int main(void) {
18+
test_setenv_getenv();
19+
return 0;
20+
}

0 commit comments

Comments
 (0)