├── .gitignore ├── LICENSE ├── Makefile ├── README.md ├── _Unwind_Resume.c ├── __fprintf_chk.c ├── __fxstat.c ├── __gcc_personality_v0.c ├── __getdelim.c ├── __lxstat.c ├── __stack_chk_fail.c ├── __xstat.c ├── abort.c ├── access.c ├── addmntent.c ├── atof.c ├── atoi.c ├── bsearch.c ├── bzero.c ├── chmod.c ├── clock_gettime.c ├── close.c ├── creat.c ├── ctime_r.c ├── dup.c ├── dup2.c ├── endmntent.c ├── environ.c ├── epoll_create.c ├── epoll_ctl.c ├── epoll_wait.c ├── errno.c ├── eventfd.c ├── eventfd_write.c ├── exit.c ├── fclose.c ├── fcntl.c ├── feof.c ├── fflush.c ├── fopen.c ├── fork.c ├── fprintf.c ├── fputc.c ├── fputs.c ├── fread.c ├── fseek.c ├── fstat.c ├── ftell.c ├── ftruncate.c ├── fwrite.c ├── getc.c ├── getegid.c ├── getenv.c ├── geteuid.c ├── getgid.c ├── getline.c ├── getmntent.c ├── getmntent_r.c ├── getpagesize.c ├── getpid.c ├── getrusage.c ├── gettimeofday.c ├── getuid.c ├── gmtime_r.c ├── h_errno.c ├── hasmntopt.c ├── iconv.c ├── iconv_close.c ├── iconv_open.c ├── include ├── alloca.h ├── arpa │ └── inet.h ├── assert.h ├── byteswap.h ├── ctype.h ├── dirent.h ├── dlfcn.h ├── emmintrin.h ├── endian.h ├── errno.h ├── fcntl.h ├── float.h ├── iconv.h ├── inttypes.h ├── langinfo.h ├── limits.h ├── linux │ ├── random.h │ └── types.h ├── locale.h ├── math.h ├── mntent.h ├── netdb.h ├── netinet │ └── in.h ├── poll.h ├── runtime_reqs.h ├── sched.h ├── signal.h ├── stdarg.h ├── stdbool.h ├── stddef.h ├── stdint.h ├── stdio.h ├── stdlib.h ├── string.h ├── strings.h ├── sys │ ├── epoll.h │ ├── eventfd.h │ ├── mman.h │ ├── param.h │ ├── resource.h │ ├── select.h │ ├── socket.h │ ├── stat.h │ ├── syscall.h │ ├── time.h │ ├── timeb.h │ ├── times.h │ ├── types.h │ ├── utsname.h │ ├── vfs.h │ └── wait.h ├── termios.h ├── time.h ├── tmmintrin.h ├── unistd.h ├── utime.h ├── wctype.h ├── wmmintrin.h └── xmmintrin.h ├── isatty.c ├── isdigit.c ├── isspace.c ├── kill.c ├── link.c ├── localtime_r.c ├── lseek.c ├── lstat.c ├── malloc.c ├── memchr.c ├── memcmp.c ├── memcpy.c ├── memmove.c ├── memset.c ├── mkdir.c ├── mkfifo.c ├── mkstemp.c ├── mmap.c ├── mprotect.c ├── mremap.c ├── munmap.c ├── nl_langinfo.c ├── open.c ├── pabort.c ├── perror.c ├── pipe.c ├── poll.c ├── printf.c ├── putchar.c ├── putenv.c ├── puts.c ├── raise.c ├── rand.c ├── read.c ├── select.c ├── setmntent.c ├── sigaction.c ├── sigaddset.c ├── sigemptyset.c ├── sigprocmask.c ├── snprintf.c ├── sprintf.c ├── stat.c ├── statfs.c ├── stdio_values.c ├── strcasecmp.c ├── strchr.c ├── strcmp.c ├── strcpy.c ├── strdup.c ├── strerror.c ├── strlen.c ├── strncmp.c ├── strncpy.c ├── strnlen.c ├── strrchr.c ├── strstr.c ├── strtol.c ├── sysconf.c ├── tcgetattr.c ├── tcsetattr.c ├── termios.c ├── time.c ├── tolower.c ├── toupper.c ├── tzset.c ├── umask.c ├── unlink.c ├── unsetenv.c ├── utime.c ├── vfprintf.c ├── vprintf.c ├── vsnprintf.c ├── waitpid.c └── write.c /.gitignore: -------------------------------------------------------------------------------- 1 | libminlibc.a 2 | *.o 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 Galois Inc. 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions 6 | are met: 7 | 8 | * Redistributions of source code must retain the above copyright 9 | notice, this list of conditions and the following disclaimer. 10 | 11 | * Redistributions in binary form must reproduce the above copyright 12 | notice, this list of conditions and the following disclaimer in 13 | the documentation and/or other materials provided with the 14 | distribution. 15 | 16 | * Neither the name of Galois, Inc. nor the names of its contributors 17 | may be used to endorse or promote products derived from this 18 | software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 21 | IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 | TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 23 | PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 24 | OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | C_FILES := $(wildcard *.c) 2 | O_FILES := $(patsubst %.c,%.o,$(C_FILES)) 3 | LIBRARY := libminlibc.a 4 | 5 | all: $(LIBRARY) 6 | 7 | clean: 8 | $(RM) $(O_FILES) $(LIBRARY) 9 | 10 | $(LIBRARY): $(O_FILES) 11 | $(AR) cr $(LIBRARY) $(O_FILES) 12 | 13 | %.o: %.c 14 | $(CC) -c -nostdinc -Iinclude/ -o $@ $< 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MinLibC 2 | 3 | This repository contains an extremely minimal version of LibC, for use in 4 | porting lightweight systems to bare metal. It intentionally errors on most file 5 | operations, to reflect the fact that most bare metal systems do not have file 6 | systems. Other routines are forwarded, as appropriate, to the underlying runtime 7 | or operating system. 8 | 9 | ## Porting to a New System 10 | 11 | All of the requirements for porting this library to a new system are defined in 12 | the included file at [include/runtime_reqs.h](include/runtime_reqs.h). 13 | 14 | 15 | -------------------------------------------------------------------------------- /_Unwind_Resume.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | void _Unwind_Resume(void *x) __attribute__((noreturn)); 11 | 12 | void _Unwind_Resume(void *x __attribute__((unused))) 13 | { 14 | printf("Call to _Unwind_Resume ... how are you using pthreads?\n"); 15 | abort(); 16 | } 17 | -------------------------------------------------------------------------------- /__fprintf_chk.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | int __fprintf_chk(FILE *stream, int flags, const char *format, ...) 11 | { 12 | va_list args; 13 | int res; 14 | 15 | va_start(args, format); 16 | res = vfprintf(stream, format, args); 17 | va_end(args); 18 | return res; 19 | } 20 | -------------------------------------------------------------------------------- /__fxstat.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | int __fxstat(int, int, struct stat*); 11 | 12 | int __fxstat(int ver __attribute__((unused)), 13 | int fildes __attribute__((unused)), 14 | struct stat *stat_buf __attribute__((unused))) 15 | { 16 | errno = ENOENT; 17 | return -1; 18 | } 19 | -------------------------------------------------------------------------------- /__gcc_personality_v0.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | int __gcc_personality_v0(int v, ...) __attribute__((noreturn)); 11 | 12 | int __gcc_personality_v0(int v, ...) 13 | { 14 | printf("You are in a very strange, bad place. (GCC personality %d)\n", v); 15 | abort(); 16 | } 17 | -------------------------------------------------------------------------------- /__getdelim.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | size_t __getdelim(char **, size_t *, int, FILE *); 12 | 13 | size_t __getdelim(char **lineptr __attribute__ ((unused)), 14 | size_t *n __attribute__ ((unused)), 15 | int delim __attribute__ ((unused)), 16 | FILE *stream __attribute__ ((unused))) 17 | { 18 | printf("__getdelim\n"); 19 | errno = ENOSYS; 20 | return (-1); 21 | } 22 | -------------------------------------------------------------------------------- /__lxstat.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | int __lxstat(int, const char *, struct stat *); 12 | 13 | int __lxstat(int ver __attribute__((unused)), 14 | const char *path __attribute__((unused)), 15 | struct stat *stat_buf __attribute__((unused))) 16 | { 17 | printf("__lxstat\n"); 18 | errno = ENOENT; 19 | return -1; 20 | } 21 | -------------------------------------------------------------------------------- /__stack_chk_fail.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | 9 | void __attribute__((noreturn)) __stack_chk_fail(void) 10 | { 11 | printf("Stack smashing detected!\n"); 12 | exit(-2357); 13 | } 14 | -------------------------------------------------------------------------------- /__xstat.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | int __xstat(int, const char *, struct stat*); 12 | 13 | int __xstat(int ver __attribute__((unused)), 14 | const char *path __attribute__((unused)), 15 | struct stat *stat_buf __attribute__((unused))) 16 | { 17 | printf("__xstat\n"); 18 | errno = ENOENT; 19 | return -1; 20 | } 21 | -------------------------------------------------------------------------------- /abort.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | void abort(void) 12 | { 13 | printf("Abort called!\n"); 14 | runtime_exit(); 15 | } 16 | -------------------------------------------------------------------------------- /access.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | int access(const char *pathname __attribute__ ((unused)), 12 | int mode __attribute__ ((unused))) 13 | { 14 | printf("access\n"); 15 | errno = ENOENT; 16 | return (-1); 17 | } 18 | -------------------------------------------------------------------------------- /addmntent.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | int addmntent(FILE *fp __attribute__((unused)), 12 | const struct mntent *mnt __attribute__((unused))) 13 | { 14 | return 1; 15 | } 16 | -------------------------------------------------------------------------------- /atof.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | 9 | #define isdigit(c) (c >= '0' && c <= '9') 10 | 11 | double atof(const char *s) 12 | { 13 | // This function stolen from either Rolf Neugebauer or Andrew Tolmach. 14 | // Probably Rolf. 15 | double a = 0.0; 16 | int e = 0; 17 | int c; 18 | while ((c = *s++) != '\0' && isdigit(c)) { 19 | a = a*10.0 + (c - '0'); 20 | } 21 | if (c == '.') { 22 | while ((c = *s++) != '\0' && isdigit(c)) { 23 | a = a*10.0 + (c - '0'); 24 | e = e-1; 25 | } 26 | } 27 | if (c == 'e' || c == 'E') { 28 | int sign = 1; 29 | int i = 0; 30 | c = *s++; 31 | if (c == '+') 32 | c = *s++; 33 | else if (c == '-') { 34 | c = *s++; 35 | sign = -1; 36 | } 37 | while (isdigit(c)) { 38 | i = i*10 + (c - '0'); 39 | c = *s++; 40 | } 41 | e += i*sign; 42 | } 43 | while (e > 0) { 44 | a *= 10.0; 45 | e--; 46 | } 47 | while (e < 0) { 48 | a *= 0.1; 49 | e++; 50 | } 51 | return a; 52 | } 53 | 54 | -------------------------------------------------------------------------------- /atoi.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | int atoi(const char *str) 11 | { 12 | int acc = 0; 13 | 14 | for(; str && isdigit(*str); ++str) { 15 | acc *= 10; 16 | acc += *str - 0x30; 17 | } 18 | 19 | return acc; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /bsearch.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | 9 | void *bsearch(const void *key, 10 | const void *base0, 11 | size_t nmemb, 12 | size_t size, 13 | int (*compar)(const void *, const void *)) 14 | { 15 | const char *base = base0; 16 | size_t lim; 17 | int cmp; 18 | const void *p; 19 | 20 | for (lim = nmemb; lim != 0; lim >>= 1) { 21 | p = base + (lim >> 1) * size; 22 | cmp = (*compar)(key, p); 23 | if (cmp == 0) 24 | /* LINTED interface spec */ 25 | return ((void *)p); 26 | if (cmp > 0) { /* key > p: move right */ 27 | /* LINTED we don't touch base */ 28 | base = (char *)p + size; 29 | lim--; 30 | } /* else move left */ 31 | } 32 | return (NULL); 33 | } 34 | 35 | -------------------------------------------------------------------------------- /bzero.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | 9 | void bzero(void *ptr, size_t size) 10 | { 11 | unsigned long i; 12 | unsigned char *p = ptr; 13 | for(i = 0; i < size; ++i) p[i] = 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /chmod.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | int chmod(const char *path __attribute__((unused)), 12 | mode_t mode __attribute__((unused))) 13 | { 14 | printf("chmod\n"); 15 | errno = ENOSYS; 16 | return (-1); 17 | } 18 | -------------------------------------------------------------------------------- /clock_gettime.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int clock_gettime(clockid_t clk_id, struct timespec *tp) 6 | { 7 | struct timeval tv; 8 | int res; 9 | 10 | if(!tp) { 11 | errno = EFAULT; 12 | return -1; 13 | } 14 | 15 | if(clk_id != CLOCK_REALTIME) { 16 | errno = EINVAL; 17 | return -1; 18 | } 19 | 20 | res = runtime_gettimeofday(&tv); 21 | if(res != 0) { 22 | errno = res; 23 | return -1; 24 | } 25 | 26 | tp->tv_sec = tv.tv_sec; 27 | tp->tv_nsec = tv.tv_usec * 1000; 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /close.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | #ifdef URANDOM 12 | #include 13 | #define MUNUSED 14 | #else 15 | #define MUNUSED __attribute((unused)) 16 | #endif /* URANDOM */ 17 | 18 | int close(int fd MUNUSED) 19 | { 20 | printf("close\n"); 21 | 22 | #ifdef URANDOM 23 | if(fd == URANDOM_FD) 24 | return urandom_close(); 25 | #endif 26 | 27 | errno = EBADF; 28 | return (-1); 29 | } 30 | -------------------------------------------------------------------------------- /creat.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | int creat(const char *path __attribute__((unused)), 12 | mode_t mode __attribute__((unused))) 13 | { 14 | printf("creat\n"); 15 | errno = ENOSYS; 16 | return (-1); 17 | } 18 | 19 | -------------------------------------------------------------------------------- /ctime_r.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | char *weekdays[7] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; 11 | char *months[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", 12 | "Aug", "Sep", "Oct", "Nov", "Dec" }; 13 | 14 | char *ctime_r(const time_t *timep, char *buf) 15 | { 16 | struct tm tm; 17 | time_t now = time((time_t*)timep); 18 | localtime_r(&now, &tm); 19 | snprintf(buf, 26, "%s %s %02i %02i:%02i:%02i %i\n", 20 | weekdays[tm.tm_wday], months[tm.tm_mon], tm.tm_mday, tm.tm_hour, 21 | tm.tm_min, tm.tm_sec, tm.tm_year + 1900); 22 | return buf; 23 | } 24 | -------------------------------------------------------------------------------- /dup.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | int dup(int fildes __attribute__((unused))) 12 | { 13 | printf("dup\n"); 14 | errno = ENOSYS; 15 | return (-1); 16 | } 17 | -------------------------------------------------------------------------------- /dup2.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | int dup2(int fildes __attribute__((unused)), 12 | int fildes2 __attribute__((unused))) 13 | { 14 | printf("dup2\n"); 15 | errno = ENOSYS; 16 | return (-1); 17 | } 18 | -------------------------------------------------------------------------------- /endmntent.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | int endmntent(FILE *fp __attribute__((unused))) 12 | { 13 | return 1; 14 | } 15 | -------------------------------------------------------------------------------- /environ.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | char **environ; 8 | -------------------------------------------------------------------------------- /epoll_create.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | int epoll_create(int size __attribute__((unused))) 12 | { 13 | printf("epoll_create\n"); 14 | errno = ENOMEM; 15 | return -1; 16 | } 17 | -------------------------------------------------------------------------------- /epoll_ctl.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | int epoll_ctl(int epfd __attribute__((unused)), 11 | int op __attribute__((unused)), 12 | int fd __attribute__((unused)), 13 | struct epoll_event *event __attribute__((unused))) 14 | { 15 | errno = EINVAL; 16 | return -1; 17 | } 18 | -------------------------------------------------------------------------------- /epoll_wait.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | int epoll_wait(int epfd __attribute__((unused)), 12 | struct epoll_event *events __attribute__((unused)), 13 | int maxevents __attribute__((unused)), 14 | int timeout) 15 | { 16 | switch(timeout) { 17 | case -1: 18 | printf("epoll_wait with endless wait. aborting.\n"); 19 | abort(); 20 | case 0: 21 | return 0; 22 | default: 23 | runtime_block(timeout); 24 | return 0; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /errno.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | 9 | int errno; 10 | 11 | int *__errno_location() 12 | { 13 | return &errno; 14 | } 15 | -------------------------------------------------------------------------------- /eventfd.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | int eventfd(unsigned int initval __attribute__((unused)), 12 | int flags __attribute__((unused))) 13 | { 14 | printf("eventfd\n"); 15 | errno = EINVAL; 16 | return -1; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /eventfd_write.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | int eventfd_write(int fd __attribute__((unused)), 12 | eventfd_t value __attribute__((unused))) 13 | { 14 | printf("eventfd_write\n"); 15 | errno = EINVAL; 16 | return -1; 17 | } 18 | -------------------------------------------------------------------------------- /exit.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | void exit(int status) 12 | { 13 | printf("Exit called with %d\n", status); 14 | runtime_exit(); 15 | } 16 | -------------------------------------------------------------------------------- /fclose.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | #ifdef PROFILING 11 | #include 12 | #define MUNUSED 13 | #else 14 | #define MUNUSED __attribute((unused)) 15 | #endif 16 | 17 | int fclose(FILE *fp MUNUSED) 18 | { 19 | #ifdef PROFILING 20 | if(fp) { 21 | profile_fclose(fp); 22 | return 0; 23 | } 24 | #endif 25 | 26 | errno = EBADF; 27 | return EOF; 28 | } 29 | -------------------------------------------------------------------------------- /fcntl.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | int fcntl(int fd __attribute__((unused)), 11 | int cmd __attribute__((unused)), 12 | ...) 13 | { 14 | errno = EACCES; 15 | return -1; 16 | } 17 | -------------------------------------------------------------------------------- /feof.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | int feof(FILE *stream __attribute__((unused))) 11 | { 12 | printf("feof\n"); 13 | errno = EBADF; 14 | return -1; 15 | } 16 | -------------------------------------------------------------------------------- /fflush.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | int fflush(FILE *stream) 12 | { 13 | if((stream == stdin) || (stream == stdout) || (stream == stderr)) { 14 | return 0; 15 | } else { 16 | #ifdef PROFILING 17 | if(stream) { 18 | profile_flush(stream); 19 | return 0; 20 | } 21 | #endif 22 | errno = EBADF; 23 | return EOF; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /fopen.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | #ifdef PROFILING 11 | #include 12 | #define MUNUSED 13 | #else 14 | #define MUNUSED __attribute((unused)) 15 | #endif 16 | 17 | FILE *fopen(const char *path MUNUSED, const char *mode MUNUSED) 18 | { 19 | #ifdef PROFILING 20 | return profile_fopen(path, mode); 21 | #else 22 | errno = EACCES; 23 | return NULL; 24 | #endif 25 | } 26 | -------------------------------------------------------------------------------- /fork.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | pid_t fork(void) 12 | { 13 | printf("fork\n"); 14 | errno = EAGAIN; 15 | return -1; 16 | } 17 | -------------------------------------------------------------------------------- /fprintf.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | int fprintf(FILE *stream, const char *format, ...) 11 | { 12 | va_list args; 13 | int res; 14 | 15 | va_start(args, format); 16 | res = vfprintf(stream, format, args); 17 | va_end(args); 18 | return res; 19 | } 20 | -------------------------------------------------------------------------------- /fputc.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | int fputc(int c, FILE *stream) 11 | { 12 | if(fprintf(stream, "%c", c) >= 1) 13 | return (int)c; 14 | else 15 | return EOF; 16 | } 17 | -------------------------------------------------------------------------------- /fputs.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | int fputs(const char *s, FILE *stream) 11 | { 12 | if((stream == stdin) || (stream == stdout) || (stream == stderr)) { 13 | return puts(s); 14 | } else { 15 | errno = EBADF; 16 | return EOF; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /fread.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | size_t fread(void *ptr __attribute__((unused)), 11 | size_t size __attribute__((unused)), 12 | size_t nmemb __attribute__((unused)), 13 | FILE *stream __attribute__((unused))) 14 | { 15 | // XXX is this the right errno value? 16 | printf("fread\n"); 17 | errno = EACCES; 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /fseek.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | int fseek(FILE *stream __attribute__((unused)), 11 | long offset __attribute__((unused)), 12 | int whence __attribute__((unused))) 13 | { 14 | printf("fseek\n"); 15 | errno = EBADF; 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /fstat.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | #ifdef URANDOM 11 | #include 12 | #define MUNUSED 13 | #else 14 | #define MUNUSED __attribute((unused)) 15 | #endif /* URANDOM */ 16 | 17 | int fstat(int fd MUNUSED, 18 | struct stat *buf MUNUSED) 19 | { 20 | #ifdef URANDOM 21 | if(fd == URANDOM_FD) 22 | return urandom_stat(buf, 1); 23 | #endif 24 | 25 | errno = EBADF; 26 | return -1; 27 | } 28 | -------------------------------------------------------------------------------- /ftell.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | long ftell(FILE *stream __attribute__((unused))) 11 | { 12 | printf("ftell\n"); 13 | errno = EBADF; 14 | return -1; 15 | } 16 | -------------------------------------------------------------------------------- /ftruncate.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | int ftruncate(int fd __attribute__ ((unused)), 12 | off_t length __attribute__ ((unused))) 13 | { 14 | printf("ftruncate\n"); 15 | errno = EPERM; 16 | return (-1); 17 | } 18 | -------------------------------------------------------------------------------- /fwrite.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) 11 | { 12 | size_t size_in_chars = size * nmemb, i; 13 | 14 | for(i = 0; i < size_in_chars; i++) 15 | if(fputc(*(char*)((unsigned long)ptr + i), stream) == EOF) 16 | return i / size; 17 | 18 | return nmemb; 19 | } 20 | -------------------------------------------------------------------------------- /getc.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | int getc(FILE *stream __attribute__((unused))) 11 | { 12 | printf("getc\n"); 13 | errno = EBADF; 14 | return -1; 15 | } 16 | -------------------------------------------------------------------------------- /getegid.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | gid_t getegid(void) 11 | { 12 | printf("getegid\n"); 13 | return 0xaffab1e; 14 | } 15 | -------------------------------------------------------------------------------- /getenv.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | 9 | char *getenv(const char *name __attribute__((unused))) 10 | { 11 | return NULL; // There is no environment, so this always returns "not found" 12 | } 13 | -------------------------------------------------------------------------------- /geteuid.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | uid_t geteuid(void) 11 | { 12 | printf("geteuid\n"); 13 | return 0x0ddba11; 14 | } 15 | -------------------------------------------------------------------------------- /getgid.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | gid_t getgid(void) 11 | { 12 | printf("getgid\n"); 13 | return 0xaffab1e; 14 | } 15 | -------------------------------------------------------------------------------- /getline.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | ssize_t getline(char **lineptr __attribute__((unused)), 11 | size_t *n __attribute__ ((unused)), 12 | FILE *stream __attribute__ ((unused))) 13 | { 14 | printf("getline\n"); 15 | errno = ENOSYS; 16 | return (-1); 17 | } 18 | -------------------------------------------------------------------------------- /getmntent.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | struct mntent *getmntent(FILE *fp __attribute__((unused))) 12 | { 13 | return NULL; 14 | } 15 | -------------------------------------------------------------------------------- /getmntent_r.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | struct mntent *getmntent_r(FILE *fp __attribute__((unused)), 12 | struct mntent *mntbuf __attribute__((unused)), 13 | char *buf __attribute__((unused)), 14 | int buflen __attribute__((unused))) 15 | { 16 | return NULL; 17 | } 18 | -------------------------------------------------------------------------------- /getpagesize.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include "runtime_reqs.h" 9 | 10 | int getpagesize(void) 11 | { 12 | return runtime_pagesize(); 13 | } 14 | -------------------------------------------------------------------------------- /getpid.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | pid_t getpid(void) 12 | { 13 | printf("getpid\n"); 14 | return 0xca5cad1a; 15 | } 16 | -------------------------------------------------------------------------------- /getrusage.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | int getrusage(int who, struct rusage *usage) 11 | { 12 | return runtime_rusage(who, usage); 13 | } 14 | -------------------------------------------------------------------------------- /gettimeofday.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | int gettimeofday(struct timeval *tv, 13 | struct timezone *tz __attribute__((unused))) 14 | { 15 | return runtime_gettimeofday(tv); 16 | } 17 | -------------------------------------------------------------------------------- /getuid.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | /* HaLVM users are affable oddballs? */ 11 | uid_t getuid(void) 12 | { 13 | printf("getuid\n"); 14 | return 0x0ddba11; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /gmtime_r.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct tm *gmtime_r(const time_t *timep, struct tm *result) 4 | { 5 | return localtime_r(timep, result); 6 | } 7 | -------------------------------------------------------------------------------- /h_errno.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | 9 | int h_errno; 10 | 11 | int *__h_errno_location() 12 | { 13 | return &h_errno; 14 | } 15 | -------------------------------------------------------------------------------- /hasmntopt.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | char *hasmntopt(const struct mntent *mnt __attribute__((unused)), 12 | const char *opt __attribute__((unused))) 13 | { 14 | return NULL; 15 | } 16 | -------------------------------------------------------------------------------- /iconv.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | size_t iconv(iconv_t cd __attribute__((unused)), 12 | char **inbuf, size_t *inbytesleft, 13 | char **outbuf, size_t *outbytesleft) 14 | { 15 | /* The main case is when inbuf is not NULL and *inbuf is not NULL */ 16 | if(inbuf && *inbuf) { 17 | size_t bytesConverted = 0; 18 | 19 | if(!outbuf || !(*outbuf)) { 20 | /* this case is not noted by the man page, which is sad */ 21 | printf("bad iconv() case; (*)inbuf is good but (*)outbuf isn't\n"); 22 | errno = EINVAL; 23 | return -1; 24 | } 25 | 26 | do { 27 | if(*inbytesleft == 0) 28 | return bytesConverted; 29 | if(*outbytesleft == 0) { 30 | errno = E2BIG; 31 | return -1; 32 | } 33 | 34 | **outbuf = **inbuf; 35 | *outbuf += 1; 36 | *inbuf += 1; 37 | *outbytesleft -= 1; 38 | *inbytesleft -= 1; 39 | } while(1); 40 | } 41 | 42 | /* the other two cases appear to not apply, or the appropriate return */ 43 | /* value is zero in any case. */ 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /iconv_close.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | 9 | int iconv_close(iconv_t cd __attribute__((unused))) 10 | { 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /iconv_open.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | 9 | /* GHC apparantly can't get along without iconv. So this is the world's */ 10 | /* most unimpressive implementation of it. There is only one iconv_t */ 11 | /* return value (0xacce55ed), and it always translates things exactly */ 12 | /* byte for byte, no matter what you do. */ 13 | iconv_t iconv_open(const char *tocode __attribute__((unused)), 14 | const char *fromcode __attribute__((unused))) 15 | { 16 | return (iconv_t)0xacce55ed; 17 | } 18 | -------------------------------------------------------------------------------- /include/alloca.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_ALLOCA_H 8 | #define MINLIBC_ALLOCA_H 9 | 10 | #define alloca(x) __builtin_alloca(x) 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /include/arpa/inet.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_ARPA_INET_H 8 | #define MINLIBC_ARPA_INET_H 9 | 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /include/assert.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_ASSERT_H 8 | #define MINLIBC_ASSERT_H 9 | 10 | #include 11 | 12 | #define assert(x) ((!(x)) ? pabort("Assertion failed at %s:%d\n", __FILE__, __LINE__) : 0) 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /include/byteswap.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_BYTESWAP_H 8 | #define MINLIBC_BYTESWAP_H 9 | 10 | #include 11 | 12 | /* glibc compatibility definitions */ 13 | #define bswap_16(x) __bswap_16(x) 14 | #define bswap_32(x) __bswap_32(x) 15 | #define bswap_64(x) __bswap_64(x) 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /include/ctype.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_CTYPE_H 8 | #define MINLIBC_CTYPE_H 9 | 10 | int isdigit(int c); 11 | int isspace(int c); 12 | int tolower(int c); 13 | int toupper(int c); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /include/dirent.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_DIRENT_H 8 | #define MINLIBC_DIRENT_H 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /include/dlfcn.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_DLFCN_H 8 | #define MINLIBC_DLFCN_H 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /include/emmintrin.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef __EMMINTRIN_H 8 | 9 | #ifndef __SSE2__ 10 | #error "SSE2 not supported, consider using flag -msse2" 11 | #endif 12 | 13 | #include 14 | 15 | // Typedefs borrowed from clang. Appears legitimate for GCC. 16 | 17 | // External interface 18 | typedef double __m128d __attribute__((__vector_size__(16))); 19 | typedef long long __m128i __attribute__((__vector_size__(16))); 20 | 21 | /* Type defines. */ 22 | // Internal interface 23 | typedef double v2df __attribute__ ((__vector_size__ (16))); 24 | typedef long long v2di __attribute__ ((__vector_size__ (16))); 25 | typedef short v8hi __attribute__((__vector_size__(16))); 26 | typedef char v16qi __attribute__((__vector_size__(16))); 27 | 28 | static inline __m128i _mm_andnot_si128(__m128i a, __m128i b) 29 | { 30 | return (__m128i)__builtin_ia32_pandn128((v2di)a,(v2di)b); 31 | } 32 | 33 | static inline __m128i _mm_and_si128(__m128i a, __m128i b) 34 | { 35 | return (__m128i)__builtin_ia32_pand128((v2di)a,(v2di)b); 36 | } 37 | 38 | static inline __m128i _mm_or_si128(__m128i a, __m128i b) 39 | { 40 | return (__m128i)__builtin_ia32_por128((v2di)a,(v2di)b); 41 | } 42 | 43 | static inline __m128i _mm_xor_si128(__m128i a, __m128i b) 44 | { 45 | return (__m128i)__builtin_ia32_pxor128((v2di)a,(v2di)b); 46 | } 47 | 48 | #define _mm_shuffle_epi32(a,imm) ((__m128i)__builtin_ia32_pshufd((v4si)a,imm)) 49 | 50 | static inline __m128i _mm_load_si128(__m128i const *a) 51 | { 52 | return *a; 53 | // What? Missing builtin. It appears this is handled by gcc code gen magically... 54 | // return (__m128i)__builtin_ia32_loaddqa((const char *)p); 55 | } 56 | 57 | static inline __m128i _mm_loadu_si128(__m128i const *a) 58 | { 59 | return (__m128i)__builtin_ia32_loaddqu((const char *)a); 60 | } 61 | 62 | static inline void _mm_store_si128(__m128i *p, __m128i b) 63 | { 64 | *p = b; 65 | // Again... 66 | // __builtin_ia32_storedqa((char *)p, (v16qi)b); 67 | } 68 | 69 | static inline void _mm_storeu_si128(__m128i *p, __m128i b) 70 | { 71 | __builtin_ia32_storedqu((char *)p, (v16qi)b); 72 | } 73 | 74 | #define _mm_slli_si128(key, imm) ((__m128i)__builtin_ia32_pslldqi128((v2di)key, imm*8)) 75 | 76 | static inline __m128i _mm_set_epi32(int i3, int i2, int i1, int i0) 77 | { 78 | // Code from clang, assuming it is equally performant for gcc 79 | return (__m128i)(v4si){ i0, i1, i2, i3}; 80 | } 81 | 82 | static inline __m128i _mm_setr_epi8(char b15, char b14, char b13, char b12, char b11 83 | ,char b10, char b9, char b8, char b7, char b6 84 | ,char b5, char b4, char b3, char b2, char b1 85 | ,char b0) 86 | { 87 | // Code from clang, assuming it is equally performant for gcc 88 | return (__m128i)(v16qi){ b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15 }; 89 | } 90 | 91 | static inline __m128i _mm_set_epi64x(long long q1, long long q0) 92 | { 93 | return (__m128i){ q0, q1 }; 94 | } 95 | 96 | static inline __m128i _mm_add_epi64(__m128i a, __m128i b) 97 | { 98 | return (__m128i)__builtin_ia32_paddq128((v2di)a, (v2di)b); 99 | } 100 | 101 | static inline __m128i _mm_slli_epi64(__m128i a, int count) 102 | { 103 | return __builtin_ia32_psllqi128(a, count); 104 | } 105 | 106 | static inline __m128i _mm_srli_epi64(__m128i a, int count) 107 | { 108 | return __builtin_ia32_psrlqi128(a, count); 109 | } 110 | 111 | static inline __m128i _mm_unpackhi_epi64(__m128i a, __m128i b) 112 | { 113 | return __builtin_ia32_punpckhqdq128(a, b); 114 | } 115 | 116 | static inline __m128i _mm_unpacklo_epi64(__m128i a, __m128i b) 117 | { 118 | return __builtin_ia32_punpcklqdq128(a, b); 119 | } 120 | 121 | static inline __m128i _mm_setr_epi32(int i0, int i1, int i2, int i3) 122 | { 123 | return (__m128i)(v4si){ i0, i1, i2, i3 }; 124 | } 125 | 126 | static inline __m128i _mm_add_epi32(__m128i a, __m128i b) 127 | { 128 | return (__m128i)((v4si)a + (v4si)b); 129 | } 130 | 131 | static inline __m128i _mm_srli_epi32(__m128i a, int count) 132 | { 133 | return (__m128i)__builtin_ia32_psrldi128((v4si)a, count); 134 | } 135 | 136 | static inline __m128i _mm_slli_epi32(__m128i a, int count) 137 | { 138 | return (__m128i)__builtin_ia32_pslldi128((v4si)a, count); 139 | } 140 | 141 | #define _MM_SHUFFLE(z,y,x,w) (((z)<<6)|((y)<<4)|((x)<<2)|(w)) 142 | #define _MM_SHUFFLE2(x,y) (((x) << 1) | (y)) 143 | 144 | #endif /* __EMMINTRIN_H */ 145 | -------------------------------------------------------------------------------- /include/endian.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_ENDIAN_H 8 | #define MINLIBC_ENDIAN_H 9 | 10 | #define __LITTLE_ENDIAN 1234 11 | #define __BIG_ENDIAN 4321 12 | #define __PDP_ENDIAN 3412 13 | 14 | #define __BYTE_ORDER __LITTLE_ENDIAN 15 | 16 | #define LITTLE_ENDIAN __LITTLE_ENDIAN 17 | #define BIG_ENDIAN __BIG_ENDIAN 18 | #define PDP_ENDIAN __PDP_ENDIAN 19 | #define BYTE_ORDER __BYTE_ORDER 20 | 21 | #define htobe16(x) __bswap_16 (x) 22 | #define htole16(x) (x) 23 | #define be16toh(x) __bswap_16 (x) 24 | #define le16toh(x) (x) 25 | 26 | #define htobe32(x) __bswap_32 (x) 27 | #define htole32(x) (x) 28 | #define be32toh(x) __bswap_32 (x) 29 | #define le32toh(x) (x) 30 | 31 | #define htobe64(x) __bswap_64 (x) 32 | #define htole64(x) (x) 33 | #define be64toh(x) __bswap_64 (x) 34 | #define le64toh(x) (x) 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /include/errno.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_ERRNO_H 8 | #define MINLIBC_ERRNO_H 9 | 10 | #define EPERM 1 11 | #define ENOENT 2 12 | #define ESRCH 3 13 | #define EINTR 4 14 | #define EIO 5 15 | #define ENXIO 6 16 | #define E2BIG 7 17 | #define ENOEXEC 8 18 | #define EBADF 9 19 | #define ECHILD 10 20 | #define EAGAIN 11 21 | #define EWOULDBLOCK EAGAIN 22 | #define ENOMEM 12 23 | #define EACCES 13 24 | #define EFAULT 14 25 | #define EBUSY 16 26 | #define EEXIST 17 27 | #define EXDEV 18 28 | #define ENODEV 19 29 | #define ENOTDIR 20 30 | #define EISDIR 21 31 | #define EINVAL 22 32 | #define ENFILE 23 33 | #define EMFILE 24 34 | #define ENOTTY 25 35 | #define ETXTBSY 26 36 | #define EFBIG 27 37 | #define ENOSPC 28 38 | #define ESPIPE 29 39 | #define EROFS 30 40 | #define EMLINK 31 41 | #define EPIPE 32 42 | #define EDOM 33 43 | #define ERANGE 34 44 | #define EDEADLK 35 45 | #define ENAMETOOLONG 36 46 | #define ENOLCK 37 47 | #define ENOTEMPTY 39 48 | #define ELOOP 40 49 | #define ENOMSG 42 50 | #define EIDRM 43 51 | #define EOPNOTSUPP 45 52 | #define EXFULL 54 53 | #define ECONNREFUSED 61 54 | #define EOVERFLOW 75 55 | #define ENOSYS 78 56 | #define EILSEQ 84 57 | #define ENOTSUP 86 58 | #define ENOTSOCK 88 59 | #define EDESTADDRREQ 89 60 | #define EMSGSIZE 90 61 | #define EPROTOTYPE 91 62 | #define ENOPROTOOPT 92 63 | #define EPROTONOSUPPORT 93 64 | #define EAFNOSUPPORT 97 65 | #define EADDRINUSE 98 66 | #define EADDRNOTAVAIL 99 67 | #define ENETDOWN 100 68 | #define ENETUNREACH 101 69 | #define ENETRESET 102 70 | #define ECONNABORTED 103 71 | #define ECONNRESET 104 72 | #define ENOBUFS 105 73 | #define EISCONN 106 74 | #define ENOTCONN 107 75 | #define ETIMEDOUT 110 76 | #define EHOSTUNREACH 113 77 | #define EALREADY 114 78 | #define EINPROGRESS 115 79 | #define ESTALE 116 80 | #define EDQUOT 122 81 | #define ECANCELED 125 82 | 83 | extern int errno; 84 | 85 | int *__errno_location(void); 86 | 87 | #endif 88 | -------------------------------------------------------------------------------- /include/fcntl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_FCNTL_H 8 | #define MINLIBC_FCNTL_H 9 | 10 | #include 11 | 12 | typedef signed long int off_t; 13 | 14 | #define O_RDONLY 0x00000000 15 | #define O_WRONLY 0x00000001 16 | #define O_RDWR 0x00000002 17 | #define O_NONBLOCK 0x00000004 18 | #define O_APPEND 0x00000008 19 | #define O_CREAT 0x00000200 20 | #define O_TRUNC 0x00000400 21 | #define O_EXCL 0x00000800 22 | 23 | #define F_GETFL 3 24 | #define F_SETFL 4 25 | 26 | int open(const char *pathname, int flags, ...); 27 | int fcntl(int fd, int cmd, ...); 28 | int creat(const char *path, mode_t mode); 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /include/float.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_FLOAT_H 8 | #define MINLIBC_FLOAT_H 9 | 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /include/iconv.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_ICONV_H 8 | #define MINLIBC_ICONV_H 9 | 10 | #include 11 | 12 | typedef void *iconv_t; 13 | 14 | iconv_t iconv_open(const char *, const char *); 15 | size_t iconv(iconv_t, char **, size_t*, char **, size_t*); 16 | int iconv_close(iconv_t cd); 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /include/inttypes.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_INTTYPES_H 8 | #define MINLIBC_INTTYPES_H 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /include/langinfo.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_LANGINFO_H 8 | #define MINLIBC_LANGINFO_H 9 | 10 | #define CODESET 4 11 | 12 | typedef int nl_item; 13 | 14 | char *nl_langinfo(nl_item item); 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /include/limits.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_LIMITS_H 8 | #define MINLIBC_LIMITS_H 9 | 10 | #define SCHAR_MAX 127 11 | #define SHRT_MAX 32767 12 | #define USHRT_MAX 65535 13 | #define INT_MAX 2147483647 14 | #define UINT_MAX 4294967295U 15 | 16 | #ifdef __x86_64__ 17 | # define LONG_MAX 9223372036854775807L 18 | # define ULONG_MAX 18446744073709551615UL 19 | #else 20 | # define LONG_MAX 2147483647L 21 | # define ULONG_MAX 4294967295UL 22 | #endif 23 | # define LONG_MIN (-LONG_MAX - 1L) 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /include/linux/random.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GaloisInc/minlibc/9029f1aaa8022069a13b84d0fc33b660862d988a/include/linux/random.h -------------------------------------------------------------------------------- /include/linux/types.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GaloisInc/minlibc/9029f1aaa8022069a13b84d0fc33b660862d988a/include/linux/types.h -------------------------------------------------------------------------------- /include/locale.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_LOCALE_H 8 | #define MINLIBC_LOCALE_H 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /include/math.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_MATH_H 8 | #define MINLIBC_MATH_H 9 | 10 | #define DBL_MIN_EXP __DBL_MIN_EXP__ 11 | #define DBL_MANT_DIG __DBL_MANT_DIG__ 12 | #define FLT_MIN_EXP __FLT_MIN_EXP__ 13 | #define FLT_MANT_DIG __FLT_MANT_DIG__ 14 | 15 | double modf(double x, double *iptr); 16 | double ldexp(double x, int exp); 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /include/mntent.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_MNTENT_H 8 | #define MINLIBC_MNTENT_H 9 | 10 | struct mntent { 11 | char *mnt_fsname; 12 | char *mnt_dir; 13 | char *mnt_type; 14 | char *mnt_opts; 15 | int mnt_freq; 16 | int mnt_passno; 17 | }; 18 | 19 | char *hasmntopt(const struct mntent *mnt, const char *opt); 20 | int endmntent(FILE *fp); 21 | struct mntent *getmntent(FILE *fp); 22 | int addmntent(FILE *fp, const struct mntent *mnt); 23 | struct mntent *getmntent_r(FILE *fp, struct mntent *mntbuf, 24 | char *buf, int buflen); 25 | FILE *setmntent(const char *filename, const char *type); 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /include/netdb.h: -------------------------------------------------------------------------------- 1 | #ifndef MINLIBC_NETDB_H 2 | #define MINLIBC_NETDB_H 3 | 4 | #define HOST_NOT_FOUND 112 5 | #define NO_DATA 113 6 | #define NO_RECOVERY 114 7 | #define TRY_AGAIN 115 8 | 9 | extern int h_errno; 10 | 11 | int *__h_errno_location(void); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /include/netinet/in.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_NETINET_IN_H 8 | #define MINLIBC_NETINET_IN_H 9 | 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /include/poll.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_POLL_H 8 | #define MINLIBC_POLL_H 9 | 10 | #include 11 | 12 | typedef unsigned long int nfds_t; 13 | 14 | struct pollfd { 15 | int fd; 16 | short int events; 17 | short int revents; 18 | }; 19 | 20 | #define POLLIN 0x001 21 | #define POLLOUT 0x004 22 | #define POLLERR 0x008 23 | #define POLLHUP 0x010 24 | 25 | int poll(struct pollfd *fds, nfds_t nfds, int timeout); 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /include/runtime_reqs.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_RUNTIME_REQS 8 | #define MINLIBC_RUNTIME_REQS 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | void runtime_write(size_t len, char *buffer); 19 | void runtime_block(unsigned long milliseconds); 20 | void runtime_exit(void) __attribute__((noreturn)); 21 | void *runtime_alloc(void *start, size_t length, int prot); 22 | void *runtime_realloc(void *start, int canmove, size_t oldlen, size_t newlen); 23 | void runtime_free(void *start, size_t length); 24 | int runtime_memprotect(void *addr, size_t length, int prot); 25 | int runtime_pagesize(void); 26 | time_t runtime_time(void); 27 | int runtime_gettimeofday(struct timeval *); 28 | int runtime_rusage(int who, struct rusage *); 29 | 30 | #ifdef URANDOM 31 | #define URANDOM_FD 9001 32 | int urandom_open(void); 33 | int urandom_stat(struct stat *buf, int check_consumers); 34 | ssize_t urandom_read(uint8_t *buf, size_t len); 35 | int urandom_close(void); 36 | #endif 37 | 38 | #ifdef PROFILING 39 | FILE *profile_fopen(const char *fname, const char *mode); 40 | void profile_flush(FILE *pfile); 41 | void profile_write(FILE *pfile, void *buffer, int amt); 42 | void profile_fclose(FILE *pfile); 43 | #endif 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /include/sched.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_SCHED_H 8 | #define MINLIBC_SCHED_H 9 | 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /include/signal.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_SIGNAL_H 8 | #define MINLIBC_SIGNAL_H 9 | 10 | #include 11 | 12 | typedef signed int siginfo_t; 13 | typedef unsigned long sigset_t; 14 | typedef void (*__sighandler_t)(int); 15 | 16 | struct sigaction { 17 | __sighandler_t sa_handler; 18 | unsigned long sa_flags; 19 | sigset_t sa_mask; 20 | }; 21 | 22 | #define SIG_DFL ((__sighandler_t)0) 23 | #define SIG_UNBLOCK 1 24 | 25 | int sigemptyset(sigset_t *set); 26 | int sigfillset(sigset_t *set); 27 | int sigaddset(sigset_t *set, int signum); 28 | int sigdelset(sigset_t *set, int signum); 29 | int sigismember(const sigset_t *set, int signum); 30 | 31 | int sigprocmask(int how, const sigset_t *set, sigset_t *oldset); 32 | int sigaction(int signum, const struct sigaction *act, 33 | struct sigaction *oldact); 34 | 35 | int raise(int sig); 36 | int kill(pid_t pid, int sig); 37 | 38 | #define SIGCONT 18 39 | #define SIGSTOP 19 40 | #define SIGTSTP 20 41 | #define SIGTTIN 21 42 | #define SIGTTOU 22 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /include/stdarg.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_STDARG_H 8 | #define MINLIBC_STDARG_H 9 | 10 | #include 11 | 12 | typedef __builtin_va_list va_list; 13 | 14 | #define va_start(v,l) __builtin_va_start(v,l) 15 | #define va_end(v) __builtin_va_end(v) 16 | #define va_arg(v,l) __builtin_va_arg(v,l) 17 | 18 | int vprintf(const char *format, va_list ap); 19 | int vfprintf(FILE *stream, const char *format, va_list ap); 20 | int vsnprintf(char *str, size_t size, const char *format, va_list ap); 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /include/stdbool.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_STDBOOL_H 8 | #define MINLIBC_STDBOOL_H 9 | 10 | #define bool _Bool 11 | #define true 1 12 | #define false 0 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /include/stddef.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_STDDEF_H 8 | #define MINLIBC_STDDEF_H 9 | 10 | typedef int ptrdiff_t; 11 | 12 | typedef unsigned long int size_t; 13 | typedef signed long int ssize_t; 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /include/stdint.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_STDINT_H 8 | #define MINLIBC_STDINT_H 9 | 10 | typedef signed char int8_t; 11 | typedef unsigned char uint8_t; 12 | typedef signed short int int16_t; 13 | typedef unsigned short int uint16_t; 14 | typedef signed int int32_t; 15 | typedef unsigned int uint32_t; 16 | 17 | #ifdef __x86_64__ 18 | typedef signed long int64_t; 19 | typedef unsigned long uint64_t; 20 | typedef signed long intptr_t; 21 | typedef unsigned long uintptr_t; 22 | #else 23 | typedef signed long long int64_t; 24 | typedef unsigned long long uint64_t; 25 | typedef signed int intptr_t; 26 | typedef unsigned int uintptr_t; 27 | #endif 28 | 29 | #define INT8_MIN (-0x7f - 1) 30 | #define INT16_MIN (-0x7fff - 1) 31 | #define INT32_MIN (-0x7fffffff - 1) 32 | #define INT64_MIN (-0x7fffffffffffffffLL - 1) 33 | 34 | #define INT8_MAX 0x7f 35 | #define INT16_MAX 0x7fff 36 | #define INT32_MAX 0x7fffffff 37 | #define INT64_MAX 0x7fffffffffffffffLL 38 | 39 | #define UINT8_MAX 0xff 40 | #define UINT16_MAX 0xffff 41 | #define UINT32_MAX 0xffffffffU 42 | #define UINT64_MAX 0xffffffffffffffffULL 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /include/stdio.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_STDIO_H 8 | 9 | #include 10 | 11 | #define MINLIBC_STDIO_H 12 | 13 | #ifndef NULL 14 | #define NULL 0 15 | #endif 16 | 17 | #define BUFSIZ 8192 18 | #define EOF (-1) 19 | 20 | #define SEEK_SET 0x1 21 | #define SEEK_CUR 0x2 22 | #define SEEK_END 0x3 23 | 24 | typedef struct FILE FILE; 25 | typedef unsigned int pid_t; 26 | 27 | extern FILE *stdin; 28 | extern FILE *stdout; 29 | extern FILE *stderr; 30 | 31 | FILE *fopen(const char *path, const char *mode); 32 | int fputc(int c, FILE *stream); 33 | int fputs(const char *s, FILE *stream); 34 | size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); 35 | size_t fwrite(const void *, size_t, size_t, FILE *); 36 | int fflush(FILE *stream); 37 | int fseek(FILE *stream, long offset, int whence); 38 | long ftell(FILE *stream); 39 | int feof(FILE *stream); 40 | int fclose(FILE *fp); 41 | 42 | int getc(FILE *stream); 43 | ssize_t getline(char **lineptr, size_t *n, FILE *stream); 44 | int putchar(int c); 45 | int puts(const char *s); 46 | 47 | int printf(const char *format, ...); 48 | int sprintf(char *str, const char *format, ...); 49 | int snprintf(char *str, size_t size, const char *format, ...); 50 | int fprintf(FILE *stream, const char *format, ...); 51 | 52 | void perror(const char *s); 53 | void pabort(const char *format, ...) __attribute__((noreturn)); 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /include/stdlib.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_STDLIB_H 8 | #define MINLIBC_STDLIB_H 9 | 10 | #define NULL 0 11 | #define EXIT_SUCCESS 0 12 | #define EXIT_FAILURE 1 13 | 14 | typedef unsigned long int size_t; 15 | 16 | double atof(const char *nptr); 17 | int atoi(const char *nptr); 18 | long int strtol(const char *nptr, char **endptr, int base); 19 | unsigned long long strtoull (const char *__restrict, char **__restrict, int); 20 | 21 | char *getenv(const char *name); 22 | void abort(void) __attribute__((noreturn)); 23 | void exit(int status) __attribute__((noreturn)); 24 | 25 | void *malloc(size_t size); 26 | void *realloc(void *ptr, size_t size); 27 | void *calloc(size_t nmemb, size_t size); 28 | void free(void *ptr); 29 | 30 | int mkstemp(char *template); 31 | 32 | void *bsearch(const void *, const void *, size_t, size_t, 33 | int (*)(const void *, const void *)); 34 | 35 | int putenv(char *str); 36 | int unsetenv(const char *name); 37 | 38 | int abs(int j); 39 | 40 | int rand (void); 41 | void srand (unsigned); 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /include/string.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_STRING_H 8 | #define MINLIBC_STRING_H 9 | 10 | #include 11 | 12 | size_t strlen(const char *s); 13 | size_t strnlen(const char *s, size_t maxlen); 14 | char *strcpy(char *dest, const char *src); 15 | char *strncpy(char *dest, const char *src, size_t n); 16 | int strcmp(const char *s1, const char *s2); 17 | char *strdup(const char *s); 18 | int strncmp(const char *s1, const char *s2, size_t n); 19 | char *strrchr(const char *s, int c); 20 | char *strerror(int errnum); 21 | char *strstr(const char *, const char *); 22 | char *strchr(const char *, int c); 23 | 24 | void *memcpy(void *dest, const void *src, size_t n); 25 | void *memset(void *s, int c, size_t n); 26 | void *memmove(void *dest, const void *src, size_t n); 27 | int memcmp(const void *s1, const void *s2, size_t n); 28 | void *memchr(const void *s, int c, size_t n); 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /include/strings.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_STRINGS_H 8 | #define MINLIBC_STRINGS_H 9 | 10 | #include 11 | 12 | void bzero(void *s, size_t n); 13 | int strcasecmp(const char *s1, const char *s2); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /include/sys/epoll.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_EPOLL_H 8 | #define MINLIBC_EPOLL_H 9 | 10 | #include 11 | #include 12 | 13 | typedef union epoll_data { 14 | void *ptr; 15 | int fd; 16 | uint32_t u32; 17 | uint64_t u64; 18 | } epoll_data_t; 19 | 20 | struct epoll_event { 21 | uint32_t events; 22 | epoll_data_t data; 23 | }; 24 | 25 | #define EPOLL_CTL_ADD 1 26 | #define EPOLL_CTL_DEL 2 27 | #define EPOLL_CTL_MOD 3 28 | 29 | #define EPOLLIN 0x1 30 | #define EPOLLOUT 0x4 31 | #define EPOLLERR 0x8 32 | #define EPOLLHUP 0x10 33 | #define EPOLLONESHOT (1u << 30) 34 | 35 | int epoll_create(int size); 36 | int epoll_ctl(int, int, int, struct epoll_event *); 37 | int epoll_wait(int, struct epoll_event *, int, int); 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /include/sys/eventfd.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_SYS_EVENTFD_H 8 | #define MINLIBC_SYS_EVENTFD_H 9 | 10 | typedef unsigned long long eventfd_t; 11 | 12 | int eventfd(unsigned int initval, int flags); 13 | int eventfd_write(int, eventfd_t val); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /include/sys/mman.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_MMAN_H 8 | #define MINLIBC_MMAN_H 9 | 10 | #include 11 | 12 | #define PROT_NONE 0x0 13 | #define PROT_READ 0x1 14 | #define PROT_WRITE 0x2 15 | #define PROT_EXEC 0x4 16 | #define PROT_NOCACHE 0x8 17 | #define PROT_READWRITE (PROT_READ | PROT_WRITE) 18 | 19 | #define MAP_SHARED 0x01 20 | #define MAP_PRIVATE 0x02 21 | #define MAP_FIXED 0x10 22 | #define MAP_ANON 0x20 23 | #define MAP_ANONYMOUS 0x20 24 | #define MAP_FAILED ((void *) -1) 25 | 26 | #define MS_ASYNC 1 27 | #define MS_INVALIDATE 2 28 | #define MS_SYNC 4 29 | 30 | #define MADV_NORMAL 0 31 | #define MADV_RANDOM 1 32 | #define MADV_SEQUENTIAL 2 33 | #define MADV_WILLNEED 3 34 | #define MADV_DONTNEED 4 35 | 36 | void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t off); 37 | int munmap(void *start, size_t length); 38 | void *mremap(void *old_address, size_t old_size, size_t new_size, int flags); 39 | int mprotect(void *addr, size_t len, int prot); 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /include/sys/param.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_SYS_PARAM_H 8 | #define MINLIBC_SYS_PARAM_H 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /include/sys/resource.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_SYS_RESOURCE_H 8 | #define MINLIBC_SYS_RESOURCE_H 9 | 10 | #include 11 | #include 12 | 13 | #define RUSAGE_SELF 0 14 | 15 | struct rusage { 16 | struct timeval ru_utime; /* user time used */ 17 | struct timeval ru_stime; /* system time used */ 18 | long ru_maxrss; /* maximum resident set size */ 19 | long ru_ixrss; /* integral shared memory size */ 20 | long ru_idrss; /* integral unshared data size */ 21 | long ru_isrss; /* integral unshared stack size */ 22 | long ru_minflt; /* page reclaims */ 23 | long ru_majflt; /* page faults */ 24 | long ru_nswap; /* swaps */ 25 | long ru_inblock; /* block input operations */ 26 | long ru_oublock; /* block output operations */ 27 | long ru_msgsnd; /* messages sent */ 28 | long ru_msgrcv; /* messages received */ 29 | long ru_nsignals; /* signals received */ 30 | long ru_nvcsw; /* voluntary context switches */ 31 | long ru_nivcsw; /* involuntary context switches */ 32 | }; 33 | 34 | int getrusage(int who, struct rusage *usage); 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /include/sys/select.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_SYS_SELECT_H 8 | #define MINLIBC_SYS_SELECT_H 9 | 10 | #include 11 | 12 | typedef struct {} fd_set; 13 | 14 | int select(int nfds, fd_set *readfds, fd_set *writefds, 15 | fd_set *exceptfds, struct timeval *timeout); 16 | 17 | #define FD_SET(x,y) /* */ 18 | #define FD_ZERO(x) /* */ 19 | #define FD_SETSIZE 1024 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /include/sys/socket.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_SYS_SOCKET_H 8 | #define MINLIBC_SYS_SOCKET_H 9 | 10 | #define PF_INET 2 11 | #define PF_INET6 10 12 | 13 | #define AF_INET PF_INET 14 | #define AF_INET6 PF_INET6 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /include/sys/stat.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_SYS_STAT_H 8 | #define MINLIBC_SYS_STAT_H 9 | 10 | #include 11 | #include 12 | 13 | #define S_IFMT 0170000 14 | #define S_IFSOCK 0140000 15 | #define S_IFLNK 0120000 16 | #define S_IFREG 0100000 17 | #define S_IFBLK 0060000 18 | #define S_IFDIR 0040000 19 | #define S_IFCHR 0020000 20 | #define S_IFIFO 0010000 21 | #define S_ISUID 0004000 22 | #define S_ISGID 0002000 23 | #define S_ISVTX 0001000 24 | #define S_IRWXU 00700 25 | #define S_IRUSR 00400 26 | #define S_IWUSR 00200 27 | #define S_IXUSR 00100 28 | #define S_IRWXG 00070 29 | #define S_IRGRP 00040 30 | #define S_IWGRP 00020 31 | #define S_IXGRP 00010 32 | #define S_IRWXO 00007 33 | #define S_IROTH 00004 34 | #define S_IWOTH 00002 35 | #define S_IXOTH 00001 36 | 37 | #define S_ISSOCK(x) (((x) & S_IFMT) == S_IFSOCK) 38 | #define S_ISFIFO(x) (((x) & S_IFMT) == S_IFIFO) 39 | #define S_ISDIR(x) (((x) & S_IFMT) == S_IFDIR) 40 | #define S_ISBLK(x) (((x) & S_IFMT) == S_IFBLK) 41 | #define S_ISCHR(x) (((x) & S_IFMT) == S_IFCHR) 42 | #define S_ISREG(x) (((x) & S_IFMT) == S_IFREG) 43 | 44 | typedef unsigned long int dev_t; 45 | typedef unsigned int nlink_t; 46 | typedef unsigned int blksize_t; 47 | typedef unsigned int blkcnt_t; 48 | 49 | struct stat { 50 | dev_t st_dev; 51 | ino_t st_ino; 52 | mode_t st_mode; 53 | nlink_t st_nlink; 54 | uid_t st_uid; 55 | gid_t st_gid; 56 | dev_t st_rdev; 57 | off_t st_size; 58 | blksize_t st_blksize; 59 | blkcnt_t st_blocks; 60 | time_t st_atime; 61 | time_t st_mtime; 62 | time_t st_ctime; 63 | }; 64 | 65 | int stat(const char *p, struct stat *buf); 66 | int fstat(int fd, struct stat *buf); 67 | int lstat(const char *path, struct stat *buf); 68 | 69 | int mkdir(const char *p, mode_t mode); 70 | int mkfifo(const char *p, mode_t mode); 71 | mode_t umask(mode_t cmask); 72 | int chmod(const char *p, mode_t mode); 73 | 74 | #endif 75 | -------------------------------------------------------------------------------- /include/sys/syscall.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GaloisInc/minlibc/9029f1aaa8022069a13b84d0fc33b660862d988a/include/sys/syscall.h -------------------------------------------------------------------------------- /include/sys/time.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_SYS_TIME_H 8 | #define MINLIBC_SYS_TIME_H 9 | 10 | #include 11 | 12 | struct timezone { 13 | int tz_minuteswest; 14 | int tz_dsttime; 15 | }; 16 | 17 | int gettimeofday(struct timeval *tv, struct timezone *tz); 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /include/sys/timeb.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_SYS_TIMEB_H 8 | #define MINLIBC_SYS_TIMEB_H 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /include/sys/times.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_SYS_TIMES_H 8 | #define MINLIBC_SYS_TIMES_H 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /include/sys/types.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_SYS_TYPES_H 8 | #define MINLIBC_SYS_TYPES_H 9 | 10 | #include 11 | 12 | typedef signed int mode_t; 13 | typedef signed long int off_t; 14 | typedef unsigned long int dev_t; 15 | typedef unsigned int ino_t; 16 | typedef unsigned int uid_t; 17 | typedef unsigned int gid_t; 18 | typedef unsigned int pid_t; 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /include/sys/utsname.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_SYS_UTSNAME_H 8 | #define MINLIBC_SYS_UTSNAME_H 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /include/sys/vfs.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_SYS_VFS_H 8 | #define MINLIBC_SYS_VFS_H 9 | 10 | struct statfs {}; 11 | 12 | int statfs(const char *path, struct statfs *buf); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /include/sys/wait.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_SYS_WAIT_H 8 | #define MINLIBC_SYS_WAIT_H 9 | 10 | #include 11 | 12 | pid_t waitpid(pid_t pid, int *stat_loc, int options); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /include/termios.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_TERMIOS_H 8 | #define MINLIBC_TERMIOS_H 9 | 10 | typedef unsigned int tcflag_t; 11 | typedef unsigned char cc_t; 12 | 13 | struct termios { 14 | tcflag_t c_iflag; 15 | tcflag_t c_oflag; 16 | tcflag_t c_cflag; 17 | tcflag_t c_lflag; 18 | cc_t c_line; 19 | cc_t c_cc[32]; 20 | }; 21 | 22 | int tcgetattr(int fd, struct termios *termios_p); 23 | int tcsetattr(int fd, int opts, const struct termios *termios_p); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /include/time.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_TIME_H 8 | #define MINLIBC_TIME_H 9 | 10 | #include 11 | 12 | #ifdef __x86_64__ 13 | typedef signed long time_t; 14 | #else 15 | typedef signed long long time_t; 16 | #endif 17 | 18 | typedef signed long suseconds_t; 19 | 20 | struct timeval { 21 | time_t tv_sec; 22 | suseconds_t tv_usec; 23 | }; 24 | 25 | struct tm { 26 | int tm_sec; 27 | int tm_min; 28 | int tm_hour; 29 | int tm_mday; 30 | int tm_mon; 31 | int tm_year; 32 | int tm_wday; 33 | int tm_yday; 34 | int tm_isdst; 35 | int tm_gmtoff; 36 | const char *tm_zone; 37 | }; 38 | 39 | time_t time(time_t *tloc); 40 | char *ctime_r(const time_t *timep, char *buf); 41 | struct tm *gmtime_r(const time_t *timep, struct tm *result); 42 | struct tm *localtime_r(const time_t *timep, struct tm *result); 43 | void tzset(void); 44 | 45 | typedef long int slong_t; 46 | 47 | struct timespec { 48 | time_t tv_sec; 49 | slong_t tv_nsec; 50 | }; 51 | 52 | typedef signed int clockid_t; 53 | 54 | #define CLOCK_REALTIME 0 55 | int clock_gettime(clockid_t clk_id, struct timespec *tp); 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /include/tmmintrin.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | 8 | #ifndef _TMMINTRIN_H 9 | #define _TMMINTRIN_H 10 | 11 | #ifndef __SSSE3__ 12 | #error "Evidently you want SSSE3... consider -mssse3" 13 | #endif 14 | 15 | extern inline __m128i _mm_shuffle_epi8(__m128i a, __m128i b) 16 | { 17 | return (__m128i)__builtin_ia32_pshufb128((v16qi)a, (v16qi)b); 18 | } 19 | 20 | #endif /* TMMINTRIN_H */ 21 | -------------------------------------------------------------------------------- /include/unistd.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_UNISTD_H 8 | #define MINLIBC_UNISTD_H 9 | 10 | #include 11 | 12 | #define _SC_CLK_TCK 2 13 | #define _SC_PAGESIZE 30 14 | 15 | extern char **environ; 16 | 17 | int getpagesize(void); 18 | 19 | off_t lseek(int fd, off_t offset, int whence); 20 | ssize_t read(int fd, void *buf, size_t count); 21 | ssize_t write(int fd, const void *buf, size_t count); 22 | int close(int fd); 23 | int ftruncate(int fd, off_t length); 24 | int isatty(int fd); 25 | int dup(int fildes); 26 | int dup2(int fildes, int fildes2); 27 | int access(const char *pathname, int); 28 | 29 | gid_t getgid(void); 30 | gid_t getegid(void); 31 | uid_t getuid(void); 32 | uid_t geteuid(void); 33 | pid_t getpid(void); 34 | 35 | long sysconf(int name); 36 | pid_t fork(void); 37 | 38 | int pipe(int fildes[2]); 39 | int link(const char *path1, const char *path2); 40 | int unlink(const char *path); 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /include/utime.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_UTIME_H 8 | #define MINLIBC_UTIME_H 9 | 10 | struct utimbuf { }; 11 | 12 | int utime(const char *filename, const struct utimbuf *times); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /include/wctype.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef MINLIBC_WCTYPE_H 8 | #define MINLIBC_WCTYPE_H 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /include/wmmintrin.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef _WMMINTRIN_H 8 | #define _WMMINTRIN_H 9 | 10 | #include 11 | 12 | #ifdef __AES__ 13 | extern inline __m128i _mm_aesdec_si128(__m128i v, __m128i rkey) 14 | { 15 | return (__m128i)__builtin_ia32_aesdec128((v2di)v, (v2di)rkey); 16 | } 17 | 18 | extern inline __m128i _mm_aesenc_si128(__m128i v, __m128i rkey) 19 | { 20 | return (__m128i)__builtin_ia32_aesenc128((v2di)v, (v2di)rkey); 21 | } 22 | 23 | extern inline __m128i _mm_aesenclast_si128(__m128i v, __m128i rkey) 24 | { 25 | return (__m128i)__builtin_ia32_aesenclast128((v2di)v, (v2di)rkey); 26 | } 27 | 28 | extern inline __m128i _mm_aesdeclast_si128(__m128i v, __m128i rkey) 29 | { 30 | return (__m128i)__builtin_ia32_aesdeclast128((v2di)v, (v2di)rkey); 31 | } 32 | 33 | extern inline __m128i _mm_aeskeygenassist_si128(__m128i ckey, const int rcon) 34 | { 35 | return (__m128i)__builtin_ia32_aeskeygenassist128((v2di)ckey, rcon); 36 | } 37 | 38 | extern inline __m128i _mm_aesimc_si128(__m128i v) 39 | { 40 | return (__m128i)__builtin_ia32_aesimc128((v2di)v); 41 | } 42 | #endif /* AES */ 43 | 44 | 45 | 46 | #ifdef __PCLMUL__ 47 | extern inline __m128i _mm_clmulepi64_si128(__m128i v1, __m128i v2, const int imm8) 48 | { 49 | return (__m128i)__builtin_ia32_pclmulqdq128((v2di)v1, (v2di)v2, (char)imm8); 50 | } 51 | #endif /* PCLMUL */ 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /include/xmmintrin.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #ifndef _XMMINTRIN_H 8 | #define _XMMINTRIN_H 9 | 10 | /* User API types */ 11 | typedef float __m128 __attribute__((__vector_size__(16))); 12 | 13 | /* Internal interface */ 14 | typedef int v4si __attribute__((__vector_size__(16))); 15 | typedef float v4sf __attribute__((__vector_size__(16))); 16 | 17 | 18 | #endif /* _XMMINTRIN_H */ 19 | -------------------------------------------------------------------------------- /isatty.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | int isatty(int fd) 12 | { 13 | if(fd >= 0 && fd <= 2) 14 | return 1; 15 | errno = EBADF; 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /isdigit.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | 9 | int isdigit(int c) 10 | { 11 | return ((c >= '0') && (c <= '9')); 12 | } 13 | -------------------------------------------------------------------------------- /isspace.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | 9 | int isspace(int c) 10 | { 11 | return ((c == ' ') || (c == '\n') || (c == '\t')); 12 | } 13 | -------------------------------------------------------------------------------- /kill.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | int kill(pid_t pid __attribute__((unused)), 12 | int sig __attribute__((unused))) 13 | { 14 | printf("kill\n"); 15 | errno = EPERM; 16 | return -1; 17 | } 18 | -------------------------------------------------------------------------------- /link.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | int link(const char *path1 __attribute__((unused)), 12 | const char *path2 __attribute__((unused))) 13 | { 14 | printf("link\n"); 15 | errno = ENOENT; 16 | return -1; 17 | } 18 | -------------------------------------------------------------------------------- /localtime_r.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | static const int days_per_month[2][12] = { 11 | { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, 12 | { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } 13 | }; 14 | 15 | static const int days_per_year[2] = { 365, 366 }; 16 | 17 | static int is_leap_year(int year) 18 | { 19 | // A years is a leap year if it is divisible by 4 except when the year 20 | // is divisible by 100 but not 400. 21 | return (year % 4 == 0) && !( (year % 100 == 0) && (year % 400 != 0) ); 22 | } 23 | 24 | /* sec min hr days */ 25 | #define secs_per_year(x) (60 * 60 * 24 * days_per_year[is_leap_year(x)]) 26 | #define secs_per_mon(x,y)(60 * 60 * 24 * days_per_month[is_leap_year(x)][y]) 27 | #define secs_per_day (60 * 60 * 24) 28 | #define secs_per_hour (60 * 60) 29 | #define secs_per_min (60) 30 | 31 | // This code disregards leap seconds. 32 | struct tm *localtime_r(const time_t *time, struct tm *result) 33 | { 34 | int cur_year = 1970; /* Groovy, man */ 35 | int cur_month = 0, cur_day = 0, cur_hour = 0, cur_min = 0; 36 | int weekday = 0, yearday = 0; 37 | int secs_left = *time; 38 | 39 | /* Then compute the weekday it is. Jan 1, 1970 was a Thursday. */ 40 | weekday = (4 + (secs_left / secs_per_day)) % 7; 41 | 42 | /* Start pulling off years */ 43 | while(secs_left >= secs_per_year(cur_year)) { 44 | ++cur_year; 45 | secs_left -= secs_per_year(cur_year); 46 | } 47 | 48 | yearday = secs_left / secs_per_day; 49 | 50 | /* Then pull off months */ 51 | while(secs_left >= secs_per_mon(cur_year, cur_month)) { 52 | ++cur_month; 53 | secs_left -= secs_per_mon(cur_year, cur_month); 54 | } 55 | 56 | /* Then pull off days */ 57 | while(secs_left >= secs_per_day) { 58 | ++cur_day; 59 | secs_left -= secs_per_day; 60 | } 61 | 62 | /* Then pull off hours */ 63 | while(secs_left >= secs_per_hour) { 64 | ++cur_hour; 65 | secs_left -= secs_per_hour; 66 | } 67 | 68 | /* Then pull off minutes */ 69 | while(secs_left >= secs_per_min) { 70 | ++cur_min; 71 | secs_left -= secs_per_min; 72 | } 73 | 74 | result->tm_sec = secs_left; 75 | result->tm_min = cur_min; 76 | result->tm_hour = cur_hour; 77 | result->tm_mday = cur_day; 78 | result->tm_mon = cur_month; 79 | result->tm_year = cur_year - 1900; 80 | result->tm_wday = weekday; 81 | result->tm_yday = yearday; 82 | result->tm_isdst = 0; 83 | result->tm_gmtoff = 0; 84 | result->tm_zone = "HALVMST"; /* HALVM Standard Time */ 85 | 86 | return result; 87 | } 88 | 89 | 90 | -------------------------------------------------------------------------------- /lseek.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | off_t lseek(int fildes __attribute__ ((unused)), 12 | off_t offset __attribute__ ((unused)), 13 | int whence __attribute__ ((unused))) 14 | { 15 | printf("lseek\n"); 16 | errno = EBADF; 17 | return -1; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /lstat.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | #ifdef URANDOM 11 | #include 12 | #include 13 | #define MUNUSED 14 | #else 15 | #define MUNUSED __attribute((unused)) 16 | #endif /* URANDOM */ 17 | 18 | int lstat(const char *path MUNUSED, 19 | struct stat *buf MUNUSED) 20 | { 21 | #ifdef URANDOM 22 | if(strncmp(path, "/dev/urandom", 13) == 0) 23 | return urandom_stat(buf, 0); 24 | #endif 25 | 26 | return -1; 27 | } 28 | -------------------------------------------------------------------------------- /malloc.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | /* $OpenBSD: malloc.c,v 1.83 2006/05/14 19:53:40 otto Exp $ */ 8 | 9 | /* 10 | * ---------------------------------------------------------------------------- 11 | * "THE BEER-WARE LICENSE" (Revision 42): 12 | * wrote this file. As long as you retain this notice you 13 | * can do whatever you want with this stuff. If we meet some day, and you think 14 | * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp 15 | * ---------------------------------------------------------------------------- 16 | */ 17 | 18 | /* 19 | * Defining MALLOC_EXTRA_SANITY will enable extra checks which are 20 | * related to internal conditions and consistency in malloc.c. This has 21 | * a noticeable runtime performance hit, and generally will not do you 22 | * any good unless you fiddle with the internals of malloc or want 23 | * to catch random pointer corruption as early as possible. 24 | */ 25 | #ifndef MALLOC_EXTRA_SANITY 26 | #undef MALLOC_EXTRA_SANITY 27 | #endif 28 | 29 | /* 30 | * Defining MALLOC_STATS will enable you to call malloc_dump() and set 31 | * the [dD] options in the MALLOC_OPTIONS environment variable. 32 | * It has no run-time performance hit, but does pull in stdio... 33 | */ 34 | #ifndef MALLOC_STATS 35 | #undef MALLOC_STATS 36 | #endif 37 | 38 | /* 39 | * What to use for Junk. This is the byte value we use to fill with 40 | * when the 'J' option is enabled. 41 | */ 42 | #define SOME_JUNK 0xd0 /* as in "Duh" :-) */ 43 | 44 | #include 45 | #include 46 | #include 47 | #include 48 | #include 49 | #include 50 | #include 51 | #include 52 | #include 53 | #include 54 | #include 55 | #include 56 | #include 57 | 58 | typedef unsigned char u_char; 59 | typedef unsigned short u_short; 60 | typedef unsigned long u_long; 61 | typedef char *caddr_t; 62 | 63 | // These are standard for x86/Xen 64 | #define PGSHIFT 12 65 | #define STDERR_FILENO 3 66 | #define arc4random() 2357 67 | 68 | struct iovec { 69 | char *iov_base; 70 | size_t iov_len; 71 | }; 72 | 73 | // This is used for output by this module 74 | static void writev(int d __attribute__((unused)), 75 | const struct iovec *iov, int iovcnt) 76 | { 77 | int i; 78 | 79 | for(i = 0; i < iovcnt; i++) 80 | runtime_write(iov[i].iov_len, iov[i].iov_base); 81 | } 82 | 83 | static void warnx(const char *fmt, ...) 84 | { 85 | va_list args; 86 | 87 | va_start(args, fmt); 88 | printf("MALLOC WARNING: "); 89 | vfprintf(0, fmt, args); 90 | va_end(args); 91 | } 92 | 93 | static unsigned long mallock; 94 | 95 | // No locking for now 96 | #define _MALLOC_LOCK_INIT() { mallock = 0; }/* */ 97 | #define _MALLOC_LOCK() while(!__sync_bool_compare_and_swap(&mallock, 0, 1)) {} 98 | #define _MALLOC_UNLOCK() mallock = 0; 99 | 100 | /* 101 | * The basic parameters you can tweak. 102 | * 103 | * malloc_pageshift pagesize = 1 << malloc_pageshift 104 | * It's probably best if this is the native 105 | * page size, but it shouldn't have to be. 106 | * 107 | * malloc_minsize minimum size of an allocation in bytes. 108 | * If this is too small it's too much work 109 | * to manage them. This is also the smallest 110 | * unit of alignment used for the storage 111 | * returned by malloc/realloc. 112 | * 113 | */ 114 | 115 | #if defined(__sparc__) 116 | #define malloc_pageshift 13U 117 | #endif /* __sparc__ */ 118 | 119 | #ifndef malloc_pageshift 120 | #define malloc_pageshift (PGSHIFT) 121 | #endif 122 | 123 | /* 124 | * No user serviceable parts behind this point. 125 | * 126 | * This structure describes a page worth of chunks. 127 | */ 128 | struct pginfo { 129 | struct pginfo *next; /* next on the free list */ 130 | void *page; /* Pointer to the page */ 131 | u_short size; /* size of this page's chunks */ 132 | u_short shift; /* How far to shift for this size chunks */ 133 | u_short free; /* How many free chunks */ 134 | u_short total; /* How many chunk */ 135 | u_long bits[1];/* Which chunks are free */ 136 | }; 137 | 138 | static __inline__ void free_pages(void *, u_long, struct pginfo *); 139 | static __inline__ void free_bytes(void *, u_long, struct pginfo *); 140 | 141 | /* How many bits per u_long in the bitmap */ 142 | #define MALLOC_BITS (8 * sizeof(u_long)) 143 | 144 | /* 145 | * This structure describes a number of free pages. 146 | */ 147 | struct pgfree { 148 | struct pgfree *next; /* next run of free pages */ 149 | struct pgfree *prev; /* prev run of free pages */ 150 | void *page; /* pointer to free pages */ 151 | void *pdir; /* pointer to the base page's dir */ 152 | size_t size; /* number of bytes free */ 153 | }; 154 | 155 | /* 156 | * Magic values to put in the page_directory 157 | */ 158 | #define MALLOC_NOT_MINE ((struct pginfo*) 0) 159 | #define MALLOC_FREE ((struct pginfo*) 1) 160 | #define MALLOC_FIRST ((struct pginfo*) 2) 161 | #define MALLOC_FOLLOW ((struct pginfo*) 3) 162 | #define MALLOC_MAGIC ((struct pginfo*) 4) 163 | 164 | #ifndef malloc_minsize 165 | #define malloc_minsize 16UL 166 | #endif 167 | 168 | #if !defined(malloc_pagesize) 169 | #define malloc_pagesize (1UL<>1) 178 | #endif 179 | 180 | /* A mask for the offset inside a page. */ 181 | #define malloc_pagemask ((malloc_pagesize)-1) 182 | 183 | #define pageround(foo) (((foo) + (malloc_pagemask)) & ~malloc_pagemask) 184 | #define ptr2index(foo) (((u_long)(foo) >> malloc_pageshift)+malloc_pageshift) 185 | #define index2ptr(idx) ((void*)(((idx)-malloc_pageshift)<dirnum) == pidx) 317 | *pdi = last_dir; 318 | else if (prev_dir != NULL && PD_IDX(prev_dir->dirnum) == pidx) 319 | *pdi = prev_dir; 320 | else if (last_dir != NULL && prev_dir != NULL) { 321 | if ((PD_IDX(last_dir->dirnum) > pidx) ? 322 | (PD_IDX(last_dir->dirnum) - pidx) : 323 | (pidx - PD_IDX(last_dir->dirnum)) 324 | < (PD_IDX(prev_dir->dirnum) > pidx) ? 325 | (PD_IDX(prev_dir->dirnum) - pidx) : 326 | (pidx - PD_IDX(prev_dir->dirnum))) 327 | *pdi = last_dir; 328 | else 329 | *pdi = prev_dir; 330 | 331 | if (PD_IDX((*pdi)->dirnum) > pidx) { 332 | for (spi = (*pdi)->prev; 333 | spi != NULL && PD_IDX(spi->dirnum) > pidx; 334 | spi = spi->prev) 335 | *pdi = spi; 336 | if (spi != NULL) 337 | *pdi = spi; 338 | } else 339 | for (spi = (*pdi)->next; 340 | spi != NULL && PD_IDX(spi->dirnum) <= pidx; 341 | spi = spi->next) 342 | *pdi = spi; 343 | } else { 344 | *pdi = (struct pdinfo *) ((caddr_t) page_dir + pdi_off); 345 | for (spi = *pdi; 346 | spi != NULL && PD_IDX(spi->dirnum) <= pidx; 347 | spi = spi->next) 348 | *pdi = spi; 349 | } 350 | 351 | return ((PD_IDX((*pdi)->dirnum) == pidx) ? 0 : 352 | (PD_IDX((*pdi)->dirnum) > pidx) ? 1 : -1); 353 | } 354 | 355 | #ifdef MALLOC_STATS 356 | void 357 | malloc_dump(int fd) 358 | { 359 | char buf[1024]; 360 | struct pginfo **pd; 361 | struct pgfree *pf; 362 | struct pdinfo *pi; 363 | u_long j; 364 | 365 | pd = page_dir; 366 | pi = (struct pdinfo *) ((caddr_t) pd + pdi_off); 367 | 368 | /* print out all the pages */ 369 | for (j = 0; j <= last_index;) { 370 | snprintf(buf, sizeof buf, "%08lx %5lu ", j << malloc_pageshift, j); 371 | write(fd, buf, strlen(buf)); 372 | if (pd[PI_OFF(j)] == MALLOC_NOT_MINE) { 373 | for (j++; j <= last_index && pd[PI_OFF(j)] == MALLOC_NOT_MINE;) { 374 | if (!PI_OFF(++j)) { 375 | if ((pi = pi->next) == NULL || 376 | PD_IDX(pi->dirnum) != PI_IDX(j)) 377 | break; 378 | pd = pi->base; 379 | j += pdi_mod; 380 | } 381 | } 382 | j--; 383 | snprintf(buf, sizeof buf, ".. %5lu not mine\n", j); 384 | write(fd, buf, strlen(buf)); 385 | } else if (pd[PI_OFF(j)] == MALLOC_FREE) { 386 | for (j++; j <= last_index && pd[PI_OFF(j)] == MALLOC_FREE;) { 387 | if (!PI_OFF(++j)) { 388 | if ((pi = pi->next) == NULL || 389 | PD_IDX(pi->dirnum) != PI_IDX(j)) 390 | break; 391 | pd = pi->base; 392 | j += pdi_mod; 393 | } 394 | } 395 | j--; 396 | snprintf(buf, sizeof buf, ".. %5lu free\n", j); 397 | write(fd, buf, strlen(buf)); 398 | } else if (pd[PI_OFF(j)] == MALLOC_FIRST) { 399 | for (j++; j <= last_index && pd[PI_OFF(j)] == MALLOC_FOLLOW;) { 400 | if (!PI_OFF(++j)) { 401 | if ((pi = pi->next) == NULL || 402 | PD_IDX(pi->dirnum) != PI_IDX(j)) 403 | break; 404 | pd = pi->base; 405 | j += pdi_mod; 406 | } 407 | } 408 | j--; 409 | snprintf(buf, sizeof buf, ".. %5lu in use\n", j); 410 | write(fd, buf, strlen(buf)); 411 | } else if (pd[PI_OFF(j)] < MALLOC_MAGIC) { 412 | snprintf(buf, sizeof buf, "(%p)\n", pd[PI_OFF(j)]); 413 | write(fd, buf, strlen(buf)); 414 | } else { 415 | snprintf(buf, sizeof buf, "%p %d (of %d) x %d @ %p --> %p\n", 416 | pd[PI_OFF(j)], pd[PI_OFF(j)]->free, 417 | pd[PI_OFF(j)]->total, pd[PI_OFF(j)]->size, 418 | pd[PI_OFF(j)]->page, pd[PI_OFF(j)]->next); 419 | write(fd, buf, strlen(buf)); 420 | } 421 | if (!PI_OFF(++j)) { 422 | if ((pi = pi->next) == NULL) 423 | break; 424 | pd = pi->base; 425 | j += (1 + PD_IDX(pi->dirnum) - PI_IDX(j)) * pdi_mod; 426 | } 427 | } 428 | 429 | for (pf = free_list.next; pf; pf = pf->next) { 430 | snprintf(buf, sizeof buf, "Free: @%p [%p...%p[ %ld ->%p <-%p\n", 431 | pf, pf->page, (char *)pf->page + pf->size, 432 | pf->size, pf->prev, pf->next); 433 | write(fd, buf, strlen(buf)); 434 | if (pf == pf->next) { 435 | snprintf(buf, sizeof buf, "Free_list loops\n"); 436 | write(fd, buf, strlen(buf)); 437 | break; 438 | } 439 | } 440 | 441 | /* print out various info */ 442 | snprintf(buf, sizeof buf, "Minsize\t%lu\n", malloc_minsize); 443 | write(fd, buf, strlen(buf)); 444 | snprintf(buf, sizeof buf, "Maxsize\t%lu\n", malloc_maxsize); 445 | write(fd, buf, strlen(buf)); 446 | snprintf(buf, sizeof buf, "Pagesize\t%lu\n", malloc_pagesize); 447 | write(fd, buf, strlen(buf)); 448 | snprintf(buf, sizeof buf, "Pageshift\t%u\n", malloc_pageshift); 449 | write(fd, buf, strlen(buf)); 450 | snprintf(buf, sizeof buf, "In use\t%lu\n", (u_long) malloc_used); 451 | write(fd, buf, strlen(buf)); 452 | snprintf(buf, sizeof buf, "Guarded\t%lu\n", (u_long) malloc_guarded); 453 | write(fd, buf, strlen(buf)); 454 | } 455 | #endif /* MALLOC_STATS */ 456 | 457 | char *__progname = "HALVM"; 458 | 459 | static void 460 | wrterror(char *p) 461 | { 462 | char *q = " error: "; 463 | struct iovec iov[5]; 464 | 465 | iov[0].iov_base = __progname; 466 | iov[0].iov_len = strlen(__progname); 467 | iov[1].iov_base = malloc_func; 468 | iov[1].iov_len = strlen(malloc_func); 469 | iov[2].iov_base = q; 470 | iov[2].iov_len = strlen(q); 471 | iov[3].iov_base = p; 472 | iov[3].iov_len = strlen(p); 473 | iov[4].iov_base = "\n"; 474 | iov[4].iov_len = 1; 475 | writev(STDERR_FILENO, iov, 5); 476 | 477 | suicide = 1; 478 | #ifdef MALLOC_STATS 479 | if (malloc_stats) 480 | malloc_dump(STDERR_FILENO); 481 | #endif /* MALLOC_STATS */ 482 | malloc_active--; 483 | if (malloc_abort) 484 | abort(); 485 | } 486 | 487 | static void 488 | wrtwarning(char *p) 489 | { 490 | char *q = " warning: "; 491 | struct iovec iov[5]; 492 | 493 | if (malloc_abort) 494 | wrterror(p); 495 | else if (malloc_silent) 496 | return; 497 | 498 | iov[0].iov_base = __progname; 499 | iov[0].iov_len = strlen(__progname); 500 | iov[1].iov_base = malloc_func; 501 | iov[1].iov_len = strlen(malloc_func); 502 | iov[2].iov_base = q; 503 | iov[2].iov_len = strlen(q); 504 | iov[3].iov_base = p; 505 | iov[3].iov_len = strlen(p); 506 | iov[4].iov_base = "\n"; 507 | iov[4].iov_len = 1; 508 | 509 | writev(STDERR_FILENO, iov, 5); 510 | } 511 | 512 | #ifdef MALLOC_STATS 513 | static void 514 | malloc_exit(void) 515 | { 516 | char *q = "malloc() warning: Couldn't dump stats\n"; 517 | int save_errno = errno, fd; 518 | 519 | fd = open("malloc.out", O_RDWR|O_APPEND); 520 | if (fd != -1) { 521 | malloc_dump(fd); 522 | close(fd); 523 | } else 524 | write(STDERR_FILENO, q, strlen(q)); 525 | errno = save_errno; 526 | } 527 | #endif /* MALLOC_STATS */ 528 | 529 | /* 530 | * Allocate a number of pages from the OS 531 | */ 532 | static void * 533 | map_pages(size_t pages) 534 | { 535 | struct pdinfo *pi, *spi; 536 | struct pginfo **pd; 537 | u_long idx, pidx, lidx; 538 | caddr_t result, tail; 539 | u_long index, lindex; 540 | void *pdregion = NULL; 541 | size_t dirs, cnt; 542 | 543 | pages <<= malloc_pageshift; 544 | result = MMAP(pages + malloc_guard); 545 | if (result == MAP_FAILED) { 546 | #ifdef MALLOC_EXTRA_SANITY 547 | wrtwarning("(ES): map_pages fails"); 548 | #endif /* MALLOC_EXTRA_SANITY */ 549 | errno = ENOMEM; 550 | return (NULL); 551 | } 552 | index = ptr2index(result); 553 | tail = result + pages + malloc_guard; 554 | lindex = ptr2index(tail) - 1; 555 | if (malloc_guard) 556 | mprotect(result + pages, malloc_guard, PROT_NONE); 557 | 558 | pidx = PI_IDX(index); 559 | lidx = PI_IDX(lindex); 560 | 561 | if (tail > malloc_brk) { 562 | malloc_brk = tail; 563 | last_index = lindex; 564 | } 565 | 566 | dirs = lidx - pidx; 567 | 568 | /* Insert directory pages, if needed. */ 569 | if (pdir_lookup(index, &pi) != 0) 570 | dirs++; 571 | if (dirs > 0) { 572 | pdregion = MMAP(malloc_pagesize * dirs); 573 | if (pdregion == MAP_FAILED) { 574 | munmap(result, tail - result); 575 | #ifdef MALLOC_EXTRA_SANITY 576 | wrtwarning("(ES): map_pages fails"); 577 | #endif 578 | errno = ENOMEM; 579 | return (NULL); 580 | } 581 | } 582 | cnt = 0; 583 | for (idx = pidx, spi = pi; idx <= lidx; idx++) { 584 | if (pi == NULL || PD_IDX(pi->dirnum) != idx) { 585 | pd = (struct pginfo **)((char *)pdregion + 586 | cnt * malloc_pagesize); 587 | cnt++; 588 | memset(pd, 0, malloc_pagesize); 589 | pi = (struct pdinfo *) ((caddr_t) pd + pdi_off); 590 | pi->base = pd; 591 | pi->prev = spi; 592 | pi->next = spi->next; 593 | pi->dirnum = idx * (malloc_pagesize / 594 | sizeof(struct pginfo *)); 595 | 596 | if (spi->next != NULL) 597 | spi->next->prev = pi; 598 | spi->next = pi; 599 | } 600 | if (idx > pidx && idx < lidx) { 601 | pi->dirnum += pdi_mod; 602 | } else if (idx == pidx) { 603 | if (pidx == lidx) { 604 | pi->dirnum += (u_long)(tail - result) >> 605 | malloc_pageshift; 606 | } else { 607 | pi->dirnum += pdi_mod - PI_OFF(index); 608 | } 609 | } else { 610 | pi->dirnum += PI_OFF(ptr2index(tail - 1)) + 1; 611 | } 612 | #ifdef MALLOC_EXTRA_SANITY 613 | if (PD_OFF(pi->dirnum) > pdi_mod || PD_IDX(pi->dirnum) > idx) { 614 | wrterror("(ES): pages directory overflow"); 615 | errno = EFAULT; 616 | return (NULL); 617 | } 618 | #endif /* MALLOC_EXTRA_SANITY */ 619 | if (idx == pidx && pi != last_dir) { 620 | prev_dir = last_dir; 621 | last_dir = pi; 622 | } 623 | spi = pi; 624 | pi = spi->next; 625 | } 626 | #ifdef MALLOC_EXTRA_SANITY 627 | if (cnt > dirs) 628 | wrtwarning("(ES): cnt > dirs"); 629 | #endif /* MALLOC_EXTRA_SANITY */ 630 | if (cnt < dirs) 631 | munmap((char *)pdregion + cnt * malloc_pagesize, 632 | (dirs - cnt) * malloc_pagesize); 633 | 634 | return (result); 635 | } 636 | 637 | /* 638 | * Initialize the world 639 | */ 640 | static void 641 | malloc_init(void) 642 | { 643 | char *p; // , b[64]; 644 | int i, j, save_errno = errno; 645 | 646 | _MALLOC_LOCK_INIT(); 647 | 648 | #ifdef MALLOC_EXTRA_SANITY 649 | malloc_junk = 1; 650 | #endif /* MALLOC_EXTRA_SANITY */ 651 | 652 | for (i = 0; i < 3; i++) { 653 | switch (i) { 654 | /* case 0: 655 | j = readlink("/etc/malloc.conf", b, sizeof b - 1); 656 | if (j <= 0) 657 | continue; 658 | b[j] = '\0'; 659 | p = b; 660 | break; 661 | case 1: 662 | if (issetugid() == 0) 663 | p = getenv("MALLOC_OPTIONS"); 664 | else 665 | continue; 666 | break; 667 | */ case 2: 668 | p = malloc_options; 669 | break; 670 | default: 671 | p = NULL; 672 | } 673 | 674 | for (; p != NULL && *p != '\0'; p++) { 675 | switch (*p) { 676 | case '>': 677 | malloc_cache <<= 1; 678 | break; 679 | case '<': 680 | malloc_cache >>= 1; 681 | break; 682 | case 'a': 683 | malloc_abort = 0; 684 | break; 685 | case 'A': 686 | malloc_abort = 1; 687 | break; 688 | #ifdef MALLOC_STATS 689 | case 'd': 690 | malloc_stats = 0; 691 | break; 692 | case 'D': 693 | malloc_stats = 1; 694 | break; 695 | #endif /* MALLOC_STATS */ 696 | case 'f': 697 | malloc_freeprot = 0; 698 | break; 699 | case 'F': 700 | malloc_freeprot = 1; 701 | break; 702 | case 'g': 703 | malloc_guard = 0; 704 | break; 705 | case 'G': 706 | malloc_guard = malloc_pagesize; 707 | break; 708 | case 'h': 709 | malloc_hint = 0; 710 | break; 711 | case 'H': 712 | malloc_hint = 1; 713 | break; 714 | case 'j': 715 | malloc_junk = 0; 716 | break; 717 | case 'J': 718 | malloc_junk = 1; 719 | break; 720 | case 'n': 721 | malloc_silent = 0; 722 | break; 723 | case 'N': 724 | malloc_silent = 1; 725 | break; 726 | case 'p': 727 | malloc_ptrguard = 0; 728 | break; 729 | case 'P': 730 | malloc_ptrguard = 1; 731 | break; 732 | case 'r': 733 | malloc_realloc = 0; 734 | break; 735 | case 'R': 736 | malloc_realloc = 1; 737 | break; 738 | #ifdef __FreeBSD__ 739 | case 'u': 740 | malloc_utrace = 0; 741 | break; 742 | case 'U': 743 | malloc_utrace = 1; 744 | break; 745 | #endif /* __FreeBSD__ */ 746 | case 'x': 747 | malloc_xmalloc = 0; 748 | break; 749 | case 'X': 750 | malloc_xmalloc = 1; 751 | break; 752 | case 'z': 753 | malloc_zero = 0; 754 | break; 755 | case 'Z': 756 | malloc_zero = 1; 757 | break; 758 | default: 759 | j = malloc_abort; 760 | malloc_abort = 0; 761 | wrtwarning("unknown char in MALLOC_OPTIONS"); 762 | malloc_abort = j; 763 | break; 764 | } 765 | } 766 | } 767 | 768 | UTRACE(0, 0, 0); 769 | 770 | /* 771 | * We want junk in the entire allocation, and zero only in the part 772 | * the user asked for. 773 | */ 774 | if (malloc_zero) 775 | malloc_junk = 1; 776 | 777 | #ifdef MALLOC_STATS 778 | if (malloc_stats && (atexit(malloc_exit) == -1)) 779 | wrtwarning("atexit(2) failed." 780 | " Will not be able to dump malloc stats on exit"); 781 | #endif /* MALLOC_STATS */ 782 | 783 | /* Allocate one page for the page directory. */ 784 | page_dir = (struct pginfo **)MMAP(malloc_pagesize); 785 | 786 | if (page_dir == MAP_FAILED) { 787 | wrterror("mmap(2) failed, check limits"); 788 | errno = ENOMEM; 789 | return; 790 | } 791 | pdi_off = (malloc_pagesize - sizeof(struct pdinfo)) & ~(malloc_minsize - 1); 792 | pdi_mod = pdi_off / sizeof(struct pginfo *); 793 | 794 | last_dir = (struct pdinfo *) ((caddr_t) page_dir + pdi_off); 795 | last_dir->base = page_dir; 796 | last_dir->prev = last_dir->next = NULL; 797 | last_dir->dirnum = malloc_pageshift; 798 | 799 | /* Been here, done that. */ 800 | malloc_started++; 801 | 802 | /* Recalculate the cache size in bytes, and make sure it's nonzero. */ 803 | if (!malloc_cache) 804 | malloc_cache++; 805 | malloc_cache <<= malloc_pageshift; 806 | errno = save_errno; 807 | } 808 | 809 | /* 810 | * Allocate a number of complete pages 811 | */ 812 | static void * 813 | malloc_pages(size_t size) 814 | { 815 | void *p, *delay_free = NULL, *tp; 816 | int i; 817 | struct pginfo **pd; 818 | struct pdinfo *pi; 819 | u_long pidx, index; 820 | struct pgfree *pf; 821 | 822 | size = pageround(size) + malloc_guard; 823 | 824 | p = NULL; 825 | /* Look for free pages before asking for more */ 826 | for (pf = free_list.next; pf; pf = pf->next) { 827 | 828 | #ifdef MALLOC_EXTRA_SANITY 829 | if (pf->size & malloc_pagemask) { 830 | wrterror("(ES): junk length entry on free_list"); 831 | errno = EFAULT; 832 | return (NULL); 833 | } 834 | if (!pf->size) { 835 | wrterror("(ES): zero length entry on free_list"); 836 | errno = EFAULT; 837 | return (NULL); 838 | } 839 | if (pf->page > (pf->page + pf->size)) { 840 | wrterror("(ES): sick entry on free_list"); 841 | errno = EFAULT; 842 | return (NULL); 843 | } 844 | if ((pi = pf->pdir) == NULL) { 845 | wrterror("(ES): invalid page directory on free-list"); 846 | errno = EFAULT; 847 | return (NULL); 848 | } 849 | if ((pidx = PI_IDX(ptr2index(pf->page))) != PD_IDX(pi->dirnum)) { 850 | wrterror("(ES): directory index mismatch on free-list"); 851 | errno = EFAULT; 852 | return (NULL); 853 | } 854 | pd = pi->base; 855 | if (pd[PI_OFF(ptr2index(pf->page))] != MALLOC_FREE) { 856 | wrterror("(ES): non-free first page on free-list"); 857 | errno = EFAULT; 858 | return (NULL); 859 | } 860 | pidx = PI_IDX(ptr2index((pf->page) + (pf->size)) - 1); 861 | for (pi = pf->pdir; pi != NULL && PD_IDX(pi->dirnum) < pidx; 862 | pi = pi->next) 863 | ; 864 | if (pi == NULL || PD_IDX(pi->dirnum) != pidx) { 865 | wrterror("(ES): last page not referenced in page directory"); 866 | errno = EFAULT; 867 | return (NULL); 868 | } 869 | pd = pi->base; 870 | if (pd[PI_OFF(ptr2index((pf->page) + (pf->size)) - 1)] != MALLOC_FREE) { 871 | wrterror("(ES): non-free last page on free-list"); 872 | errno = EFAULT; 873 | return (NULL); 874 | } 875 | #endif /* MALLOC_EXTRA_SANITY */ 876 | 877 | if (pf->size < size) 878 | continue; 879 | 880 | if (pf->size == size) { 881 | p = pf->page; 882 | pi = pf->pdir; 883 | if (pf->next != NULL) 884 | pf->next->prev = pf->prev; 885 | pf->prev->next = pf->next; 886 | delay_free = pf; 887 | break; 888 | } 889 | p = pf->page; 890 | pf->page = (char *) pf->page + size; 891 | pf->size -= size; 892 | pidx = PI_IDX(ptr2index(pf->page)); 893 | for (pi = pf->pdir; pi != NULL && PD_IDX(pi->dirnum) < pidx; 894 | pi = pi->next) 895 | ; 896 | if (pi == NULL || PD_IDX(pi->dirnum) != pidx) { 897 | wrterror("(ES): hole in directories"); 898 | errno = EFAULT; 899 | return (NULL); 900 | } 901 | tp = pf->pdir; 902 | pf->pdir = pi; 903 | pi = tp; 904 | break; 905 | } 906 | 907 | size -= malloc_guard; 908 | 909 | #ifdef MALLOC_EXTRA_SANITY 910 | if (p != NULL && pi != NULL) { 911 | pidx = PD_IDX(pi->dirnum); 912 | pd = pi->base; 913 | } 914 | if (p != NULL && pd[PI_OFF(ptr2index(p))] != MALLOC_FREE) { 915 | wrterror("(ES): allocated non-free page on free-list"); 916 | errno = EFAULT; 917 | return (NULL); 918 | } 919 | #endif /* MALLOC_EXTRA_SANITY */ 920 | 921 | if (p != NULL && (malloc_guard || malloc_freeprot)) 922 | mprotect(p, size, PROT_READ | PROT_WRITE); 923 | 924 | size >>= malloc_pageshift; 925 | /* Map new pages */ 926 | if (p == NULL) 927 | p = map_pages(size); 928 | if (p != NULL) { 929 | index = ptr2index(p); 930 | pidx = PI_IDX(index); 931 | pdir_lookup(index, &pi); 932 | #ifdef MALLOC_EXTRA_SANITY 933 | if (pi == NULL || PD_IDX(pi->dirnum) != pidx) { 934 | wrterror("(ES): mapped pages not found in directory"); 935 | errno = EFAULT; 936 | return (NULL); 937 | } 938 | #endif /* MALLOC_EXTRA_SANITY */ 939 | if (pi != last_dir) { 940 | prev_dir = last_dir; 941 | last_dir = pi; 942 | } 943 | pd = pi->base; 944 | pd[PI_OFF(index)] = MALLOC_FIRST; 945 | for (i = 1; (size_t)i < size; i++) { 946 | if (!PI_OFF(index + i)) { 947 | pidx++; 948 | pi = pi->next; 949 | #ifdef MALLOC_EXTRA_SANITY 950 | if (pi == NULL || PD_IDX(pi->dirnum) != pidx) { 951 | wrterror("(ES): hole in mapped pages directory"); 952 | errno = EFAULT; 953 | return (NULL); 954 | } 955 | #endif /* MALLOC_EXTRA_SANITY */ 956 | pd = pi->base; 957 | } 958 | pd[PI_OFF(index + i)] = MALLOC_FOLLOW; 959 | } 960 | if (malloc_guard) { 961 | if (!PI_OFF(index + i)) { 962 | pidx++; 963 | pi = pi->next; 964 | #ifdef MALLOC_EXTRA_SANITY 965 | if (pi == NULL || PD_IDX(pi->dirnum) != pidx) { 966 | wrterror("(ES): hole in mapped pages directory"); 967 | errno = EFAULT; 968 | return (NULL); 969 | } 970 | #endif /* MALLOC_EXTRA_SANITY */ 971 | pd = pi->base; 972 | } 973 | pd[PI_OFF(index + i)] = MALLOC_FIRST; 974 | } 975 | malloc_used += size << malloc_pageshift; 976 | malloc_guarded += malloc_guard; 977 | 978 | if (malloc_junk) 979 | memset(p, SOME_JUNK, size << malloc_pageshift); 980 | } 981 | if (delay_free) { 982 | if (px == NULL) 983 | px = delay_free; 984 | else 985 | ifree(delay_free); 986 | } 987 | return (p); 988 | } 989 | 990 | /* 991 | * Allocate a page of fragments 992 | */ 993 | 994 | static __inline__ int 995 | malloc_make_chunks(int bits) 996 | { 997 | struct pginfo *bp, **pd; 998 | struct pdinfo *pi; 999 | #ifdef MALLOC_EXTRA_SANITY 1000 | u_long pidx; 1001 | #endif /* MALLOC_EXTRA_SANITY */ 1002 | void *pp; 1003 | long i, k; 1004 | size_t l; 1005 | 1006 | /* Allocate a new bucket */ 1007 | pp = malloc_pages((size_t) malloc_pagesize); 1008 | if (pp == NULL) 1009 | return (0); 1010 | 1011 | /* Find length of admin structure */ 1012 | l = sizeof *bp - sizeof(u_long); 1013 | l += sizeof(u_long) * 1014 | (((malloc_pagesize >> bits) + MALLOC_BITS - 1) / MALLOC_BITS); 1015 | 1016 | /* Don't waste more than two chunks on this */ 1017 | 1018 | /* 1019 | * If we are to allocate a memory protected page for the malloc(0) 1020 | * case (when bits=0), it must be from a different page than the 1021 | * pginfo page. 1022 | * --> Treat it like the big chunk alloc, get a second data page. 1023 | */ 1024 | if (bits != 0 && (1UL << (bits)) <= l + l) { 1025 | bp = (struct pginfo *) pp; 1026 | } else { 1027 | bp = (struct pginfo *) imalloc(l); 1028 | if (bp == NULL) { 1029 | ifree(pp); 1030 | return (0); 1031 | } 1032 | } 1033 | 1034 | /* memory protect the page allocated in the malloc(0) case */ 1035 | if (bits == 0) { 1036 | bp->size = 0; 1037 | bp->shift = 1; 1038 | i = malloc_minsize - 1; 1039 | while (i >>= 1) 1040 | bp->shift++; 1041 | bp->total = bp->free = malloc_pagesize >> bp->shift; 1042 | bp->page = pp; 1043 | 1044 | k = mprotect(pp, malloc_pagesize, PROT_NONE); 1045 | if (k < 0) { 1046 | ifree(pp); 1047 | ifree(bp); 1048 | return (0); 1049 | } 1050 | } else { 1051 | bp->size = (1UL << bits); 1052 | bp->shift = bits; 1053 | bp->total = bp->free = malloc_pagesize >> bits; 1054 | bp->page = pp; 1055 | } 1056 | 1057 | /* set all valid bits in the bitmap */ 1058 | k = bp->total; 1059 | i = 0; 1060 | 1061 | /* Do a bunch at a time */ 1062 | for (; (k - i) >= (int)MALLOC_BITS; i += MALLOC_BITS) 1063 | bp->bits[i / MALLOC_BITS] = ~0UL; 1064 | 1065 | for (; i < k; i++) 1066 | bp->bits[i / MALLOC_BITS] |= 1UL << (i % MALLOC_BITS); 1067 | 1068 | k = (long)l; 1069 | if (bp == bp->page) { 1070 | /* Mark the ones we stole for ourselves */ 1071 | for (i = 0; k > 0; i++) { 1072 | bp->bits[i / MALLOC_BITS] &= ~(1UL << (i % MALLOC_BITS)); 1073 | bp->free--; 1074 | bp->total--; 1075 | k -= (1 << bits); 1076 | } 1077 | } 1078 | /* MALLOC_LOCK */ 1079 | 1080 | pdir_lookup(ptr2index(pp), &pi); 1081 | #ifdef MALLOC_EXTRA_SANITY 1082 | pidx = PI_IDX(ptr2index(pp)); 1083 | if (pi == NULL || PD_IDX(pi->dirnum) != pidx) { 1084 | wrterror("(ES): mapped pages not found in directory"); 1085 | errno = EFAULT; 1086 | return (0); 1087 | } 1088 | #endif /* MALLOC_EXTRA_SANITY */ 1089 | if (pi != last_dir) { 1090 | prev_dir = last_dir; 1091 | last_dir = pi; 1092 | } 1093 | pd = pi->base; 1094 | pd[PI_OFF(ptr2index(pp))] = bp; 1095 | 1096 | bp->next = page_dir[bits]; 1097 | page_dir[bits] = bp; 1098 | 1099 | /* MALLOC_UNLOCK */ 1100 | return (1); 1101 | } 1102 | 1103 | /* 1104 | * Allocate a fragment 1105 | */ 1106 | static void * 1107 | malloc_bytes(size_t size) 1108 | { 1109 | int i, j; 1110 | size_t k; 1111 | u_long u, *lp; 1112 | struct pginfo *bp; 1113 | 1114 | /* Don't bother with anything less than this */ 1115 | /* unless we have a malloc(0) requests */ 1116 | if (size != 0 && size < malloc_minsize) 1117 | size = malloc_minsize; 1118 | 1119 | /* Find the right bucket */ 1120 | if (size == 0) 1121 | j = 0; 1122 | else { 1123 | j = 1; 1124 | i = size - 1; 1125 | while (i >>= 1) 1126 | j++; 1127 | } 1128 | 1129 | /* If it's empty, make a page more of that size chunks */ 1130 | if (page_dir[j] == NULL && !malloc_make_chunks(j)) 1131 | return (NULL); 1132 | 1133 | bp = page_dir[j]; 1134 | 1135 | /* Find first word of bitmap which isn't empty */ 1136 | for (lp = bp->bits; !*lp; lp++); 1137 | 1138 | /* Find that bit, and tweak it */ 1139 | u = 1; 1140 | k = 0; 1141 | while (!(*lp & u)) { 1142 | u += u; 1143 | k++; 1144 | } 1145 | 1146 | if (malloc_guard) { 1147 | /* Walk to a random position. */ 1148 | i = arc4random() % bp->free; 1149 | while (i > 0) { 1150 | u += u; 1151 | k++; 1152 | if (k >= MALLOC_BITS) { 1153 | lp++; 1154 | u = 1; 1155 | k = 0; 1156 | } 1157 | #ifdef MALLOC_EXTRA_SANITY 1158 | if (lp - bp->bits > (bp->total - 1) / MALLOC_BITS) { 1159 | wrterror("chunk overflow"); 1160 | errno = EFAULT; 1161 | return (NULL); 1162 | } 1163 | #endif /* MALLOC_EXTRA_SANITY */ 1164 | if (*lp & u) 1165 | i--; 1166 | } 1167 | } 1168 | *lp ^= u; 1169 | 1170 | /* If there are no more free, remove from free-list */ 1171 | if (!--bp->free) { 1172 | page_dir[j] = bp->next; 1173 | bp->next = NULL; 1174 | } 1175 | /* Adjust to the real offset of that chunk */ 1176 | k += (lp - bp->bits) * MALLOC_BITS; 1177 | k <<= bp->shift; 1178 | 1179 | if (malloc_junk && bp->size != 0) 1180 | memset((char *)bp->page + k, SOME_JUNK, (size_t)bp->size); 1181 | 1182 | return ((u_char *) bp->page + k); 1183 | } 1184 | 1185 | /* 1186 | * Magic so that malloc(sizeof(ptr)) is near the end of the page. 1187 | */ 1188 | #define PTR_GAP (malloc_pagesize - sizeof(void *)) 1189 | #define PTR_SIZE (sizeof(void *)) 1190 | #define PTR_ALIGNED(p) (((unsigned long)p & malloc_pagemask) == PTR_GAP) 1191 | 1192 | /* 1193 | * Allocate a piece of memory 1194 | */ 1195 | static void * 1196 | imalloc(size_t size) 1197 | { 1198 | void *result; 1199 | int ptralloc = 0; 1200 | 1201 | if (!malloc_started) 1202 | malloc_init(); 1203 | 1204 | if (suicide) 1205 | abort(); 1206 | 1207 | /* does not matter if malloc_bytes fails */ 1208 | if (px == NULL) 1209 | px = malloc_bytes(sizeof *px); 1210 | 1211 | if (malloc_ptrguard && size == PTR_SIZE) { 1212 | ptralloc = 1; 1213 | size = malloc_pagesize; 1214 | } 1215 | if ((size + malloc_pagesize) < size) { /* Check for overflow */ 1216 | result = NULL; 1217 | errno = ENOMEM; 1218 | } else if (size <= malloc_maxsize) 1219 | result = malloc_bytes(size); 1220 | else 1221 | result = malloc_pages(size); 1222 | 1223 | if (malloc_abort == 1 && result == NULL) 1224 | wrterror("allocation failed"); 1225 | if (malloc_zero && result != NULL) 1226 | memset(result, 0, size); 1227 | 1228 | if (result && ptralloc) 1229 | return ((char *) result + PTR_GAP); 1230 | return (result); 1231 | } 1232 | 1233 | /* 1234 | * Change the size of an allocation. 1235 | */ 1236 | static void * 1237 | irealloc(void *ptr, size_t size) 1238 | { 1239 | void *p; 1240 | size_t osize; 1241 | u_long index, i; 1242 | struct pginfo **mp; 1243 | struct pginfo **pd; 1244 | struct pdinfo *pi; 1245 | #ifdef MALLOC_EXTRA_SANITY 1246 | u_long pidx; 1247 | #endif /* MALLOC_EXTRA_SANITY */ 1248 | 1249 | if (suicide) 1250 | abort(); 1251 | 1252 | if (!malloc_started) { 1253 | wrtwarning("malloc() has never been called"); 1254 | return (NULL); 1255 | } 1256 | if (malloc_ptrguard && PTR_ALIGNED(ptr)) { 1257 | if (size <= PTR_SIZE) 1258 | return (ptr); 1259 | 1260 | p = imalloc(size); 1261 | if (p) 1262 | memcpy(p, ptr, PTR_SIZE); 1263 | ifree(ptr); 1264 | return (p); 1265 | } 1266 | index = ptr2index(ptr); 1267 | 1268 | if (index < malloc_pageshift) { 1269 | wrtwarning("junk pointer, too low to make sense"); 1270 | return (NULL); 1271 | } 1272 | if (index > last_index) { 1273 | wrtwarning("junk pointer, too high to make sense"); 1274 | return (NULL); 1275 | } 1276 | pdir_lookup(index, &pi); 1277 | #ifdef MALLOC_EXTRA_SANITY 1278 | pidx = PI_IDX(index); 1279 | if (pi == NULL || PD_IDX(pi->dirnum) != pidx) { 1280 | wrterror("(ES): mapped pages not found in directory"); 1281 | errno = EFAULT; 1282 | return (NULL); 1283 | } 1284 | #endif /* MALLOC_EXTRA_SANITY */ 1285 | if (pi != last_dir) { 1286 | prev_dir = last_dir; 1287 | last_dir = pi; 1288 | } 1289 | pd = pi->base; 1290 | mp = &pd[PI_OFF(index)]; 1291 | 1292 | if (*mp == MALLOC_FIRST) { /* Page allocation */ 1293 | 1294 | /* Check the pointer */ 1295 | if ((u_long) ptr & malloc_pagemask) { 1296 | wrtwarning("modified (page-) pointer"); 1297 | return (NULL); 1298 | } 1299 | /* Find the size in bytes */ 1300 | i = index; 1301 | if (!PI_OFF(++i)) { 1302 | pi = pi->next; 1303 | if (pi != NULL && PD_IDX(pi->dirnum) != PI_IDX(i)) 1304 | pi = NULL; 1305 | if (pi != NULL) 1306 | pd = pi->base; 1307 | } 1308 | for (osize = malloc_pagesize; 1309 | pi != NULL && pd[PI_OFF(i)] == MALLOC_FOLLOW;) { 1310 | osize += malloc_pagesize; 1311 | if (!PI_OFF(++i)) { 1312 | pi = pi->next; 1313 | if (pi != NULL && PD_IDX(pi->dirnum) != PI_IDX(i)) 1314 | pi = NULL; 1315 | if (pi != NULL) 1316 | pd = pi->base; 1317 | } 1318 | } 1319 | 1320 | if (!malloc_realloc && size <= osize && 1321 | size > osize - malloc_pagesize) { 1322 | if (malloc_junk) 1323 | memset((char *)ptr + size, SOME_JUNK, osize - size); 1324 | return (ptr); /* ..don't do anything else. */ 1325 | } 1326 | } else if (*mp >= MALLOC_MAGIC) { /* Chunk allocation */ 1327 | 1328 | /* Check the pointer for sane values */ 1329 | if ((u_long) ptr & ((1UL << ((*mp)->shift)) - 1)) { 1330 | wrtwarning("modified (chunk-) pointer"); 1331 | return (NULL); 1332 | } 1333 | /* Find the chunk index in the page */ 1334 | i = ((u_long) ptr & malloc_pagemask) >> (*mp)->shift; 1335 | 1336 | /* Verify that it isn't a free chunk already */ 1337 | if ((*mp)->bits[i / MALLOC_BITS] & (1UL << (i % MALLOC_BITS))) { 1338 | wrtwarning("chunk is already free"); 1339 | return (NULL); 1340 | } 1341 | osize = (*mp)->size; 1342 | 1343 | if (!malloc_realloc && size <= osize && 1344 | (size > osize / 2 || osize == malloc_minsize)) { 1345 | if (malloc_junk) 1346 | memset((char *) ptr + size, SOME_JUNK, osize - size); 1347 | return (ptr); /* ..don't do anything else. */ 1348 | } 1349 | } else { 1350 | wrtwarning("irealloc: pointer to wrong page"); 1351 | return (NULL); 1352 | } 1353 | 1354 | p = imalloc(size); 1355 | 1356 | if (p != NULL) { 1357 | /* copy the lesser of the two sizes, and free the old one */ 1358 | /* Don't move from/to 0 sized region !!! */ 1359 | if (osize != 0 && size != 0) { 1360 | if (osize < size) 1361 | memcpy(p, ptr, osize); 1362 | else 1363 | memcpy(p, ptr, size); 1364 | } 1365 | ifree(ptr); 1366 | } 1367 | return (p); 1368 | } 1369 | 1370 | /* 1371 | * Free a sequence of pages 1372 | */ 1373 | static __inline__ void 1374 | free_pages(void *ptr, u_long index, struct pginfo * info) 1375 | { 1376 | u_long i, pidx, lidx; 1377 | size_t l, cachesize = 0; 1378 | struct pginfo **pd; 1379 | struct pdinfo *pi, *spi; 1380 | struct pgfree *pf, *pt = NULL; 1381 | caddr_t tail; 1382 | 1383 | if (info == MALLOC_FREE) { 1384 | wrtwarning("page is already free"); 1385 | return; 1386 | } 1387 | if (info != MALLOC_FIRST) { 1388 | wrtwarning("free_pages: pointer to wrong page"); 1389 | return; 1390 | } 1391 | if ((u_long) ptr & malloc_pagemask) { 1392 | wrtwarning("modified (page-) pointer"); 1393 | return; 1394 | } 1395 | /* Count how many pages and mark them free at the same time */ 1396 | pidx = PI_IDX(index); 1397 | pdir_lookup(index, &pi); 1398 | #ifdef MALLOC_EXTRA_SANITY 1399 | if (pi == NULL || PD_IDX(pi->dirnum) != pidx) { 1400 | wrterror("(ES): mapped pages not found in directory"); 1401 | errno = EFAULT; 1402 | return; 1403 | } 1404 | #endif /* MALLOC_EXTRA_SANITY */ 1405 | 1406 | spi = pi; /* Save page index for start of region. */ 1407 | 1408 | pd = pi->base; 1409 | pd[PI_OFF(index)] = MALLOC_FREE; 1410 | i = 1; 1411 | if (!PI_OFF(index + i)) { 1412 | pi = pi->next; 1413 | if (pi == NULL || PD_IDX(pi->dirnum) != PI_IDX(index + i)) 1414 | pi = NULL; 1415 | else 1416 | pd = pi->base; 1417 | } 1418 | while (pi != NULL && pd[PI_OFF(index + i)] == MALLOC_FOLLOW) { 1419 | pd[PI_OFF(index + i)] = MALLOC_FREE; 1420 | i++; 1421 | if (!PI_OFF(index + i)) { 1422 | if ((pi = pi->next) == NULL || 1423 | PD_IDX(pi->dirnum) != PI_IDX(index + i)) 1424 | pi = NULL; 1425 | else 1426 | pd = pi->base; 1427 | } 1428 | } 1429 | 1430 | l = i << malloc_pageshift; 1431 | 1432 | if (malloc_junk) 1433 | memset(ptr, SOME_JUNK, l); 1434 | 1435 | malloc_used -= l; 1436 | malloc_guarded -= malloc_guard; 1437 | if (malloc_guard) { 1438 | #ifdef MALLOC_EXTRA_SANITY 1439 | if (pi == NULL || PD_IDX(pi->dirnum) != PI_IDX(index + i)) { 1440 | wrterror("(ES): hole in mapped pages directory"); 1441 | errno = EFAULT; 1442 | return; 1443 | } 1444 | #endif /* MALLOC_EXTRA_SANITY */ 1445 | pd[PI_OFF(index + i)] = MALLOC_FREE; 1446 | l += malloc_guard; 1447 | } 1448 | tail = (caddr_t)ptr + l; 1449 | /* 1450 | if (malloc_hint) 1451 | madvise(ptr, l, MADV_FREE); 1452 | 1453 | if (malloc_freeprot) 1454 | mprotect(ptr, l, PROT_NONE); 1455 | */ 1456 | /* Add to free-list. */ 1457 | if (px == NULL && (px = malloc_bytes(sizeof *px)) == NULL) 1458 | goto not_return; 1459 | px->page = ptr; 1460 | px->pdir = spi; 1461 | px->size = l; 1462 | 1463 | if (free_list.next == NULL) { 1464 | /* Nothing on free list, put this at head. */ 1465 | px->next = NULL; 1466 | px->prev = &free_list; 1467 | free_list.next = px; 1468 | pf = px; 1469 | px = NULL; 1470 | } else { 1471 | /* 1472 | * Find the right spot, leave pf pointing to the modified 1473 | * entry. 1474 | */ 1475 | 1476 | /* Race ahead here, while calculating cache size. */ 1477 | for (pf = free_list.next; 1478 | (caddr_t)ptr > ((caddr_t)pf->page + pf->size) 1479 | && pf->next != NULL; 1480 | pf = pf->next) 1481 | cachesize += pf->size; 1482 | 1483 | /* Finish cache size calculation. */ 1484 | pt = pf; 1485 | while (pt) { 1486 | cachesize += pt->size; 1487 | pt = pt->next; 1488 | } 1489 | 1490 | if ((caddr_t)pf->page > tail) { 1491 | /* Insert before entry */ 1492 | px->next = pf; 1493 | px->prev = pf->prev; 1494 | pf->prev = px; 1495 | px->prev->next = px; 1496 | pf = px; 1497 | px = NULL; 1498 | } else if (((caddr_t)pf->page + pf->size) == ptr) { 1499 | /* Append to the previous entry. */ 1500 | cachesize -= pf->size; 1501 | pf->size += l; 1502 | if (pf->next != NULL && 1503 | pf->next->page == ((caddr_t)pf->page + pf->size)) { 1504 | /* And collapse the next too. */ 1505 | pt = pf->next; 1506 | pf->size += pt->size; 1507 | pf->next = pt->next; 1508 | if (pf->next != NULL) 1509 | pf->next->prev = pf; 1510 | } 1511 | } else if (pf->page == tail) { 1512 | /* Prepend to entry. */ 1513 | cachesize -= pf->size; 1514 | pf->size += l; 1515 | pf->page = ptr; 1516 | pf->pdir = spi; 1517 | } else if (pf->next == NULL) { 1518 | /* Append at tail of chain. */ 1519 | px->next = NULL; 1520 | px->prev = pf; 1521 | pf->next = px; 1522 | pf = px; 1523 | px = NULL; 1524 | } else { 1525 | wrterror("freelist is destroyed"); 1526 | errno = EFAULT; 1527 | return; 1528 | } 1529 | } 1530 | 1531 | if (pf->pdir != last_dir) { 1532 | prev_dir = last_dir; 1533 | last_dir = pf->pdir; 1534 | } 1535 | 1536 | /* Return something to OS ? */ 1537 | if (pf->size > (malloc_cache - cachesize)) { 1538 | 1539 | /* 1540 | * Keep the cache intact. Notice that the '>' above guarantees that 1541 | * the pf will always have at least one page afterwards. 1542 | */ 1543 | if (munmap((char *) pf->page + (malloc_cache - cachesize), 1544 | pf->size - (malloc_cache - cachesize)) != 0) 1545 | goto not_return; 1546 | tail = (caddr_t)pf->page + pf->size; 1547 | lidx = ptr2index(tail) - 1; 1548 | pf->size = malloc_cache - cachesize; 1549 | 1550 | index = ptr2index((caddr_t)pf->page + pf->size); 1551 | 1552 | pidx = PI_IDX(index); 1553 | if (prev_dir != NULL && PD_IDX(prev_dir->dirnum) >= pidx) 1554 | prev_dir = NULL; /* Will be wiped out below ! */ 1555 | 1556 | for (pi = pf->pdir; pi != NULL && PD_IDX(pi->dirnum) < pidx; 1557 | pi = pi->next) 1558 | ; 1559 | 1560 | spi = pi; 1561 | if (pi != NULL && PD_IDX(pi->dirnum) == pidx) { 1562 | pd = pi->base; 1563 | 1564 | for (i = index; i <= lidx;) { 1565 | if (pd[PI_OFF(i)] != MALLOC_NOT_MINE) { 1566 | pd[PI_OFF(i)] = MALLOC_NOT_MINE; 1567 | #ifdef MALLOC_EXTRA_SANITY 1568 | if (!PD_OFF(pi->dirnum)) { 1569 | wrterror("(ES): pages directory underflow"); 1570 | errno = EFAULT; 1571 | return; 1572 | } 1573 | #endif /* MALLOC_EXTRA_SANITY */ 1574 | pi->dirnum--; 1575 | } 1576 | #ifdef MALLOC_EXTRA_SANITY 1577 | else 1578 | wrtwarning("(ES): page already unmapped"); 1579 | #endif /* MALLOC_EXTRA_SANITY */ 1580 | i++; 1581 | if (!PI_OFF(i)) { 1582 | /* 1583 | * If no page in that dir, free 1584 | * directory page. 1585 | */ 1586 | if (!PD_OFF(pi->dirnum)) { 1587 | /* Remove from list. */ 1588 | if (spi == pi) 1589 | spi = pi->prev; 1590 | if (pi->prev != NULL) 1591 | pi->prev->next = pi->next; 1592 | if (pi->next != NULL) 1593 | pi->next->prev = pi->prev; 1594 | pi = pi->next; 1595 | munmap(pd, malloc_pagesize); 1596 | } else 1597 | pi = pi->next; 1598 | if (pi == NULL || 1599 | PD_IDX(pi->dirnum) != PI_IDX(i)) 1600 | break; 1601 | pd = pi->base; 1602 | } 1603 | } 1604 | if (pi && !PD_OFF(pi->dirnum)) { 1605 | /* Resulting page dir is now empty. */ 1606 | /* Remove from list. */ 1607 | if (spi == pi) /* Update spi only if first. */ 1608 | spi = pi->prev; 1609 | if (pi->prev != NULL) 1610 | pi->prev->next = pi->next; 1611 | if (pi->next != NULL) 1612 | pi->next->prev = pi->prev; 1613 | pi = pi->next; 1614 | munmap(pd, malloc_pagesize); 1615 | } 1616 | } 1617 | if (pi == NULL && malloc_brk == tail) { 1618 | /* Resize down the malloc upper boundary. */ 1619 | last_index = index - 1; 1620 | malloc_brk = index2ptr(index); 1621 | } 1622 | 1623 | /* XXX: We could realloc/shrink the pagedir here I guess. */ 1624 | if (pf->size == 0) { /* Remove from free-list as well. */ 1625 | if (px) 1626 | ifree(px); 1627 | if ((px = pf->prev) != &free_list) { 1628 | if (pi == NULL && last_index == (index - 1)) { 1629 | if (spi == NULL) { 1630 | malloc_brk = NULL; 1631 | i = 11; 1632 | } else { 1633 | pd = spi->base; 1634 | if (PD_IDX(spi->dirnum) < pidx) 1635 | index = 1636 | ((PD_IDX(spi->dirnum) + 1) * 1637 | pdi_mod) - 1; 1638 | for (pi = spi, i = index; 1639 | pd[PI_OFF(i)] == MALLOC_NOT_MINE; 1640 | i--) 1641 | #ifdef MALLOC_EXTRA_SANITY 1642 | if (!PI_OFF(i)) { 1643 | pi = pi->prev; 1644 | if (pi == NULL || i == 0) 1645 | break; 1646 | pd = pi->base; 1647 | i = (PD_IDX(pi->dirnum) + 1) * pdi_mod; 1648 | } 1649 | #else /* !MALLOC_EXTRA_SANITY */ 1650 | { 1651 | } 1652 | #endif /* MALLOC_EXTRA_SANITY */ 1653 | malloc_brk = index2ptr(i + 1); 1654 | } 1655 | last_index = i; 1656 | } 1657 | if ((px->next = pf->next) != NULL) 1658 | px->next->prev = px; 1659 | } else { 1660 | if ((free_list.next = pf->next) != NULL) 1661 | free_list.next->prev = &free_list; 1662 | } 1663 | px = pf; 1664 | last_dir = prev_dir; 1665 | prev_dir = NULL; 1666 | } 1667 | } 1668 | not_return: 1669 | if (pt != NULL) 1670 | ifree(pt); 1671 | } 1672 | 1673 | /* 1674 | * Free a chunk, and possibly the page it's on, if the page becomes empty. 1675 | */ 1676 | 1677 | /* ARGSUSED */ 1678 | static __inline__ void 1679 | free_bytes(void *ptr, u_long index __attribute__((unused)), 1680 | struct pginfo * info) 1681 | { 1682 | struct pginfo **mp, **pd; 1683 | struct pdinfo *pi; 1684 | #ifdef MALLOC_EXTRA_SANITY 1685 | u_long pidx; 1686 | #endif /* MALLOC_EXTRA_SANITY */ 1687 | void *vp; 1688 | long i; 1689 | 1690 | /* Find the chunk number on the page */ 1691 | i = ((u_long) ptr & malloc_pagemask) >> info->shift; 1692 | 1693 | if ((u_long) ptr & ((1UL << (info->shift)) - 1)) { 1694 | wrtwarning("modified (chunk-) pointer"); 1695 | return; 1696 | } 1697 | if (info->bits[i / MALLOC_BITS] & (1UL << (i % MALLOC_BITS))) { 1698 | wrtwarning("chunk is already free"); 1699 | return; 1700 | } 1701 | if (malloc_junk && info->size != 0) 1702 | memset(ptr, SOME_JUNK, (size_t)info->size); 1703 | 1704 | info->bits[i / MALLOC_BITS] |= 1UL << (i % MALLOC_BITS); 1705 | info->free++; 1706 | 1707 | if (info->size != 0) 1708 | mp = page_dir + info->shift; 1709 | else 1710 | mp = page_dir; 1711 | 1712 | if (info->free == 1) { 1713 | /* Page became non-full */ 1714 | 1715 | /* Insert in address order */ 1716 | while (*mp != NULL && (*mp)->next != NULL && 1717 | (*mp)->next->page < info->page) 1718 | mp = &(*mp)->next; 1719 | info->next = *mp; 1720 | *mp = info; 1721 | return; 1722 | } 1723 | if (info->free != info->total) 1724 | return; 1725 | 1726 | /* Find & remove this page in the queue */ 1727 | while (*mp != info) { 1728 | mp = &((*mp)->next); 1729 | #ifdef MALLOC_EXTRA_SANITY 1730 | if (!*mp) { 1731 | wrterror("(ES): Not on queue"); 1732 | errno = EFAULT; 1733 | return; 1734 | } 1735 | #endif /* MALLOC_EXTRA_SANITY */ 1736 | } 1737 | *mp = info->next; 1738 | 1739 | /* Free the page & the info structure if need be */ 1740 | pdir_lookup(ptr2index(info->page), &pi); 1741 | #ifdef MALLOC_EXTRA_SANITY 1742 | pidx = PI_IDX(ptr2index(info->page)); 1743 | if (pi == NULL || PD_IDX(pi->dirnum) != pidx) { 1744 | wrterror("(ES): mapped pages not found in directory"); 1745 | errno = EFAULT; 1746 | return; 1747 | } 1748 | #endif /* MALLOC_EXTRA_SANITY */ 1749 | if (pi != last_dir) { 1750 | prev_dir = last_dir; 1751 | last_dir = pi; 1752 | } 1753 | pd = pi->base; 1754 | pd[PI_OFF(ptr2index(info->page))] = MALLOC_FIRST; 1755 | 1756 | /* If the page was mprotected, unprotect it before releasing it */ 1757 | if (info->size == 0) 1758 | mprotect(info->page, malloc_pagesize, PROT_READ | PROT_WRITE); 1759 | 1760 | vp = info->page; /* Order is important ! */ 1761 | if (vp != (void *) info) 1762 | ifree(info); 1763 | ifree(vp); 1764 | } 1765 | 1766 | static void 1767 | ifree(void *ptr) 1768 | { 1769 | struct pginfo *info, **pd; 1770 | u_long index; 1771 | #ifdef MALLOC_EXTRA_SANITY 1772 | u_long pidx; 1773 | #endif /* MALLOC_EXTRA_SANITY */ 1774 | struct pdinfo *pi; 1775 | 1776 | if (!malloc_started) { 1777 | wrtwarning("malloc() has never been called"); 1778 | return; 1779 | } 1780 | /* If we're already sinking, don't make matters any worse. */ 1781 | if (suicide) 1782 | return; 1783 | 1784 | if (malloc_ptrguard && PTR_ALIGNED(ptr)) 1785 | ptr = (char *) ptr - PTR_GAP; 1786 | 1787 | index = ptr2index(ptr); 1788 | 1789 | if (index < malloc_pageshift) { 1790 | warnx("(%p)", ptr); 1791 | wrtwarning("ifree: junk pointer, too low to make sense"); 1792 | return; 1793 | } 1794 | if (index > last_index) { 1795 | warnx("(%p)", ptr); 1796 | wrtwarning("ifree: junk pointer, too high to make sense"); 1797 | return; 1798 | } 1799 | pdir_lookup(index, &pi); 1800 | #ifdef MALLOC_EXTRA_SANITY 1801 | pidx = PI_IDX(index); 1802 | if (pi == NULL || PD_IDX(pi->dirnum) != pidx) { 1803 | wrterror("(ES): mapped pages not found in directory"); 1804 | errno = EFAULT; 1805 | return; 1806 | } 1807 | #endif /* MALLOC_EXTRA_SANITY */ 1808 | if (pi != last_dir) { 1809 | prev_dir = last_dir; 1810 | last_dir = pi; 1811 | } 1812 | pd = pi->base; 1813 | info = pd[PI_OFF(index)]; 1814 | 1815 | if (info < MALLOC_MAGIC) 1816 | free_pages(ptr, index, info); 1817 | else 1818 | free_bytes(ptr, index, info); 1819 | 1820 | /* does not matter if malloc_bytes fails */ 1821 | if (px == NULL) 1822 | px = malloc_bytes(sizeof *px); 1823 | 1824 | return; 1825 | } 1826 | 1827 | /* 1828 | * Common function for handling recursion. Only 1829 | * print the error message once, to avoid making the problem 1830 | * potentially worse. 1831 | */ 1832 | static void 1833 | malloc_recurse(void) 1834 | { 1835 | static int noprint; 1836 | 1837 | if (noprint == 0) { 1838 | noprint = 1; 1839 | wrtwarning("recursive call"); 1840 | } 1841 | malloc_active--; 1842 | _MALLOC_UNLOCK(); 1843 | errno = EDEADLK; 1844 | } 1845 | 1846 | /* 1847 | * These are the public exported interface routines. 1848 | */ 1849 | void * 1850 | malloc(size_t size) 1851 | { 1852 | void *r; 1853 | 1854 | _MALLOC_LOCK(); 1855 | malloc_func = " in malloc():"; 1856 | if (malloc_active++) { 1857 | malloc_recurse(); 1858 | return (NULL); 1859 | } 1860 | r = imalloc(size); 1861 | UTRACE(0, size, r); 1862 | malloc_active--; 1863 | _MALLOC_UNLOCK(); 1864 | if (malloc_xmalloc && r == NULL) { 1865 | wrterror("out of memory"); 1866 | errno = ENOMEM; 1867 | } 1868 | return (r); 1869 | } 1870 | 1871 | void 1872 | free(void *ptr) 1873 | { 1874 | /* This is legal. XXX quick path */ 1875 | if (ptr == NULL) 1876 | return; 1877 | 1878 | _MALLOC_LOCK(); 1879 | malloc_func = " in free():"; 1880 | if (malloc_active++) { 1881 | malloc_recurse(); 1882 | return; 1883 | } 1884 | ifree(ptr); 1885 | UTRACE(ptr, 0, 0); 1886 | malloc_active--; 1887 | _MALLOC_UNLOCK(); 1888 | return; 1889 | } 1890 | 1891 | void * 1892 | realloc(void *ptr, size_t size) 1893 | { 1894 | void *r; 1895 | 1896 | _MALLOC_LOCK(); 1897 | malloc_func = " in realloc():"; 1898 | if (malloc_active++) { 1899 | malloc_recurse(); 1900 | return (NULL); 1901 | } 1902 | 1903 | if (ptr == NULL) 1904 | r = imalloc(size); 1905 | else 1906 | r = irealloc(ptr, size); 1907 | 1908 | UTRACE(ptr, size, r); 1909 | malloc_active--; 1910 | _MALLOC_UNLOCK(); 1911 | if (malloc_xmalloc && r == NULL) { 1912 | wrterror("out of memory"); 1913 | errno = ENOMEM; 1914 | } 1915 | return (r); 1916 | } 1917 | 1918 | void *calloc(size_t n, size_t size) 1919 | { 1920 | void *ptr = malloc(n * size); 1921 | bzero(ptr, n * size); 1922 | return ptr; 1923 | } 1924 | -------------------------------------------------------------------------------- /memchr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | void *memchr(const void *s, int c, size_t n) 11 | { 12 | size_t i; 13 | 14 | for(i = 0; i < n; i++) 15 | if(((unsigned char*)s)[i] == (unsigned char)c) 16 | return (void*)((unsigned long)s + i); 17 | 18 | return NULL; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /memcmp.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | 9 | int memcmp(const void *s1, const void *s2, size_t n) 10 | { 11 | unsigned char *str1 = (void*)s1; 12 | unsigned char *str2 = (void*)s2; 13 | size_t pos; 14 | 15 | for(pos = 0; pos < n; pos++) { 16 | if(str1[pos] < str2[pos]) 17 | return 1; 18 | if(str1[pos] > str2[pos]) 19 | return -1; 20 | } 21 | return 0; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /memcpy.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | 9 | void *memcpy(void *dest, const void *src, size_t count) 10 | { 11 | /* This would be a prime candidate for reimplementation in assembly */ 12 | char *in_src = (char*)src; 13 | char *in_dest = (char*)dest; 14 | 15 | while(count--) 16 | *in_dest++ = *in_src++; 17 | return dest; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /memmove.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | 9 | void *memmove(void *dest, const void *src, size_t n) 10 | { 11 | void *d0 = dest; 12 | char *d = (char *) dest; 13 | char *s = (char *) src; 14 | if (s < d) 15 | for (s += n, d += n; 0 != n; --n) 16 | *--d = *--s; 17 | else if (s != d) 18 | for (; 0 != n; --n) 19 | *d++ = *s++; 20 | return d0; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /memset.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | 9 | void *memset(void *s,int c, size_t count) 10 | { 11 | char *xs = (char *) s; 12 | 13 | while (count--) 14 | *xs++ = c; 15 | 16 | return s; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /mkdir.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | int mkdir(const char *path __attribute__((unused)), 12 | mode_t mode __attribute__((unused))) 13 | { 14 | printf("mkdir\n"); 15 | errno = ENOENT; 16 | return -1; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /mkfifo.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | int mkfifo(const char *pathname __attribute__ ((unused)), 12 | mode_t mode __attribute__ ((unused))) 13 | { 14 | printf("mkfifo\n"); 15 | errno = ENOENT; 16 | return -1; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /mkstemp.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | 9 | int mkstemp(char *template __attribute__ ((unused))) 10 | { 11 | return (-1); 12 | } 13 | -------------------------------------------------------------------------------- /mmap.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | void *mmap(void *start, size_t length, int prot, 12 | int flags __attribute__((unused)), 13 | int fd, off_t offset __attribute__((unused))) 14 | { 15 | if(fd != -1) 16 | return NULL; 17 | 18 | return runtime_alloc(start, length, prot); 19 | } 20 | -------------------------------------------------------------------------------- /mprotect.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | int mprotect(void *addr, size_t len, int prot) 11 | { 12 | return runtime_memprotect(addr, len, prot); 13 | } 14 | -------------------------------------------------------------------------------- /mremap.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | void *mremap(void *old_address, size_t old_size, size_t new_size, int flags) 11 | { 12 | return runtime_realloc(old_address, flags, old_size, new_size); 13 | } 14 | -------------------------------------------------------------------------------- /munmap.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | int munmap(void *start, size_t length) 11 | { 12 | runtime_free(start, length); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /nl_langinfo.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | 9 | char *nl_langinfo(nl_item item __attribute__((unused))) 10 | { 11 | return "UTF8"; 12 | } 13 | -------------------------------------------------------------------------------- /open.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | #ifdef URANDOM 12 | #include 13 | #include 14 | #define MUNUSED 15 | #else 16 | #define MUNUSED __attribute((unused)) 17 | #endif /* URANDOM */ 18 | 19 | int open(const char *pathname MUNUSED, 20 | int flags MUNUSED, 21 | ...) 22 | { 23 | printf("open\n"); 24 | 25 | #ifdef URANDOM 26 | if(strncmp(pathname, "/dev/urandom", 13) == 0 && (flags & ~O_NONBLOCK) == O_RDONLY) 27 | return urandom_open(); 28 | #endif 29 | 30 | errno = EACCES; 31 | return -1; 32 | } 33 | -------------------------------------------------------------------------------- /pabort.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | void pabort(const char *format, ...) 12 | { 13 | va_list args; 14 | 15 | va_start(args, format); 16 | printf("ABORT: "); 17 | vfprintf(stderr, format, args); 18 | va_end(args); 19 | runtime_exit(); 20 | } 21 | -------------------------------------------------------------------------------- /perror.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | void perror(const char *s) 12 | { 13 | printf("%s: %s\n", s, strerror(errno)); 14 | } 15 | -------------------------------------------------------------------------------- /pipe.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | int pipe(int fildes[2] __attribute__((unused))) 12 | { 13 | printf("pipe\n"); 14 | errno = EMFILE; 15 | return -1; 16 | } 17 | -------------------------------------------------------------------------------- /poll.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | int poll(struct pollfd *fds __attribute__((unused)), 12 | nfds_t nfds, 13 | int timeout) 14 | { 15 | printf("poll called with %ld fds and a timeout of %d\n", nfds, timeout); 16 | switch(timeout) { 17 | case -1: 18 | printf(" ... which will never wake up, so I'm giving up.\n"); 19 | abort(); 20 | case 0: 21 | return 0; 22 | default: 23 | runtime_block(timeout); 24 | return 0; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /printf.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | int printf(const char *format, ...) 11 | { 12 | va_list args; 13 | int res; 14 | 15 | va_start(args, format); 16 | res = vfprintf(stdout, format, args); 17 | va_end(args); 18 | return res; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /putchar.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | int putchar(int c) 12 | { 13 | runtime_write(1, (char*)&c); 14 | return c; 15 | } 16 | -------------------------------------------------------------------------------- /putenv.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | int putenv(char *str) 12 | { 13 | printf("WARNING: putenv('%s')\n", str); 14 | return ENOMEM; 15 | } 16 | -------------------------------------------------------------------------------- /puts.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | int puts(const char *s) 13 | { 14 | int len = strlen(s); 15 | runtime_write(len, (char*)s); 16 | runtime_write(1,"\n"); 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /raise.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | int raise(int sig) 12 | { 13 | printf("Someone called raise! (signal: %d)\n", sig); 14 | return -ENOSYS; 15 | } 16 | -------------------------------------------------------------------------------- /rand.c: -------------------------------------------------------------------------------- 1 | // This file is from the musl project: 2 | // http://musl-libc.org 3 | // License: MIT (Please refer to the original project for details) 4 | 5 | #include 6 | #include 7 | 8 | static uint64_t seed; 9 | 10 | void srand(unsigned s) 11 | { 12 | seed = s-1; 13 | } 14 | 15 | int rand(void) 16 | { 17 | seed = 6364136223846793005ULL*seed + 1; 18 | return seed>>33; 19 | } 20 | -------------------------------------------------------------------------------- /read.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | #ifdef URANDOM 12 | #include 13 | #define MUNUSED 14 | #else 15 | #define MUNUSED __attribute((unused)) 16 | #endif /* URANDOM */ 17 | 18 | ssize_t read(int fildes MUNUSED, 19 | void *buf MUNUSED, 20 | size_t nbyte MUNUSED) 21 | { 22 | printf("read\n"); 23 | 24 | #ifdef URANDOM 25 | if(fildes == URANDOM_FD) 26 | return urandom_read(buf, nbyte); 27 | #endif 28 | 29 | errno = EBADF; 30 | return -1; 31 | } 32 | 33 | -------------------------------------------------------------------------------- /select.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | int select(int nfds __attribute__((unused)), 13 | fd_set *reads __attribute__((unused)), 14 | fd_set *writes __attribute__((unused)), 15 | fd_set *excs __attribute__((unused)), 16 | struct timeval *timeout) 17 | { 18 | printf("select\n"); 19 | if(!timeout) { 20 | printf("select() called with endless wait. aborting.\n"); 21 | abort(); 22 | } 23 | 24 | runtime_block((timeout->tv_sec * 1000) + (timeout->tv_usec / 1000)); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /setmntent.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | FILE *setmntent(const char *filename __attribute__((unused)), 12 | const char *type __attribute__((unused))) 13 | { 14 | return NULL; 15 | } 16 | -------------------------------------------------------------------------------- /sigaction.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | int sigaction(int signum __attribute__((unused)), 11 | const struct sigaction *act __attribute__((unused)), 12 | struct sigaction *oldact __attribute__((unused))) 13 | { 14 | errno = ENOSYS; 15 | return -1; 16 | } 17 | -------------------------------------------------------------------------------- /sigaddset.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | int sigaddset(sigset_t *set __attribute__ ((unused)), 12 | int signum __attribute__ ((unused))) 13 | { 14 | printf("sigaddset\n"); 15 | errno = EINVAL; 16 | return -1; 17 | } 18 | -------------------------------------------------------------------------------- /sigemptyset.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | int sigemptyset(sigset_t *set __attribute__ ((unused))) 12 | { 13 | printf("sigemptyset\n"); 14 | errno = EINVAL; 15 | return -1; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /sigprocmask.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | int sigprocmask(int how __attribute__((unused)), 12 | const sigset_t *set __attribute__((unused)), 13 | sigset_t *oldset __attribute__((unused))) 14 | { 15 | printf("sigprocmask\n"); 16 | errno = EINVAL; 17 | return -1; 18 | } 19 | -------------------------------------------------------------------------------- /snprintf.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | int snprintf(char *buf, size_t size, const char *fmt, ...) 12 | { 13 | va_list args; 14 | int i; 15 | 16 | va_start(args, fmt); 17 | i = vsnprintf(buf, size, fmt, args); 18 | va_end(args); 19 | return i; 20 | } 21 | -------------------------------------------------------------------------------- /sprintf.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | int sprintf(char * buf, const char *fmt, ...) 12 | { 13 | va_list args; 14 | int i; 15 | 16 | va_start(args, fmt); 17 | i = vsnprintf(buf, 0xFFFFFFFFUL, fmt, args); 18 | va_end(args); 19 | return i; 20 | } 21 | -------------------------------------------------------------------------------- /stat.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | #ifdef URANDOM 11 | #include 12 | #include 13 | #define MUNUSED 14 | #else 15 | #define MUNUSED __attribute((unused)) 16 | #endif /* URANDOM */ 17 | 18 | int stat(const char *path MUNUSED, 19 | struct stat *buf MUNUSED) 20 | { 21 | #ifdef URANDOM 22 | if(strncmp(path, "/dev/urandom", 13) == 0) 23 | return urandom_stat(buf, 0); 24 | #endif 25 | 26 | errno = EACCES; 27 | return -1; 28 | } 29 | -------------------------------------------------------------------------------- /statfs.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | int statfs(const char *path __attribute__ ((unused)), 12 | struct statfs *buf __attribute__ ((unused))) 13 | { 14 | printf("statfs\n"); 15 | errno = ENOSYS; 16 | return (-1); 17 | } 18 | 19 | -------------------------------------------------------------------------------- /stdio_values.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | 9 | FILE *stdout = (FILE*)0x0badbead; 10 | FILE *stdin = (FILE*)0xdeadbead; 11 | FILE *stderr = (FILE*)0xeebadbad; 12 | -------------------------------------------------------------------------------- /strcasecmp.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | int strcasecmp(const char *s1, const char *s2) 11 | { 12 | int result; 13 | 14 | while (1) { 15 | result = tolower(*s1) - tolower(*s2); 16 | if (result != 0 || *s1 == '\0') 17 | break; 18 | 19 | ++s1; 20 | ++s2; 21 | } 22 | 23 | return result; 24 | } 25 | -------------------------------------------------------------------------------- /strchr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | char *strchr(const char *s, int c) 11 | { 12 | const char *cur; 13 | for (cur = s; *cur; cur++) { 14 | if (*cur == c) { 15 | return ((char*)cur); 16 | } 17 | } 18 | 19 | return NULL; 20 | } 21 | -------------------------------------------------------------------------------- /strcmp.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | 9 | int strcmp(const char *cs, const char *ct) 10 | { 11 | register signed char __res; 12 | 13 | while (1) { 14 | if ((__res = *cs - *ct++) != 0 || !*cs++) 15 | break; 16 | } 17 | return __res; 18 | } 19 | -------------------------------------------------------------------------------- /strcpy.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | char *strcpy(char *dest, const char *src) 11 | { 12 | char *retval = dest; 13 | // FIXME: Make higher-speed version? 14 | while(*src) *dest++ = *src++; 15 | return retval; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /strdup.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | char *strdup(const char *s) 11 | { 12 | size_t bufsize = strlen(s) + 1; 13 | char *retval = malloc(bufsize); 14 | if(retval) memcpy(retval, s, bufsize); 15 | return retval; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /strerror.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | char *strerror(int errnum) 12 | { 13 | static char buf[1024]; 14 | switch(errnum) { 15 | case EBADF: return "Bad file.\n"; 16 | case EACCES: return "Access prohibited.\n"; 17 | } 18 | sprintf(buf, "Unkown error: %d", errnum); 19 | return buf; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /strlen.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | 9 | size_t strlen(const char *s) 10 | { 11 | const char *sc; 12 | 13 | for (sc = s; *sc != '\0'; ++sc) 14 | /* nothing */; 15 | return sc - s; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /strncmp.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | 9 | int strncmp(const char *str1, const char *str2, size_t count) 10 | { 11 | register signed char __res = 0; 12 | 13 | while(count--) { 14 | if ((__res = *str1 - *str2) != 0 || !*str1++ || !*str2++) 15 | break; 16 | } 17 | return __res; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /strncpy.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | 9 | char *strncpy(char *dest, const char *src, size_t count) 10 | { 11 | char *tmp = dest; 12 | while (count-- && (*dest++ = *src++) != '\0') 13 | /* nothing */; 14 | return tmp; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /strnlen.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | 9 | size_t strnlen(const char *s, size_t count) 10 | { 11 | const char *sc; 12 | 13 | for (sc = s; count-- && *sc != '\0'; ++sc) 14 | /* nothing */; 15 | return sc - s; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /strrchr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | char *strrchr(const char *s, int c) 11 | { 12 | char *retval = NULL; 13 | const char *cur; 14 | for(cur = s; *cur; cur++) 15 | if(*cur == c) 16 | retval = (char*)cur; 17 | return retval; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /strstr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | char *strstr(const char *str1, const char *str2) 11 | { 12 | size_t len_str2 = strlen(str2); 13 | char *cur; 14 | 15 | for (cur = (char*)str1; cur != NULL; cur = strchr(cur, *str2)) { 16 | if (!strncmp(cur, str2, len_str2)) { 17 | break; 18 | } 19 | cur++; 20 | } 21 | 22 | return cur; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /strtol.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define isdigit(c) (c >= '0' && c <= '9') 7 | 8 | long int strtol(const char *nptr, char **endptr, int base) 9 | { 10 | /* Taken from the NetBSD libc implementation. As per the license, here's 11 | * the copyright notice & etc. attached to it: 12 | * 13 | * Copyright (c) 1990, 1993 14 | * The Regents of the University of California. All rights reserved. 15 | * Redistribution and use in source and binary forms, with or without 16 | * modification, are permitted provided that the following conditions 17 | * are met: 18 | * 1. Redistributions of source code must retain the above copyright 19 | * notice, this list of conditions and the following disclaimer. 20 | * 2. Redistributions in binary form must reproduce the above copyright 21 | * notice, this list of conditions and the following disclaimer in the 22 | * documentation and/or other materials provided with the distribution. 23 | * 3. Neither the name of the University nor the names of its contributors 24 | * may be used to endorse or promote products derived from this software 25 | * without specific prior written permission. 26 | * 27 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 | * SUCH DAMAGE. 37 | */ 38 | #define isalpha(c) (((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'))) 39 | #define isupper(c) ((c >= 'A') && (c <= 'Z')) 40 | #define isspace(c) ((c == ' ') || (c == '\n') || (c == '\t')) 41 | const char *s; 42 | long acc, cutoff; 43 | int c; 44 | int neg, any, cutlim; 45 | 46 | assert(nptr != NULL); 47 | /* endptr may be NULL */ 48 | 49 | /* 50 | * Skip white space and pick up leading +/- sign if any. 51 | * If base is 0, allow 0x for hex and 0 for octal, else 52 | * assume decimal; if base is already 16, allow 0x. 53 | */ 54 | s = nptr; 55 | do { 56 | c = (unsigned char) *s++; 57 | } while (isspace(c)); 58 | if (c == '-') { 59 | neg = 1; 60 | c = *s++; 61 | } else { 62 | neg = 0; 63 | if (c == '+') 64 | c = *s++; 65 | } 66 | if ((base == 0 || base == 16) && c == '0' && (*s == 'x' || *s == 'X')) { 67 | c = s[1]; 68 | s += 2; 69 | base = 16; 70 | } 71 | if (base == 0) 72 | base = c == '0' ? 8 : 10; 73 | 74 | /* 75 | * Compute the cutoff value between legal numbers and illegal 76 | * numbers. That is the largest legal value, divided by the 77 | * base. An input number that is greater than this value, if 78 | * followed by a legal input character, is too big. One that 79 | * is equal to this value may be valid or not; the limit 80 | * between valid and invalid numbers is then based on the last 81 | * digit. For instance, if the range for longs is 82 | * [-2147483648..2147483647] and the input base is 10, 83 | * cutoff will be set to 214748364 and cutlim to either 84 | * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated 85 | * a value > 214748364, or equal but the next digit is > 7 (or 8), 86 | * the number is too big, and we will return a range error. 87 | * 88 | * Set any if any `digits' consumed; make it negative to indicate 89 | * overflow. 90 | */ 91 | cutoff = neg ? LONG_MIN : LONG_MAX; 92 | cutlim = (int)(cutoff % base); 93 | cutoff /= base; 94 | if (neg) { 95 | if (cutlim > 0) { 96 | cutlim -= base; 97 | cutoff += 1; 98 | } 99 | cutlim = -cutlim; 100 | } 101 | for (acc = 0, any = 0;; c = (unsigned char) *s++) { 102 | if (isdigit(c)) 103 | c -= '0'; 104 | else if (isalpha(c)) 105 | c -= isupper(c) ? 'A' - 10 : 'a' - 10; 106 | else 107 | break; 108 | if (c >= base) 109 | break; 110 | if (any < 0) 111 | continue; 112 | if (neg) { 113 | if (acc < cutoff || (acc == cutoff && c > cutlim)) { 114 | any = -1; 115 | acc = LONG_MIN; 116 | errno = ERANGE; 117 | } else { 118 | any = 1; 119 | acc *= base; 120 | acc -= c; 121 | } 122 | } else { 123 | if (acc > cutoff || (acc == cutoff && c > cutlim)) { 124 | any = -1; 125 | acc = LONG_MAX; 126 | errno = ERANGE; 127 | } else { 128 | any = 1; 129 | acc *= base; 130 | acc += c; 131 | } 132 | } 133 | } 134 | if (endptr != 0) 135 | *endptr = (char*)(any ? s - 1 : nptr); 136 | return (acc); 137 | } 138 | 139 | unsigned long long strtoull (const char*__restrict nptr, char **__restrict endptr, int base) { 140 | return (unsigned long long) strtol(nptr, endptr, base); 141 | // HACK: Hope it won't break things severely before I change to musl 142 | } 143 | -------------------------------------------------------------------------------- /sysconf.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | long sysconf(int name) 12 | { 13 | switch(name) { 14 | case _SC_PAGESIZE: 15 | return 4096; 16 | case _SC_CLK_TCK: 17 | printf("i was called.\n"); 18 | return 100; /* FIXME: number of clock ticks per second */ 19 | } 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /tcgetattr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | int tcgetattr(int fd __attribute__((unused)), 12 | struct termios *termios_p __attribute__((unused))) 13 | { 14 | printf("tcgetattr\n"); 15 | errno = ENOSYS; 16 | return -1; 17 | } 18 | -------------------------------------------------------------------------------- /tcsetattr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | int tcsetattr(int fd __attribute__((unused)), 12 | int optional_actions __attribute__((unused)), 13 | const struct termios *termios_p __attribute__((unused))) 14 | { 15 | printf("tcsetattr\n"); 16 | errno = ENOSYS; 17 | return -1; 18 | } 19 | -------------------------------------------------------------------------------- /termios.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | 9 | static void *saved_termios[3] = { NULL, NULL, NULL }; 10 | 11 | void *__hscore_get_saved_termios(int); 12 | void __hscore_set_saved_termios(int fd, void *ts); 13 | 14 | void *__hscore_get_saved_termios(int fd) 15 | { 16 | if(0 <= fd && fd < 3) 17 | return saved_termios[fd]; 18 | else 19 | return NULL; 20 | } 21 | 22 | void __hscore_set_saved_termios(int fd, void *ts) 23 | { 24 | if(0 <= fd && fd < 3) 25 | saved_termios[fd] = ts; 26 | } 27 | -------------------------------------------------------------------------------- /time.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | 10 | time_t time(time_t *t) 11 | { 12 | time_t retval = runtime_time(); 13 | if(t) *t = retval; 14 | return retval; 15 | } 16 | -------------------------------------------------------------------------------- /tolower.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | 9 | int tolower(int c) 10 | { 11 | if (c >= 'A' && c <= 'Z') 12 | return c - 'A'; 13 | else 14 | return c; 15 | } 16 | -------------------------------------------------------------------------------- /toupper.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | 9 | int toupper(int c) 10 | { 11 | if (c >= 'a' && c <= 'z') 12 | return c - ('a' - 'A'); 13 | else 14 | return c; 15 | } 16 | -------------------------------------------------------------------------------- /tzset.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | 9 | void tzset(void) 10 | { 11 | /* nothing */ 12 | } 13 | -------------------------------------------------------------------------------- /umask.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | mode_t umask(mode_t cmask) 12 | { 13 | static mode_t mask = 0; 14 | int retval = mask; 15 | printf("umask\n"); 16 | mask = cmask; 17 | return retval; 18 | } 19 | -------------------------------------------------------------------------------- /unlink.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | int unlink(const char *pathname __attribute__ ((unused))) 12 | { 13 | printf("unlink\n"); 14 | errno = ENOENT; 15 | return (-1); 16 | } 17 | -------------------------------------------------------------------------------- /unsetenv.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | 9 | int unsetenv(const char *n __attribute__((unused))) 10 | { 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /utime.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | int utime(const char *path __attribute__((unused)), 12 | const struct utimbuf *times __attribute__((unused))) 13 | { 14 | printf("utime\n"); 15 | errno = ENOENT; 16 | return -1; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /vfprintf.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | int vfprintf(FILE *stream, const char *format, va_list ap) 13 | { 14 | static char buf[4096]; 15 | 16 | if((stream == stdin) || (stream == stdout) || (stream == stderr)) { 17 | int r = vsnprintf(buf, sizeof(buf), format, ap); 18 | runtime_write(r, buf); 19 | return r; 20 | } 21 | 22 | #ifdef PROFILING 23 | if(stream) { 24 | int r = vsnprintf(buf, sizeof(buf), format, ap); 25 | profile_write(stream, buf, r); 26 | return r; 27 | } 28 | #endif 29 | 30 | return -1; 31 | } 32 | -------------------------------------------------------------------------------- /vprintf.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | 9 | int vprintf(const char *format, va_list ap) 10 | { 11 | return vfprintf(stdout, format, ap); 12 | } 13 | -------------------------------------------------------------------------------- /vsnprintf.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #define isdigit(c) ((c) >= '0' && (c) <= '9') 13 | #define isxdigit(c) (isdigit(c) || \ 14 | (((c) >= 'a') && ((c) <= 'f')) || \ 15 | (((c) >= 'A') && ((c) <= 'F'))) 16 | #define islower(c) (((c) >= 'a') && ((c) <= 'z')) 17 | #define toupper(c) (islower(c) ? ((c) - ('a' - 'A')) : (c)) 18 | 19 | static int skip_atoi(const char **s) 20 | { 21 | int i=0; 22 | 23 | while (isdigit(**s)) 24 | i = i*10 + *((*s)++) - '0'; 25 | return i; 26 | } 27 | 28 | #define ZEROPAD 1 /* pad with zero */ 29 | #define SIGN 2 /* unsigned/signed long */ 30 | #define PLUS 4 /* show plus */ 31 | #define SPACE 8 /* space if plus */ 32 | #define LEFT 16 /* left justified */ 33 | #define SPECIAL 32 /* 0x */ 34 | #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */ 35 | 36 | static char * number(char * buf, char * end, long long num, int base, int size, int precision, int type) 37 | { 38 | char c,sign,tmp[66]; 39 | const char *digits; 40 | const char small_digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; 41 | const char large_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 42 | int i; 43 | 44 | digits = (type & LARGE) ? large_digits : small_digits; 45 | if (type & LEFT) 46 | type &= ~ZEROPAD; 47 | if (base < 2 || base > 36) 48 | return buf; 49 | c = (type & ZEROPAD) ? '0' : ' '; 50 | sign = 0; 51 | if (type & SIGN) { 52 | if (num < 0) { 53 | sign = '-'; 54 | num = -num; 55 | size--; 56 | } else if (type & PLUS) { 57 | sign = '+'; 58 | size--; 59 | } else if (type & SPACE) { 60 | sign = ' '; 61 | size--; 62 | } 63 | } 64 | if (type & SPECIAL) { 65 | if (base == 16) 66 | size -= 2; 67 | else if (base == 8) 68 | size--; 69 | } 70 | i = 0; 71 | if (num == 0) 72 | tmp[i++]='0'; 73 | else 74 | { 75 | /* XXX KAF: force unsigned mod and div. */ 76 | unsigned long long num2=(unsigned long long)num; 77 | unsigned int base2=(unsigned int)base; 78 | while (num2 != 0) { tmp[i++] = digits[num2%base2]; num2 /= base2; } 79 | } 80 | if (i > precision) 81 | precision = i; 82 | size -= precision; 83 | if (!(type&(ZEROPAD+LEFT))) { 84 | while(size-->0) { 85 | if (buf <= end) 86 | *buf = ' '; 87 | ++buf; 88 | } 89 | } 90 | if (sign) { 91 | if (buf <= end) 92 | *buf = sign; 93 | ++buf; 94 | } 95 | if (type & SPECIAL) { 96 | if (base==8) { 97 | if (buf <= end) 98 | *buf = '0'; 99 | ++buf; 100 | } else if (base==16) { 101 | if (buf <= end) 102 | *buf = '0'; 103 | ++buf; 104 | if (buf <= end) 105 | *buf = digits[33]; 106 | ++buf; 107 | } 108 | } 109 | if (!(type & LEFT)) { 110 | while (size-- > 0) { 111 | if (buf <= end) 112 | *buf = c; 113 | ++buf; 114 | } 115 | } 116 | while (i < precision--) { 117 | if (buf <= end) 118 | *buf = '0'; 119 | ++buf; 120 | } 121 | while (i-- > 0) { 122 | if (buf <= end) 123 | *buf = tmp[i]; 124 | ++buf; 125 | } 126 | while (size-- > 0) { 127 | if (buf <= end) 128 | *buf = ' '; 129 | ++buf; 130 | } 131 | return buf; 132 | } 133 | 134 | 135 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) 136 | { 137 | // Written by Rolf 138 | int len; 139 | unsigned long long num; 140 | int i, base; 141 | char *str, *end, c; 142 | const char *s; 143 | double fnum; 144 | 145 | int flags; /* flags to number() */ 146 | 147 | int field_width; /* width of output field */ 148 | int precision; /* min. # of digits for integers; max 149 | number of chars for from string */ 150 | int qualifier; /* 'h', 'l', or 'L' for integer fields */ 151 | /* 'z' support added 23/7/1999 S.H. */ 152 | /* 'z' changed to 'Z' --davidm 1/25/99 */ 153 | 154 | str = buf; 155 | end = buf + size - 1; 156 | 157 | if (end < buf - 1) { 158 | end = ((void *) -1); 159 | size = end - buf + 1; 160 | } 161 | 162 | for (; *fmt ; ++fmt) { 163 | if (*fmt != '%') { 164 | if (str <= end) 165 | *str = *fmt; 166 | ++str; 167 | continue; 168 | } 169 | 170 | /* process flags */ 171 | flags = 0; 172 | repeat: 173 | ++fmt; /* this also skips first '%' */ 174 | switch (*fmt) { 175 | case '-': flags |= LEFT; goto repeat; 176 | case '+': flags |= PLUS; goto repeat; 177 | case ' ': flags |= SPACE; goto repeat; 178 | case '#': flags |= SPECIAL; goto repeat; 179 | case '0': flags |= ZEROPAD; goto repeat; 180 | } 181 | 182 | /* get field width */ 183 | field_width = -1; 184 | if (isdigit(*fmt)) 185 | field_width = skip_atoi(&fmt); 186 | else if (*fmt == '*') { 187 | ++fmt; 188 | /* it's the next argument */ 189 | field_width = va_arg(args, int); 190 | if (field_width < 0) { 191 | field_width = -field_width; 192 | flags |= LEFT; 193 | } 194 | } 195 | 196 | /* get the precision */ 197 | precision = -1; 198 | if (*fmt == '.') { 199 | ++fmt; 200 | if (isdigit(*fmt)) 201 | precision = skip_atoi(&fmt); 202 | else if (*fmt == '*') { 203 | ++fmt; 204 | /* it's the next argument */ 205 | precision = va_arg(args, int); 206 | } 207 | if (precision < 0) 208 | precision = 0; 209 | } 210 | 211 | /* get the conversion qualifier */ 212 | qualifier = -1; 213 | if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || *fmt =='Z' || *fmt =='z') { 214 | qualifier = *fmt; 215 | ++fmt; 216 | if (qualifier == 'l' && *fmt == 'l') { 217 | qualifier = 'L'; 218 | ++fmt; 219 | } 220 | } 221 | if (*fmt == 'q') { 222 | qualifier = 'L'; 223 | ++fmt; 224 | } 225 | 226 | /* default base */ 227 | base = 10; 228 | 229 | switch (*fmt) { 230 | case 'c': 231 | if (!(flags & LEFT)) { 232 | while (--field_width > 0) { 233 | if (str <= end) 234 | *str = ' '; 235 | ++str; 236 | } 237 | } 238 | c = (unsigned char) va_arg(args, int); 239 | if (str <= end) 240 | *str = c; 241 | ++str; 242 | while (--field_width > 0) { 243 | if (str <= end) 244 | *str = ' '; 245 | ++str; 246 | } 247 | continue; 248 | 249 | case 's': 250 | s = va_arg(args, char *); 251 | if (!s) 252 | s = ""; 253 | 254 | len = strnlen(s, precision); 255 | 256 | if (!(flags & LEFT)) { 257 | while (len < field_width--) { 258 | if (str <= end) 259 | *str = ' '; 260 | ++str; 261 | } 262 | } 263 | for (i = 0; i < len; ++i) { 264 | if (str <= end) 265 | *str = *s; 266 | ++str; ++s; 267 | } 268 | while (len < field_width--) { 269 | if (str <= end) 270 | *str = ' '; 271 | ++str; 272 | } 273 | continue; 274 | 275 | case 'p': 276 | if (field_width == -1) { 277 | field_width = 2*sizeof(void *); 278 | flags |= ZEROPAD; 279 | } 280 | str = number(str, end, 281 | (unsigned long) va_arg(args, void *), 282 | 16, field_width, precision, flags); 283 | continue; 284 | 285 | case 'n': 286 | /* FIXME: 287 | * What does C99 say about the overflow case here? */ 288 | if (qualifier == 'l') { 289 | long * ip = va_arg(args, long *); 290 | *ip = (str - buf); 291 | } else if (qualifier == 'Z') { 292 | size_t * ip = va_arg(args, size_t *); 293 | *ip = (str - buf); 294 | } else { 295 | int * ip = va_arg(args, int *); 296 | *ip = (str - buf); 297 | } 298 | continue; 299 | 300 | case '%': 301 | if (str <= end) 302 | *str = '%'; 303 | ++str; 304 | continue; 305 | 306 | case 'o': 307 | base = 8; 308 | break; 309 | 310 | case 'X': 311 | flags |= LARGE; 312 | case 'x': 313 | base = 16; 314 | break; 315 | 316 | case 'd': 317 | case 'i': 318 | flags |= SIGN; 319 | case 'u': 320 | break; 321 | 322 | case 'f': 323 | /* this is a pretty crappy way to do this */ 324 | fnum = va_arg(args, double); 325 | num = (unsigned long long)fnum; 326 | str = number(str, end, num, 10, field_width, -1, flags); 327 | if(str < end) { 328 | fnum = fnum - num; 329 | if(precision != 0) { 330 | *str++ = '.'; 331 | precision = (precision == -1) ? 10 : precision; 332 | while(precision != 0) { 333 | fnum *= 10; 334 | num = (unsigned long long)fnum; 335 | *str++ = '0' + num; 336 | fnum -= num; 337 | precision--; 338 | } 339 | } 340 | } 341 | continue; 342 | 343 | default: 344 | if (str <= end) 345 | *str = '%'; 346 | ++str; 347 | if (*fmt) { 348 | if (str <= end) 349 | *str = *fmt; 350 | ++str; 351 | } else { 352 | --fmt; 353 | } 354 | continue; 355 | } 356 | if (qualifier == 'L') 357 | num = va_arg(args, long long); 358 | else if (qualifier == 'l') { 359 | num = va_arg(args, unsigned long); 360 | if (flags & SIGN) 361 | num = (signed long) num; 362 | } else if (qualifier == 'Z') { 363 | num = va_arg(args, size_t); 364 | } else if (qualifier == 'h') { 365 | num = (unsigned short) va_arg(args, int); 366 | if (flags & SIGN) 367 | num = (signed short) num; 368 | } else { 369 | num = va_arg(args, unsigned int); 370 | if (flags & SIGN) 371 | num = (signed int) num; 372 | } 373 | 374 | str = number(str, end, num, base, 375 | field_width, precision, flags); 376 | } 377 | if (str <= end) 378 | *str = '\0'; 379 | else if (size > 0) 380 | /* don't write out a null byte if the buf size is zero */ 381 | *end = '\0'; 382 | /* the trailing null byte doesn't count towards the total 383 | * ++str; 384 | */ 385 | return str-buf; 386 | } 387 | -------------------------------------------------------------------------------- /waitpid.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | pid_t waitpid(pid_t pid __attribute__((unused)), 12 | int *stat_loc __attribute__((unused)), 13 | int options __attribute__((unused))) 14 | { 15 | printf("waitpid\n"); 16 | errno = ECHILD; 17 | return -1; 18 | } 19 | -------------------------------------------------------------------------------- /write.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014, Galois, Inc. 3 | * This sotware is distributed under a standard, three-clause BSD license. 4 | * Please see the file LICENSE, distributed with this software, for specific 5 | * terms and conditions. 6 | */ 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | ssize_t write(int filedes, const void *buf, size_t nbyte) 13 | { 14 | printf("write\n"); 15 | if(filedes == 0 || filedes > 2) { 16 | errno = EBADF; 17 | return -1; 18 | } 19 | runtime_write(nbyte, (char*)buf); 20 | return nbyte; 21 | } 22 | 23 | --------------------------------------------------------------------------------