Skip to content

Commit 357342a

Browse files
committed
Patch musl for hyperlight
Signed-off-by: Ludvig Liljenberg <[email protected]>
1 parent 053e1ec commit 357342a

File tree

36 files changed

+101
-353
lines changed

36 files changed

+101
-353
lines changed

src/hyperlight_guest/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ log = { version = "0.4", default-features = false }
2929
[build-dependencies]
3030
cc = "1.2"
3131
cfg-if = "1.0"
32+
glob = "0.3.1"

src/hyperlight_guest/build.rs

Lines changed: 18 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -52,59 +52,12 @@ fn cargo_main() {
5252
}
5353

5454
if cfg!(feature = "libc") {
55-
cfg.file("third_party/libc/musl/src/ctype/isalpha.c")
56-
.file("third_party/libc/musl/src/ctype/isalnum.c")
57-
.file("third_party/libc/musl/src/ctype/isdigit.c")
58-
.file("third_party/libc/musl/src/ctype/isgraph.c")
59-
.file("third_party/libc/musl/src/ctype/islower.c")
60-
.file("third_party/libc/musl/src/ctype/isprint.c")
61-
.file("third_party/libc/musl/src/ctype/isspace.c")
62-
.file("third_party/libc/musl/src/ctype/isupper.c")
63-
.file("third_party/libc/musl/src/ctype/isxdigit.c")
64-
.file("third_party/libc/musl/src/ctype/tolower.c")
65-
.file("third_party/libc/musl/src/ctype/toupper.c")
66-
.file("third_party/libc/musl/src/errno/__errno_location.c")
67-
.file("third_party/libc/musl/src/internal/floatscan.c")
68-
.file("third_party/libc/musl/src/internal/intscan.c")
69-
.file("third_party/libc/musl/src/internal/shgetc.c")
70-
.file("third_party/libc/musl/src/math/copysign.c")
71-
.file("third_party/libc/musl/src/math/copysignl.c")
72-
.file("third_party/libc/musl/src/math/fabs.c")
73-
.file("third_party/libc/musl/src/math/fabsl.c")
74-
.file("third_party/libc/musl/src/math/fmod.c")
75-
.file("third_party/libc/musl/src/math/fmodl.c")
76-
.file("third_party/libc/musl/src/math/scalbnl.c")
77-
.file("third_party/libc/musl/src/math/__signbit.c")
78-
.file("third_party/libc/musl/src/math/__signbitl.c")
79-
.file("third_party/libc/musl/src/math/__fpclassify.c")
80-
.file("third_party/libc/musl/src/math/__fpclassifyl.c")
81-
.file("third_party/libc/musl/src/stdio/__toread.c")
82-
.file("third_party/libc/musl/src/stdio/__uflow.c")
83-
.file("third_party/libc/musl/src/stdlib/atoi.c")
84-
.file("third_party/libc/musl/src/stdlib/strtod.c")
85-
.file("third_party/libc/musl/src/stdlib/strtol.c")
86-
.file("third_party/libc/musl/src/stdlib/qsort.c")
87-
.file("third_party/libc/musl/src/stdlib/qsort_nr.c")
88-
.file("third_party/libc/musl/src/stdlib/bsearch.c")
89-
.file("third_party/libc/musl/src/string/memchr.c")
90-
.file("third_party/libc/musl/src/string/memcmp.c")
91-
.file("third_party/libc/musl/src/string/memcpy.c")
92-
.file("third_party/libc/musl/src/string/memmove.c")
93-
.file("third_party/libc/musl/src/string/memset.c")
94-
.file("third_party/libc/musl/src/string/stpncpy.c")
95-
.file("third_party/libc/musl/src/string/strchr.c")
96-
.file("third_party/libc/musl/src/string/strchrnul.c")
97-
.file("third_party/libc/musl/src/string/strcmp.c")
98-
.file("third_party/libc/musl/src/string/strcspn.c")
99-
.file("third_party/libc/musl/src/string/strlen.c")
100-
.file("third_party/libc/musl/src/string/strncasecmp.c")
101-
.file("third_party/libc/musl/src/string/strncat.c")
102-
.file("third_party/libc/musl/src/string/strncmp.c")
103-
.file("third_party/libc/musl/src/string/strncpy.c")
104-
.file("third_party/libc/musl/src/string/strspn.c")
105-
.file("third_party/libc/musl/src/string/strstr.c")
106-
.file("third_party/libc/musl/src/prng/rand.c")
107-
.include("third_party/libc/musl/src/include")
55+
let entries = glob::glob("third_party/libc/musl/**/*.[cs]") // .c and .s files
56+
.expect("glob pattern should be valid")
57+
.filter_map(Result::ok);
58+
cfg.files(entries);
59+
60+
cfg.include("third_party/libc/musl/src/include")
10861
.include("third_party/libc/musl/include")
10962
.include("third_party/libc/musl/src/internal")
11063
.include("third_party/libc/musl/arch/generic")
@@ -124,24 +77,22 @@ fn cargo_main() {
12477
feature = "libc",
12578
feature = "alloca"
12679
)) {
127-
if is_pe {
128-
cfg.define("hidden", "");
129-
cfg.define("weak_alias(old, new) ", " ");
130-
cfg.define("__DEFINED_va_list", None);
131-
cfg.define("__DEFINED___isoc_va_list", None);
132-
}
133-
cfg.define("__x86_64__", None);
134-
cfg.define("__LITTLE_ENDIAN__", None);
80+
cfg.define("HYPERLIGHT", None); // used in certain musl files for conditional compilation
13581

13682
// silence compiler warnings
137-
cfg.flag("-Wno-sign-compare");
138-
cfg.flag("-Wno-bitwise-op-parentheses");
139-
cfg.flag("-Wno-unknown-pragmas");
140-
cfg.flag("-Wno-shift-op-parentheses");
141-
cfg.flag("-Wno-logical-op-parentheses");
142-
cfg.flag("-Wno-unused-but-set-variable");
83+
cfg.flag("-Wno-unused-command-line-argument") // including .s files makes clang believe arguments are unused
84+
.flag("-Wno-sign-compare")
85+
.flag("-Wno-bitwise-op-parentheses")
86+
.flag("-Wno-unknown-pragmas")
87+
.flag("-Wno-shift-op-parentheses")
88+
.flag("-Wno-logical-op-parentheses")
89+
.flag("-Wno-unused-but-set-variable")
90+
.flag("-Wno-unused-parameter")
91+
.flag("-Wno-string-plus-int");
14392

14493
if is_pe {
94+
cfg.flag("-Wno-unused-label");
95+
cfg.flag("-Wno-unused-variable");
14596
cfg.compiler("clang-cl");
14697
} else {
14798
cfg.flag("-fPIC");

src/hyperlight_guest/third_party/libc/musl/arch/x86_64/bits/alltypes.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515

1616
#ifndef __cplusplus
1717
#if defined(__NEED_wchar_t) && !defined(__DEFINED_wchar_t)
18+
#ifdef _MSC_VER
19+
typedef unsigned short wchar_t;
20+
#else
1821
typedef int wchar_t;
22+
#endif
1923
#define __DEFINED_wchar_t
2024
#endif
2125

src/hyperlight_guest/third_party/libc/musl/arch/x86_64/bits/float.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@
44
#define FLT_EVAL_METHOD 0
55
#endif
66

7+
#ifdef _MSC_VER
8+
#define LDBL_TRUE_MIN 4.9406564584124654e-324
9+
#define LDBL_MIN 2.2250738585072014e-308
10+
#define LDBL_MAX 1.7976931348623158e+308
11+
#define LDBL_EPSILON 2.2204460492503131e-016
12+
13+
#define LDBL_MANT_DIG 53
14+
#define LDBL_MIN_EXP (-1021)
15+
#define LDBL_MAX_EXP 1024
16+
17+
#define LDBL_DIG 15
18+
#define LDBL_MIN_10_EXP (-307)
19+
#define LDBL_MAX_10_EXP308
20+
21+
#define DECIMAL_DIG 17
22+
#else
723
#define LDBL_TRUE_MIN 3.6451995318824746025e-4951L
824
#define LDBL_MIN 3.3621031431120935063e-4932L
925
#define LDBL_MAX 1.1897314953572317650e+4932L
@@ -18,3 +34,4 @@
1834
#define LDBL_MAX_10_EXP 4932
1935

2036
#define DECIMAL_DIG 21
37+
#endif

src/hyperlight_guest/third_party/libc/musl/include/math.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ extern "C" {
1111
#define __NEED_double_t
1212
#include <bits/alltypes.h>
1313

14-
#if 100*__GNUC__+__GNUC_MINOR__ >= 303
14+
#if 100*__GNUC__+__GNUC_MINOR__ >= 303 || defined (HYPERLIGHT)
1515
#define NAN __builtin_nanf("")
1616
#define INFINITY __builtin_inff()
1717
#else

src/hyperlight_guest/third_party/libc/musl/include/monetary.h

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/hyperlight_guest/third_party/libc/musl/include/stdio.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ typedef union _G_fpos64_t {
5959
double __align;
6060
} fpos_t;
6161

62+
#ifndef HYPERLIGHT
63+
6264
extern FILE *const stdin;
6365
extern FILE *const stdout;
6466
extern FILE *const stderr;
@@ -221,4 +223,6 @@ FILE *fopencookie(void *, const char *, cookie_io_functions_t);
221223
}
222224
#endif
223225

226+
#endif //HYPERLIGHT
227+
224228
#endif

src/hyperlight_guest/third_party/libc/musl/include/stdlib.h

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,19 @@ unsigned long strtoul (const char *__restrict, char **__restrict, int);
3434
long long strtoll (const char *__restrict, char **__restrict, int);
3535
unsigned long long strtoull (const char *__restrict, char **__restrict, int);
3636

37-
int rand (void);
38-
void srand (unsigned);
39-
37+
// These 5 functions are implemented in rust in hyperlight_guest
38+
_Noreturn void abort (void);
4039
void *malloc (size_t);
4140
void *calloc (size_t, size_t);
4241
void *realloc (void *, size_t);
4342
void free (void *);
43+
44+
#ifndef HYPERLIGHT
45+
int rand (void);
46+
void srand (unsigned);
47+
4448
void *aligned_alloc(size_t, size_t);
4549

46-
_Noreturn void abort (void);
4750
int atexit (void (*) (void));
4851
_Noreturn void exit (int);
4952
_Noreturn void _Exit (int);
@@ -53,6 +56,7 @@ _Noreturn void quick_exit (int);
5356
char *getenv (const char *);
5457

5558
int system (const char *);
59+
#endif // HYPERLIGHT
5660

5761
void *bsearch (const void *, const void *, size_t, size_t, int (*)(const void *, const void *));
5862
void qsort (void *, size_t, size_t, int (*)(const void *, const void *));
@@ -84,9 +88,9 @@ size_t __ctype_get_mb_cur_max(void);
8488
#define RAND_MAX (0x7fffffff)
8589

8690

87-
#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
91+
#if (defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
8892
|| defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
89-
|| defined(_BSD_SOURCE)
93+
|| defined(_BSD_SOURCE)) && !defined(HYPERLIGHT)
9094

9195
#define WNOHANG 1
9296
#define WUNTRACED 2
@@ -110,8 +114,8 @@ int rand_r (unsigned *);
110114
#endif
111115

112116

113-
#if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
114-
|| defined(_BSD_SOURCE)
117+
#if (defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
118+
|| defined(_BSD_SOURCE)) && !defined(HYPERLIGHT)
115119
char *realpath (const char *__restrict, char *__restrict);
116120
long int random (void);
117121
void srandom (unsigned int);
@@ -136,7 +140,7 @@ unsigned short *seed48 (unsigned short [3]);
136140
void lcong48 (unsigned short [7]);
137141
#endif
138142

139-
#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
143+
#if (defined(_GNU_SOURCE) || defined(_BSD_SOURCE)) && !defined(HYPERLIGHT)
140144
#include <alloca.h>
141145
char *mktemp (char *);
142146
int mkstemps (char *, int);
@@ -163,7 +167,7 @@ double strtod_l(const char *__restrict, char **__restrict, struct __locale_struc
163167
long double strtold_l(const char *__restrict, char **__restrict, struct __locale_struct *);
164168
#endif
165169

166-
#if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE)
170+
#if (defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE)) && !defined(HYPERLIGHT)
167171
#define mkstemp64 mkstemp
168172
#define mkostemp64 mkostemp
169173
#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)

src/hyperlight_guest/third_party/libc/musl/include/sys/time.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,24 @@ struct itimerval {
1919
struct timeval it_value;
2020
};
2121

22-
int getitimer (int, struct itimerval *);
23-
int setitimer (int, const struct itimerval *__restrict, struct itimerval *__restrict);
24-
int utimes (const char *, const struct timeval [2]);
25-
2622
#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
2723
struct timezone {
2824
int tz_minuteswest;
2925
int tz_dsttime;
3026
};
27+
28+
#ifndef HYPERLIGHT
29+
int getitimer (int, struct itimerval *);
30+
int setitimer (int, const struct itimerval *__restrict, struct itimerval *__restrict);
31+
int utimes (const char *, const struct timeval [2]);
32+
3133
int futimes(int, const struct timeval [2]);
3234
int futimesat(int, const char *, const struct timeval [2]);
3335
int lutimes(const char *, const struct timeval [2]);
3436
int settimeofday(const struct timeval *, const struct timezone *);
3537
int adjtime (const struct timeval *, struct timeval *);
38+
#endif // HYPERLIGHT
39+
3640
#define timerisset(t) ((t)->tv_sec || (t)->tv_usec)
3741
#define timerclear(t) ((t)->tv_sec = (t)->tv_usec = 0)
3842
#define timercmp(s,t,op) ((s)->tv_sec == (t)->tv_sec ? \

src/hyperlight_guest/third_party/libc/musl/include/time.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ struct tm {
5151
const char *__tm_zone;
5252
};
5353

54+
#ifndef HYPERLIGHT
5455
clock_t clock (void);
5556
time_t time (time_t *);
5657
double difftime (time_t, time_t);
@@ -61,14 +62,15 @@ struct tm *localtime (const time_t *);
6162
char *asctime (const struct tm *);
6263
char *ctime (const time_t *);
6364
int timespec_get(struct timespec *, int);
65+
#endif // HYPERLIGHT
6466

6567
#define CLOCKS_PER_SEC 1000000L
6668

6769
#define TIME_UTC 1
6870

69-
#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
71+
#if (defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
7072
|| defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
71-
|| defined(_BSD_SOURCE)
73+
|| defined(_BSD_SOURCE)) && !defined(HYPERLIGHT)
7274

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

@@ -118,7 +120,7 @@ extern char *tzname[2];
118120
#endif
119121

120122

121-
#if defined(_XOPEN_SOURCE) || defined(_BSD_SOURCE) || defined(_GNU_SOURCE)
123+
#if (defined(_XOPEN_SOURCE) || defined(_BSD_SOURCE) || defined(_GNU_SOURCE)) && !defined(HYPERLIGHT)
122124
char *strptime (const char *__restrict, const char *__restrict, struct tm *__restrict);
123125
extern int daylight;
124126
extern long timezone;
@@ -127,7 +129,7 @@ struct tm *getdate (const char *);
127129
#endif
128130

129131

130-
#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
132+
#if (defined(_GNU_SOURCE) || defined(_BSD_SOURCE)) && !defined(HYPERLIGHT)
131133
int stime(const time_t *);
132134
time_t timegm(struct tm *);
133135
#endif

0 commit comments

Comments
 (0)