Skip to content

Commit

Permalink
Fix optimizer interfering with some native subs
Browse files Browse the repository at this point in the history
When a native sub has only native arguments and is declared as "is native"
without args (i.e. already loaded library like libc) and has a very simple
function body like {} or { * } but not { ... } or other stub markers, the
optimizer could statically inline the original function body, circumventing
the call to the native function.

Mark all native subs as soft, to prevent static inlining.
Fixes GH #3244
  • Loading branch information
niner committed Oct 20, 2019
1 parent 9b181a7 commit d662912
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lib/NativeCall.pm6
Expand Up @@ -592,6 +592,8 @@ our role Native[Routine $r, $libname where Str|Callable|List|IO::Path|Distributi
nqp::bindattr(self, Code, '$!do', $do);
nqp::setcodename($do, $!name);
}

method soft(--> True) {} # prevent inlining of the original function body
}

multi sub postcircumfix:<[ ]>(CArray:D \array, $pos) is raw is export(:DEFAULT, :types) is default {
Expand Down
14 changes: 11 additions & 3 deletions t/04-nativecall/00-misc.t
Expand Up @@ -5,7 +5,7 @@ use Test::Helpers;
use CompileTestLib;
compile_test_lib '00-misc';

plan 3;
plan 4;

{ # https://github.com/rakudo/rakudo/issues/3235
role Foo {
Expand All @@ -15,13 +15,19 @@ plan 3;
}
};

is Foo.test, 3;
is Foo.test, 3, "body of a native sub declared in a role body replaced";

my &NCstrlen := BEGIN {
sub NCstrlen(Str --> int32) is native('./00-misc') { !!! };
};

is NCstrlen('123'), 3;
is NCstrlen('123'), 3, "body of a native sub declared in a BEGIN block replaced";
}

unless $*DISTRO.is-win { # https://github.com/rakudo/rakudo/issues/3244
# Test needs native arguments, an "is native" without args and an empty body
sub abs(int32 --> int32) is native {};
is abs(-1), 1, "optimizer doesn't inline the native sub's original body";
}

{ # https://github.com/rakudo/rakudo/issues/1576
Expand All @@ -37,3 +43,5 @@ plan 3;
:compiler-args[«-I "$dir.absolute()" -MFoo»], :out("3\n5\n7\n9\n"),
'no segfaults when using NC routine after using it during precomp';
}

# vim:ft=perl6

0 comments on commit d662912

Please sign in to comment.