ivmai / bdwgc

The Boehm-Demers-Weiser conservative C/C++ Garbage Collector (bdwgc, also known as bdw-gc, boehm-gc, libgc)
https://www.hboehm.info/gc/
Other
2.98k stars 407 forks source link

Strange MemorySanitizer warnings on E2K #457

Open ivmai opened 2 years ago

ivmai commented 2 years ago

Source: master (2afde2458) Host: Linux/E2K Compiler: lcc:1.26.12:Jun--5-2022:e2k-v4-linux; gcc (GCC) 9.3.0 compatible How to reproduce (1): ./autogen.sh && ./configure && make -j check CFLAGS_EXTRA="-fsanitize=memory" && cat gctest.log

==20773==WARNING: MemorySanitizer: use-of-uninitialized-value
    #0 0x4585e20e9ad8 in GC_expand_hp_inner /export/home/ivmai/bdwgc/extra/../alloc.c:336
    #1 0x4585e2171e10 in GC_init /export/home/ivmai/bdwgc/extra/../misc.c:1313
    #2 0x4585e21c3a50 in GC_pthread_create /export/home/ivmai/bdwgc/extra/../pthread_support.c:2195
    #3 0x23e518 in main /export/home/ivmai/bdwgc/tests/gctest.c:2435

How to reproduce (2): ./autogen.sh && ./configure && make -j check CFLAGS_EXTRA="-fsanitize=memory -O0" && cat gctest.log

==22711==WARNING: MemorySanitizer: use-of-uninitialized-value
    #0 0x4579401d4a00 in GC_exclude_static_roots_inner /export/home/ivmai/bdwgc/extra/../mark_rts.c:580
    #1 0x4579402031a0 in GC_init /export/home/ivmai/bdwgc/extra/../misc.c:1210
    #2 0x457940207038 in GC_enable_incremental /export/home/ivmai/bdwgc/extra/../misc.c:1404
    #3 0x252ff0 in enable_incremental_mode /export/home/ivmai/bdwgc/tests/gctest.c:1958
    #4 0x254c08 in main /export/home/ivmai/bdwgc/tests/gctest.c:2419

Related issue #411

ivmai commented 2 years ago

GC_expand_hp_inner /export/home/ivmai/bdwgc/extra/../alloc.c:336 :

static size_t min_bytes_allocd_minimum = 1;
...
static word min_bytes_allocd(void)
{
 ...
    result = scan_size / GC_free_space_divisor;
    if (GC_incremental) {
      result /= 2;
    }
    return result > min_bytes_allocd_minimum // <-- line 336
            ? result : min_bytes_allocd_minimum;
}
ivmai commented 2 years ago

GC_exclude_static_roots_inner /export/home/ivmai/bdwgc/extra/../mark_rts.c:580 :

    struct exclusion * next;
    ...
    if (0 == GC_excl_table_entries) {
        next = 0;
    } else {
        next = GC_next_exclusion((ptr_t)start);
    }
    if (0 != next) { // <-- line 580
ivmai commented 2 years ago

gcc -I include -O0 -fsanitize=memory tests/gctest.c extra/gc.c && ./a.out

==12626==WARNING: MemorySanitizer: use-of-uninitialized-value
    #0 0x2f9bc8 in GC_exclude_static_roots_inner /export/home/ivmai/bdwgc/extra/../mark_rts.c:600
ivmai commented 2 years ago

I've created a small reproducible testcase.c:

#include <stddef.h>

typedef char * ptr_t;

struct exclusion {
    ptr_t e_start;
    ptr_t e_end;
};

struct _GC_arrays {
# define GC_excl_table_entries GC_arrays._excl_table_entries
  size_t _excl_table_entries;
# define GC_excl_table GC_arrays._excl_table
  struct exclusion _excl_table[512];
};

struct _GC_arrays GC_arrays = { 0 };

struct exclusion * next = 0;

void GC_exclude_static_roots_inner(void *start)
{
    size_t next_index;
    if (0 != next) {
      size_t i;
      next_index = 0;
      for (i = GC_excl_table_entries; i > next_index; --i) {
        GC_excl_table[i] = GC_excl_table[i-1];
      }
    } else {
      next_index = GC_excl_table_entries;
    }
    GC_excl_table[next_index].e_start = (ptr_t)start;
}

int main(void) {
  GC_exclude_static_roots_inner(0);
  return 0;
}

gcc -O0 -g -fsanitize=memory testcase.c && ./a.out Output:

==16175==WARNING: MemorySanitizer: use-of-uninitialized-value
    #0 0x213790 in GC_exclude_static_roots_inner /export/home/ivmai/bdwgc/t.c:33
    #1 0x2138e0 in main /export/home/ivmai/bdwgc/t.c:37
    #2 0x4650607bf0d0 in __libc_start_main (/lib64/libc.so.6+0x220d0)
    #3 0x2fec8 in _start (/export/home/ivmai/bdwgc/a.out+0x2fec8)
    #4 0x46505ffd4298 in _start
ivmai commented 2 years ago

As said by lcc folks, MSan is not yet supported properly by lcc (unlike ASan). I will close this issue after fixing it in lcc.