| 1 |
/**************************************************************************/ |
| 2 |
/* */ |
| 3 |
/* This file is part of Frama-C. */ |
| 4 |
/* */ |
| 5 |
/* Copyright (C) 2007-2022 */ |
| 6 |
/* CEA (Commissariat à l'énergie atomique et aux énergies */ |
| 7 |
/* alternatives) */ |
| 8 |
/* */ |
| 9 |
/* you can redistribute it and/or modify it under the terms of the GNU */ |
| 10 |
/* Lesser General Public License as published by the Free Software */ |
| 11 |
/* Foundation, version 2.1. */ |
| 12 |
/* */ |
| 13 |
/* It is distributed in the hope that it will be useful, */ |
| 14 |
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ |
| 15 |
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ |
| 16 |
/* GNU Lesser General Public License for more details. */ |
| 17 |
/* */ |
| 18 |
/* See the GNU Lesser General Public License version 2.1 */ |
| 19 |
/* for more details (enclosed in the file licenses/LGPLv2.1). */ |
| 20 |
/* */ |
| 21 |
/**************************************************************************/ |
| 22 |
|
| 23 |
#include "__fc_builtin.h" |
| 24 |
__PUSH_FC_STDLIB |
| 25 |
#include "limits.h" |
| 26 |
|
| 27 |
/* Those builtins implementations could probably be removed entirely for |
| 28 |
Value, as the spec is informative enough. There remains a slight difference |
| 29 |
with Frama_C_float/double_interval and +0./-0., because the specification |
| 30 |
is not sufficient to to exclude -0. when requiring >= +0. */ |
| 31 |
|
| 32 |
int volatile Frama_C_entropy_source; |
| 33 |
|
| 34 |
// Additional entropy sources for some interval functions |
| 35 |
unsigned int volatile __fc_unsigned_int_entropy; |
| 36 |
long volatile __fc_long_entropy; |
| 37 |
unsigned long volatile __fc_unsigned_long_entropy; |
| 38 |
|
| 39 |
//@ assigns Frama_C_entropy_source \from Frama_C_entropy_source; |
| 40 |
void Frama_C_update_entropy(void) { |
| 41 |
Frama_C_entropy_source = Frama_C_entropy_source; |
| 42 |
} |
| 43 |
|
| 44 |
void Frama_C_make_unknown(char *p, size_t l) { |
| 45 |
Frama_C_update_entropy(); |
| 46 |
for (size_t i = 0; i < l; i++) { |
| 47 |
p[i] = Frama_C_entropy_source; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
int Frama_C_nondet(int a, int b) |
| 52 |
{ |
| 53 |
Frama_C_update_entropy(); |
| 54 |
return Frama_C_entropy_source ? a : b; |
| 55 |
} |
| 56 |
|
| 57 |
void *Frama_C_nondet_ptr(void *a, void *b) |
| 58 |
{ |
| 59 |
Frama_C_update_entropy(); |
| 60 |
return Frama_C_entropy_source ? a : b; |
| 61 |
} |
| 62 |
|
| 63 |
int Frama_C_interval(int min, int max) |
| 64 |
{ |
| 65 |
int r, aux; |
| 66 |
Frama_C_update_entropy(); |
| 67 |
aux = Frama_C_entropy_source; |
| 68 |
if (aux >= min && aux <= max) |
| 69 |
r = aux; |
| 70 |
else |
| 71 |
r = min; |
| 72 |
return r; |
| 73 |
} |
| 74 |
|
| 75 |
char Frama_C_char_interval(char min, char max) |
| 76 |
{ |
| 77 |
return (char)Frama_C_interval(min, max); |
| 78 |
} |
| 79 |
|
| 80 |
float Frama_C_float_interval(float min, float max) |
| 81 |
{ |
| 82 |
Frama_C_update_entropy(); |
| 83 |
return Frama_C_entropy_source ? min : max; |
| 84 |
} |
| 85 |
|
| 86 |
double Frama_C_double_interval(double min, double max) |
| 87 |
{ |
| 88 |
Frama_C_update_entropy(); |
| 89 |
return Frama_C_entropy_source ? min : max; |
| 90 |
} |
| 91 |
|
| 92 |
unsigned char Frama_C_unsigned_char_interval(unsigned char min, unsigned char max) |
| 93 |
{ |
| 94 |
return (unsigned char)Frama_C_interval(min, max); |
| 95 |
} |
| 96 |
|
| 97 |
unsigned int Frama_C_unsigned_int_interval(unsigned int min, unsigned int max) |
| 98 |
{ |
| 99 |
unsigned int r, aux; |
| 100 |
Frama_C_update_entropy(); |
| 101 |
aux = __fc_unsigned_int_entropy; |
| 102 |
if (aux >= min && aux <= max) |
| 103 |
r = aux; |
| 104 |
else |
| 105 |
r = min; |
| 106 |
return r; |
| 107 |
} |
| 108 |
|
| 109 |
long Frama_C_long_interval(long min, long max) |
| 110 |
{ |
| 111 |
long r, aux; |
| 112 |
Frama_C_update_entropy(); |
| 113 |
aux = __fc_long_entropy; |
| 114 |
if (aux >= min && aux <= max) |
| 115 |
r = aux; |
| 116 |
else |
| 117 |
r = min; |
| 118 |
return r; |
| 119 |
} |
| 120 |
|
| 121 |
unsigned long Frama_C_unsigned_long_interval(unsigned long min, unsigned long max) |
| 122 |
{ |
| 123 |
unsigned long r, aux; |
| 124 |
Frama_C_update_entropy(); |
| 125 |
aux = __fc_unsigned_long_entropy; |
| 126 |
if (aux >= min && aux <= max) |
| 127 |
r = aux; |
| 128 |
else |
| 129 |
r = min; |
| 130 |
return r; |
| 131 |
} |
| 132 |
|
| 133 |
extern void __builtin_abort(void) __attribute__((noreturn)); // GCC builtin |
| 134 |
|
| 135 |
void Frama_C_abort(void) |
| 136 |
{ |
| 137 |
__builtin_abort(); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
__POP_FC_STDLIB |