From: Maxime Devos <maximedevos@telenet.be>
To: Felix Lechner <felix.lechner@lease-up.com>,
"guile-devel@gnu.org" <guile-devel@gnu.org>
Subject: RE: Calling Glibc's malloc(3) from Guile
Date: Sun, 19 Jan 2025 14:07:09 +0100 [thread overview]
Message-ID: <20250119140709.31792E00o3GvLVE01179Ve@xavier.telenet-ops.be> (raw)
In-Reply-To: <87ed165t3a.fsf@lease-up.com>
[-- 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 --]
prev parent reply other threads:[~2025-01-19 13:07 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
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 message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250119140709.31792E00o3GvLVE01179Ve@xavier.telenet-ops.be \
--to=maximedevos@telenet.be \
--cc=felix.lechner@lease-up.com \
--cc=guile-devel@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).