Skip to content

Commit

Permalink
32bit support (#64)
Browse files Browse the repository at this point in the history
* First line of 64bit support, try to use inline ifs

Major changes builder.rs

* Finished with 32bit, cosmetic need to be done

* most ifs removed, composition with struct

* fixed bug with the memory writing

* 32bit overflow bad state added, some polishings needs to be done

* added 32 bit flag

* 32bit flag bug fixes, to small memory

* Overflow check redone

* Overflow check redone

---------

Co-authored-by: Patrick Weber <[email protected]>
  • Loading branch information
Jety567 and Patrick Weber authored Apr 17, 2024
1 parent c977be2 commit 1d77ed2
Show file tree
Hide file tree
Showing 12 changed files with 411 additions and 69 deletions.
16 changes: 16 additions & 0 deletions examples/addOverflow.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
uint64_t main() {
uint64_t a;
uint64_t* x;

a = 2147483647;

x = malloc(8);

*x = 0;

read(0, x, 1);

a = a + *x;

return 0;
}
17 changes: 17 additions & 0 deletions examples/mulOverflow.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
uint64_t main() {
uint64_t a;
uint64_t* x;
uint64_t y;

a = 2147483647;

x = malloc(8);

*x = 0;

read(0, x, 1);

a = a * *x;

return 0;
}
40 changes: 40 additions & 0 deletions examples/negative.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// printing the negative decimal number -7 in C*

// libcstar procedures for printing
void init_library();
void print(uint64_t* s);
void print_integer(uint64_t n);
void print_hexadecimal(uint64_t n, uint64_t a);
void print_octal(uint64_t n, uint64_t a);
void print_binary(uint64_t n, uint64_t a);
void println();

uint64_t main() {
// initialize selfie's libcstar library
init_library();

// print the integer literal -7 in decimal
print("-7 in decimal: ");
print_integer(-7);
print(" (as signed 64-bit integer)\n");

// print the integer literal -7 in decimal
print("-7 in decimal: ");
print_unsigned_integer(-7);
print(" (as unsigned integer)\n");

// print the integer literal -7 in hexadecimal
print("-7 in hexadecimal: ");
print_hexadecimal(-7, 0);
println();

// print the integer literal -7 in octal
print("-7 in octal: ");
print_octal(-7, 0);
println();

// print the integer literal -7 in binary
print("-7 in binary: ");
print_binary(-7, 0);
println();
}
16 changes: 16 additions & 0 deletions examples/subOverflow.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
uint64_t main() {
uint64_t a;
uint64_t* x;

a = -2147483645;

x = malloc(8);

*x = 0;

read(0, x, 1);

a = a - *x;

return a;
}
6 changes: 6 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,12 @@ pub fn args() -> Command {
.long("from-btor2")
.num_args(0)
)
.arg(
Arg::new("32-bit")
.help("Run beator with an 32 bit version of the Program")
.long("32bit")
.num_args(0)
)
.arg(
Arg::new("extras")
.help("Arguments passed to emulated program")
Expand Down
25 changes: 25 additions & 0 deletions src/engine/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,28 @@ pub fn prepare_unix_stack(argv: &[String], sp: u64) -> Vec<u64> {
stack.push(argc);
stack
}

pub fn prepare_unix_stack32bit(argv: &[String], sp: u32) -> Vec<u64> {
let mut stack32 = vec![];
let argc32 = argv.len() as u32;
let argv_ptrs32: Vec<u32> = argv
.iter()
.rev()
.map(|arg| {
let c_string = arg.to_owned() + "\0";
for chunk in c_string.as_bytes().chunks_exact(size_of::<u32>()).rev() {
stack32.push(LittleEndian::read_u32(chunk));
}
sp - (stack32.len() * size_of::<u32>()) as u32
})
.collect();
stack32.push(0); // terminate env table
stack32.push(0); // terminate argv table
for argv_ptr in argv_ptrs32 {
stack32.push(argv_ptr);
}
// Casting the 32 bit values to 64bit
stack32.push(argc32);
let stack: Vec<u64> = stack32.iter().map(|&x| x as u64).collect();
stack
}
2 changes: 1 addition & 1 deletion src/guinea/cli2gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ fn output_handling(ui: &mut Ui, data: &mut Guineacorn) {

fn stringify_model(model: &Model) -> String {
let mut buf = BufWriter::new(Vec::new());
let _ = write_model(model, &mut buf);
let _ = write_model(model, &mut buf, true);
let bytes = buf.into_inner().unwrap();
String::from_utf8(bytes).unwrap()
}
Expand Down
1 change: 1 addition & 0 deletions src/guinea/components/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub(crate) fn load(
ByteSize::mib(1).as_u64(),
8,
32,
false,
&argv,
)
.unwrap();
Expand Down
19 changes: 16 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ fn main() -> Result<()> {
// process global flags
let log_level = expect_arg::<LogLevel>(&matches, "verbose")?;

// global bool is64_bit
let mut is64_bit = true;

init_logger(log_level)?;

// process subcommands
Expand Down Expand Up @@ -94,11 +97,21 @@ fn main() -> Result<()> {
let arg0 = expect_arg::<String>(args, "input-file")?;
let extras = collect_arg_values(args, "extras");

let flag_32bit = args.get_flag("32-bit");

let mut model = if !input_is_dimacs {
let mut model = if !input_is_btor2 {
let program = load_object_file(&input)?;
is64_bit = program.is64;
let argv = [vec![arg0], extras].concat();
generate_model(&program, memory_size, max_heap, max_stack, &argv)?
generate_model(
&program,
memory_size,
max_heap,
max_stack,
flag_32bit,
&argv,
)?
} else {
parse_btor2_file(&input)
};
Expand Down Expand Up @@ -223,10 +236,10 @@ fn main() -> Result<()> {
}
}
} else if output_to_stdout {
write_model(&model.unwrap(), stdout())?;
write_model(&model.unwrap(), stdout(), is64_bit)?;
} else if let Some(ref output_path) = output {
let file = File::create(output_path)?;
write_model(&model.unwrap(), file)?;
write_model(&model.unwrap(), file, is64_bit)?;
}
} else {
let is_ising = args.get_flag("ising");
Expand Down
4 changes: 2 additions & 2 deletions src/unicorn/btor2file_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ impl BTOR2Parser {
let line = self.lines.get(&nid).unwrap().clone();
if line[2] == "bitvec" {
if let Ok(answer) = line[3].parse::<usize>() {
return get_nodetype(answer);
return get_nodetype(answer, true);
}
panic!("Unsupported bitvec: {:?}", line);
} else if line[2] == "array" {
Expand Down Expand Up @@ -313,7 +313,7 @@ impl BTOR2Parser {
let bits_src = bits_dest - bits_ext;
current_node = Some(NodeRef::from(Node::Ext {
nid,
from: get_nodetype(bits_src),
from: get_nodetype(bits_src, true),
value,
}))
}
Expand Down
Loading

0 comments on commit 1d77ed2

Please sign in to comment.