petdance/scratch — Issue #799: freeze opcode segfaults on working bytecode from Rakudo
Author: petdance
Created: 2011-11-14 06:13:34+00:00
State: open
Labels: bug, core, Pri-normal, Sev-medium

The following compiles bytecode from PIR and perl6 source strings and verifies that a subroutine works in both cases. The PIR example can be frozen (and thawed in other tests) but the perl6 example causes freeze to segfault. Tested only on linux/amd64. Fixing this would be assist a project to build a versioned module database for Rakudo (Synopsis 11).
```
# freeze-test.pir
# Assumes parrot was installed by Rakudo's Configure --gen-parrot
# - edit the load_bytecode line otherwise.

.sub 'testmain' :main
.local string testPIRsource
testPIRsource = <<' testPIRsource_end'
.sub test_PIR
.param string x
.local string result
result = 'test PIR '
result .= x
.return ( result )
.end
testPIRsource_end
.local pmc PIRcompiler, testPIRbytecode, parrotnamespace, test_PIR
PIRcompiler = compreg 'PIR' # parrot/docs/pdds/draft/pdd06_pasm.pod
testPIRbytecode = PIRcompiler( testPIRsource )
parrotnamespace = get_root_namespace [ 'parrot' ]
test_PIR = parrotnamespace.'find_sub' ('test_PIR')
$S0 = test_PIR ( '10 b' ) # execute the generated bytecode
print $S0
say ' PIR code compiled, ready to be frozen'

.local string testP6source
testP6source = <<' testP6source_end'
sub test_P6 ( Str $x ) {
return "test P6 $x";
}
testP6source_end
# the next line assumes rakudo configured with --gen-parrot
load_bytecode '../perl6.pbc' # adjust directory if different
.local pmc P6compiler, testP6bytecode, P6namespace, test_P6
# see similar code in .sub 'eval' in rakudo/src/builtins/control.pir
P6compiler = compreg 'perl6'
testP6bytecode = P6compiler.'compile' ( testP6source )
P6namespace = get_root_namespace [ 'perl6' ]
test_P6 = P6namespace.'find_sub' ('test_P6')
$S0 = test_P6 ( '10 c' )
print $S0
say ' Perl 6 code compiles, ready to be frozen'

.local string frozenPIR, filename
frozenPIR = freeze testPIRbytecode
filename = "/tmp/bytecode_from_PIR.frozen"
.local pmc filehandle
filehandle = open filename, "w"
print filehandle, frozenPIR
close filehandle
$I0 = stat filename, 1 # 1 means get file size
print $I0
say ' bytes frozen from PIR'

.local string frozenP6
# Uncomment the next line to see the problem
# frozenP6 = freeze testP6bytecode # segfaults
filename = "/tmp/bytecode_from_P6.frozen"
filehandle = open filename, "w"
print filehandle, frozenP6
close filehandle
$I0 = stat filename, 1 # 1 means get file size
print $I0
say ' bytes frozen from Perl 6'

.end

# Original problem encountered in:
# http://gitorious.org/parrot-module-lib/main/blobs/master/t/10-make-bytecode.t
```

Originally http://trac.parrot.org/parrot/ticket/802 by @mberends