www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 16636] New: Memory corruption when using OSX pthread function

https://issues.dlang.org/show_bug.cgi?id=16636

          Issue ID: 16636
           Summary: Memory corruption when using OSX pthread function in
                    32-bit with -g enabled
           Product: D
           Version: D2
          Hardware: x86
                OS: Mac OS X
            Status: NEW
          Severity: major
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: aliloko gmail.com

DMD version: 

    DMD64 D Compiler v2.072.0-b2


OS: 

    macOS Sierra 10.12 on x86 processor



Build test case with comand:


    $ rdmd -m32 -g main.d

    What happens is that there is a deadlock instead of finishing.

    Many ways not to work-around the bug:
     - remove the _created bool from the structure (44-byte struct instead of
48)
     - change the size of this boolean to 8-bytes (52 byte struct)
     - compile for 64-bit
     - remove the -g flags


Source:

------------------- main.d -------------------------


import core.sys.posix.pthread;

UncheckedMutex makeMutex() nothrow  nogc
{
    return UncheckedMutex(42);
}

struct UncheckedMutex
{
    private this(int dummyArg) nothrow  nogc
    {
        pthread_mutex_init( &m_hndl, null );
    }

    ~this() nothrow  nogc
    {
        pthread_mutex_destroy(&m_hndl);
    }

     disable this(this);

    /// Lock mutex
    void lock() nothrow  nogc
    {
        int res = pthread_mutex_lock(&m_hndl);
        if (res != 0)
            assert(false);
    }

    // undocumented function for internal use
    void unlock() nothrow  nogc
    {
        int res = pthread_mutex_unlock(&m_hndl );
        if (res != 0)
            assert(false);
    }

private:
    pthread_mutex_t m_hndl = cast(pthread_mutex_t)0;

    // removing this fix the deadlock
    bool _created;
}

void main()
{
    auto mutex = makeMutex();
    foreach(i; 0..2)
    {
        mutex.lock(); // the second call will deadlock here
        mutex.unlock();
    }
    mutex.destroy();
}


------------------- main.d -------------------------

--
Oct 24 2016