(unless (which "strace")
  (test-skip 1))
(test-equal "manual munmap does not lead to double free"
  1                                     ;single munmap call
  (begin
    (define mmap-address #f)
    (call-with-output-file temp-file
      (lambda (p)
        (display "something interesting\n" p)))
    (sync)
    (system (string-append "strace -o " strace-output
                           " -p " (number->string (getpid))
                           " -e trace=munmap &"))
    (let* ((bv (file->bytevector temp-file))
           (address (pk 'addr (pointer-address (pk 'ptr: (bytevector->pointer bv))))))
      ;; (format #t "ptr: ~x\n" address)
      (set! mmap-address address)
      (munmap bv))
    (gc)                                ; (gc) (gc) (gc) (gc)
    (sync)
    (let ((text (pk 'text (call-with-input-file strace-output get-string-all)))
          (pattern (pk 'pattern (format #f "munmap(0x~x" mmap-address))))
      (length (filter (cut string-prefix-ci? pattern <>)
                      (pk 'lines (string-split text #\newline)))))))

Generated by apteryx using scpaste at Mon Oct 27 22:32:40 2025. JST. (original)