Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8350455

Malloc errors when concurrently allocating and freeing memory.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • 25
    • core-libs
    • generic
    • os_x

      The following test occasionally crashes with exit code 134:

          /*
           * @test
           * @modules java.base/jdk.internal.misc
           * @build TestVTStressHeap
           * @run main/othervm TestVTStressHeap
           */
          
          import jdk.internal.misc.Unsafe;
          
          import java.time.Duration;
          import java.util.Arrays;
          import java.util.stream.IntStream;
          
          import static java.time.temporal.ChronoUnit.SECONDS;
          
          public class TestVTStressHeap {
              private static final Unsafe U = Unsafe.getUnsafe();
          
              public static void main(String[] args) throws InterruptedException {
                  Thread[] vThreads = IntStream.range(0, 1024).mapToObj(_ ->
                          Thread.ofVirtual().start(() -> {
                              while (!Thread.interrupted()) {
                                  for (int i = 0; i < 1_000_000; i++) {
                                      long addr = U.allocateMemory(Long.BYTES);
                                      U.freeMemory(addr);
                                  }
                                  Thread.yield(); // make sure the driver thread gets a chance.
                              }
                          })).toArray(Thread[]::new);
                  Thread.sleep(Duration.of(10, SECONDS));
                  Arrays.stream(vThreads).forEach(Thread::interrupt);
              }
          }

      When a core dump is produced, one of the threads will be inside __pthread_kill, with a stack that looks like this:

        thread #24, stop reason = signal SIGSTOP
          frame #0: 0x00007ff802ea61e2 libsystem_kernel.dylib`__pthread_kill + 10
          frame #1: 0x00007ff802eddee6 libsystem_pthread.dylib`pthread_kill + 263
          frame #2: 0x00007ff802e04b45 libsystem_c.dylib`abort + 123
          frame #3: 0x00007ff802d1b752 libsystem_malloc.dylib`malloc_vreport + 888
          frame #4: 0x00007ff802d30a08 libsystem_malloc.dylib`malloc_zone_error + 183
          frame #5: 0x00007ff802d2907c libsystem_malloc.dylib`nanov2_guard_corruption_detected + 34
          frame #6: 0x00007ff802d2837f libsystem_malloc.dylib`nanov2_allocate_outlined + 385
          frame #7: 0x00007ff802d0e168 libsystem_malloc.dylib`nanov2_malloc + 529
          frame #8: 0x000000011025d174 libjvm.dylib`os::malloc(unsigned long, MemTag, NativeCallStack const&) + 404
          frame #9: 0x000000011025cf29 libjvm.dylib`os::malloc(unsigned long, MemTag) + 57
          frame #10: 0x0000000120cfc972
          frame #11: 0x0000000120d01f80

      Or like this:

        thread #23, stop reason = signal SIGSTOP
          frame #0: 0x00007ff8052e41e2 libsystem_kernel.dylib`__pthread_kill + 10
          frame #1: 0x00007ff80531bee6 libsystem_pthread.dylib`pthread_kill + 263
          frame #2: 0x00007ff805242b45 libsystem_c.dylib`abort + 123
          frame #3: 0x00007ff805159752 libsystem_malloc.dylib`malloc_vreport + 888
          frame #4: 0x00007ff80515cb31 libsystem_malloc.dylib`malloc_report + 151
          frame #5: 0x0000000120fffc72
          frame #6: 0x000000012100508c

      This issue has only been observed on mac (x64 & aarch64). It is unclear whether this is an issue in the JDK, or in the mac standard library implementation of malloc/free.

            jpai Jaikiran Pai
            jvernee Jorn Vernee
            Votes:
            0 Vote for this issue
            Watchers:
            10 Start watching this issue

              Created:
              Updated: