Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segfault in only Mercury after solutions in a particular code path #72

Open
jrfondren opened this issue Aug 25, 2019 · 3 comments
Open
Assignees

Comments

@jrfondren
Copy link

Sorry, although this seems to be a very consistent bug (crashes on Linux on 14.01, on DEV from last night, on a July 13 rotd, and on a rotd from a few days ago on OpenBSD), it's still very sensitive and my attempts to cut the code down have all suppressed the segfault.

Although there's some novel foreign_proc code in this project, none if it's reached before the segfault. There some semipure code involved in getting mutable variable data (that doesn't change for the lifetime of the program, so is promised to be pure).

Hopefully you can observe the segfault with the following commands:

$ git clone https://github.com/jrfondren/mmc-get.git
...
$ cd mmc-get
$ git checkout a2e15d837a2423172212703c204c38601dd9db29
...
HEAD is now at a2e15d8 get a segfaulting commit hash
$ mmc --make mmcget
...
$ ./mmcget update
...
$ ./mmcget get mmc-get

*** Mercury runtime: caught segmentation violation ***
cause: address not mapped to object
address involved: (nil)
This may have been caused by a stack overflow, due to unbounded recursion.
exiting from signal handler
Segmentation fault (core dumped)

You'll get a warning that's incidentally from the affected code (fixing it to avoid the warning doesn't fix the problem):

    else if
        Args = ["get", Want],
        solutions(want(Want), [R @ unreviewed(Package)])
    then
        write_package(R, !IO),  % segfaults
        ask_to_clone(R, !IO)
        %write_package(unreviewed(Package), !IO),
        %ask_to_clone(unreviewed(Package), !IO)

The non-segfaulting version, commented here, still sometimes segfaults. It's much less reliable than this version.

some relevant code:

% in mmcget.m
:- pred want(string::in, reviewed::out) is nondet.
want(Substr, R) :-
    ( packages(P), R = reviewed(P) ; unreviewed(P), R = unreviewed(P) ),
    sub_string_search(to_lower(P^name), to_lower(Substr), _).
...
% in manager.m
:- pred packages(package::out) is nondet.
:- pred unreviewed(package::out) is nondet.

:- mutable(reviewed, list(package), [], ground, [untrailed, attach_to_io_state]).
:- mutable(unreviewed, list(package), [], ground, [untrailed, attach_to_io_state]).
:- initialize init_packages/2.

:- pragma promise_pure packages/1.
packages(P) :-
    semipure get_reviewed(Ps),
    list.member(P, Ps).

:- pragma promise_pure unreviewed/1.
unreviewed(P) :-
    semipure get_unreviewed(Ps),
    list.member(P, Ps).

where init_packages supplies the mutable variables from the files you get with 'mmcget update'.

other 'get' commands work fine:

$ ./mmcget get getr
Cloning into 'getr'...
remote: Enumerating objects: 14, done.
remote: Counting objects: 100% (14/14), done.
remote: Compressing objects: 100% (10/10), done.
remote: Total 24 (delta 4), reused 12 (delta 4), pack-reused 10
Unpacking objects: 100% (24/24), done.

the distinction seems to be that mmc-get is the only target that's in unreviewed.list, which on a Linux system you'll find in $HOME/.config/mmc-get/ after doing the update. Copying targets that work into unreviewed and changing the name to make them distinctive results in new targets that segfault. If they're not distinctive, the same data can be worked with just fine:

$ tail -1 ~/.config/mmc-get/reviewed.list
package("PaulBone/protobuf-mercury", other, git, head, "https://github.com/PaulBone/protobuf-mercury.git", ["serializing", "wrapper", "nonauthor", "special_license"], [], [], [], [c], "Protocol buffers", "").
$ tail -1 ~/.config/mmc-get/reviewed.list >> ~/.config/mmc-get/unreviewed.list 
$ ./mmcget get protobuf
[1] 
Name             : PaulBone/protobuf-mercury
URL              : https://github.com/PaulBone/protobuf-mercury.git
Tags             : serializing wrapper nonauthor special_license
License          : other
Builds           : 
Dependencies     : []
Version Control  : git (releases: head)
FFI              : [c]
Description      : Protocol buffers
[2] 
Name (UNREVIEWED)? PaulBone/protobuf-mercury
URL              ? https://github.com/PaulBone/protobuf-mercury.git
Tags             ? serializing wrapper nonauthor special_license
License          ? other
Builds           ? 
Dependencies     ? []
Version Control  : git (releases: head)
FFI              ? [c]
Description      ? Protocol buffers

Ambiguous result. Your choice? [1..2, show, brief, q] 2

Name (UNREVIEWED)? PaulBone/protobuf-mercury
URL              ? https://github.com/PaulBone/protobuf-mercury.git
Tags             ? serializing wrapper nonauthor special_license
License          ? other
Builds           ? 
Dependencies     ? []
Version Control  : git (releases: head)
FFI              ? [c]
Description      ? Protocol buffers
Really get unreviewed package? [y/n] y
Cloning into 'PaulBone/protobuf-mercury'...
remote: Enumerating objects: 412, done.
remote: Total 412 (delta 0), reused 0 (delta 0), pack-reused 412
Receiving objects: 100% (412/412), 86.57 KiB | 428.00 KiB/s, done.
Resolving deltas: 100% (180/180), done.

$ tail -1 ~/.config/mmc-get/reviewed.list | perl -pe 's/PaulBone/xPaulBone/' >> ~/.config/mmc-get/unreviewed.list 
$ ./mmcget get xPaulBone/protobuf-mercury

*** Mercury runtime: caught segmentation violation ***
cause: address not mapped to object
address involved: (nil)
This may have been caused by a stack overflow, due to unbounded recursion.
exiting from signal handler
Segmentation fault (core dumped)
@wangp
Copy link
Member

wangp commented Aug 26, 2019

Attached a reduced test case (Github doesn't allow .m file extension).
bug72.m.txt

@zsomogyi
Copy link
Contributor

zsomogyi commented Aug 26, 2019 via email

@zsomogyi
Copy link
Contributor

I have diagnosed the actual problem in a post on mercury-developers.

@zsomogyi zsomogyi self-assigned this Jan 5, 2020
zsomogyi added a commit that referenced this issue Jan 12, 2021
This fix uses the approach discussed on m-dev 2020 nov 16/17 for fixing
github issue #72, whose core problem is a need for information flow
back to a the caller from a callee when the callee fills in the
argument of a function symbol whose representation is a direct_arg tag.
In most cases when the callee fills in the value of an argument,
the caller can see it because the argument is in a word on the heap,
but when the function symbol uses a direct_arg tag, that is not the case.

compiler/direct_arg_in_out.m:
    A new module that implements the transformation proposed on m-dev.
    It creates a fresh clone variable every time an argument of a direct_arg
    tag function symbol is (or may be) updated. This can happen several
    times if a type has more than one function symbol with a direct_arg tag.
    Since the affected variable can be bound to only one function symbol
    at the start, its argument can be filled in only once, but the
    compiler cannot know in advance what function symbol the variable
    contains, and therefore which of the possibly several fill-in sites
    (which fill in the arguments of different function symbols) executed
    in sequence will actually update the variable.

    The transformation ensures that once a variable is cloned, it is
    never referred to again. It also ensures that in a branched control
    structure (if-then-else, disjunction or switch), all branches will use
    the *same* variable to represent the latest version of each cloned
    variable at the end, so that following code has a consistent view
    regardless of through which branch execution has reached it.

    There are three situations that the transformation cannot and does not
    handle.

    1. Situations in which the mode of an argument is either an inst variable,
       or an abstract inst. In either case, the pass cannot know whether
       it should apply its transformation to the argument.

    2. Situations where a procedure that has such an argument is
       exported to C code as a function. In that case, the C signature
       of the function we would generate would be different from what
       the user would normally expect. We could modify the documentation
       of the export pragma, but I don't think there much point due to
       lack of demand. (The problem cannot arise when targeting any language
       other than C, because we use direct_arg tags only with the low level
       data representation, which we only use for C.)

    3. Situations where a procedure that has such an argument is defined
       by foreign_proc. Again, dealing with the problem would require
       nontrivial changes to the documented interface between code in
       foreign_procs and the surrounding Mercury code, and I see no demand
       for code that could benefit from that.

    In these cases, this module generates error messages.

compiler/transform_hlds.m:
    Include the new module in the transform_hlds package.

    Delete unnecessary module qualification on some existing inclusions.
    Put some existing inclusions into a more meaningful order.

compiler/notes/compiler_design.html:
    Document the new pass. Fix some nearby prose.

compiler/lambda.m:
compiler/simplify_proc.m:
    Use a predicate exported by direct_arg_in_out.m to test, for each
    procedure, whether the procedure has any argument positions that are
    subject to the problem that direct_arg_in_out.m addresses.
    simplify_proc.m does this for all procedures it processes;
    lambda.m does this for all the procedures it creates from
    lambda expressions.

    Give a predicate in simplify_proc.m a better name.

    Sort a list of predicate names.

compiler/hlds_module.m:
    Add a field to the module_info that simplify_proc.m and lambda.m
    can use to tell direct_arg_in_out.m what work (if any) it needs to do.

compiler/mercury_compile_middle_passes.m:
    Invoke direct_arg_in_out.m if the new field in the HLDS indicates
    that it has some work to do. (In the vast majority of compiler invocations,
    it won't have any.)

compiler/hlds_pred.m:
    The new code in direct_arg_in_out.m creates a clone of each procedure
    affected by the problem, before deleting the originals (to make sure that
    no references to the unfixed versions of now-fixed procedures remain.)
    Make it possible to create exact clones of both predicates and procedures
    by adding two pairs of predicates, {pred,proc}_prepare_to_clone and
    {pred,proc}_create.

    Add the direct_arg_in_out transformation as a possible source
    of transformed predicates.

library/private_builtin.m:
    Add a new builtin operation, partial_inst_copy, that the new module
    generates calls to.

configure.ac:
    Require the installed compiler to recognize partial_inst_copy
    as a no_type_info builtin.

compiler/builtin_ops.m:
    Recognize the new builtin. (This was committed before the rest; the diff
    to private_builtin.m can be done only once the change to builtin_ops.m
    is part of the installed compiler.)

compiler/options.m:
    Add a way to test whether the builtin_ops.m in the installed compiler
    recognizes the new builtin.

compiler/dead_proc_elim.m:
    Do not delete the new primitive before direct_arg_in_out.m has had
    a chance to generate calls to it.

    Add an XXX.

compiler/error_util.m:
    Recognize the new module as a source of error messages.

compiler/pred_table.m:
    Add a pair of utility predicates to be used when looking up
    builtin predicates, for which the compiler writer knows that
    there should be exactly one match. These are used in direct_arg_in_out.m.

compiler/simplify_goal_call.m:
    Replace some existing code with calls to the new predicates
    in pred_table.m.

compiler/hlds_goal.m:
    Add modes to rename_vars_in_goal_expr that express the fact
    that when an atomic goal_expr has some variables renamed inside it,
    it does not suddenly become some *other* kind of goal_expr.
    New code in direct_arg_in_out.m relies on this.

compiler/hlds_out_goal.m:
    When the HLDS we are dumping out is malformed because it contains
    calls to predicates that have been deleted, the compiler used to abort
    at such calls. (I ran into this while debugging direct_arg_in_out.m.)

    Fix this. When such calls are encountered, we now print out as much
    information we can about the call, and prefix the call with an
    unmistakable prefix to draw attention to the problem.

compiler/inst_util.m:
    Fix a bug that prevented direct_arg_in_out.m from even being invoked
    on some test code for it.

    The bug was in code that we use to unify a headvar's initial inst
    with its final inst. When the initial inst was a non-ground bound_inst
    such as the ones used in tests/hard_coded/gh72.m, and the final inst
    was simply "ground", this code quite properly returned a bound_inst
    (which, unlike ground, can show the exact set of function symbols
    that the headvar could be bound to). The problem was that it
    reused the original bound_inst's test results, including the one
    that said the final inst is NOT ground, which of course is wrong
    for any inst unified with ground. Fix two instances of this bug.

compiler/modes.m:
    Make some of the code I had to traverse to find the bug in inst_util.m
    easier to read and understand.

    Replace some uses of booleans with bespoke enum types.

    Change the argument lists of some predicates to put related arguments
    next to each other.

    Give some variables more descriptive names.

compiler/layout_out.m:
    Conform to the change in hlds_pred.m.

compiler/var_locn.m:
    Fix a code generation bug. When filling-in the value of the argument
    of a function symbol represented by a direct_arg tag, the code we
    generated for it worked only if the direct_arg tag used 0
    as its ptag value. In the test cases we initially used for
    github issue 72, that was the case, but the new tests/hard_coded/gh72.m
    has direct_tag args that use other ptag values as well.

    Document the reason why the updated code works.

compiler/term_constr_initial.m:
    Add the new primitive predicate added to private_builtin.m,
    partial_inst_copy, to a table of builtins that do not take type_infos,
    even though their signatures contain type variables.

    Fix a bunch of old bugs: most other such primitives were not listed
    either.

mdbcomp/program_representation.m:
    Add partial_inst_copy to the master list of builtins that do not take
    type_infos even though their signatures contain type variables. (Done
    by an earlier commit.)

    Document the fact that any updates here require updates to
    term_constr_initial.m.

library/multi_map.m:
    We have long had multi_map.add and multi_map.set as synonyms,
    but we only had multi_map.reverse_set. Add multi_map.reverse_add
    as a synonym for it.

    Define the "set" versions in terms of the "add" versions,
    instead of vice versa.

NEWS:
    Document the new predicates in multi_map.m.

tests/hard_coded/gh72a.m:
    Fix typo.

tests/hard_coded/gh72.{m,exp}:
    A new, much more comprehensive test case than gh72a.m.
    This one tries to tickle github issue 72 in as many forms of code
    as I can think of.

tests/invalid/gh72_errors.{m,err_exp}:
    A test case for testing the generation of error messages for
    two out of the three kinds of situations that direct_arg_in_out.m
    cannot handle. (Proposals for how to test the third category welcome.)

tests/hard_coded/Mmakefile:
tests/invalid/Mmakefile:
    Enable the two new test cases, as well as two old ones, gh72[ab].m,
    that previously we didn't pass.

tests/invalid/Mercury.option:
    Do not compile gh72_error.m with --errorcheck-only, since its errors
    are reported by a pass that --errorcheck-only does not invoke.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants