www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 17821] New: atomicStore is buggy when target is larger than

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

          Issue ID: 17821
           Summary: atomicStore is buggy when target is larger than source
           Product: D
           Version: D2
          Hardware: All
               URL: http://dlang.org/
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P3
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: eyal weka.io

LDC version of core.atomic.atomicStore:

    void atomicStore(MemoryOrder ms = MemoryOrder.seq, T, V1)( ref shared T
val, V1 newval ) pure nothrow  nogc  trusted
        if( __traits( compiles, { val = newval; } ) )
    {
        alias Int = _AtomicType!T;
        auto target = cast(shared(Int)*)cast(void*)&val;
        auto newPtr = cast(Int*)&newval; // this cast is wrong!
        llvm_atomic_store!Int(*newPtr, target, _ordering!(ms));
    }

If V1 is a smaller type than T it will cast ptr-to-V1 to ptr-to-T and
dereference that to read garbage.

Example:

    shared ulong x;
    atomicStore(x, 0); // this assigns the low 32 bits correctly, but the top
32 bits of x are set to garbage from the stack

--
Sep 10 2017