johnsonjh / duma

duma: Detect Unintended Memory Access (D.U.M.A.) - A Red-Zone memory allocator
https://github.com/johnsonjh/duma
GNU General Public License v2.0
101 stars 10 forks source link

Many tests fail on FreeBSD #164

Open yurivict opened 3 years ago

yurivict commented 3 years ago
Test project /disk-samsung/freebsd-ports/devel/duma/work/.build
      Start  1: dumatest_static
 1/12 Test  #1: dumatest_static ..................***Exception: SegFault  0.00 sec
      Start  2: tstheap_static
 2/12 Test  #2: tstheap_static ...................***Exception: SegFault  0.00 sec
      Start  3: thread-test_static
 3/12 Test  #3: thread-test_static ...............   Passed    0.02 sec
      Start  4: dumatestpp_static
 4/12 Test  #4: dumatestpp_static ................***Exception: SegFault  0.00 sec
      Start  5: testoperators_static
 5/12 Test  #5: testoperators_static .............***Exception: SegFault  0.00 sec
      Start  6: testmemlimit_static
 6/12 Test  #6: testmemlimit_static ..............   Passed    0.00 sec
      Start  7: dumatest_shared
 7/12 Test  #7: dumatest_shared ..................***Exception: SegFault  0.00 sec
      Start  8: tstheap_shared
 8/12 Test  #8: tstheap_shared ...................***Exception: SegFault  0.00 sec
      Start  9: thread-test_shared
 9/12 Test  #9: thread-test_shared ...............***Exception: SegFault  0.00 sec
      Start 10: dumatestpp_shared
10/12 Test #10: dumatestpp_shared ................***Exception: SegFault  0.00 sec
      Start 11: testoperators_shared
11/12 Test #11: testoperators_shared .............***Exception: SegFault  0.00 sec
      Start 12: testmemlimit_shared
12/12 Test #12: testmemlimit_shared ..............***Exception: SegFault  0.00 sec

17% tests passed, 10 tests failed out of 12

Total Test time (real) =   0.06 sec

The following tests FAILED:
      1 - dumatest_static (SEGFAULT)
      2 - tstheap_static (SEGFAULT)
      4 - dumatestpp_static (SEGFAULT)
      5 - testoperators_static (SEGFAULT)
      7 - dumatest_shared (SEGFAULT)
      8 - tstheap_shared (SEGFAULT)
      9 - thread-test_shared (SEGFAULT)
     10 - dumatestpp_shared (SEGFAULT)
     11 - testoperators_shared (SEGFAULT)
     12 - testmemlimit_shared (SEGFAULT)

2_5_21-315-gdce042d OS: FreeBSD 13

yurivict commented 3 years ago

For example, the dumatest_static crashes here: In FreeBSD libraries:

 605│         } else if (__predict_false(*m <= THR_MUTEX_DESTROYED)) {
 606│                 if (*m == THR_MUTEX_DESTROYED) {
 607│                         ret = EINVAL;
 608│                 } else {
 609├───────────────────────> ret = init_static(_get_curthread(), mutex);
 610│                         if (ret == 0)
 611│                                 *m = *mutex;
 612│                 }
 613│         }

called from here in duma:

130│ static void lock() {
131├─> if (pthread_mutex_trylock(&mutex)) {
132│     if (mutextid == pthread_self()) {
133│       ++locknr;
134│       return;
135│     } else {
136│       pthread_mutex_lock(&mutex);
137│     }
138│   }
139│   mutextid = pthread_self();
140│   locknr = 1;
141│ }

... during static initialization.

johnsonjh commented 3 years ago

@yurivict

Thank you for the report.

Are you able to replicate this using the GNU Make based build? I have a feeling this might have to do with the requirement for passing -pthread to the compiler and linker and/or defining _REENTRANT and/or _THREADSAFE on some platforms, or some combination of these.

I'll be able to look at this over the weekend - I have a FreeBSD/ARM64 system available locally, which I hope will be sufficient to reproduce and create a fix.

johnsonjh commented 3 years ago

@yurivict I'll be running some tests this evening, thanks.

yurivict commented 3 years ago

Same with GNU make-based build.

johnsonjh commented 3 years ago
$ uname -a
FreeBSD rpi3br1 13.0-RELEASE-p4 FreeBSD 13.0-RELEASE-p4 #0: Tue Aug 24 07:38:07 UTC 2021     root@arm64-builder.daemonology.net:/usr/obj/usr/src/arm64.aarch64/sys/GENERIC  arm64

Seeing the same results as you, same crash, so at least it's something I can debug here. Same with GCC and Clang as well.

As an experiment, I tried to build the non-threaded library (you'll need to use gmake -k to build as it tries to compile a threaded test when using cmake - I'll fix that) and still find some problems.

Test project /home/jhj/src/duma/build                                                                                
      Start  1: dumatest_static  
 1/12 Test  #1: dumatest_static ..................   Passed    0.01 sec           
      Start  2: tstheap_static      
 2/12 Test  #2: tstheap_static ...................   Passed    1.33 sec           
      Start  3: thread-test_static 
 3/12 Test  #3: thread-test_static ...............***Not Run   0.00 sec
      Start  4: dumatestpp_static
 4/12 Test  #4: dumatestpp_static ................   Passed    0.01 sec
      Start  5: testoperators_static
 5/12 Test  #5: testoperators_static .............   Passed    0.01 sec
      Start  6: testmemlimit_static
 6/12 Test  #6: testmemlimit_static ..............   Passed    0.49 sec
      Start  7: dumatest_shared
 7/12 Test  #7: dumatest_shared ..................***Exception: SegFault  0.27 sec
      Start  8: tstheap_shared
 8/12 Test  #8: tstheap_shared ...................***Exception: SegFault  2.97 sec
      Start  9: thread-test_shared
 9/12 Test  #9: thread-test_shared ...............***Exception: SegFault  2.98 sec
      Start 10: dumatestpp_shared
10/12 Test #10: dumatestpp_shared ................***Exception: SegFault  0.26 sec
      Start 11: testoperators_shared
11/12 Test #11: testoperators_shared .............***Exception: SegFault  0.25 sec
      Start 12: testmemlimit_shared
12/12 Test #12: testmemlimit_shared ..............***Exception: SegFault  0.26 sec

42% tests passed, 7 tests failed out of 12

Total Test time (real) =   8.87 sec

The following tests FAILED:
          3 - thread-test_static (Not Run)
          7 - dumatest_shared (SEGFAULT)
          8 - tstheap_shared (SEGFAULT)
          9 - thread-test_shared (SEGFAULT)
         10 - dumatestpp_shared (SEGFAULT)
         11 - testoperators_shared (SEGFAULT)
         12 - testmemlimit_shared (SEGFAULT)
Errors while running CTest

FreeBSD has changed a bit since I last looked at it - I'm reading about the "new" libthr vs. libpthread and hope to have a fix for you shortly, and some build enhancements.

johnsonjh commented 3 years ago

A local NetBSD systems seems OK - still investigating.

johnsonjh commented 3 years ago

Just to give you an update that I've not forgotten about this and hope to have a solution shortly. I can avoid the problem with inefficient management of the lock and not using trylock, but, that kind of defeats the purpose! I'm sure it's something I'm doing wrong.

yurivict commented 3 years ago

Thanks!

johnsonjh commented 3 years ago

I've asked a friend who has some more FreeBSD experience to help and hopefully I'll hear something soon.