Skip to content

Commit 6379184

Browse files
committed
[guest/setjmp] remove custom setjmp custom impl in favour of musl's
Our implementations of setjmp and longjmp conformed to the win64 ABI. With removal of PE support, we can leverage musl's implementation instead. Signed-off-by: danbugs <[email protected]>
1 parent d353fc7 commit 6379184

File tree

7 files changed

+89
-159
lines changed

7 files changed

+89
-159
lines changed

src/hyperlight_guest/build.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ fn cargo_main() {
121121
copy_includes(&include_dir, "third_party/printf/");
122122
}
123123
if cfg!(feature = "libc") {
124-
copy_includes(&include_dir, "include");
125124
copy_includes(&include_dir, "third_party/musl/include");
126125
copy_includes(&include_dir, "third_party/musl/arch/generic");
127126
copy_includes(&include_dir, "third_party/musl/arch/x86_64");

src/hyperlight_guest/include/setjmp.h

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

src/hyperlight_guest/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ pub(crate) mod guest_logger;
4242
pub mod memory;
4343
pub mod print;
4444
pub(crate) mod security_check;
45-
pub mod setjmp;
4645

4746
pub mod chkstk;
4847
pub mod error;

src/hyperlight_guest/src/setjmp.rs

Lines changed: 0 additions & 110 deletions
This file was deleted.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#ifndef _SETJMP_H
2+
#define _SETJMP_H
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
#include <features.h>
9+
10+
#include <bits/setjmp.h>
11+
12+
typedef struct __jmp_buf_tag {
13+
__jmp_buf __jb;
14+
unsigned long __fl;
15+
unsigned long __ss[128/sizeof(long)];
16+
} jmp_buf[1];
17+
18+
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)
19+
#define __setjmp_attr __attribute__((__returns_twice__))
20+
#else
21+
#define __setjmp_attr
22+
#endif
23+
24+
#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
25+
|| defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
26+
|| defined(_BSD_SOURCE)
27+
typedef jmp_buf sigjmp_buf;
28+
int sigsetjmp (sigjmp_buf, int) __setjmp_attr;
29+
_Noreturn void siglongjmp (sigjmp_buf, int);
30+
#endif
31+
32+
#if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
33+
|| defined(_BSD_SOURCE)
34+
int _setjmp (jmp_buf) __setjmp_attr;
35+
_Noreturn void _longjmp (jmp_buf, int);
36+
#endif
37+
38+
int setjmp (jmp_buf) __setjmp_attr;
39+
_Noreturn void longjmp (jmp_buf, int);
40+
41+
#define setjmp setjmp
42+
43+
#undef __setjmp_attr
44+
45+
#ifdef __cplusplus
46+
}
47+
#endif
48+
49+
#endif
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* Copyright 2011-2012 Nicholas J. Kain, licensed under standard MIT license */
2+
.global _longjmp
3+
.global longjmp
4+
.type _longjmp,@function
5+
.type longjmp,@function
6+
_longjmp:
7+
longjmp:
8+
xor %eax,%eax
9+
cmp $1,%esi /* CF = val ? 0 : 1 */
10+
adc %esi,%eax /* eax = val + !val */
11+
mov (%rdi),%rbx /* rdi is the jmp_buf, restore regs from it */
12+
mov 8(%rdi),%rbp
13+
mov 16(%rdi),%r12
14+
mov 24(%rdi),%r13
15+
mov 32(%rdi),%r14
16+
mov 40(%rdi),%r15
17+
mov 48(%rdi),%rsp
18+
jmp *56(%rdi) /* goto saved address without altering rsp */
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* Copyright 2011-2012 Nicholas J. Kain, licensed under standard MIT license */
2+
.global __setjmp
3+
.global _setjmp
4+
.global setjmp
5+
.type __setjmp,@function
6+
.type _setjmp,@function
7+
.type setjmp,@function
8+
__setjmp:
9+
_setjmp:
10+
setjmp:
11+
mov %rbx,(%rdi) /* rdi is jmp_buf, move registers onto it */
12+
mov %rbp,8(%rdi)
13+
mov %r12,16(%rdi)
14+
mov %r13,24(%rdi)
15+
mov %r14,32(%rdi)
16+
mov %r15,40(%rdi)
17+
lea 8(%rsp),%rdx /* this is our rsp WITHOUT current ret addr */
18+
mov %rdx,48(%rdi)
19+
mov (%rsp),%rdx /* save return addr ptr for new rip */
20+
mov %rdx,56(%rdi)
21+
xor %eax,%eax /* always return 0 */
22+
ret

0 commit comments

Comments
 (0)