Skip to content

Commit 1282565

Browse files
bulk88khwilliamson
authored andcommitted
Update POD for Newx/Renew/Safefree vs libc analogs about heap corruption
This isn't documented anywhere except in perlapi, in the per-macro/per-function section. A seasoned dev, will read perlguts ONCE, write code, then pass a Newx() pointer, to some 3rd party library or native OS API, and then instant disaster, or invisible disaster. If Newx() and libc malloc() pointers are interchangeable, on ONE particular OS, with ONE particular perl build, with ONE particular set of build flags, that is undefined behavior. Have fun with #define USE_MDH or -DUSE_MDH or -DDEBUGGING or #define MYMALLOC -DMYMALLOC. Also, for anyone reading this in the future. DO NOT EVER DOCUMENT the permutations where Newx() is libc malloc(). Perl core reserves the right, to separate Newx() and malloc(), at any time in a maint release if there are technical reasons to do so. Also libperl.so/.dll embedders, if libperl is unloaded from the process, and deallocs all Newx() blocks globally, and a 3rd party library still loaded in the process, thinking it owns that "malloc()" block, that was given ownership of, in 3rd party lib API contract, SEGV time.
1 parent b4a1dca commit 1282565

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

pod/perlclib.pod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,10 @@ should use C<sv_gets> instead:
166166
p = realloc(p, n) Renew(p, n, t)
167167

168168
It is not portable to try to allocate 0 bytes; allocating 1 or more is
169-
portable.
169+
portable. Never pass pointers between C<Newx>, C<Renew>, C<Safefree> and
170+
I<libc> equivalents C<malloc>, C<realloc>, C<free>. They are not from the
171+
same memory pool or allocator. Either an instant or delayed I<SEGV> will
172+
occur, or subtle memory leaks or subtle heap corruption.
170173

171174
memcpy(dst, src, n) Copy(src, dst, n, t)
172175
memmove(dst, src, n) Move(src, dst, n, t)

pod/perldelta.pod

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,22 @@ Additionally, the following selected changes have been made:
231231

232232
=head3 L<perl5416delta>
233233

234+
=head4 F<pod/perlguts.pod> and F<pod/perlclib.pod>
235+
234236
=over 4
235237

236-
=item *
238+
=item Memory Allocation in C/XS
239+
240+
Documentation was updated to reflect that mixing C<Newx>, C<Renew>, and
241+
C<Safefree> vs C<malloc>, C<realloc>, and C<free> are not allowed, and mixing
242+
pointers between the 2 classes of APIs is not allowed. Updates made in
243+
F<pod/perlguts.pod> and F<pod/perlclib.pod>.
244+
245+
=back
246+
247+
=over 4
237248

249+
=item
238250
Added the change note for apostrophes in names being reinstated, which
239251
was delayed by the review cycle.
240252

pod/perlguts.pod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2384,7 +2384,10 @@ marked with correct flags.
23842384
All memory meant to be used with the Perl API functions should be manipulated
23852385
using the macros described in this section. The macros provide the necessary
23862386
transparency between differences in the actual malloc implementation that is
2387-
used within perl.
2387+
used within perl. Never pass pointers between C<Newx>, C<Renew>, C<Safefree>
2388+
and I<libc> equivalents C<malloc>, C<realloc>, C<free>. They are not from the
2389+
same memory pool or allocator. Either an instant or delayed I<SEGV> will
2390+
occur, or subtle memory leaks or subtle heap corruption.
23882391

23892392
The following three macros are used to initially allocate memory :
23902393

0 commit comments

Comments
 (0)