Skip to content

Commit

Permalink
Fix reproducible build issue caused by changing memory addresses of l…
Browse files Browse the repository at this point in the history
…abels

When a QASTCompilerInstance is somehow referenced (directly or indirectly) by
compiled bytecode, this pulls in references to frames and thus the frames'
%!labels lookup hash which was keyed on the nqp::objectid of those labels.
Since nqp::objectid uses memory addresses and those may change from run to
run, this could lead to differences in build results. Use an incremented id
counter instead, just like we do for QAST::Block's cuids.
  • Loading branch information
niner committed Jul 17, 2021
1 parent 07f6ddd commit df9e48e
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions lib/MAST/Nodes.nqp
Expand Up @@ -410,12 +410,29 @@ module Arg {
# Labels (used directly in the instruction stream indicates where the
# label goes; can also be used as an instruction operand).
class MAST::Label is MAST::Node {
my int $cur_id := 0;
my $cur_id_lock := NQPLock.new;
has $!id;
method new() {
my $label := nqp::create(self);

nqp::lock($cur_id_lock);
$cur_id := $cur_id + 1;
nqp::unlock($cur_id_lock);
$label.set-id(~$cur_id);

$*MAST_FRAME.keep-label($label);
$label
}

method set-id($id) {
$!id := $id;
}

method id() {
$!id
}

method dump_lines(@lines, $indent) {
my int $addr := nqp::where(self);
nqp::push(@lines, $indent ~ "MAST::Label <$addr>");
Expand Down Expand Up @@ -1139,7 +1156,7 @@ class MAST::Frame is MAST::Node {
method labels() { %!labels }
method label-fixups() { %!label-fixups }
method resolve-label($label) {
%!labels{nqp::objectid($label)}
%!labels{$label.id}
}

method keep-label(MAST::Label $l) {
Expand All @@ -1148,7 +1165,7 @@ class MAST::Frame is MAST::Node {

method add-label(MAST::Label $i) {
my int $pos := nqp::elems($!bytecode);
my str $key := nqp::objectid($i);
my str $key := $i.id;
if %!labels{$key} {
nqp::die("Duplicate label at $pos");
}
Expand All @@ -1172,7 +1189,7 @@ class MAST::Frame is MAST::Node {
$Arg::obj # $MVM_reg_obj := 8;
];
method compile_label($bytecode, $arg) {
my $key := nqp::objectid($arg);
my $key := $arg.id;
my %labels := self.labels;
nqp::push_i(@!child-label-fixups, nqp::elems($!bytecode));
if nqp::existskey(%labels, $key) {
Expand Down

0 comments on commit df9e48e

Please sign in to comment.