Skip to content

Commit 7af8e4b

Browse files
committed
Revert "[compiler-rt] Move IsStackOverflow from asan into sanitizer_common"
Windows is broken. This reverts commit r312951 llvm-svn: 312984
1 parent a6acd23 commit 7af8e4b

File tree

3 files changed

+44
-49
lines changed

3 files changed

+44
-49
lines changed

compiler-rt/lib/asan/asan_posix.cc

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,56 @@ namespace __asan {
3535

3636
void AsanOnDeadlySignal(int signo, void *siginfo, void *context) {
3737
ScopedDeadlySignal signal_scope(GetCurrentThread());
38+
int code = (int)((siginfo_t*)siginfo)->si_code;
3839
// Write the first message using fd=2, just in case.
3940
// It may actually fail to write in case stderr is closed.
4041
internal_write(2, SanitizerToolName, internal_strlen(SanitizerToolName));
4142
static const char kDeadlySignal[] = ":DEADLYSIGNAL\n";
4243
internal_write(2, kDeadlySignal, sizeof(kDeadlySignal) - 1);
4344
SignalContext sig = SignalContext::Create(siginfo, context);
44-
if (IsStackOverflow(((siginfo_t *)siginfo)->si_code, sig))
45+
46+
// Access at a reasonable offset above SP, or slightly below it (to account
47+
// for x86_64 or PowerPC redzone, ARM push of multiple registers, etc) is
48+
// probably a stack overflow.
49+
#ifdef __s390__
50+
// On s390, the fault address in siginfo points to start of the page, not
51+
// to the precise word that was accessed. Mask off the low bits of sp to
52+
// take it into account.
53+
bool IsStackAccess = sig.addr >= (sig.sp & ~0xFFF) &&
54+
sig.addr < sig.sp + 0xFFFF;
55+
#else
56+
bool IsStackAccess = sig.addr + 512 > sig.sp && sig.addr < sig.sp + 0xFFFF;
57+
#endif
58+
59+
#if __powerpc__
60+
// Large stack frames can be allocated with e.g.
61+
// lis r0,-10000
62+
// stdux r1,r1,r0 # store sp to [sp-10000] and update sp by -10000
63+
// If the store faults then sp will not have been updated, so test above
64+
// will not work, because the fault address will be more than just "slightly"
65+
// below sp.
66+
if (!IsStackAccess && IsAccessibleMemoryRange(sig.pc, 4)) {
67+
u32 inst = *(unsigned *)sig.pc;
68+
u32 ra = (inst >> 16) & 0x1F;
69+
u32 opcd = inst >> 26;
70+
u32 xo = (inst >> 1) & 0x3FF;
71+
// Check for store-with-update to sp. The instructions we accept are:
72+
// stbu rs,d(ra) stbux rs,ra,rb
73+
// sthu rs,d(ra) sthux rs,ra,rb
74+
// stwu rs,d(ra) stwux rs,ra,rb
75+
// stdu rs,ds(ra) stdux rs,ra,rb
76+
// where ra is r1 (the stack pointer).
77+
if (ra == 1 &&
78+
(opcd == 39 || opcd == 45 || opcd == 37 || opcd == 62 ||
79+
(opcd == 31 && (xo == 247 || xo == 439 || xo == 183 || xo == 181))))
80+
IsStackAccess = true;
81+
}
82+
#endif // __powerpc__
83+
84+
// We also check si_code to filter out SEGV caused by something else other
85+
// then hitting the guard page or unmapped memory, like, for example,
86+
// unaligned memory access.
87+
if (IsStackAccess && (code == si_SEGV_MAPERR || code == si_SEGV_ACCERR))
4588
ReportStackOverflow(sig);
4689
else
4790
ReportDeadlySignal(signo, sig);

compiler-rt/lib/sanitizer_common/sanitizer_common.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,6 @@ void SetSoftRssLimitExceededCallback(void (*Callback)(bool exceeded));
306306
// Functions related to signal handling.
307307
typedef void (*SignalHandlerType)(int, void *, void *);
308308
HandleSignalMode GetHandleSignalMode(int signum);
309-
bool IsStackOverflow(int code, const struct SignalContext &sig);
310309
void InstallDeadlySignalHandlers(SignalHandlerType handler);
311310
const char *DescribeSignalOrException(int signo);
312311
// Alternative signal stack (POSIX-only).

compiler-rt/lib/sanitizer_common/sanitizer_posix.cc

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -321,53 +321,6 @@ const char *DescribeSignalOrException(int signo) {
321321
return "UNKNOWN SIGNAL";
322322
}
323323

324-
#if !SANITIZER_GO
325-
bool IsStackOverflow(int code, const SignalContext &sig) {
326-
// Access at a reasonable offset above SP, or slightly below it (to account
327-
// for x86_64 or PowerPC redzone, ARM push of multiple registers, etc) is
328-
// probably a stack overflow.
329-
#ifdef __s390__
330-
// On s390, the fault address in siginfo points to start of the page, not
331-
// to the precise word that was accessed. Mask off the low bits of sp to
332-
// take it into account.
333-
bool IsStackAccess =
334-
sig.addr >= (sig.sp & ~0xFFF) && sig.addr < sig.sp + 0xFFFF;
335-
#else
336-
bool IsStackAccess = sig.addr + 512 > sig.sp && sig.addr < sig.sp + 0xFFFF;
337-
#endif
338-
339-
#if __powerpc__
340-
// Large stack frames can be allocated with e.g.
341-
// lis r0,-10000
342-
// stdux r1,r1,r0 # store sp to [sp-10000] and update sp by -10000
343-
// If the store faults then sp will not have been updated, so test above
344-
// will not work, because the fault address will be more than just "slightly"
345-
// below sp.
346-
if (!IsStackAccess && IsAccessibleMemoryRange(sig.pc, 4)) {
347-
u32 inst = *(unsigned *)sig.pc;
348-
u32 ra = (inst >> 16) & 0x1F;
349-
u32 opcd = inst >> 26;
350-
u32 xo = (inst >> 1) & 0x3FF;
351-
// Check for store-with-update to sp. The instructions we accept are:
352-
// stbu rs,d(ra) stbux rs,ra,rb
353-
// sthu rs,d(ra) sthux rs,ra,rb
354-
// stwu rs,d(ra) stwux rs,ra,rb
355-
// stdu rs,ds(ra) stdux rs,ra,rb
356-
// where ra is r1 (the stack pointer).
357-
if (ra == 1 &&
358-
(opcd == 39 || opcd == 45 || opcd == 37 || opcd == 62 ||
359-
(opcd == 31 && (xo == 247 || xo == 439 || xo == 183 || xo == 181))))
360-
IsStackAccess = true;
361-
}
362-
#endif // __powerpc__
363-
364-
// We also check si_code to filter out SEGV caused by something else other
365-
// then hitting the guard page or unmapped memory, like, for example,
366-
// unaligned memory access.
367-
return IsStackAccess && (code == si_SEGV_MAPERR || code == si_SEGV_ACCERR);
368-
}
369-
#endif //! SANITIZER_GO
370-
371324
} // namespace __sanitizer
372325

373326
#endif // SANITIZER_POSIX

0 commit comments

Comments
 (0)