Skip to content

Commit 20dc5be

Browse files
committed
Add qsort and bsearch safe rules
1 parent 571f697 commit 20dc5be

3 files changed

Lines changed: 180 additions & 1 deletion

File tree

rules/cstdlib/tgt_refcount.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,50 @@ fn f4(a0: AnyPtr, a1: usize) -> AnyPtr {
1818
fn f5(a0: usize, a1: usize) -> AnyPtr {
1919
libcc2rs::calloc_refcount(a0, a1)
2020
}
21+
22+
fn f8(a0: AnyPtr, a1: AnyPtr, a2: usize, a3: usize, a4: fn(AnyPtr, AnyPtr) -> i32) -> AnyPtr {
23+
let __base = a1.reinterpret_cast::<u8>();
24+
let mut __lo: isize = 0;
25+
let mut __hi: isize = a2 as isize - 1;
26+
let mut __found: Option<AnyPtr> = None;
27+
while __lo <= __hi && __found.is_none() {
28+
let __mid = (__lo + __hi) / 2;
29+
let __elem = __base.offset(__mid as usize * a3);
30+
let __r = a4(a0.clone(), __elem.to_any());
31+
if __r == 0 {
32+
__found = Some(__elem.to_any());
33+
} else if __r < 0 {
34+
__hi = __mid - 1;
35+
} else {
36+
__lo = __mid + 1;
37+
}
38+
}
39+
match __found {
40+
Some(__p) => __p,
41+
None => AnyPtr::default(),
42+
}
43+
}
44+
45+
fn f9(a0: AnyPtr, a1: usize, a2: usize, a3: fn(AnyPtr, AnyPtr) -> i32) {
46+
let __base = a0.reinterpret_cast::<u8>();
47+
for __i in 0..a1 {
48+
let mut __min = __i;
49+
for __j in (__i + 1)..a1 {
50+
if a3(
51+
__base.offset(__j * a2).to_any(),
52+
__base.offset(__min * a2).to_any(),
53+
) < 0
54+
{
55+
__min = __j;
56+
}
57+
}
58+
if __min != __i {
59+
for __b in 0..a2 {
60+
let __x = __base.offset(__i * a2 + __b).read();
61+
let __y = __base.offset(__min * a2 + __b).read();
62+
__base.offset(__i * a2 + __b).write(__y);
63+
__base.offset(__min * a2 + __b).write(__x);
64+
}
65+
}
66+
}
67+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
pub fn cmp_int_0(a: AnyPtr, b: AnyPtr) -> i32 {
10+
let a: Value<AnyPtr> = Rc::new(RefCell::new(a));
11+
let b: Value<AnyPtr> = Rc::new(RefCell::new(b));
12+
let x: Value<i32> = Rc::new(RefCell::new(
13+
((*a.borrow()).reinterpret_cast::<i32>().read()),
14+
));
15+
let y: Value<i32> = Rc::new(RefCell::new(
16+
((*b.borrow()).reinterpret_cast::<i32>().read()),
17+
));
18+
return ((((*x.borrow()) > (*y.borrow())) as i32) - (((*x.borrow()) < (*y.borrow())) as i32));
19+
}
20+
pub fn main() {
21+
std::process::exit(main_0());
22+
}
23+
fn main_0() -> i32 {
24+
let arr: Value<Box<[i32]>> = Rc::new(RefCell::new(Box::new([5, 2, 9, 1, 7, 3, 8, 4])));
25+
{
26+
let __base = ((arr.as_pointer() as Ptr<i32>) as Ptr<i32>)
27+
.to_any()
28+
.reinterpret_cast::<u8>();
29+
for __i in 0..8_usize {
30+
let mut __min = __i;
31+
for __j in (__i + 1)..8_usize {
32+
if cmp_int_0(
33+
__base.offset(__j * ::std::mem::size_of::<i32>()).to_any(),
34+
__base.offset(__min * ::std::mem::size_of::<i32>()).to_any(),
35+
) < 0
36+
{
37+
__min = __j;
38+
}
39+
}
40+
if __min != __i {
41+
for __b in 0..::std::mem::size_of::<i32>() {
42+
let __x = __base
43+
.offset(__i * ::std::mem::size_of::<i32>() + __b)
44+
.read();
45+
let __y = __base
46+
.offset(__min * ::std::mem::size_of::<i32>() + __b)
47+
.read();
48+
__base
49+
.offset(__i * ::std::mem::size_of::<i32>() + __b)
50+
.write(__y);
51+
__base
52+
.offset(__min * ::std::mem::size_of::<i32>() + __b)
53+
.write(__x);
54+
}
55+
}
56+
}
57+
};
58+
let i: Value<i32> = Rc::new(RefCell::new(0));
59+
'loop_: while ((((*i.borrow()) < 7) as i32) != 0) {
60+
assert!(
61+
((((*arr.borrow())[(*i.borrow()) as usize]
62+
<= (*arr.borrow())[((*i.borrow()) + 1) as usize]) as i32)
63+
!= 0)
64+
);
65+
(*i.borrow_mut()).prefix_inc();
66+
}
67+
let key: Value<i32> = Rc::new(RefCell::new(7));
68+
let hit: Value<Ptr<i32>> = Rc::new(RefCell::new(
69+
{
70+
let __base = ((arr.as_pointer() as Ptr<i32>) as Ptr<i32>)
71+
.to_any()
72+
.reinterpret_cast::<u8>();
73+
let mut __lo: isize = 0;
74+
let mut __hi: isize = 8_usize as isize - 1;
75+
let mut __found: Option<AnyPtr> = None;
76+
while __lo <= __hi && __found.is_none() {
77+
let __mid = (__lo + __hi) / 2;
78+
let __elem = __base.offset(__mid as usize * ::std::mem::size_of::<i32>());
79+
let __r = cmp_int_0(
80+
((key.as_pointer()) as Ptr<i32>).to_any().clone(),
81+
__elem.to_any(),
82+
);
83+
if __r == 0 {
84+
__found = Some(__elem.to_any());
85+
} else if __r < 0 {
86+
__hi = __mid - 1;
87+
} else {
88+
__lo = __mid + 1;
89+
}
90+
}
91+
match __found {
92+
Some(__p) => __p,
93+
None => AnyPtr::default(),
94+
}
95+
}
96+
.reinterpret_cast::<i32>(),
97+
));
98+
assert!((((!((*hit.borrow()).is_null())) as i32) != 0));
99+
assert!((((((*hit.borrow()).read()) == 7) as i32) != 0));
100+
let miss_key: Value<i32> = Rc::new(RefCell::new(42));
101+
let miss: Value<Ptr<i32>> = Rc::new(RefCell::new(
102+
{
103+
let __base = ((arr.as_pointer() as Ptr<i32>) as Ptr<i32>)
104+
.to_any()
105+
.reinterpret_cast::<u8>();
106+
let mut __lo: isize = 0;
107+
let mut __hi: isize = 8_usize as isize - 1;
108+
let mut __found: Option<AnyPtr> = None;
109+
while __lo <= __hi && __found.is_none() {
110+
let __mid = (__lo + __hi) / 2;
111+
let __elem = __base.offset(__mid as usize * ::std::mem::size_of::<i32>());
112+
let __r = cmp_int_0(
113+
((miss_key.as_pointer()) as Ptr<i32>).to_any().clone(),
114+
__elem.to_any(),
115+
);
116+
if __r == 0 {
117+
__found = Some(__elem.to_any());
118+
} else if __r < 0 {
119+
__hi = __mid - 1;
120+
} else {
121+
__lo = __mid + 1;
122+
}
123+
}
124+
match __found {
125+
Some(__p) => __p,
126+
None => AnyPtr::default(),
127+
}
128+
}
129+
.reinterpret_cast::<i32>(),
130+
));
131+
assert!(((((*miss.borrow()).is_null()) as i32) != 0));
132+
return 0;
133+
}

tests/unit/qsort_bsearch.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// no-compile: refcount
21
#include <assert.h>
32
#include <stdlib.h>
43
#include <string.h>

0 commit comments

Comments
 (0)