Skip to content

Commit 3caaadd

Browse files
avaginianlancetaylor
authored andcommitted
runtime: don't crash if vsyscall and vdso are disabled on x86_64
If vdso is disabled, the goruntime calls gettimeofday from vsyscall, but if vsyscall is disabled too, all golang binaries crash: SIGSEGV {si_signo=SIGSEGV, si_code=SEGV_MAPERR, si_addr=0xffffffffff600000} --- killed by SIGSEGV (core dumped) ++ vsyscall doesn't work as it was designed for a long time due to security reasons and now vsyscall is a little more expensive than real syscalls: torvalds/linux@5cec93c216db This patch reworks the code to call syscalls if the vdso library isn't available. Change-Id: I16cbf3f49871bea91e26af1f49aa0ae2fbd3a01d GitHub-Last-Rev: 1d133cd GitHub-Pull-Request: #41681 Reviewed-on: https://go-review.googlesource.com/c/go/+/257982 Run-TryBot: Tobias Klauser <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Michael Pratt <[email protected]> Trust: Michael Pratt <[email protected]>
1 parent 0e85fd7 commit 3caaadd

File tree

3 files changed

+22
-24
lines changed

3 files changed

+22
-24
lines changed

src/runtime/sys_linux_amd64.s

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#define SYS_futex 202
4141
#define SYS_sched_getaffinity 204
4242
#define SYS_epoll_create 213
43+
#define SYS_clock_gettime 228
4344
#define SYS_exit_group 231
4445
#define SYS_epoll_ctl 233
4546
#define SYS_tgkill 234
@@ -241,15 +242,15 @@ noswitch:
241242
SUBQ $16, SP // Space for results
242243
ANDQ $~15, SP // Align for C code
243244

245+
MOVL $0, DI // CLOCK_REALTIME
246+
LEAQ 0(SP), SI
244247
MOVQ runtime·vdsoClockgettimeSym(SB), AX
245248
CMPQ AX, $0
246249
JEQ fallback
247-
MOVL $0, DI // CLOCK_REALTIME
248-
LEAQ 0(SP), SI
249250
CALL AX
251+
ret:
250252
MOVQ 0(SP), AX // sec
251253
MOVQ 8(SP), DX // nsec
252-
ret:
253254
MOVQ R12, SP // Restore real SP
254255
// Restore vdsoPC, vdsoSP
255256
// We don't worry about being signaled between the two stores.
@@ -264,13 +265,8 @@ ret:
264265
MOVL DX, nsec+8(FP)
265266
RET
266267
fallback:
267-
LEAQ 0(SP), DI
268-
MOVQ $0, SI
269-
MOVQ runtime·vdsoGettimeofdaySym(SB), AX
270-
CALL AX
271-
MOVQ 0(SP), AX // sec
272-
MOVL 8(SP), DX // usec
273-
IMULQ $1000, DX
268+
MOVQ $SYS_clock_gettime, AX
269+
SYSCALL
274270
JMP ret
275271

276272
// func nanotime1() int64
@@ -306,15 +302,15 @@ noswitch:
306302
SUBQ $16, SP // Space for results
307303
ANDQ $~15, SP // Align for C code
308304

305+
MOVL $1, DI // CLOCK_MONOTONIC
306+
LEAQ 0(SP), SI
309307
MOVQ runtime·vdsoClockgettimeSym(SB), AX
310308
CMPQ AX, $0
311309
JEQ fallback
312-
MOVL $1, DI // CLOCK_MONOTONIC
313-
LEAQ 0(SP), SI
314310
CALL AX
311+
ret:
315312
MOVQ 0(SP), AX // sec
316313
MOVQ 8(SP), DX // nsec
317-
ret:
318314
MOVQ R12, SP // Restore real SP
319315
// Restore vdsoPC, vdsoSP
320316
// We don't worry about being signaled between the two stores.
@@ -332,13 +328,8 @@ ret:
332328
MOVQ AX, ret+0(FP)
333329
RET
334330
fallback:
335-
LEAQ 0(SP), DI
336-
MOVQ $0, SI
337-
MOVQ runtime·vdsoGettimeofdaySym(SB), AX
338-
CALL AX
339-
MOVQ 0(SP), AX // sec
340-
MOVL 8(SP), DX // usec
341-
IMULQ $1000, DX
331+
MOVQ $SYS_clock_gettime, AX
332+
SYSCALL
342333
JMP ret
343334

344335
TEXT runtime·rtsigprocmask(SB),NOSPLIT,$0-28

src/runtime/vdso_linux_amd64.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ var vdsoSymbolKeys = []vdsoSymbolKey{
1717
{"__vdso_clock_gettime", 0xd35ec75, 0x6e43a318, &vdsoClockgettimeSym},
1818
}
1919

20-
// initialize with vsyscall fallbacks
2120
var (
22-
vdsoGettimeofdaySym uintptr = 0xffffffffff600000
23-
vdsoClockgettimeSym uintptr = 0
21+
vdsoGettimeofdaySym uintptr
22+
vdsoClockgettimeSym uintptr
2423
)

src/syscall/asm_linux_amd64.s

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
// System calls for AMD64, Linux
1010
//
1111

12+
#define SYS_gettimeofday 96
13+
1214
// func Syscall(trap int64, a1, a2, a3 uintptr) (r1, r2, err uintptr);
1315
// Trap # in AX, args in DI SI DX R10 R8 R9, return in AX DX
1416
// Note that this differs from "standard" ABI convention, which
@@ -144,13 +146,19 @@ TEXT ·gettimeofday(SB),NOSPLIT,$0-16
144146
MOVQ tv+0(FP), DI
145147
MOVQ $0, SI
146148
MOVQ runtime·vdsoGettimeofdaySym(SB), AX
149+
TESTQ AX, AX
150+
JZ fallback
147151
CALL AX
148-
152+
ret:
149153
CMPQ AX, $0xfffffffffffff001
150154
JLS ok7
151155
NEGQ AX
152156
MOVQ AX, err+8(FP)
153157
RET
158+
fallback:
159+
MOVL $SYS_gettimeofday, AX
160+
SYSCALL
161+
JMP ret
154162
ok7:
155163
MOVQ $0, err+8(FP)
156164
RET

0 commit comments

Comments
 (0)