Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C library: Refine and improve stdio models #8043

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/bsd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ jobs:
# gmake TAGS='[!shouldfail]' -C jbmc/unit test
echo "Run regression tests"
gmake -C regression/cbmc test
gmake -C regression/cbmc-library test
# gmake -C regression test-parallel JOBS=2
# gmake -C regression/cbmc test-paths-lifo
# env PATH=$PATH:`pwd`/src/solvers gmake -C regression/cbmc test-cprover-smt2
Expand Down Expand Up @@ -123,6 +124,10 @@ jobs:
# gmake TAGS='[!shouldfail]' -C jbmc/unit test
echo "Run regression tests"
gmake -C regression/cbmc test
# TODO: fileno and *fprintf tests are failing, requires debugging
# https://github.com/openbsd/src/blob/master/include/stdio.h may be
# useful (likely need to allocate __sF)
gmake -C regression/cbmc-library test || true
# gmake -C regression test-parallel JOBS=2
# gmake -C regression/cbmc test-paths-lifo
# env PATH=$PATH:`pwd`/src/solvers gmake -C regression/cbmc test-cprover-smt2
Expand Down Expand Up @@ -187,6 +192,7 @@ jobs:
echo "Run regression tests"
# TODO: we need to model some more library functions
gmake -C regression/cbmc test || true
gmake -C regression/cbmc-library test || true
# gmake -C regression test-parallel JOBS=2
# gmake -C regression/cbmc test-paths-lifo
# env PATH=$PATH:`pwd`/src/solvers gmake -C regression/cbmc test-cprover-smt2
Expand Down
8 changes: 2 additions & 6 deletions regression/cbmc-library/fileno-01/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@

int main()
{
// requires initialization of stdin/stdout/stderr
// assert(fileno(stdin) == 0);
// assert(fileno(stdout) == 1);
// assert(fileno(stderr) == 2);

int fd;
FILE *some_file = fdopen(fd, "");
assert(fileno(some_file) >= -1);
if(some_file)
assert(fileno(some_file) >= -1);

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ int main()
int n = 5;
int result = ackermann(m, n);

printf("Result of the Ackermann function: %d\n", result);
// we don't currently have contracts on what printf is assigning to
// printf("Result of the Ackermann function: %d\n", result);
return 0;
}

Expand Down
139 changes: 122 additions & 17 deletions src/ansi-c/library/stdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,7 @@
#define __CPROVER_STDIO_H_INCLUDED
#endif

/* undefine macros in OpenBSD's stdio.h that are problematic to the checker. */
#if defined(__OpenBSD__)
#undef getchar
#undef putchar
#undef getc
#undef feof
#undef ferror
#undef fileno
#endif

__CPROVER_bool __VERIFIER_nondet___CPROVER_bool(void);

Expand Down Expand Up @@ -197,7 +189,8 @@ __CPROVER_HIDE:;
__CPROVER_set_must(stream, "closed");
#endif
int return_value=__VERIFIER_nondet_int();
free(stream);
if(stream != stdin && stream != stdout && stream != stderr)
free(stream);
return return_value;
}

Expand All @@ -213,25 +206,83 @@ __CPROVER_HIDE:;
#define __CPROVER_STDLIB_H_INCLUDED
#endif

#ifndef __CPROVER_ERRNO_H_INCLUDED
# include <errno.h>
# define __CPROVER_ERRNO_H_INCLUDED
#endif

FILE *fdopen(int handle, const char *mode)
{
__CPROVER_HIDE:;
(void)handle;
if(handle < 0)
{
errno = EBADF;
return NULL;
}
(void)*mode;
#ifdef __CPROVER_STRING_ABSTRACTION
__CPROVER_assert(__CPROVER_is_zero_string(mode),
"fdopen zero-termination of 2nd argument");
#endif

#if !defined(__linux__) || defined(__GLIBC__)
FILE *f=malloc(sizeof(FILE));
#if defined(_WIN32) || defined(__OpenBSD__) || defined(__NetBSD__)
switch(handle)
{
case 0:
return stdin;
case 1:
return stdout;
case 2:
return stderr;
default:
{
FILE *f = malloc(sizeof(FILE));
__CPROVER_assume(fileno(f) == handle);
return f;
}
}
#else
// libraries need to expose the definition of FILE; this is the
# if !defined(__linux__) || defined(__GLIBC__)
static FILE stdin_file;
static FILE stdout_file;
static FILE stderr_file;
# else
// libraries need not expose the definition of FILE; this is the
// case for musl
FILE *f=malloc(sizeof(int));
#endif
static int stdin_file;
static int stdout_file;
static int stderr_file;
# endif

FILE *f = NULL;
switch(handle)
{
case 0:
stdin = &stdin_file;
__CPROVER_havoc_object(&stdin_file);
f = &stdin_file;
break;
case 1:
stdout = &stdout_file;
__CPROVER_havoc_object(&stdout_file);
f = &stdout_file;
break;
case 2:
stderr = &stderr_file;
__CPROVER_havoc_object(&stderr_file);
f = &stderr_file;
break;
default:
# if !defined(__linux__) || defined(__GLIBC__)
f = malloc(sizeof(FILE));
# else
f = malloc(sizeof(int));
# endif
}

__CPROVER_assume(fileno(f) == handle);
return f;
#endif
}

/* FUNCTION: _fdopen */
Expand All @@ -251,19 +302,60 @@ FILE *fdopen(int handle, const char *mode)
#define __CPROVER_STDLIB_H_INCLUDED
#endif

#ifndef __CPROVER_ERRNO_H_INCLUDED
# include <errno.h>
# define __CPROVER_ERRNO_H_INCLUDED
#endif

#ifdef __APPLE__

# ifndef LIBRARY_CHECK
FILE *stdin;
FILE *stdout;
FILE *stderr;
# endif

FILE *_fdopen(int handle, const char *mode)
{
__CPROVER_HIDE:;
(void)handle;
if(handle < 0)
{
errno = EBADF;
return NULL;
}
(void)*mode;
#ifdef __CPROVER_STRING_ABSTRACTION
__CPROVER_assert(__CPROVER_is_zero_string(mode),
"fdopen zero-termination of 2nd argument");
#endif

FILE *f=malloc(sizeof(FILE));
static FILE stdin_file;
static FILE stdout_file;
static FILE stderr_file;

FILE *f = NULL;
switch(handle)
{
case 0:
stdin = &stdin_file;
__CPROVER_havoc_object(&stdin_file);
f = &stdin_file;
break;
case 1:
stdout = &stdout_file;
__CPROVER_havoc_object(&stdout_file);
f = &stdout_file;
break;
case 2:
stderr = &stderr_file;
__CPROVER_havoc_object(&stderr_file);
f = &stderr_file;
break;
default:
f = malloc(sizeof(FILE));
}

__CPROVER_assume(fileno(f) == handle);
return f;
}
#endif
Expand Down Expand Up @@ -466,6 +558,8 @@ __CPROVER_HIDE:;
#define __CPROVER_STDIO_H_INCLUDED
#endif

#undef feof

int __VERIFIER_nondet_int(void);

int feof(FILE *stream)
Expand Down Expand Up @@ -498,6 +592,8 @@ int feof(FILE *stream)
#define __CPROVER_STDIO_H_INCLUDED
#endif

#undef ferror

int __VERIFIER_nondet_int(void);

int ferror(FILE *stream)
Expand Down Expand Up @@ -530,6 +626,8 @@ int ferror(FILE *stream)
#define __CPROVER_STDIO_H_INCLUDED
#endif

#undef fileno

int __VERIFIER_nondet_int(void);

int fileno(FILE *stream)
Expand Down Expand Up @@ -695,6 +793,8 @@ int fgetc(FILE *stream)
#define __CPROVER_STDIO_H_INCLUDED
#endif

#undef getc

int __VERIFIER_nondet_int(void);

int getc(FILE *stream)
Expand Down Expand Up @@ -731,6 +831,8 @@ int getc(FILE *stream)
#define __CPROVER_STDIO_H_INCLUDED
#endif

#undef getchar

int __VERIFIER_nondet_int(void);

int getchar(void)
Expand Down Expand Up @@ -1602,10 +1704,13 @@ FILE *__acrt_iob_func(unsigned fd)
switch(fd)
{
case 0:
__CPROVER_havoc_object(&stdin_file);
return &stdin_file;
case 1:
__CPROVER_havoc_object(&stdout_file);
return &stdout_file;
case 2:
__CPROVER_havoc_object(&stderr_file);
return &stderr_file;
default:
return (FILE *)0;
Expand Down
Loading