Skip to content

Fix OP_GETGV symbol encoding for $&, $\, $', $1..$9` #10

Open
hadashiA wants to merge 1 commit into
picoruby:masterfrom
hadashiA:fix-special-var-encoding
Open

Fix OP_GETGV symbol encoding for $&, $\, $', $1..$9` #10
hadashiA wants to merge 1 commit into
picoruby:masterfrom
hadashiA:fix-special-var-encoding

Conversation

@hadashiA
Copy link
Copy Markdown
Contributor

Summary

codegen_prism.inc mis-encodes two kinds of global-variable reads in
OP_GETGV operands:

  1. Back-references ($&, $`, $', $+) all use
    MRC_SYM_2(back_ref), the presym bound to the literal "$+", so
    every back-reference collapses to "$+" in the dumped bytecode.

  2. Numbered references ($1..$9) build the name in a local stack
    buffer and hand the pointer to pm_constant_pool_insert_constant,
    which stores the pointer without copying. After the codegen
    function returns the buffer is reused, so the dumped symbol bytes
    are garbage (we observed \x08G for $1).

Reproduce

/world/ =~ 'hello world'
p $`, $&, $+, $1
$ mrbc -v repro.rb                                                  
... GETGV  R2  $+    # was $` in source
... GETGV  R2  $+    # was $& in source
... GETGV  R2  $+
... GETGV  R2  G     # garbage; was $1 in source                                                                                                                                                                            

Fix

  • Back-reference: use cast->name (the Prism constant_id for the actual
    token bytes — already in the pool).
  • Numbered reference: mrc_malloc a heap copy and hand ownership to the
    pool via pm_constant_pool_insert_owned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant