Bug 17315 – Assigning a delegate to a function compiles but causes segfault

Status
RESOLVED
Resolution
DUPLICATE
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2017-04-10T07:33:33Z
Last change time
2023-05-18T10:46:13Z
Assigned to
No Owner
Creator
Tomer Filiba (weka)

Comments

Comment #0 by tomer — 2017-04-10T07:33:33Z
using `DMD64 D Compiler v2.073.0` on ubuntu 16.04 =========================================== class Base { __gshared static Base function()[string] registry; static Base load(string name) {return registry[name]();} } class Child: Base { shared static this() { import std.traits: fullyQualifiedName; registry[fullyQualifiedName!(typeof(this))] = &_load; // <<< this shouldn't compile } Base _load() {return new typeof(this);} } void main() { auto inst = Base.load("dtest.Child"); // segfault } =========================================== Program received signal SIGSEGV, Segmentation fault. 0x0000000000453f4a in invariant._d_invariant(Object) () (gdb) bt #0 0x0000000000453f4a in invariant._d_invariant(Object) () #1 0x0000000000440272 in dtest.Child._load() (this=0x477d7b <_TMP1+11>) at src/dtest.d:69 #2 0x00000000004401ad in dtest.Base.load(immutable(char)[]) (name=...) at src/dtest.d:60 #3 0x000000000044029c in D main () at src/dtest.d:81 =========================================== if i make `_load` a static function (as it should be), everything works as expected class Child: Base { ... static Base _load() {return new typeof(this);} }
Comment #1 by razvan.nitu1305 — 2023-05-18T08:44:31Z
I cannot reproduce this. It compiles and runs successfully. When you are taking the address of _load the currently returns a function pointer (set to the actual code of the function in the .text section). This behavior is reported as being a bug in previous bug reports (such as https://issues.dlang.org/show_bug.cgi?id=3720). So, since the segfault no longer manifests, this bug report is resolved.
Comment #2 by eyal — 2023-05-18T10:39:55Z
A small change to _load to make it actually use the incorrect "this" shows it's still broken: import std; class Base { __gshared static Base function()[string] registry; static Base load(string name) {return registry[name]();} } class Child: Base { shared static this() { registry[__traits(identifier, typeof(this))] = &_load; // <<< this shouldn't compile } int x = 1; Base _load() { assert(x == 1, x.text); // [email protected](15): 1970086008 return new typeof(this); } } void main() { auto inst = Base.load("Child"); // segfault }
Comment #3 by razvan.nitu1305 — 2023-05-18T10:46:13Z
Yes, then it's a dupe of 3720 *** This issue has been marked as a duplicate of issue 3720 ***