xref: /linux/tools/include/nolibc/stdlib.h (revision 1e3c374e9fd5ef0bf1ebcb866505b1aad404959e)
1 /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
2 /*
3  * stdlib function definitions for NOLIBC
4  * Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu>
5  */
6 
7 /* make sure to include all global symbols */
8 #include "nolibc.h"
9 
10 #ifndef _NOLIBC_STDLIB_H
11 #define _NOLIBC_STDLIB_H
12 
13 #include "std.h"
14 #include "arch.h"
15 #include "types.h"
16 #include "sys.h"
17 #include "string.h"
18 #include <linux/auxvec.h>
19 
20 struct nolibc_heap {
21 	size_t	len;
22 	char	user_p[] __attribute__((__aligned__));
23 };
24 
25 /* Buffer used to store int-to-ASCII conversions. Will only be implemented if
26  * any of the related functions is implemented. The area is large enough to
27  * store "18446744073709551615" or "-9223372036854775808" and the final zero.
28  */
29 static __attribute__((unused)) char itoa_buffer[21];
30 
31 /*
32  * As much as possible, please keep functions alphabetically sorted.
33  */
34 
35 static __inline__
36 int abs(int j)
37 {
38 	return j >= 0 ? j : -j;
39 }
40 
41 static __inline__
42 long labs(long j)
43 {
44 	return j >= 0 ? j : -j;
45 }
46 
47 static __inline__
48 long long llabs(long long j)
49 {
50 	return j >= 0 ? j : -j;
51 }
52 
53 /* must be exported, as it's used by libgcc for various divide functions */
54 void abort(void);
55 __attribute__((weak,unused,noreturn,section(".text.nolibc_abort")))
56 void abort(void)
57 {
58 	_sys_kill(_sys_getpid(), SIGABRT);
59 	for (;;);
60 }
61 
62 static __attribute__((unused))
63 long atol(const char *s)
64 {
65 	unsigned long ret = 0;
66 	unsigned long d;
67 	int neg = 0;
68 
69 	if (*s == '-') {
70 		neg = 1;
71 		s++;
72 	}
73 
74 	while (1) {
75 		d = (*s++) - '0';
76 		if (d > 9)
77 			break;
78 		ret *= 10;
79 		ret += d;
80 	}
81 
82 	return neg ? -ret : ret;
83 }
84 
85 static __attribute__((unused))
86 int atoi(const char *s)
87 {
88 	return atol(s);
89 }
90 
91 static __attribute__((unused))
92 void free(void *ptr)
93 {
94 	struct nolibc_heap *heap;
95 
96 	if (!ptr)
97 		return;
98 
99 	heap = container_of(ptr, struct nolibc_heap, user_p);
100 	munmap(heap, heap->len);
101 }
102 
103 #ifndef NOLIBC_NO_RUNTIME
104 /* getenv() tries to find the environment variable named <name> in the
105  * environment array pointed to by global variable "environ" which must be
106  * declared as a char **, and must be terminated by a NULL (it is recommended
107  * to set this variable to the "envp" argument of main()). If the requested
108  * environment variable exists its value is returned otherwise NULL is
109  * returned.
110  */
111 static __attribute__((unused))
112 char *getenv(const char *name)
113 {
114 	int idx, i;
115 
116 	if (environ) {
117 		for (idx = 0; environ[idx]; idx++) {
118 			for (i = 0; name[i] && name[i] == environ[idx][i];)
119 				i++;
120 			if (!name[i] && environ[idx][i] == '=')
121 				return &environ[idx][i+1];
122 		}
123 	}
124 	return NULL;
125 }
126 #endif /* NOLIBC_NO_RUNTIME */
127 
128 static __attribute__((unused))
129 void *malloc(size_t len)
130 {
131 	struct nolibc_heap *heap;
132 
133 	/* Always allocate memory with size multiple of 4096. */
134 	len  = sizeof(*heap) + len;
135 	len  = (len + 4095UL) & -4096UL;
136 	heap = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE,
137 		    -1, 0);
138 	if (__builtin_expect(heap == MAP_FAILED, 0))
139 		return NULL;
140 
141 	heap->len = len;
142 	return heap->user_p;
143 }
144 
145 static __attribute__((unused))
146 void *calloc(size_t size, size_t nmemb)
147 {
148 	size_t x;
149 
150 	if (__builtin_expect(__builtin_mul_overflow(size, nmemb, &x), 0)) {
151 		SET_ERRNO(ENOMEM);
152 		return NULL;
153 	}
154 
155 	/*
156 	 * No need to zero the heap, the MAP_ANONYMOUS in malloc()
157 	 * already does it.
158 	 */
159 	return malloc(x);
160 }
161 
162 static __attribute__((unused))
163 void *realloc(void *old_ptr, size_t new_size)
164 {
165 	struct nolibc_heap *heap;
166 	size_t user_p_len;
167 	void *ret;
168 
169 	if (!old_ptr)
170 		return malloc(new_size);
171 
172 	heap = container_of(old_ptr, struct nolibc_heap, user_p);
173 	user_p_len = heap->len - sizeof(*heap);
174 	/*
175 	 * Don't realloc() if @user_p_len >= @new_size, this block of
176 	 * memory is still enough to handle the @new_size. Just return
177 	 * the same pointer.
178 	 */
179 	if (user_p_len >= new_size)
180 		return old_ptr;
181 
182 	ret = malloc(new_size);
183 	if (__builtin_expect(!ret, 0))
184 		return NULL;
185 
186 	memcpy(ret, heap->user_p, user_p_len);
187 	munmap(heap, heap->len);
188 	return ret;
189 }
190 
191 /* Converts the unsigned 64bit integer <in> to base <base> ascii into
192  * buffer <buffer>, which must be long enough to store the number and the
193  * trailing zero. The buffer is filled from the first byte, and the number
194  * of characters emitted (not counting the trailing zero) is returned.
195  * The function uses 'multiply by reciprocal' for the divisions and
196  * requires the caller pass the correct reciprocal.
197  *
198  * Note that unlike __div64_const32() in asm-generic/div64.h there isn't
199  * an extra shift done (by ___p), the reciprocal has to be lower resulting
200  * in a slightly low quotient.
201  * Keep things simple by correcting for the error.
202  * This also saves calculating the 'low * low' product (e2 below) which is
203  * very unlikely to be significant.
204  *
205  * Some maths:
206  *	recip = p2 / base - e1;		// With e1 < base.
207  *	q = (recip * in - e2) / p2;	// With e2 < p2.
208  *        = base / in - (e1 * in + e2) / p2;
209  *        > base / in - (e1 * p2 + p2) / p2;
210  *        = base / in - ((e1 + 1) * p2) / p2;
211  *        > base / in - base;
212  * So the maximum error is less than 'base'.
213  * Hence the largest possible digit is '2 * base - 1'.
214  * For base 10 e1 is 6 and you can get digits of 15 (eg from 2**64-1).
215  * Error e1 is largest for a base that is a factor of 2**64+1, the smallest is 274177
216  * and converting 2**42-1 in base 274177 does generate a digit of 274177+274175.
217  * This all means only a single correction is needed rather than a loop.
218  *
219  * __int128 isn't used for mips because gcc prior to 10.0 will call
220  * __multi3 for MIPS64r6. The same also happens for SPARC and clang.
221  */
222 #define _NOLIBC_U64TOA_RECIP(base) ((base) & 1 ? ~0ull / (base) : (1ull << 63) / ((base) / 2))
223 static __attribute__((unused, noinline))
224 int _nolibc_u64toa_base(uint64_t in, char *buffer, unsigned int base, uint64_t recip)
225 {
226 	unsigned int digits = 0;
227 	unsigned int dig;
228 	uint64_t q;
229 	char *p;
230 
231 	/* Generate least significant digit first */
232 	do {
233 #if defined(__SIZEOF_INT128__) && !defined(__mips__) && !defined(__sparc__)
234 		q = ((unsigned __int128)in * recip) >> 64;
235 #else
236 		uint64_t p = (uint32_t)in * (recip >> 32);
237 		q = (in >> 32) * (recip >> 32) + (p >> 32);
238 		p = (uint32_t)p + (in >> 32) * (uint32_t)recip;
239 		q += p >> 32;
240 #endif
241 		dig = in - q * base;
242 		/* Correct for any rounding errors */
243 		if (dig >= base) {
244 			dig -= base;
245 			q++;
246 		}
247 		if (dig > 9)
248 			dig += 'a' - '0' - 10;
249 		buffer[digits++] = '0' + dig;
250 	} while ((in = q));
251 
252 	buffer[digits] = 0;
253 
254 	/* Order reverse to result */
255 	for (p = buffer + digits - 1; p > buffer; buffer++, p--) {
256 		dig = *buffer;
257 		*buffer = *p;
258 		*p = dig;
259 	}
260 
261 	return digits;
262 }
263 
264 /* Converts the unsigned long integer <in> to its hex representation into
265  * buffer <buffer>, which must be long enough to store the number and the
266  * trailing zero (17 bytes for "ffffffffffffffff" or 9 for "ffffffff"). The
267  * buffer is filled from the first byte, and the number of characters emitted
268  * (not counting the trailing zero) is returned.
269  */
270 static __inline__ __attribute__((unused))
271 int utoh_r(unsigned long in, char *buffer)
272 {
273 	return _nolibc_u64toa_base(in, buffer, 16, _NOLIBC_U64TOA_RECIP(16));
274 }
275 
276 /* converts unsigned long <in> to an hex string using the static itoa_buffer
277  * and returns the pointer to that string.
278  */
279 static __inline__ __attribute__((unused))
280 char *utoh(unsigned long in)
281 {
282 	utoh_r(in, itoa_buffer);
283 	return itoa_buffer;
284 }
285 
286 /* Converts the unsigned long integer <in> to its string representation into
287  * buffer <buffer>, which must be long enough to store the number and the
288  * trailing zero (21 bytes for 18446744073709551615 in 64-bit, 11 for
289  * 4294967295 in 32-bit). The buffer is filled from the first byte, and the
290  * number of characters emitted (not counting the trailing zero) is returned.
291  */
292 static __inline__ __attribute__((unused))
293 int utoa_r(unsigned long in, char *buffer)
294 {
295 	return _nolibc_u64toa_base(in, buffer, 10, _NOLIBC_U64TOA_RECIP(10));
296 }
297 
298 /* Converts the signed long integer <in> to its string representation into
299  * buffer <buffer>, which must be long enough to store the number and the
300  * trailing zero (21 bytes for -9223372036854775808 in 64-bit, 12 for
301  * -2147483648 in 32-bit). The buffer is filled from the first byte, and the
302  * number of characters emitted (not counting the trailing zero) is returned.
303  */
304 static __attribute__((unused))
305 int itoa_r(long in, char *buffer)
306 {
307 	char *ptr = buffer;
308 	int len = 0;
309 
310 	if (in < 0) {
311 		in = -(unsigned long)in;
312 		*(ptr++) = '-';
313 		len++;
314 	}
315 	len += utoa_r(in, ptr);
316 	return len;
317 }
318 
319 /* for historical compatibility, same as above but returns the pointer to the
320  * buffer.
321  */
322 static __inline__ __attribute__((unused))
323 char *ltoa_r(long in, char *buffer)
324 {
325 	itoa_r(in, buffer);
326 	return buffer;
327 }
328 
329 /* converts long integer <in> to a string using the static itoa_buffer and
330  * returns the pointer to that string.
331  */
332 static __inline__ __attribute__((unused))
333 char *itoa(long in)
334 {
335 	itoa_r(in, itoa_buffer);
336 	return itoa_buffer;
337 }
338 
339 /* converts long integer <in> to a string using the static itoa_buffer and
340  * returns the pointer to that string. Same as above, for compatibility.
341  */
342 static __inline__ __attribute__((unused))
343 char *ltoa(long in)
344 {
345 	itoa_r(in, itoa_buffer);
346 	return itoa_buffer;
347 }
348 
349 /* converts unsigned long integer <in> to a string using the static itoa_buffer
350  * and returns the pointer to that string.
351  */
352 static __inline__ __attribute__((unused))
353 char *utoa(unsigned long in)
354 {
355 	utoa_r(in, itoa_buffer);
356 	return itoa_buffer;
357 }
358 
359 /* Converts the unsigned 64-bit integer <in> to its hex representation into
360  * buffer <buffer>, which must be long enough to store the number and the
361  * trailing zero (17 bytes for "ffffffffffffffff"). The buffer is filled from
362  * the first byte, and the number of characters emitted (not counting the
363  * trailing zero) is returned.
364  */
365 static __inline__ __attribute__((unused))
366 int u64toh_r(uint64_t in, char *buffer)
367 {
368 	return _nolibc_u64toa_base(in, buffer, 16, _NOLIBC_U64TOA_RECIP(16));
369 }
370 
371 /* converts uint64_t <in> to an hex string using the static itoa_buffer and
372  * returns the pointer to that string.
373  */
374 static __inline__ __attribute__((unused))
375 char *u64toh(uint64_t in)
376 {
377 	u64toh_r(in, itoa_buffer);
378 	return itoa_buffer;
379 }
380 
381 /* Converts the unsigned 64-bit integer <in> to its string representation into
382  * buffer <buffer>, which must be long enough to store the number and the
383  * trailing zero (21 bytes for 18446744073709551615). The buffer is filled from
384  * the first byte, and the number of characters emitted (not counting the
385  * trailing zero) is returned.
386  */
387 static __inline__ __attribute__((unused))
388 int u64toa_r(uint64_t in, char *buffer)
389 {
390 	return _nolibc_u64toa_base(in, buffer, 10, _NOLIBC_U64TOA_RECIP(10));
391 }
392 
393 /* Converts the signed 64-bit integer <in> to its string representation into
394  * buffer <buffer>, which must be long enough to store the number and the
395  * trailing zero (21 bytes for -9223372036854775808). The buffer is filled from
396  * the first byte, and the number of characters emitted (not counting the
397  * trailing zero) is returned.
398  */
399 static __attribute__((unused))
400 int i64toa_r(int64_t in, char *buffer)
401 {
402 	char *ptr = buffer;
403 	int len = 0;
404 
405 	if (in < 0) {
406 		in = -(uint64_t)in;
407 		*(ptr++) = '-';
408 		len++;
409 	}
410 	len += u64toa_r(in, ptr);
411 	return len;
412 }
413 
414 /* converts int64_t <in> to a string using the static itoa_buffer and returns
415  * the pointer to that string.
416  */
417 static __inline__ __attribute__((unused))
418 char *i64toa(int64_t in)
419 {
420 	i64toa_r(in, itoa_buffer);
421 	return itoa_buffer;
422 }
423 
424 /* converts uint64_t <in> to a string using the static itoa_buffer and returns
425  * the pointer to that string.
426  */
427 static __inline__ __attribute__((unused))
428 char *u64toa(uint64_t in)
429 {
430 	u64toa_r(in, itoa_buffer);
431 	return itoa_buffer;
432 }
433 
434 static __attribute__((unused))
435 uintmax_t __strtox(const char *nptr, char **endptr, int base, intmax_t lower_limit, uintmax_t upper_limit)
436 {
437 	const char signed_ = lower_limit != 0;
438 	unsigned char neg = 0, overflow = 0;
439 	uintmax_t val = 0, limit, old_val;
440 	char c;
441 
442 	if (base < 0 || base > 36) {
443 		SET_ERRNO(EINVAL);
444 		goto out;
445 	}
446 
447 	while (isspace(*nptr))
448 		nptr++;
449 
450 	if (*nptr == '+') {
451 		nptr++;
452 	} else if (*nptr == '-') {
453 		neg = 1;
454 		nptr++;
455 	}
456 
457 	if (signed_ && neg)
458 		limit = -(uintmax_t)lower_limit;
459 	else
460 		limit = upper_limit;
461 
462 	if ((base == 0 || base == 16) &&
463 	    (strncmp(nptr, "0x", 2) == 0 || strncmp(nptr, "0X", 2) == 0)) {
464 		base = 16;
465 		nptr += 2;
466 	} else if (base == 0 && strncmp(nptr, "0", 1) == 0) {
467 		base = 8;
468 		nptr += 1;
469 	} else if (base == 0) {
470 		base = 10;
471 	}
472 
473 	while (*nptr) {
474 		c = *nptr;
475 
476 		if (c >= '0' && c <= '9')
477 			c -= '0';
478 		else if (c >= 'a' && c <= 'z')
479 			c = c - 'a' + 10;
480 		else if (c >= 'A' && c <= 'Z')
481 			c = c - 'A' + 10;
482 		else
483 			goto out;
484 
485 		if (c >= base)
486 			goto out;
487 
488 		nptr++;
489 		old_val = val;
490 		val *= base;
491 		val += c;
492 
493 		if (val > limit || val < old_val)
494 			overflow = 1;
495 	}
496 
497 out:
498 	if (overflow) {
499 		SET_ERRNO(ERANGE);
500 		val = limit;
501 	}
502 	if (endptr)
503 		*endptr = (char *)nptr;
504 	return neg ? -val : val;
505 }
506 
507 static __attribute__((unused))
508 long strtol(const char *nptr, char **endptr, int base)
509 {
510 	return __strtox(nptr, endptr, base, LONG_MIN, LONG_MAX);
511 }
512 
513 static __attribute__((unused))
514 unsigned long strtoul(const char *nptr, char **endptr, int base)
515 {
516 	return __strtox(nptr, endptr, base, 0, ULONG_MAX);
517 }
518 
519 static __attribute__((unused))
520 long long strtoll(const char *nptr, char **endptr, int base)
521 {
522 	return __strtox(nptr, endptr, base, LLONG_MIN, LLONG_MAX);
523 }
524 
525 static __attribute__((unused))
526 unsigned long long strtoull(const char *nptr, char **endptr, int base)
527 {
528 	return __strtox(nptr, endptr, base, 0, ULLONG_MAX);
529 }
530 
531 static __attribute__((unused))
532 intmax_t strtoimax(const char *nptr, char **endptr, int base)
533 {
534 	return __strtox(nptr, endptr, base, INTMAX_MIN, INTMAX_MAX);
535 }
536 
537 static __attribute__((unused))
538 uintmax_t strtoumax(const char *nptr, char **endptr, int base)
539 {
540 	return __strtox(nptr, endptr, base, 0, UINTMAX_MAX);
541 }
542 
543 #endif /* _NOLIBC_STDLIB_H */
544