Skip to content

Commit ece7b09

Browse files
committed
Add counter of read/write bytes in files
1 parent 6f5ee62 commit ece7b09

4 files changed

Lines changed: 34 additions & 1 deletion

File tree

runtime/POSIX/fd.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ ssize_t read(int fd, void *buf, size_t count) {
401401

402402
memcpy(buf, f->dfile->contents + f->off, count);
403403
f->off += count;
404+
f->dfile->read_bytes_real += count;
404405
if (fd == 0 && __exe_env.max_off < f->off) {
405406
__exe_env.max_off = f->off;
406407
}
@@ -473,6 +474,7 @@ ssize_t write(int fd, const void *buf, size_t count) {
473474
__exe_fs.stdout_writes += actual_count;
474475

475476
f->off += count;
477+
f->dfile->write_bytes_real += count;
476478
if (fd == 0 && __exe_env.max_off < f->off) {
477479
__exe_env.max_off = f->off;
478480
}

runtime/POSIX/fd.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ typedef struct {
4141
unsigned size; /* in bytes */
4242
char* contents;
4343
struct stat64* stat;
44+
45+
unsigned read_bytes_symbolic; /* bytes that were read from file */
46+
unsigned read_bytes_real;
47+
48+
unsigned write_bytes_symbolic; /* bytes that were written to file */
49+
unsigned write_bytes_real;
4450
} exe_disk_file_t;
4551

4652
typedef enum {

runtime/POSIX/fd_init.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,33 @@ static void __create_new_dfile(exe_disk_file_t *dfile, unsigned size,
5555

5656
const char *sp;
5757
char sname[64];
58-
for (sp=name; *sp; ++sp)
58+
char read_bytes_name[64];
59+
char write_bytes_name[64];
60+
for (sp=name; *sp; ++sp){
5961
sname[sp-name] = *sp;
62+
read_bytes_name[sp - name] = *sp;
63+
write_bytes_name[sp - name] = *sp;
64+
}
6065
memcpy(&sname[sp-name], "-stat", 6);
66+
memcpy(&read_bytes_name[sp - name], "-read", 6);
67+
memcpy(&write_bytes_name[sp - name], "-write", 7);
6168

6269
assert(size);
6370

6471
dfile->size = size;
6572
dfile->contents = malloc(dfile->size);
73+
74+
unsigned *ptr_read = malloc(sizeof(unsigned));
75+
unsigned *ptr_write = malloc(sizeof(unsigned));
76+
klee_make_symbolic(ptr_read, sizeof(unsigned), read_bytes_name);
77+
klee_make_symbolic(ptr_write, sizeof(unsigned), write_bytes_name);
78+
memcpy(&dfile->read_bytes_symbolic, ptr_read, sizeof(unsigned));
79+
memcpy(&dfile->write_bytes_symbolic, ptr_write, sizeof(unsigned));
80+
free(ptr_read);
81+
free(ptr_write);
82+
dfile->read_bytes_real = 0;
83+
dfile->write_bytes_real = 0;
84+
6685
if (!dfile->contents)
6786
klee_report_error(__FILE__, __LINE__, "out of memory in klee_init_env", "user.err");
6887
klee_make_symbolic(dfile->contents, dfile->size, name);

runtime/POSIX/klee_init_env.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,12 @@ int __klee_posix_wrapped_main(int argc, char **argv, char **envp);
241241

242242
void check_stdin_read() {
243243
klee_assume(__exe_env.stdin_off == __exe_env.max_off);
244+
for (int i = 0; i < __exe_fs.n_sym_files; i++) {
245+
klee_assume(__exe_fs.sym_files[i].read_bytes_real ==
246+
__exe_fs.sym_files[i].read_bytes_symbolic);
247+
klee_assume(__exe_fs.sym_files[i].write_bytes_real ==
248+
__exe_fs.sym_files[i].write_bytes_symbolic);
249+
}
244250
}
245251

246252
/* This wrapper gets called instead of main if POSIX setup is used */

0 commit comments

Comments
 (0)