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

[uClibc][MIPS32] Segfault when cross-compiling code using variadics #2989

Closed
rracariu opened this issue Jan 31, 2019 · 6 comments · Fixed by ldc-developers/druntime#153
Closed

Comments

@rracariu
Copy link
Contributor

Cross compiling the following code gives a segfault when targeting MIPS32.

import core.stdc.stdarg;

extern (C) int res(int a, ...)
{
    va_list args;
    va_start(args, a);

    int i;
    va_arg(args, i);
    va_arg(args, i);

    return i;
}

void main()
{
    import std.stdio;
    writeln("Res ", res(4, 56, 3));
}

The compiler debug:

ldc-openwrt-uclib-mips -v args.d
binary    /dlang/ldc-1.13.0/bin/ldc2
version   1.13.0 (DMD v2.083.1, LLVM 7.0.1)
config    /dlang/ldc-1.13.0/etc/ldc2.conf (mipsel-unknown-linux-uclibc)
predefs   LDC all D_Version2 assert D_ModuleInfo D_Exceptions D_TypeInfo MIPS MIPS32 MIPS_SoftFloat D_SoftFloat MIPS_O32 LittleEndian D_PIC linux Posix CRuntime_UClibc LDC_LLVM_700
parse     args
importall args
import    object        (/dlang/ldc-1.13.0/bin/../import/object.d)
import    core.internal.hash    (/dlang/ldc-1.13.0/bin/../import/core/internal/hash.d)
import    core.internal.traits  (/dlang/ldc-1.13.0/bin/../import/core/internal/traits.d)
import    core.internal.convert (/dlang/ldc-1.13.0/bin/../import/core/internal/convert.d)
import    core.stdc.stdarg      (/dlang/ldc-1.13.0/bin/../import/core/stdc/stdarg.d)
semantic  args
entry     main          args.d
semantic2 args
semantic3 args
import    std.stdio     (/dlang/ldc-1.13.0/bin/../import/std/stdio.d)
import    core.stdc.stddef      (/dlang/ldc-1.13.0/bin/../import/core/stdc/stddef.d)
import    core.stdc.stdio       (/dlang/ldc-1.13.0/bin/../import/core/stdc/stdio.d)
import    core.stdc.stdint      (/dlang/ldc-1.13.0/bin/../import/core/stdc/stdint.d)
import    core.stdc.config      (/dlang/ldc-1.13.0/bin/../import/core/stdc/config.d)
import    core.stdc.signal      (/dlang/ldc-1.13.0/bin/../import/core/stdc/signal.d)
import    core.stdc.wchar_      (/dlang/ldc-1.13.0/bin/../import/core/stdc/wchar_.d)
import    core.stdc.time        (/dlang/ldc-1.13.0/bin/../import/core/stdc/time.d)
import    core.sys.posix.sys.types      (/dlang/ldc-1.13.0/bin/../import/core/sys/posix/sys/types.d)
import    core.sys.posix.config (/dlang/ldc-1.13.0/bin/../import/core/sys/posix/config.d)
import    std.algorithm.mutation        (/dlang/ldc-1.13.0/bin/../import/std/algorithm/mutation.d)
import    std.traits    (/dlang/ldc-1.13.0/bin/../import/std/traits.d)
import    std.meta      (/dlang/ldc-1.13.0/bin/../import/std/meta.d)
import    std.functional        (/dlang/ldc-1.13.0/bin/../import/std/functional.d)
import    std.range.primitives  (/dlang/ldc-1.13.0/bin/../import/std/range/primitives.d)
import    std.typecons  (/dlang/ldc-1.13.0/bin/../import/std/typecons.d)
import    std.format    (/dlang/ldc-1.13.0/bin/../import/std/format.d)
import    core.vararg   (/dlang/ldc-1.13.0/bin/../import/core/vararg.d)
import    std.exception (/dlang/ldc-1.13.0/bin/../import/std/exception.d)
import    core.atomic   (/dlang/ldc-1.13.0/bin/../import/core/atomic.d)
import    ldc.intrinsics        (/dlang/ldc-1.13.0/bin/../import/ldc/intrinsics.di)
import    core.stdc.errno       (/dlang/ldc-1.13.0/bin/../import/core/stdc/errno.d)
import    std.utf       (/dlang/ldc-1.13.0/bin/../import/std/utf.d)
import    core.internal.string  (/dlang/ldc-1.13.0/bin/../import/core/internal/string.d)
import    std.algorithm.comparison      (/dlang/ldc-1.13.0/bin/../import/std/algorithm/comparison.d)
import    std.internal.attributes       (/dlang/ldc-1.13.0/bin/../import/std/internal/attributes.d)
import    std.algorithm.internal        (/dlang/ldc-1.13.0/bin/../import/std/algorithm/internal.d)
import    core.stdc.string      (/dlang/ldc-1.13.0/bin/../import/core/stdc/string.d)
import    std.conv      (/dlang/ldc-1.13.0/bin/../import/std/conv.d)
import    std.ascii     (/dlang/ldc-1.13.0/bin/../import/std/ascii.d)
code      args
/dlang/ldc/bin/ldc2(_ZN4llvm3sys15PrintStackTraceERNS_11raw_ostreamE+0x1a)[0x2a600da]
/dlang/ldc/bin/ldc2(_ZN4llvm3sys17RunSignalHandlersEv+0x35)[0x2a5e7d5]
/dlang/ldc/bin/ldc2[0x2a5e8ec]
/lib/x86_64-linux-gnu/libpthread.so.0(+0x11390)[0x7f75d89d1390]
/dlang/ldc/bin/ldc2(_ZNK10IrFunction11getLLVMFuncEv+0x0)[0xce9770]
/dlang/ldc/bin/ldc2(_Z17DtoDefineFunctionP15FuncDeclarationb+0x10a)[0xc7a57a]
/dlang/ldc/bin/ldc2[0xc6d833]
/dlang/ldc/bin/ldc2(_Z19Declaration_codegenP7DsymbolP7IRState+0x24)[0xc70444]
/dlang/ldc/bin/ldc2(_Z13codegenModuleP7IRStateP6Module+0xad)[0xc91abd]
/dlang/ldc/bin/ldc2(_ZN3ldc13CodeGenerator4emitEP6Module+0x99)[0xd3e969]
/dlang/ldc/bin/ldc2(_Z14codegenModulesR5ArrayIP6ModuleE+0x16e)[0xd13d7e]
/dlang/ldc/bin/ldc2(_Z13mars_mainBodyR5ArrayIPKcES3_+0x1410)[0xb33050]
/dlang/ldc/bin/ldc2(_Z7cppmainiPPc+0x1e41)[0xd12881]
/dlang/ldc/bin/ldc2(_D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZv+0x50)[0x2b9e500]
/dlang/ldc/bin/ldc2(_d_run_main+0x1f6)[0x2b9e316]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f75d80e0830]
/dlang/ldc/bin/ldc2[0xa5f06f]
/usr/bin/ldc-openwrt-uclib-mips: line 4:  3295 Segmentation fault      (core dumped) ~/dlang/ldc/bin/ldc2 -mtriple=mipsel-unknown-linux-uclibc -mcpu=mips32r2 -float-abi=soft -gcc=mipsel-openwrt-linux-uclibc-gcc -linker=bfd -L=-L/dlang/ldc/lib-openwrt-uclib-mips -defaultlib=phobos2-ldc,druntime-ldc,drtsupp,:libatomic.a "$@"```
@kinke kinke changed the title [uClibc][MIPS32] Segfault when cross-compiling code using varg [uClibc][MIPS32] Segfault when cross-compiling code using variadics Jan 31, 2019
@kinke
Copy link
Member

kinke commented Jan 31, 2019

Cross-compiling shouldn't matter, it's just that core.stdc.stdarg defaults to the LLVM intrinsics for generic/unsupported architectures [which don't work anywhere IIRC ;), leading to LLVM assertions/crashes]. Specifically, there's (at least some) support for MIPS64, but not for MIPS32, e.g., here.

@rracariu
Copy link
Contributor Author

Yeah, I remember testing this with an older LDC and it worked back then. I guess is a LLVM regression then?

@rracariu
Copy link
Contributor Author

I was looking at this http://math-atlas.sourceforge.net/devel/assembly/mipsabi32.pdf and http://math-atlas.sourceforge.net/devel/assembly/mipsabi64.pdf and my understanding is the MIPS32 rules apply to MIPS64.

Am I correct?

@kinke
Copy link
Member

kinke commented Jan 31, 2019

Sorry, I don't have the time to look into this, but the logic is almost always the same for the 'simpler' architectures - read current value from pointer, then bump the pointer by type size + alignment padding, with alignment mostly being the size_t size. I'd try replacing MIPS64 in that file by a (new) MIPS_Any.

@rracariu
Copy link
Contributor Author

OK, will try that later when I have access to the hardware to test. Having MIPS_Any compiles and links, just need to test this on the machine.
Thanks!

@rracariu
Copy link
Contributor Author

Same code from MIPS64 works for MIPS32 on my HW.

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

Successfully merging a pull request may close this issue.

2 participants