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 "struct ModuleReference" added to code file #551

Closed
JinShil opened this issue Dec 5, 2013 · 9 comments
Closed

Comments

@JinShil
Copy link

JinShil commented Dec 5, 2013

I'm trying to port the D Runtime to the ARM Cortex-M, and therefore, I'm incrementally importing parts of the Runtime from the LDC runtime codebase and trying to figure out how it works.

Whenever I add the following code to any of my files, the compiler generates a segmentation fault.

// This linked list is created by a compiler generated function inserted
// into the .ctor list by the compiler.
struct ModuleReference
{
    ModuleReference* next;
    ModuleInfo*      mod;
}

Output

ldc2 -c start.d object.d rt/sections.d

0  ldc2            0x000000000132b002 llvm::sys::PrintStackTrace(_IO_FILE*) + 34
1  ldc2            0x000000000132ae69
2  libpthread.so.0 0x00007fe3a7759870
3  ldc2            0x0000000000714e86
4  ldc2            0x00000000007186a5 LLVM_D_InitRuntime() + 53
5  ldc2            0x00000000006dcb78 Module::genLLVMModule(llvm::LLVMContext&) + 632
6  ldc2            0x00000000005cf9bb main + 8059
7  libc.so.6       0x00007fe3a699fbc5 __libc_start_main + 245
8  ldc2            0x00000000005f0245
Segmentation fault (core dumped)

If I remove the struct, the compiler executes fine.

I know it might take a while to get to this bug, but I'm stalled if I can't get this struct in my code. So, if you know a temporary workaround, please let me know.

LDC Version Info:
LDC - the LLVM D compiler (0.12.1):
based on DMD v2.063.2 and LLVM 3.3
Default target: x86_64-unknown-linux-gnu
Host CPU: corei7

@redstar
Copy link
Member

redstar commented Dec 5, 2013

Thanks for the report. Could you please add some more infos about the ldc version you are using? E.g. output of ldc2 -version and used operating system.

Are you aware of this thread in the forum? http://forum.dlang.org/post/bcfnbrbrgytdycalklav@forum.dlang.org Maybe you could join forces with Mike.

@JinShil
Copy link
Author

JinShil commented Dec 5, 2013

I am Mike. I just updated the LDC version information. I'm using Arch Linux 64-bit.

@redstar
Copy link
Member

redstar commented Dec 5, 2013

:-) I was not sure because of the different nicknames.

Could you run ldc2 under control of gdb and see if it prints more information? E.g. gdb --args ldc2 -c start.d object.d rt/sections.d, run it (r) and produce a backtrace at the point of the crash (bt). The problem seems to be in LLVM_D_InitRuntime(), but I don't see what could go wrong.

Is there a possibility that I can compile your code on my box? E.g., do you have a GitHub fork of druntime?

@JinShil
Copy link
Author

JinShil commented Dec 5, 2013

Here's the backtrace:

Program received signal SIGSEGV, Segmentation fault.
0x0000000000714e86 in ?? ()
(gdb) bt
#0  0x0000000000714e86 in ?? ()
#1  0x00000000007186a5 in LLVM_D_InitRuntime() ()
#2  0x00000000006dcb78 in Module::genLLVMModule(llvm::LLVMContext&) ()
#3  0x00000000005cf9bb in main ()

I don't have a fork yet. I'm still just trying to learn the basics, and figure out the dependencies. My code is really simple, so I'll just paste it here.

*** start.d ***

module start;

//Must be stored as second 32-bit word in .text section
immutable void function() ResetHandler = &OnReset;

void SendCommand(int command, void* message)
{  }

void OnReset()
{
  while(true)
  {
    // Create semihosting message message
    uint[3] message =
      [
    2,                //stderr
    cast(uint)"hello\r\n".ptr,    //ptr to string
    7                             //size of string
      ];

    //Send semihosting command
    SendCommand(0x05, &message);
  }
}

*** object.d ***

module object;

alias immutable(char)[]  string;

/**
 * All D class objects inherit from Object.
 */
class Object
{
    /**
     * Convert Object to a human readable string.
     */
    string toString()
    {
        return typeid(this).name;
    }
} 

struct ModuleInfo
{  }

alias TypeInfo_Class Classinfo;

/**
 * Runtime type information about a type.
 * Can be retrieved for any type using a
 * <a href="../expression.html#typeidexpression">TypeidExpression</a>.
 */
class TypeInfo
{  }

/**
 * Runtime type information about a class.
 * Can be retrieved from an object instance by using the
 * $(LINK2 ../property.html#classinfo, .classinfo) property.
 */
class TypeInfo_Class : TypeInfo
{
  string      name;           /// class name
}

*** rt/sections.d ***

module rt.sections;

extern (C) __gshared ModuleReference* _Dmodule_ref;   // start of linked list

private:

// This linked list is created by a compiler generated function inserted
// into the .ctor list by the compiler.
struct ModuleReference
{
    ModuleReference* next;
    ModuleInfo*      mod;
}

That's all there is to it so far.

@redstar
Copy link
Member

redstar commented Dec 5, 2013

It looks like the crash is caused by some missing TypeInfo classes. Did you try to use object.di?

@redstar
Copy link
Member

redstar commented Dec 5, 2013

The compiler assumes that (most of) the types in object.di exist. If they miss then crashes occur because checks are missing.

@redstar
Copy link
Member

redstar commented Dec 5, 2013

This is missing in your version of object.d:

class TypeInfo_AssociativeArray : TypeInfo
{
    TypeInfo value;
    TypeInfo key;
    TypeInfo impl;
}

But you should really consider using the provided object.di / object.d_ files. They are really essential.

@JinShil
Copy link
Author

JinShil commented Dec 5, 2013

Yep, that's it. I will be adding in object.di and object.d, but the defaults include too many other things, and I'm trying to figure out what is absolutely necessary. Anyway TypeInfo_AssociativeArray gets me past the crash.

I guess this report should really be "Add the necessary checks for required types", but I understand my situation is a corner case.

@redstar
Copy link
Member

redstar commented Dec 5, 2013

The scope of the upcoming DMD release 2.065 is to reduce compiler crashes. Obviously, we should do the same for ldc. I will add some checks.

@redstar redstar closed this as completed in 43acc1f Dec 8, 2013
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

2 participants