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

segmentation fault when using tuple over member variable in more than one method #266

Closed
ghost opened this issue Jan 16, 2013 · 8 comments
Closed

Comments

@ghost
Copy link

ghost commented Jan 16, 2013

test.d:

template Tuple(Stuff ...) {
    alias Stuff Tuple;
}
struct S {
    int i;
    alias Tuple!i t;
    void a() {
        auto x = t;
    }
    void b() {
        auto x = t;
    }
}

ldc2 -c test.d:

0  ldc2            0x00000000011953ef
1  ldc2            0x0000000001195859
2  libpthread.so.0 0x00007f914eba5cb0
3  ldc2            0x00000000006cc443 DtoNestedVariable(Loc, Type*, VarDeclaration*, bool) + 147
4  ldc2            0x000000000069f43a ThisExp::toElem(IRState*) + 202
5  ldc2            0x000000000069f0e0 DotVarExp::toElem(IRState*) + 224
6  ldc2            0x000000000069cf6d AssignExp::toElem(IRState*) + 173
7  ldc2            0x00000000006c567f DtoVarDeclaration(VarDeclaration*) + 111
8  ldc2            0x00000000006c5b68 DtoDeclarationExp(Dsymbol*) + 312
9  ldc2            0x00000000006c5d57 DtoDeclarationExp(Dsymbol*) + 807
10 ldc2            0x00000000006c5a95 DtoDeclarationExp(Dsymbol*) + 101
11 ldc2            0x000000000069bd21 DeclarationExp::toElem(IRState*) + 65
12 ldc2            0x000000000069bc4d Expression::toElemDtor(IRState*) + 77
13 ldc2            0x000000000067c6aa ExpStatement::toIR(IRState*) + 74
14 ldc2            0x000000000067bfab CompoundStatement::toIR(IRState*) + 91
15 ldc2            0x0000000000664ad5 DtoDefineFunction(FuncDeclaration*) + 1797
16 ldc2            0x00000000006cd1e9 Ir::emitFunctionBodies() + 41
17 ldc2            0x0000000000668d1d Module::genLLVMModule(llvm::LLVMContext&, Ir*) + 669
18 ldc2            0x000000000059a577 main + 7623
19 libc.so.6       0x00007f914dddf76d __libc_start_main + 237
20 ldc2            0x00000000005ad0e9
Segmentation fault (core dumped)
@dnadlinger
Copy link
Member

Thanks for the solid bug report. Unfortunately, I won't be able to work on LDC during the next two weeks, but I'll try to have a look at it as soon as possible in February.

@smunix
Copy link

smunix commented Feb 8, 2013

Here is the whole stacktrace:

0 in getParentFunc of /home/smunix/programming/d-projects/ldc/gen/nested.cpp:30
1 in DtoNestedVariable of /home/smunix/programming/d-projects/ldc/gen/nested.cpp:86
2 in ThisExp::toElem of /home/smunix/programming/d-projects/ldc/gen/toir.cpp:1542
3 in DotVarExp::toElem of /home/smunix/programming/d-projects/ldc/gen/toir.cpp:1425
4 in AssignExp::toElem of /home/smunix/programming/d-projects/ldc/gen/toir.cpp:612
5 in DtoInitializer of /home/smunix/programming/d-projects/ldc/gen/llvmhelpers.cpp:1445
6 in DtoVarDeclaration of /home/smunix/programming/d-projects/ldc/gen/llvmhelpers.cpp:1146
7 in DtoDeclarationExp of /home/smunix/programming/d-projects/ldc/gen/llvmhelpers.cpp:1183
8 in DtoDeclarationExp of /home/smunix/programming/d-projects/ldc/gen/llvmhelpers.cpp:1258
9 in DtoDeclarationExp of /home/smunix/programming/d-projects/ldc/gen/llvmhelpers.cpp:1174
10 in DeclarationExp::toElem of /home/smunix/programming/d-projects/ldc/gen/toir.cpp:95
11 in Expression::toElemDtor of /home/smunix/programming/d-projects/ldc/gen/toir.cpp:72
12 in ExpStatement::toIR of /home/smunix/programming/d-projects/ldc/gen/statements.cpp:208
13 in CompoundStatement::toIR of /home/smunix/programming/d-projects/ldc/gen/statements.cpp:55
14 in DtoDefineFunction of /home/smunix/programming/d-projects/ldc/gen/functions.cpp:1079
15 in Ir::emitFunctionBodies of /home/smunix/programming/d-projects/ldc/ir/ir.cpp:62
16 in Module::genLLVMModule of /home/smunix/programming/d-projects/ldc/gen/module.cpp:301
17 in main of /home/smunix/programming/d-projects/ldc/driver/main.cpp:1100

@smunix
Copy link

smunix commented Feb 8, 2013

As we can see, in frame 1 the ThisExp ("this.i") is being evaluated from function is "void b()". "getParentFunc" tries its best to put a hand on "this" parent while checking that it's function "void b()", but this never happens. At some point before, in function "void a()", I suspect that the ThisExp was given "void a()" as its parent symbol, and that never changed since then.

(gdb) fr 12
#12 0x00000000008de3d0 in ExpStatement::toIR (this=0x1e185d0, p=0x7fffffffd240) at /home/smunix/programming/d-projects/ldc/gen/statements.cpp:208
208 e = exp->toElemDtor(p);
(gdb) p p->func()->decl->toChars()
$105 = 0x1e17e74 "b"

(gdb) fr 1
#1 0x00000000008be040 in DtoNestedVariable (loc=..., astype=0x1e16d80, vd=0x1eb61d0, byref=true) at /home/smunix/programming/d-projects/ldc/gen/nested.cpp:86
86 fd = getParentFunc(fd, false);
(gdb) p fd->toChars()
$102 = 0x1e17e74 "b"
(gdb) p vd->toParent2()->toChars()
$103 = 0x1e17584 "a"
(gdb) p vd->toChars()

As the following excerpt (func=DtoNestedVariable, file nested.cpp) from the code shows it, we are in function b and we check whether we can assess the needed frame or not. "assert(fd)". Since this->parent is "a" and not "b", we'll never reach "b"'s frame from the this pointer. At one point, fd turns out to be null and the assertion fails.

// Check whether we can access the needed frame
FuncDeclaration *fd = irfunc->decl;
while (fd != vdparent) {
    if (fd->isStatic()) {
        error(loc, "function %s cannot access frame of function %s", irfunc->decl->toPrettyChars(), vdparent->toPrettyChars());
        return new DVarValue(astype, vd, llvm::UndefValue::get(getPtrToType(DtoType(astype))));
    }
    fd = getParentFunc(fd, false);
    assert(fd);
}

@smunix
Copy link

smunix commented Feb 8, 2013

I still need to figure out why "this" keeps saying that its parent function is "void a() {}" while being accessed from function "void b() {}".

@John-Colvin
Copy link
Contributor

Gdc also ICEs on this: http://bugzilla.gdcproject.org/show_bug.cgi?id=49

@John-Colvin
Copy link
Contributor

relevant: dlang/dmd#1880 and dlang/dmd#1881

@dnadlinger
Copy link
Member

@John-Colvin: Thanks a lot for revisiting the open issues! Wish I could spend more time actually hacking on LDC right now…

redstar added a commit that referenced this issue Aug 18, 2013
@redstar
Copy link
Member

redstar commented Aug 18, 2013

Upstream is fixed and merged.

@redstar redstar closed this as completed Aug 18, 2013
redstar pushed a commit that referenced this issue Sep 27, 2014
@safe pure nothrow attributes in core.bitop.
redstar pushed a commit that referenced this issue Sep 27, 2014
redstar pushed a commit that referenced this issue Sep 27, 2014
This reverts commit 4df6c75.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants