Skip to content

Commit

Permalink
Set builtin:: globs to readonly so that attempted replacements of the…
Browse files Browse the repository at this point in the history
…m do not break optimisations
  • Loading branch information
leonerd committed Oct 10, 2024
1 parent 9acaa98 commit 706e8be
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 1 deletion.
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -6080,6 +6080,7 @@ t/lib/common.pl Helper for lib/{warnings,feature}.t
t/lib/commonsense.t See if configuration meets basic needs
t/lib/Count.pm Helper for t/op/method.t
t/lib/croak.t Test calls to Perl_croak() in the C source.
t/lib/croak/builtin Test croak calls from builtin.c
t/lib/croak/class Test croak calls from class.c
t/lib/croak/gv Test croak calls from gv.c
t/lib/croak/mg Test croak calls from mg.c
Expand Down
7 changes: 7 additions & 0 deletions builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,13 @@ Perl_boot_core_builtin(pTHX)
if(builtin->checker) {
cv_set_call_checker_flags(cv, builtin->checker, newSVuv(PTR2UV(builtin)), 0);
}

/* Because of all these callcheckers and other optimisations, it would
* all break if we permitted runtime replacement of the functions,
* e.g. by glob tricks like `*builtin::reftype = sub { ... }`.
* Prevent modification of the GV so as to avoid this problem.
*/
SvREADONLY_on((SV *)CvGV(cv));
}

newXS_flags("builtin::import", &XS_builtin_import, __FILE__, NULL, 0);
Expand Down
12 changes: 11 additions & 1 deletion lib/builtin.pm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package builtin 0.015;
package builtin 0.016;

use v5.40;

Expand Down Expand Up @@ -106,6 +106,16 @@ The following bundles currently exist:
:5.40 true false weaken unweaken is_weak blessed refaddr reftype
ceil floor is_tainted trim indexed
=head2 Read-only Functions
Various optimisations that apply to many functions in the L<builtin> package
would be broken if the functions are ever replaced or changed, such as by
assignment into glob references. Because of this, the globs that contain
them are set read-only since Perl version 5.41.5, preventing such replacement.
$ perl -e '*builtin::reftype = sub { "BOO" }'
Modification of a read-only value attempted at -e line 1.
=head1 FUNCTIONS
=head2 true
Expand Down
10 changes: 10 additions & 0 deletions pod/perldelta.pod
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ here, but most should go in the L</Performance Enhancements> section.

[ List each enhancement as a =head2 entry ]

=head2 Functions in the C<builtin::> package no longer permit replacement

Various optimisations that apply to many functions in the L<builtin> package
would be broken if the functions are ever replaced or changed, such as by
assignment into glob references. Because of this, the globs that contain
them are now set read-only, preventing such replacement.

$ perl -e '*builtin::reftype = sub { "BOO" }'
Modification of a read-only value attempted at -e line 1.

=head1 Security

XXX Any security-related notices go here. In particular, any security
Expand Down
6 changes: 6 additions & 0 deletions t/lib/croak/builtin
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
__END__
########
# Attempted replacement of builtin:: functions is not allowed as of Perl 5.41.5
*builtin::reftype = sub { "BOO" };
EXPECT
Modification of a read-only value attempted at - line 2.

0 comments on commit 706e8be

Please sign in to comment.