Skip to content

Commit

Permalink
Patch musl for Hyperlight
Browse files Browse the repository at this point in the history
Signed-off-by: Ludvig Liljenberg <[email protected]>
  • Loading branch information
ludfjig committed Dec 19, 2024
1 parent c604b99 commit 42ee008
Show file tree
Hide file tree
Showing 27 changed files with 77 additions and 50 deletions.
17 changes: 17 additions & 0 deletions src/hyperlight_guest/third_party/musl/arch/x86_64/bits/float.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@
#define FLT_EVAL_METHOD 0
#endif

#ifdef _MSC_VER
#define LDBL_TRUE_MIN 4.9406564584124654e-324
#define LDBL_MIN 2.2250738585072014e-308
#define LDBL_MAX 1.7976931348623158e+308
#define LDBL_EPSILON 2.2204460492503131e-016

#define LDBL_MANT_DIG 53
#define LDBL_MIN_EXP (-1021)
#define LDBL_MAX_EXP 1024

#define LDBL_DIG 15
#define LDBL_MIN_10_EXP (-307)
#define LDBL_MAX_10_EXP308

#define DECIMAL_DIG 17
#else
#define LDBL_TRUE_MIN 3.6451995318824746025e-4951L
#define LDBL_MIN 3.3621031431120935063e-4932L
#define LDBL_MAX 1.1897314953572317650e+4932L
Expand All @@ -18,3 +34,4 @@
#define LDBL_MAX_10_EXP 4932

#define DECIMAL_DIG 21
#endif
2 changes: 1 addition & 1 deletion src/hyperlight_guest/third_party/musl/include/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extern "C" {
#define __NEED_double_t
#include <bits/alltypes.h>

#if 100*__GNUC__+__GNUC_MINOR__ >= 303
#if 100*__GNUC__+__GNUC_MINOR__ >= 303 || defined (__clang__) && defined(_MSC_VER)
#define NAN __builtin_nanf("")
#define INFINITY __builtin_inff()
#else
Expand Down
4 changes: 4 additions & 0 deletions src/hyperlight_guest/third_party/musl/include/stdio.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ typedef union _G_fpos64_t {
double __align;
} fpos_t;

#ifndef HYPERLIGHT

extern FILE *const stdin;
extern FILE *const stdout;
extern FILE *const stderr;
Expand Down Expand Up @@ -221,4 +223,6 @@ FILE *fopencookie(void *, const char *, cookie_io_functions_t);
}
#endif

#endif //HYPERLIGHT

#endif
22 changes: 13 additions & 9 deletions src/hyperlight_guest/third_party/musl/include/stdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,19 @@ unsigned long strtoul (const char *__restrict, char **__restrict, int);
long long strtoll (const char *__restrict, char **__restrict, int);
unsigned long long strtoull (const char *__restrict, char **__restrict, int);

int rand (void);
void srand (unsigned);

// These 5 functions are implemented in rust in hyperlight_guest
_Noreturn void abort (void);
void *malloc (size_t);
void *calloc (size_t, size_t);
void *realloc (void *, size_t);
void free (void *);

#ifndef HYPERLIGHT
int rand (void);
void srand (unsigned);

void *aligned_alloc(size_t, size_t);

_Noreturn void abort (void);
int atexit (void (*) (void));
_Noreturn void exit (int);
_Noreturn void _Exit (int);
Expand All @@ -53,6 +56,7 @@ _Noreturn void quick_exit (int);
char *getenv (const char *);

int system (const char *);
#endif // HYPERLIGHT

void *bsearch (const void *, const void *, size_t, size_t, int (*)(const void *, const void *));
void qsort (void *, size_t, size_t, int (*)(const void *, const void *));
Expand Down Expand Up @@ -84,9 +88,9 @@ size_t __ctype_get_mb_cur_max(void);
#define RAND_MAX (0x7fffffff)


#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
#if (defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
|| defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
|| defined(_BSD_SOURCE)
|| defined(_BSD_SOURCE)) && !defined(HYPERLIGHT)

#define WNOHANG 1
#define WUNTRACED 2
Expand All @@ -110,8 +114,8 @@ int rand_r (unsigned *);
#endif


#if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
|| defined(_BSD_SOURCE)
#if (defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
|| defined(_BSD_SOURCE)) && !defined(HYPERLIGHT)
char *realpath (const char *__restrict, char *__restrict);
long int random (void);
void srandom (unsigned int);
Expand All @@ -136,8 +140,8 @@ unsigned short *seed48 (unsigned short [3]);
void lcong48 (unsigned short [7]);
#endif

#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
#include <alloca.h>
#if (defined(_GNU_SOURCE) || defined(_BSD_SOURCE)) && !defined(HYPERLIGHT)
char *mktemp (char *);
int mkstemps (char *, int);
int mkostemps (char *, int, int);
Expand Down
12 changes: 8 additions & 4 deletions src/hyperlight_guest/third_party/musl/include/sys/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,24 @@ struct itimerval {
struct timeval it_value;
};

int getitimer (int, struct itimerval *);
int setitimer (int, const struct itimerval *__restrict, struct itimerval *__restrict);
int utimes (const char *, const struct timeval [2]);

#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
struct timezone {
int tz_minuteswest;
int tz_dsttime;
};

#ifndef HYPERLIGHT
int getitimer (int, struct itimerval *);
int setitimer (int, const struct itimerval *__restrict, struct itimerval *__restrict);
int utimes (const char *, const struct timeval [2]);

int futimes(int, const struct timeval [2]);
int futimesat(int, const char *, const struct timeval [2]);
int lutimes(const char *, const struct timeval [2]);
int settimeofday(const struct timeval *, const struct timezone *);
int adjtime (const struct timeval *, struct timeval *);
#endif // HYPERLIGHT

#define timerisset(t) ((t)->tv_sec || (t)->tv_usec)
#define timerclear(t) ((t)->tv_sec = (t)->tv_usec = 0)
#define timercmp(s,t,op) ((s)->tv_sec == (t)->tv_sec ? \
Expand Down
10 changes: 6 additions & 4 deletions src/hyperlight_guest/third_party/musl/include/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ struct tm {
const char *__tm_zone;
};

#ifndef HYPERLIGHT
clock_t clock (void);
time_t time (time_t *);
double difftime (time_t, time_t);
Expand All @@ -61,14 +62,15 @@ struct tm *localtime (const time_t *);
char *asctime (const struct tm *);
char *ctime (const time_t *);
int timespec_get(struct timespec *, int);
#endif // HYPERLIGHT

#define CLOCKS_PER_SEC 1000000L

#define TIME_UTC 1

#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
#if (defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
|| defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
|| defined(_BSD_SOURCE)
|| defined(_BSD_SOURCE)) && !defined(HYPERLIGHT)

size_t strftime_l (char * __restrict, size_t, const char * __restrict, const struct tm * __restrict, locale_t);

Expand Down Expand Up @@ -118,7 +120,7 @@ extern char *tzname[2];
#endif


#if defined(_XOPEN_SOURCE) || defined(_BSD_SOURCE) || defined(_GNU_SOURCE)
#if (defined(_XOPEN_SOURCE) || defined(_BSD_SOURCE) || defined(_GNU_SOURCE)) && !defined(HYPERLIGHT)
char *strptime (const char *__restrict, const char *__restrict, struct tm *__restrict);
extern int daylight;
extern long timezone;
Expand All @@ -127,7 +129,7 @@ struct tm *getdate (const char *);
#endif


#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
#if (defined(_GNU_SOURCE) || defined(_BSD_SOURCE)) && !defined(HYPERLIGHT)
int stime(const time_t *);
time_t timegm(struct tm *);
#endif
Expand Down
8 changes: 6 additions & 2 deletions src/hyperlight_guest/third_party/musl/include/wchar.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ wchar_t *wcsncat (wchar_t *__restrict, const wchar_t *__restrict, size_t);
int wcscmp (const wchar_t *, const wchar_t *);
int wcsncmp (const wchar_t *, const wchar_t *, size_t);

#ifndef HYPERLIGHT
int wcscoll(const wchar_t *, const wchar_t *);
size_t wcsxfrm (wchar_t *__restrict, const wchar_t *__restrict, size_t);
#endif

wchar_t *wcschr (const wchar_t *, wchar_t);
wchar_t *wcsrchr (const wchar_t *, wchar_t);
Expand Down Expand Up @@ -104,7 +106,7 @@ long long wcstoll (const wchar_t *__restrict, wchar_t **__restrict, int);
unsigned long long wcstoull (const wchar_t *__restrict, wchar_t **__restrict, int);



#ifndef HYPERLIGHT
int fwide (FILE *, int);


Expand Down Expand Up @@ -136,9 +138,10 @@ wchar_t *fgetws (wchar_t *__restrict, int, FILE *__restrict);
int fputws (const wchar_t *__restrict, FILE *__restrict);

wint_t ungetwc (wint_t, FILE *);
size_t wcsftime (wchar_t *__restrict, size_t, const wchar_t *__restrict, const struct tm *__restrict);

#endif // HYPERLIGHT
struct tm;
size_t wcsftime (wchar_t *__restrict, size_t, const wchar_t *__restrict, const struct tm *__restrict);

#undef iswdigit

Expand All @@ -157,6 +160,7 @@ int fputws_unlocked (const wchar_t *__restrict, FILE *__restrict);
size_t wcsftime_l (wchar_t *__restrict, size_t, const wchar_t *__restrict, const struct tm *__restrict, locale_t);
#endif


#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
|| defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
FILE *open_wmemstream(wchar_t **, size_t *);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#include <errno.h>
#include "pthread_impl.h"

int errno_val;

int *__errno_location(void)
{
return &__pthread_self()->errno_val;
return &errno_val;
}

weak_alias(__errno_location, ___errno_location);
weak_alias(__errno_location, ___errno_location);
3 changes: 2 additions & 1 deletion src/hyperlight_guest/third_party/musl/src/exit/assert.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#include <stdio.h>
#include <stdlib.h>
#include "printf.h"

_Noreturn void __assert_fail(const char *expr, const char *file, int line, const char *func)
{
fprintf(stderr, "Assertion failed: %s (%s: %s: %d)\n", expr, file, func, line);
printf("Assertion failed: %s (%s: %s: %d)\n", expr, file, func, line);
abort();
}
8 changes: 0 additions & 8 deletions src/hyperlight_guest/third_party/musl/src/fenv/x86_64/fenv.s
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.global feclearexcept
.type feclearexcept,@function
feclearexcept:
# maintain exceptions in the sse mxcsr, clear x87 exceptions
mov %edi,%ecx
Expand All @@ -20,7 +19,6 @@ feclearexcept:
ret

.global feraiseexcept
.type feraiseexcept,@function
feraiseexcept:
and $0x3f,%edi
stmxcsr -8(%rsp)
Expand All @@ -30,8 +28,6 @@ feraiseexcept:
ret

.global __fesetround
.hidden __fesetround
.type __fesetround,@function
__fesetround:
push %rax
xor %eax,%eax
Expand All @@ -49,7 +45,6 @@ __fesetround:
ret

.global fegetround
.type fegetround,@function
fegetround:
push %rax
stmxcsr (%rsp)
Expand All @@ -59,15 +54,13 @@ fegetround:
ret

.global fegetenv
.type fegetenv,@function
fegetenv:
xor %eax,%eax
fnstenv (%rdi)
stmxcsr 28(%rdi)
ret

.global fesetenv
.type fesetenv,@function
fesetenv:
xor %eax,%eax
inc %rdi
Expand All @@ -86,7 +79,6 @@ fesetenv:
ret

.global fetestexcept
.type fetestexcept,@function
fetestexcept:
and $0x3f,%edi
push %rax
Expand Down
13 changes: 9 additions & 4 deletions src/hyperlight_guest/third_party/musl/src/internal/stdio_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
#define _STDIO_IMPL_H

#include <stdio.h>
#ifndef HYPERLIGHT
#include "syscall.h"

#define UNGET 8

#define FFINALLOCK(f) ((f)->lock>=0 ? __lockfile((f)) : 0)
#define FLOCK(f) int __need_unlock = ((f)->lock>=0 ? __lockfile((f)) : 0)
#define FUNLOCK(f) do { if (__need_unlock) __unlockfile((f)); } while (0)
#endif // HYPERLIGHT

#define F_PERM 1
#define F_NORD 4
Expand Down Expand Up @@ -47,6 +49,11 @@ struct _IO_FILE {
struct __locale_struct *locale;
};

hidden int __toread(FILE *);
hidden int __towrite(FILE *);
int __uflow(FILE *);

#ifndef HYPERLIGHT
extern hidden FILE *volatile __stdin_used;
extern hidden FILE *volatile __stdout_used;
extern hidden FILE *volatile __stderr_used;
Expand All @@ -60,16 +67,13 @@ hidden size_t __stdout_write(FILE *, const unsigned char *, size_t);
hidden off_t __stdio_seek(FILE *, off_t, int);
hidden int __stdio_close(FILE *);

hidden int __toread(FILE *);
hidden int __towrite(FILE *);

hidden void __stdio_exit(void);
hidden void __stdio_exit_needed(void);

#if defined(__PIC__) && (100*__GNUC__+__GNUC_MINOR__ >= 303)
__attribute__((visibility("protected")))
#endif
int __overflow(FILE *, int), __uflow(FILE *);
int __overflow(FILE *, int);

hidden int __fseeko(FILE *, off_t, int);
hidden int __fseeko_unlocked(FILE *, off_t, int);
Expand Down Expand Up @@ -110,3 +114,4 @@ hidden FILE *__fopen_rb_ca(const char *, FILE *, unsigned char *, size_t);
hidden int __fclose_ca(FILE *);

#endif
#endif
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# see ../i386/acos.s

.global acosl
.type acosl,@function
acosl:
fldt 8(%rsp)
1: fld %st(0)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.global asinl
.type asinl,@function
asinl:
fldt 8(%rsp)
1: fld %st(0)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.global atan2l
.type atan2l,@function
atan2l:
fldt 8(%rsp)
fldt 24(%rsp)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.global atanl
.type atanl,@function
atanl:
fldt 8(%rsp)
fld1
Expand Down
Loading

0 comments on commit 42ee008

Please sign in to comment.