Skip to content

Commit 15aa61f

Browse files
committed
Adding support for posix_memalign from the stdlib in c.
1 parent ec79bdd commit 15aa61f

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/ansi-c/library/stdlib.c

+45
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,51 @@ inline void *valloc(__CPROVER_size_t malloc_size)
406406
return malloc(malloc_size);
407407
}
408408

409+
/* FUNCTION: posix_memalign */
410+
411+
#ifndef __CPROVER_ERRNO_H_INCLUDED
412+
#include <errno.h>
413+
#define __CPROVER_ERRNO_H_INCLUDED
414+
#endif
415+
416+
#undef posix_memalign
417+
418+
inline void *malloc(__CPROVER_size_t malloc_size);
419+
inline int
420+
posix_memalign(void **ptr, __CPROVER_size_t alignment, __CPROVER_size_t size)
421+
{
422+
__CPROVER_HIDE:;
423+
424+
__CPROVER_size_t multiplier = alignment / sizeof(void *);
425+
// Modeling the posix_memalign checks on alignment.
426+
if(
427+
alignment % sizeof(void *) != 0 || ((multiplier) & (multiplier - 1)) != 0 ||
428+
alignment == 0)
429+
{
430+
return EINVAL;
431+
}
432+
// The address of the allocated memory is supposed to be aligned with
433+
// alignment. As cbmc doesn't model address alignment,
434+
// assuming MALLOC_ALIGNMENT = MAX_INT_VALUE seems fair.
435+
// As _mid_memalign simplifies for alignment <= MALLOC_ALIGNMENT
436+
// to a malloc call, it should be sound, if we do it too.
437+
438+
// The originial posix_memalign check on the pointer is:
439+
440+
// void *tmp = malloc(size);
441+
// if(tmp != NULL){
442+
// *ptr = tmp;
443+
// return 0;
444+
// }
445+
// return ENOMEM;
446+
447+
// As _CPROVER_allocate used in malloc never returns null,
448+
// this check is not applicable and can be simplified:
449+
450+
*ptr = malloc(size);
451+
return 0;
452+
}
453+
409454
/* FUNCTION: random */
410455

411456
long __VERIFIER_nondet_long();

0 commit comments

Comments
 (0)