Skip to content

Commit 737a9bf

Browse files
authored
etc.linux.memoryerror: Adapt to dynamic SIGSTKSZ since glibc v2.34 (#22473)
This fixes segfault-handler tests for LDC Linux CI, apparently caused by an insufficient alternate-stack size. Claude was able to diagnose the problem: ldc-developers/ldc#5041 (comment)
1 parent c75c2d2 commit 737a9bf

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

druntime/src/core/sys/posix/unistd.d

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,21 @@ version (CRuntime_Glibc)
671671
_SC_LEVEL4_CACHE_LINESIZE,
672672

673673
_SC_IPV6 = _SC_LEVEL1_ICACHE_SIZE + 50,
674-
_SC_RAW_SOCKETS
674+
_SC_RAW_SOCKETS,
675+
_SC_V7_ILP32_OFF32,
676+
_SC_V7_ILP32_OFFBIG,
677+
_SC_V7_LP64_OFF64,
678+
_SC_V7_LPBIG_OFFBIG,
679+
_SC_SS_REPL_MAX,
680+
_SC_TRACE_EVENT_NAME_MAX,
681+
_SC_TRACE_NAME_MAX,
682+
_SC_TRACE_SYS_MAX,
683+
_SC_TRACE_USER_EVENT_MAX,
684+
_SC_XOPEN_STREAMS,
685+
_SC_THREAD_ROBUST_PRIO_INHERIT,
686+
_SC_THREAD_ROBUST_PRIO_PROTECT,
687+
_SC_MINSIGSTKSZ,
688+
_SC_SIGSTKSZ,
675689
}
676690
}
677691
else version (Darwin)

druntime/src/etc/linux/memoryerror.d

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import ucontext = core.sys.posix.ucontext;
5050

5151
version (MemoryAssertSupported)
5252
{
53+
import core.stdc.stdlib : malloc;
5354
import core.sys.posix.signal : SA_ONSTACK, sigaltstack, SIGSTKSZ, stack_t;
5455
}
5556

@@ -423,7 +424,30 @@ version (MemoryAssertSupported)
423424

424425
// Set up alternate stack, because segfaults can be caused by stack overflow,
425426
// in which case the stack is already exhausted
426-
__gshared ubyte[SIGSTKSZ] altStack;
427+
428+
__gshared void[] altStack; // lazily allocated once only via malloc; never free'd
429+
if (altStack.ptr is null)
430+
{
431+
version (CRuntime_Glibc)
432+
{
433+
// glibc v2.34 switched to a dynamic SIGSTKSZ
434+
import core.sys.posix.unistd : sysconf, _SC_SIGSTKSZ;
435+
436+
auto size = sysconf(_SC_SIGSTKSZ);
437+
if (size <= 0)
438+
size = SIGSTKSZ;
439+
}
440+
else
441+
{
442+
const size = SIGSTKSZ;
443+
}
444+
445+
if (auto p = malloc(size))
446+
altStack = p[0 .. size];
447+
else
448+
return false;
449+
}
450+
427451
stack_t ss;
428452
ss.ss_sp = altStack.ptr;
429453
ss.ss_size = altStack.length;

0 commit comments

Comments
 (0)