Bug 21504 – Incorrect eponymous overload called by codegen

Status
NEW
Severity
critical
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2020-12-26T16:28:55Z
Last change time
2024-12-13T19:13:39Z
Keywords
safe, wrong-code
Assigned to
No Owner
Creator
Vladimir Panteleev
Moved to GitHub: dmd#19845 →

Comments

Comment #0 by dlang-bugzilla — 2020-12-26T16:28:55Z
See the following program. According to semantics (return value), we call one overload, but codegen actually calls another: //////////////////////////// test.d /////////////////////////// @safe: template toHex() { char[] toHex(in ubyte[] data, char[] buf) pure { assert(false, "Completely irrelevant overload"); } char[n*2] toHex(size_t n)(in ubyte[n] data) pure { char[n*2] buf; return buf; } string toHex(in ubyte[] data) pure { assert(false, "Should not be called"); // Is called } } void main() { ubyte[40] hmacBytes; // Semantics thinks that this will call the second overload // (returning a static array), // but codegen actually calls the third overload! auto hmac = hmacBytes.toHex(); // Yes, according to the type of the return value, // we called the second overload: static assert(is(typeof(hmac) == char[80])); } /////////////////////////////////////////////////////////////// This seems to further confuse the compiler, and cause invalid slices to be passed to further consumers of the return value, leading to segfaults / memory corruption.
Comment #1 by dlang-bugzilla — 2023-02-27T19:03:41Z
LDC segfaults instead of producing wrong code: #0 0x00007f8155ed545a (/usr/lib/libLLVM-14.so+0xcd545a) #1 0x00007f8155ed2aef llvm::sys::RunSignalHandlers() (/usr/lib/libLLVM-14.so+0xcd2aef) #2 0x00007f8155ed2c3d (/usr/lib/libLLVM-14.so+0xcd2c3d) #3 0x00007f8154c51a00 (/usr/lib/libc.so.6+0x38a00) #4 0x000055c5f0bed781 DtoLVal(DValue*) (/usr/bin/ldc2+0x98b781) #5 0x000055c5f0bd86d9 DtoArrayPtr(DValue*) (/usr/bin/ldc2+0x9766d9) #6 0x000055c5f0bd91f8 DtoArrayAssign(Loc const&, DValue*, DValue*, EXP, bool) (/usr/bin/ldc2+0x9771f8) #7 0x000055c5f0b25406 DtoAssign(Loc const&, DValue*, DValue*, EXP, bool) (/usr/bin/ldc2+0x8c3406) #8 0x000055c5f0b8823d (/usr/bin/ldc2+0x92623d) #9 0x000055c5f0b7ab86 toElem(Expression*) (/usr/bin/ldc2+0x918b86) #10 0x000055c5f0b25c54 DtoVarDeclaration(VarDeclaration*) (/usr/bin/ldc2+0x8c3c54) #11 0x000055c5f0b293a3 DtoDeclarationExp(Dsymbol*) (/usr/bin/ldc2+0x8c73a3) #12 0x000055c5f0b856cc (/usr/bin/ldc2+0x9236cc) #13 0x000055c5f0b7ae16 toElemDtor(Expression*) (/usr/bin/ldc2+0x918e16) #14 0x000055c5f0b63b59 (/usr/bin/ldc2+0x901b59) #15 0x000055c5f0b63189 (/usr/bin/ldc2+0x901189) #16 0x000055c5f0b63189 (/usr/bin/ldc2+0x901189) #17 0x000055c5f0b62a3f Statement_toIR(Statement*, IRState*) (/usr/bin/ldc2+0x900a3f) #18 0x000055c5f0b1af6d DtoDefineFunction(FuncDeclaration*, bool) (/usr/bin/ldc2+0x8b8f6d) #19 0x000055c5f0bec420 (/usr/bin/ldc2+0x98a420) #20 0x000055c5f0bec366 Declaration_codegen(Dsymbol*) (/usr/bin/ldc2+0x98a366) #21 0x000055c5f0b32422 codegenModule(IRState*, Module*) (/usr/bin/ldc2+0x8d0422) #22 0x000055c5f0bffddd ldc::CodeGenerator::emit(Module*) (/usr/bin/ldc2+0x99dddd) #23 0x000055c5f0bce74c codegenModules(Array<Module*>&) (/usr/bin/ldc2+0x96c74c) #24 0x000055c5f0852a2a mars_mainBody(Param&, Array<char const*>&, Array<char const*>&) (/usr/bin/ldc2+0x5f0a2a) #25 0x000055c5f0bd19f7 cppmain() (/usr/bin/ldc2+0x96f9f7) #26 0x000055c5f0d81d8c _D2rt6dmain212_d_run_main2UAAamPUQgZiZ6runAllMFZv (/usr/bin/ldc2+0xb1fd8c) #27 0x000055c5f0d81ba6 _d_run_main2 (/usr/bin/ldc2+0xb1fba6) #28 0x000055c5f0d819fe _d_run_main (/usr/bin/ldc2+0xb1f9fe) #29 0x000055c5f0670c86 main (/usr/bin/ldc2+0x40ec86) #30 0x00007f8154c3c290 (/usr/lib/libc.so.6+0x23290) #31 0x00007f8154c3c34a __libc_start_main (/usr/lib/libc.so.6+0x2334a) #32 0x000055c5f0673115 _start (/usr/bin/ldc2+0x411115) Error: Error executing /usr/bin/ldc2: Segmentation fault (core dumped)
Comment #2 by robert.schadek — 2024-12-13T19:13:39Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/19845 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB