File Coverage

blib/lib/Test2/Plugin/Wasm.pm
Criterion Covered Total %
statement 41 56 73.2
branch 8 18 44.4
condition n/a
subroutine 9 10 90.0
pod 0 1 0.0
total 58 85 68.2


line stmt bran cond sub pod time code
1             package Test2::Plugin::Wasm;
2              
3 11     11   2533032 use strict;
  11         83  
  11         321  
4 11     11   62 use warnings;
  11         21  
  11         255  
5 11     11   284 use 5.008004;
  11         46  
6 11     11   56 use Test2::API qw( context );
  11         20  
  11         520  
7 11     11   76 use Test2::Mock;
  11         21  
  11         301  
8 11     11   6989 use Data::Dumper ();
  11         71493  
  11         360  
9 11     11   5159 use FFI::C::Util qw( c_to_perl );
  11         93392  
  11         5457  
10              
11             # ABSTRACT: Test2 plugin for WebAssembly extensions
12             our $VERSION = '0.22'; # VERSION
13              
14              
15             sub get_virtual_memory_limit
16             {
17 11     11 0 38 my $ctx = context();
18 11 50       788 if($^O eq 'linux')
19             {
20 11         7862 require FFI::Platypus;
21 11         92805 require FFI::C::StructDef;
22 11         209427 my $ffi = FFI::Platypus->new( api => 1, lib => [undef] );
23 11         13523 my $rlimit;
24 11 50       54 if($ffi->find_symbol('getrlimit'))
25             {
26 11 50       780 $ctx->note("linux : found getrlimit") if $ENV{TEST2_PLUGIN_WASM_DEBUG};
27 11         102 $rlimit = FFI::C::StructDef->new(
28             $ffi,
29             name => 'rlimit',
30             members => [
31             rlim_cur => 'uint32',
32             rlim_max => 'uint32',
33             ],
34             )->create;
35 11         19803 my $ret = $ffi->function( getrlimit => [ 'int', 'rlimit' ] => 'int' )->call(9,$rlimit);
36 11 50       2341 if($ret == -1)
37             {
38 0 0       0 $ctx->note("getrlimit failed: $!") if $ENV{TEST2_PLUGIN_WASM_DEBUG};
39 0         0 undef $rlimit;
40             }
41             }
42 11 50       61 if(defined $rlimit)
43             {
44 11         111 my $cur = $rlimit->rlim_cur;
45             $ctx->note("rlimit = " . Data::Dumper->new(
46             [c_to_perl($rlimit)])->Terse(1)->Indent(0)->Dump
47 11 50       1793 ) if $ENV{TEST2_PLUGIN_WASM_DEBUG};
48 11         99 $ctx->release;
49 11 50       227 $cur = 0 if $cur == 0xffffffff;
50 11         51 return $cur;
51             }
52             }
53 0         0 $ctx->release;
54 0         0 return undef;
55             }
56              
57             our $config_mock;
58              
59             sub import
60             {
61 11     11   156 my $ctx = context();
62 11         64310 my $vm_limit = get_virtual_memory_limit();
63 11 50       271 if($vm_limit)
64             {
65 0         0 require Wasm::Wasmtime::Config;
66             $config_mock = Test2::Mock->new(
67             class => 'Wasm::Wasmtime::Config',
68             around => [
69             new => sub {
70 0     0   0 my $orig = shift;
71 0         0 my $self = shift->$orig(@_);
72 0         0 my $ctx = context();
73 0         0 $ctx->note("virtual memory address limit detected, try to set limits to zero");
74 0         0 $self->static_memory_maximum_size(0);
75 0         0 $self->static_memory_guard_size(0);
76 0         0 $self->dynamic_memory_guard_size(0);
77 0         0 $ctx->release;
78 0         0 $self;
79             },
80 0         0 ],
81             );
82             }
83 11         66 $ctx->release;
84             }
85              
86             1;
87              
88             __END__