| 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 |
/* ISO C: 7.20 */ |
| 24 |
#include "stdlib.h" |
| 25 |
#include "__fc_builtin.h" |
| 26 |
#include "ctype.h" |
| 27 |
#include "string.h" |
| 28 |
#include "limits.h" |
| 29 |
#include "errno.h" |
| 30 |
__PUSH_FC_STDLIB |
| 31 |
|
| 32 |
int abs (int i) |
| 33 |
{ |
| 34 |
if (i < 0) |
| 35 |
return -i; |
| 36 |
return i; |
| 37 |
} |
| 38 |
|
| 39 |
int atoi(const char *p) |
| 40 |
{ |
| 41 |
int n; |
| 42 |
int c, neg = 0; |
| 43 |
unsigned char *up = (unsigned char *)p; |
| 44 |
|
| 45 |
if (!isdigit(c = *up)) { |
| 46 |
while (isspace(c)) |
| 47 |
c = *++up; |
| 48 |
switch (c) { |
| 49 |
case '-': |
| 50 |
neg++; |
| 51 |
/* FALLTHROUGH */ |
| 52 |
case '+': |
| 53 |
c = *++up; |
| 54 |
} |
| 55 |
if (!isdigit(c)) |
| 56 |
return (0); |
| 57 |
} |
| 58 |
for (n = '0' - c; isdigit(c = *++up); ) { |
| 59 |
n *= 10; /* two steps to avoid unnecessary overflow */ |
| 60 |
n += '0' - c; /* accum neg to avoid surprises at MAX */ |
| 61 |
} |
| 62 |
return (neg ? n : -n); |
| 63 |
} |
| 64 |
|
| 65 |
void *calloc(size_t nmemb, size_t size) |
| 66 |
{ |
| 67 |
size_t l = nmemb * size; |
| 68 |
// test overflow, and fail if detected |
| 69 |
if (size != 0 && l / size != nmemb) { |
| 70 |
return 0; |
| 71 |
} |
| 72 |
char *p = malloc(l); |
| 73 |
if (p) memset(p, 0, l); |
| 74 |
return p; |
| 75 |
} |
| 76 |
|
| 77 |
char *__fc_env[ARG_MAX]; |
| 78 |
// To provide for some non-determinism, __fc_initenv initializes the |
| 79 |
// environment with an arbitrary string |
| 80 |
#define __FC_INITENV_LEN 64 |
| 81 |
static char __fc_env_strings[__FC_INITENV_LEN]; |
| 82 |
|
| 83 |
static void __fc_initenv() { |
| 84 |
static char init; |
| 85 |
if (!init) { |
| 86 |
// -1 to ensure null-termination |
| 87 |
Frama_C_make_unknown(__fc_env_strings, __FC_INITENV_LEN-1); |
| 88 |
for (int i = 0; i < ARG_MAX; i++) { |
| 89 |
__fc_env[i] = __fc_env_strings + Frama_C_interval(0,__FC_INITENV_LEN-1); |
| 90 |
} |
| 91 |
init = 1; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
// all *env functions below follow this pattern: |
| 96 |
// - check input argument(s); |
| 97 |
// - initialize the environment, if not done already |
| 98 |
// - perform the actual function |
| 99 |
|
| 100 |
char *getenv(const char *name) |
| 101 |
{ |
| 102 |
//@ assert !strchr(name, '='); |
| 103 |
|
| 104 |
__fc_initenv(); |
| 105 |
if (Frama_C_nondet(0, 1)) { |
| 106 |
return __fc_env[Frama_C_interval(0, ARG_MAX-1)]; |
| 107 |
} else { |
| 108 |
return 0; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
int putenv(char *string) |
| 113 |
{ |
| 114 |
char *separator __attribute__((unused)) = strchr(string, '='); |
| 115 |
//@ assert string_contains_separator: separator != \null; |
| 116 |
//@ assert name_is_not_empty: separator != string; |
| 117 |
|
| 118 |
__fc_initenv(); |
| 119 |
|
| 120 |
// possible cases: |
| 121 |
// 1. key in string not found in env: |
| 122 |
// a. no more memory ==> ENOMEM |
| 123 |
// b. available memory ==> modify env to point to string |
| 124 |
// 2. key in string found in env ==> modify an existing entry |
| 125 |
if (Frama_C_nondet(0, 1)) { |
| 126 |
if (Frama_C_nondet(0, 1)) { |
| 127 |
//TODO: errno = ENOMEM; |
| 128 |
return Frama_C_interval(INT_MIN, INT_MAX); // return a non-zero value |
| 129 |
} |
| 130 |
__fc_env[Frama_C_interval(0, ARG_MAX-1)] = string; |
| 131 |
} |
| 132 |
return 0; |
| 133 |
} |
| 134 |
|
| 135 |
int setenv(const char *name, const char *value, int overwrite) |
| 136 |
{ |
| 137 |
if (strchr(name, '=')) { |
| 138 |
//TODO: errno = EINVAL; |
| 139 |
return -1; |
| 140 |
} |
| 141 |
size_t namelen = strlen(name); |
| 142 |
if (namelen == 0) { |
| 143 |
//TODO: errno = EINVAL; |
| 144 |
return -1; |
| 145 |
} |
| 146 |
|
| 147 |
__fc_initenv(); |
| 148 |
|
| 149 |
// possible cases: |
| 150 |
// 1. found 'name' and will overwrite, or did not find, but no more memory |
| 151 |
// 2. found 'name' but will not overwrite |
| 152 |
// 3. did not find name and has available memory |
| 153 |
if (Frama_C_nondet(0, 1)) { |
| 154 |
//TODO: errno = ENOMEM; |
| 155 |
return -1; |
| 156 |
} else { |
| 157 |
if (Frama_C_nondet(0, 1)) { |
| 158 |
Frama_C_make_unknown(__fc_env_strings, __FC_INITENV_LEN-1); |
| 159 |
} |
| 160 |
__fc_env[Frama_C_interval(0,ARG_MAX-1)] = __fc_env_strings + Frama_C_interval(0,__FC_INITENV_LEN-1); |
| 161 |
return 0; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
int unsetenv(const char *name) |
| 166 |
{ |
| 167 |
if (strchr(name, '=')) { |
| 168 |
//TODO: errno = EINVAL; |
| 169 |
return -1; |
| 170 |
} |
| 171 |
size_t namelen = strlen(name); |
| 172 |
if (namelen == 0) { |
| 173 |
//TODO: errno = EINVAL; |
| 174 |
return -1; |
| 175 |
} |
| 176 |
|
| 177 |
__fc_initenv(); |
| 178 |
|
| 179 |
if (Frama_C_nondet(0, 1)) { |
| 180 |
__fc_env[Frama_C_interval(0,ARG_MAX-1)] = 0; |
| 181 |
} |
| 182 |
return 0; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
unsigned short __fc_random48_counter[3]; |
| 187 |
|
| 188 |
|
| 189 |
// Note: this implementation does not check the alignment, since it cannot |
| 190 |
// currently be specified in the memory model of most plug-ins |
| 191 |
int posix_memalign(void **memptr, size_t alignment, size_t size) { |
| 192 |
// By default, specifications in the libc are ignored for defined functions, |
| 193 |
// and since we do not actually use alignment, we need to check its validity. |
| 194 |
// The assertion below is the requires in the specification. |
| 195 |
/*@ assert alignment_is_a_suitable_power_of_two: |
| 196 |
alignment >= sizeof(void*) && |
| 197 |
((size_t)alignment & ((size_t)alignment - 1)) == 0; |
| 198 |
*/ |
| 199 |
*memptr = malloc(size); |
| 200 |
if (!*memptr) return ENOMEM; |
| 201 |
return 0; |
| 202 |
} |
| 203 |
|
| 204 |
char *realpath(const char *restrict file_name, char *restrict resolved_name) |
| 205 |
{ |
| 206 |
if (!file_name) { |
| 207 |
errno = EINVAL; |
| 208 |
return NULL; |
| 209 |
} |
| 210 |
// do path search |
| 211 |
|
| 212 |
// simulate possible errors |
| 213 |
switch (Frama_C_interval(0, 6)) { |
| 214 |
case 0: errno = EACCES; return NULL; |
| 215 |
case 1: errno = EIO; return NULL; |
| 216 |
case 2: errno = ELOOP; return NULL; |
| 217 |
case 3: errno = ENAMETOOLONG; return NULL; |
| 218 |
case 4: errno = ENOENT; return NULL; |
| 219 |
case 5: errno = ENOTDIR; return NULL; |
| 220 |
default: break; |
| 221 |
} |
| 222 |
int realpath_len = Frama_C_interval(1, PATH_MAX); |
| 223 |
if (!resolved_name) { |
| 224 |
resolved_name = malloc(PATH_MAX); |
| 225 |
if (!resolved_name) { |
| 226 |
errno = ENOMEM; |
| 227 |
return NULL; |
| 228 |
} |
| 229 |
} |
| 230 |
Frama_C_make_unknown(resolved_name, realpath_len); |
| 231 |
resolved_name[realpath_len-1] = '\0'; |
| 232 |
return resolved_name; |
| 233 |
} |
| 234 |
|
| 235 |
char *canonicalize_file_name(const char *path) { |
| 236 |
return realpath(path, NULL); |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
__POP_FC_STDLIB |