unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* Calling Glibc's malloc(3) from Guile
@ 2025-01-13 20:58 Felix Lechner via Developers list for Guile, the GNU extensibility library
  2025-01-19 13:07 ` Maxime Devos
  0 siblings, 1 reply; 2+ messages in thread
From: Felix Lechner via Developers list for Guile, the GNU extensibility library @ 2025-01-13 20:58 UTC (permalink / raw)
  To: guile-devel

Hi,

May I call Glibc's malloc(3) from Guile?  Thanks!

Kind regards,
Felix Lechner

P.S. Following a series of friendly interactions on #guile, I subscribed
to this list.  There is no need to copy me separately on a reply.



^ permalink raw reply	[flat|nested] 2+ messages in thread

* RE: Calling Glibc's malloc(3) from Guile
  2025-01-13 20:58 Calling Glibc's malloc(3) from Guile Felix Lechner via Developers list for Guile, the GNU extensibility library
@ 2025-01-19 13:07 ` Maxime Devos
  0 siblings, 0 replies; 2+ messages in thread
From: Maxime Devos @ 2025-01-19 13:07 UTC (permalink / raw)
  To: Felix Lechner, guile-devel@gnu.org

[-- Attachment #1: Type: text/plain, Size: 1641 bytes --]

There is nothing that forbids doing that, to my knowledge (do so the same way you would call other libc functions). But remember to check for ENOMEM/NULL (and eventually free it).

Something to keep in mind though (not specific to malloc): if you put the pointer in some object, and the record type of that object has a record printer, and that record printer dereferences the pointer, then when the object is printed after deallocation, you (almost tautologically) have use-after-free, and before initialisation you have another problem.

While obvious, this can happen in unexpected situations. Consider:

;; ENOMEM / NULL handling omitted.
;; assume that the record printer, when the pointer field isn’t #false,
;; dereferences the pointer and prints something about it.
;; (if it doesn’t check for #false, you also have problems, but different problems)
(define (allocate-thing)
  (let ((r (make-record-thingie))
          (p (malloc-something)))
    ;; even if setting the field is integrated in the constructor,
    ;; then internally Guile will split the allocation and setting the fields
   ;; (although, different than C, it is still initialised after allocation, to #false IIRC)
    (set-some-field! r p)
    (do-some-initialisation! r)
    r))
,trace (allocate-thing)

Because ‘trace’ looks at each procedure call (including do-some-initialisation!) inside and prints the argument, you end up with use before initialisation (a variant with use-after-free can also be written).

(For a non-malloc example, see the bug report (+ patch) about GOOPS methods and ,trace.)

Best regards,
Maxime Devos

[-- Attachment #2: Type: text/html, Size: 4121 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-01-19 13:07 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-01-13 20:58 Calling Glibc's malloc(3) from Guile Felix Lechner via Developers list for Guile, the GNU extensibility library
2025-01-19 13:07 ` Maxime Devos

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).