Files
mercury/tests/hard_coded/thread_commit.m
Peter Wang 1b7e5adecf Don't use __builtin_setjmp, __builtin_longjmp on AArch64 with older GCCs.
There is an issue on Linux/aarch64 with older versions of gcc where a
commit performed on a thread using the gcc __builtin_longjmp function
causes an assertion failure in pthread_create() if the Mercury runtime
is dynamically linked:

    pthread_create.c:430: start_thread: Assertion `freesize < pd->stackblock_size' failed.

or a crash if the Mercury runtime is statically linked:

    *** Mercury runtime: caught segmentation violation ***
    cause: address not mapped to object

The gcc versions tested are:

    BAD - gcc version 6.3.0 20170516 (Debian 6.3.0-18) from Debian 9
    BAD - gcc version 8.3.0 (Debian 8.3.0-2) from Debian 10
    OK  - gcc version 10.2.1 20210110 (Debian 10.2.1-6) from Debian 11

on Debian with glibc.

runtime/mercury.h:
    Define MR_USE_GCC_BUILTIN_SETJMP_LONGJMP if we decide to use gcc's
    __builtin_setjmp and __builtin_longjmp functions instead of the
    standard functions.

    Use standard setjmp/longjmp on aarch64 if gcc version is < 10.

    Update the comment for MR_builtin_jmp_buf to refer to the GCC manual
    instead of the GCC source code. Use an array of 'intptr_t's instead
    of 'void *'s, to match the manual.

tests/hard_coded/Mmakefile
tests/hard_coded/thread_commit.m:
tests/hard_coded/thread_commit.exp:
tests/hard_coded/thread_commit.exp2:
    Add test case.
2022-05-26 12:47:21 +10:00

107 lines
2.9 KiB
Mathematica

%---------------------------------------------------------------------------%
% vim: ts=4 sw=4 et ft=mercury
%---------------------------------------------------------------------------%
%
% This program checks if performing a commit on a thread causes any issues.
%
% The .exp file is for grades supporting native threads.
% The .exp2 file is for grades not supporting native threads.
%
%---------------------------------------------------------------------------%
:- module thread_commit.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is cc_multi.
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
:- implementation.
:- import_module int.
:- import_module maybe.
:- import_module thread.
:- import_module thread.semaphore.
:- type tree
---> leaf(int)
; branch(tree, tree).
main(!IO) :-
( if thread.can_spawn_native then
semaphore.init(JoinSem, !IO),
io.write_string("Spawning thread...\n", !IO),
thread.spawn_native(thread_proc(JoinSem), SpawnRes, !IO),
(
SpawnRes = ok(_Thread),
semaphore.wait(JoinSem, !IO),
io.write_string("thread done.\n", !IO),
% Repeat test on main thread so program doesn't exit immediately.
io.write_string("Running test on main thread...\n", !IO),
tree_test(!IO),
io.write_string("done.\n", !IO)
;
SpawnRes = error(Error),
io.print_line(Error, !IO)
)
else
io.write_string("spawn_native not supported.\n", !IO)
).
:- pred thread_proc(semaphore::in, thread::in, io::di, io::uo) is cc_multi.
thread_proc(JoinSem, _Thread, !IO) :-
cc_multi_equal(!IO),
io.write_string("Running test on thread...\n", !IO),
tree_test(!IO),
semaphore.signal(JoinSem, !IO).
:- pred tree_test(io::di, io::uo) is det.
tree_test(!IO) :-
build_tree(10, Tree, 0, _Acc),
( if all_odd(Tree) then
io.print_line("All ints are odd.", !IO)
else
io.print_line("Not all ints are odd.", !IO)
).
:- pred build_tree(int::in, tree::out, int::in, int::out) is det.
build_tree(Height, Tree, !Acc) :-
( if Height =< 0 then
Tree = leaf(!.Acc),
!:Acc = !.Acc + 2
else
build_tree(Height - 1, TreeA, !Acc),
build_tree(Height - 2, TreeB, !Acc),
Tree = branch(TreeA, TreeB)
).
:- pred all_odd(tree::in) is semidet.
all_odd(Tree) :-
% This may perform a commit.
all [X] (
leaf(Leaf, Tree)
=>
not(int.even(Leaf))
).
:- pred leaf(int::out, tree::in) is nondet.
leaf(Leaf, Tree) :-
(
Tree = leaf(Leaf)
;
Tree = branch(TreeA, TreeB),
( leaf(Leaf, TreeA)
; leaf(Leaf, TreeB)
)
).
%---------------------------------------------------------------------------%