clangupc / upc2c

Clang based UPC to C Translator
https://clangupc.github.io/clang-upc2c
Other
4 stars 3 forks source link

miscompiled statically-scoped shared variables #91

Closed PHHargrove closed 10 years ago

PHHargrove commented 10 years ago

Copied from Dan Bonachea's report in the Berkeley UPC Bugzilla (https://upc-bugs.lbl.gov/bugzilla/show_bug.cgi?id=3216):

UPC2C is miscompiling atomic_test on all platforms. The core problem is demonstrated in the following test program:

#include <stdio.h>

#define ASSERT(expr) do { if (!(expr)) printf("ERROR %i: Assert
%s\n",__LINE__,#expr); } while(0)

static shared int I;
int main() {
  shared int *pI1;
  shared int *pI2;
  shared int *pI3;
  if (!MYTHREAD) I = 1;
  pI1 = &I;
  { 
    static shared int I;
    if (!MYTHREAD) I = 2;
    pI2 = &I;
  }
  { 
    static shared int I;
    if (!MYTHREAD) I = 3;
    pI3 = &I;
  }

  upc_barrier;

  ASSERT(pI1 != pI2);
  ASSERT(pI1 != pI3);
  ASSERT(pI2 != pI3);
  ASSERT(*pI1 == 1);
  ASSERT(*pI2 == 2);
  ASSERT(*pI3 == 3);

  printf("done.\n");
  return 0;
}

trans_bupc passes the test. UPC2C fails:

{freebsd9-amd64 ~/UPC/code}
/home/phargrov/upcnightly/cupc2c/runtime/inst/dbg_cupc2c/bin/upcc -save-temps
staticbug.upc
{freebsd9-amd64 ~/UPC/code} ./staticbug
UPCR: UPC thread 0 of 1 on freebsd9-amd64.qemu (pshm node 0 of 1, process 0 of
1, pid=31352)
ERROR 25: Assert pI1 != pI2
ERROR 26: Assert pI1 != pI3
ERROR 27: Assert pI2 != pI3
ERROR 28: Assert *pI1 == 1
ERROR 29: Assert *pI2 == 2
done.

The relevant generated code:

static upcr_pshared_ptr_t I;
static upcr_pshared_ptr_t I;
static upcr_pshared_ptr_t I;
int user_main() {
    UPCR_BEGIN_FUNCTION();
    int _bupc_spilld0;
    int _bupc_spilld1;
    int _bupc_spilld2;
    int _bupc_spilld3;
    {
        upcr_pshared_ptr_t pI1;
        upcr_pshared_ptr_t pI2;
        upcr_pshared_ptr_t pI3;
...
extern void UPCRI_ALLOC_staticbug_2018553948() {
    UPCR_BEGIN_FUNCTION();
    upcr_startup_pshalloc_t _bupc_pinfo[] = { UPCRT_STARTUP_PSHALLOC(I, 4UL,
1UL, 0UL, 4UL, ""), UPCRT_STARTUP_PSHALLOC(I, 4UL, 1UL, 0UL, 4UL, ""),
UPCRT_STARTUP_PSHALLOC(I, 4UL, 1UL, 0UL, 4UL, "") };
    upcr_startup_pshalloc(_bupc_pinfo, 3UL);
}

The problem is UPC2C has promoted the two block-scoped static shared variables (I) to global scope (in order to allow allocation from a different function at startup), but has not mangled the names to ensure uniqueness. Consequently, the shared variables which were separate in UPC now name-collide with each other and a similarly-named variable at global scope, resulting in all three variables aliased to the same shared space. Small changes to the declarations in the test (eg adding a definite blocksize to one of the I declarations) results in backend type mismatch errors from the duplicate declarations.

This miscompilation and resulting incorrect aliasing leads to interference between most of the test modules in atomic_test and memory corruption due to accessing beyond the end of differently-sized objects.

I don't know the release schedule for UPC2C, but this seems like a blocker.

PHHargrove commented 10 years ago

OOPS - this is a duplicate of an already-resolved issue. Sorry for being a step behind - I am recovering from a 10-day email backlog due to vacation. -Paul