Skip to content

Commit b0c319c

Browse files
committed
Harden test and self-segfault causing native posix method
1 parent 1369e7d commit b0c319c

4 files changed

Lines changed: 76 additions & 4 deletions

File tree

graalpython/com.oracle.graal.python.test/src/tests/test_traceback.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ def test_faulthandler_sigsegv_builtin():
664664
import faulthandler
665665

666666
try:
667-
if not __graalpython__.is_native:
667+
if not __graalpython__.is_native or __graalpython__.posix_module_backend() == "java":
668668
return
669669
except NameError:
670670
pass # CPython

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NFIPosixSupport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1170,7 +1170,7 @@ public void kill(long pid, int signal,
11701170
public void signalSelf(int signal,
11711171
@Shared("invoke") @Cached InvokeNativeFunction invokeNode) throws PosixException {
11721172
if (!ImageInfo.inImageRuntimeCode()) {
1173-
throw new UnsupportedPosixFeatureException("faulthandler self-signals are only supported in native standalone");
1173+
throw new UnsupportedPosixFeatureException("self-signals are only supported in native standalone");
11741174
}
11751175
int res = invokeNode.callInt(this, PosixNativeFunction.signal_self, signal);
11761176
if (res == -1) {

graalpython/python-libposix/src/posix.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@
7777
#include <unistd.h>
7878
#include <pwd.h>
7979

80+
int32_t signal_self_segv(void);
81+
8082
#ifdef __APPLE__
8183
#include <util.h>
8284
#else
@@ -608,8 +610,7 @@ int32_t signal_self(int32_t signal) {
608610
abort();
609611
break;
610612
case SIGSEGV:
611-
*((volatile int *)0) = 1;
612-
break;
613+
return signal_self_segv();
613614
default:
614615
errno = EINVAL;
615616
return -1;
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright (c) 2020, 2026, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
42+
#include <stdint.h>
43+
44+
#if defined(_MSC_VER)
45+
#pragma optimize("", off)
46+
#define SIGNAL_SELF_SEGV_ATTR __declspec(noinline)
47+
#elif defined(__clang__)
48+
#pragma clang optimize off
49+
#define SIGNAL_SELF_SEGV_ATTR __attribute__((optnone, noinline))
50+
#elif defined(__GNUC__)
51+
#define SIGNAL_SELF_SEGV_ATTR __attribute__((optimize("O0"), noinline, noclone))
52+
#else
53+
#define SIGNAL_SELF_SEGV_ATTR
54+
#endif
55+
56+
/*
57+
* Keep the deliberate SIGSEGV in its own translation unit and discourage
58+
* inlining/optimization to minimize the optimizer's freedom around the
59+
* undefined null-dereference below.
60+
*/
61+
SIGNAL_SELF_SEGV_ATTR int32_t signal_self_segv(void) {
62+
volatile int *p = (volatile int *)(uintptr_t)0;
63+
*p = 1;
64+
return 0;
65+
}
66+
67+
#if defined(_MSC_VER)
68+
#pragma optimize("", on)
69+
#elif defined(__clang__)
70+
#pragma clang optimize on
71+
#endif

0 commit comments

Comments
 (0)