Skip to content

Commit 43f686d

Browse files
committed
Add ub tests for int to pointer conversion
1 parent bd41414 commit 43f686d

6 files changed

Lines changed: 222 additions & 0 deletions
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// panic-ub: refcount
2+
// nondet-result: unsafe
3+
4+
#include <stdint.h>
5+
6+
int main(void) {
7+
int arr[4] = {1, 2, 3, 4};
8+
9+
union {
10+
int *p;
11+
uintptr_t bits;
12+
} u;
13+
14+
u.p = arr;
15+
u.bits += 100 * sizeof(int);
16+
int *p = u.p;
17+
18+
return *p == 0 ? 0 : 1;
19+
}

tests/ub/int_to_pointer_random.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// panic-ub: refcount
2+
// nondet-result: unsafe
3+
4+
#include <stdint.h>
5+
6+
int main(void) {
7+
union {
8+
int *p;
9+
uintptr_t bits;
10+
} u;
11+
12+
u.bits = 0xdeadbeef;
13+
int *p = u.p;
14+
15+
return *p == 0 ? 0 : 1;
16+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 main() {
10+
std::process::exit(main_0());
11+
}
12+
fn main_0() -> i32 {
13+
let arr: Value<Box<[i32]>> = Rc::new(RefCell::new(Box::new([1, 2, 3, 4])));
14+
pub struct anon_0 {
15+
__bytes: Value<Box<[u8]>>,
16+
}
17+
impl anon_0 {
18+
pub fn p(&self) -> Ptr<Ptr<i32>> {
19+
(self.__bytes.as_pointer() as Ptr<u8>).reinterpret_cast()
20+
}
21+
pub fn bits(&self) -> Ptr<u64> {
22+
(self.__bytes.as_pointer() as Ptr<u8>).reinterpret_cast()
23+
}
24+
}
25+
impl Clone for anon_0 {
26+
fn clone(&self) -> Self {
27+
anon_0 {
28+
__bytes: Rc::new(RefCell::new(self.__bytes.borrow().clone())),
29+
}
30+
}
31+
}
32+
impl Default for anon_0 {
33+
fn default() -> Self {
34+
anon_0 {
35+
__bytes: Rc::new(RefCell::new(Box::from([0u8; 8]))),
36+
}
37+
}
38+
}
39+
impl ByteRepr for anon_0 {
40+
fn byte_size() -> usize {
41+
8
42+
}
43+
fn to_bytes(&self, buf: &mut [u8]) {
44+
buf.copy_from_slice(&self.__bytes.borrow());
45+
}
46+
fn from_bytes(buf: &[u8]) -> Self {
47+
anon_0 {
48+
__bytes: Rc::new(RefCell::new(Box::from(buf))),
49+
}
50+
}
51+
};
52+
let u: Value<anon_0> = <Value<anon_0>>::default();
53+
(*u.borrow_mut()).p().write((arr.as_pointer() as Ptr<i32>));
54+
let rhs_0 = ((((*u.borrow()).bits().read()) as u64)
55+
.wrapping_add(((100_usize).wrapping_mul((::std::mem::size_of::<i32>() as usize)) as u64)))
56+
as u64;
57+
(*u.borrow_mut()).bits().write(rhs_0);
58+
let p: Value<Ptr<i32>> = Rc::new(RefCell::new(((*u.borrow()).p().read()).clone()));
59+
return if (((((*p.borrow()).read()) == 0) as i32) != 0) {
60+
0
61+
} else {
62+
1
63+
};
64+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 main() {
10+
std::process::exit(main_0());
11+
}
12+
fn main_0() -> i32 {
13+
pub struct anon_0 {
14+
__bytes: Value<Box<[u8]>>,
15+
}
16+
impl anon_0 {
17+
pub fn p(&self) -> Ptr<Ptr<i32>> {
18+
(self.__bytes.as_pointer() as Ptr<u8>).reinterpret_cast()
19+
}
20+
pub fn bits(&self) -> Ptr<u64> {
21+
(self.__bytes.as_pointer() as Ptr<u8>).reinterpret_cast()
22+
}
23+
}
24+
impl Clone for anon_0 {
25+
fn clone(&self) -> Self {
26+
anon_0 {
27+
__bytes: Rc::new(RefCell::new(self.__bytes.borrow().clone())),
28+
}
29+
}
30+
}
31+
impl Default for anon_0 {
32+
fn default() -> Self {
33+
anon_0 {
34+
__bytes: Rc::new(RefCell::new(Box::from([0u8; 8]))),
35+
}
36+
}
37+
}
38+
impl ByteRepr for anon_0 {
39+
fn byte_size() -> usize {
40+
8
41+
}
42+
fn to_bytes(&self, buf: &mut [u8]) {
43+
buf.copy_from_slice(&self.__bytes.borrow());
44+
}
45+
fn from_bytes(buf: &[u8]) -> Self {
46+
anon_0 {
47+
__bytes: Rc::new(RefCell::new(Box::from(buf))),
48+
}
49+
}
50+
};
51+
let u: Value<anon_0> = <Value<anon_0>>::default();
52+
(*u.borrow_mut()).bits().write(3735928559_u64);
53+
let p: Value<Ptr<i32>> = Rc::new(RefCell::new(((*u.borrow()).p().read()).clone()));
54+
return if (((((*p.borrow()).read()) == 0) as i32) != 0) {
55+
0
56+
} else {
57+
1
58+
};
59+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 fn main() {
10+
unsafe {
11+
std::process::exit(main_0() as i32);
12+
}
13+
}
14+
unsafe fn main_0() -> i32 {
15+
let mut arr: [i32; 4] = [1, 2, 3, 4];
16+
#[repr(C)]
17+
#[derive(Copy, Clone)]
18+
pub union anon_0 {
19+
pub p: *mut i32,
20+
pub bits: u64,
21+
}
22+
impl Default for anon_0 {
23+
fn default() -> Self {
24+
unsafe { std::mem::zeroed() }
25+
}
26+
};
27+
let mut u: anon_0 = <anon_0>::default();
28+
u.p = arr.as_mut_ptr();
29+
u.bits = ((u.bits as u64)
30+
.wrapping_add(((100_usize).wrapping_mul((::std::mem::size_of::<i32>() as usize)) as u64)))
31+
as u64;
32+
let mut p: *mut i32 = u.p;
33+
return if ((((*p) == (0)) as i32) != 0) { 0 } else { 1 };
34+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 fn main() {
10+
unsafe {
11+
std::process::exit(main_0() as i32);
12+
}
13+
}
14+
unsafe fn main_0() -> i32 {
15+
#[repr(C)]
16+
#[derive(Copy, Clone)]
17+
pub union anon_0 {
18+
pub p: *mut i32,
19+
pub bits: u64,
20+
}
21+
impl Default for anon_0 {
22+
fn default() -> Self {
23+
unsafe { std::mem::zeroed() }
24+
}
25+
};
26+
let mut u: anon_0 = <anon_0>::default();
27+
u.bits = 3735928559_u64;
28+
let mut p: *mut i32 = u.p;
29+
return if ((((*p) == (0)) as i32) != 0) { 0 } else { 1 };
30+
}

0 commit comments

Comments
 (0)