I found a potential issue with vstring usage in r_assemble_list() in 
cgemit.cxx.

The opnd[] and result[] are storing addresses off a vstring 'buf' while 
it is being appended. If one of the 'append' triggers a realloc and new 
block has to be alloc'ed, the old pointers in opnd[] and result[] become 
stale. This can lead to garbage printed in the output asm files.

Below is one possible way to fix it by storing the offsets instead of 
addresses while 'buf' is growing.

Our code has diverged from open64 so much, I do not have an environment 
setup to build/test the trunk. I merely point it out since it could bite 
someone later. If this needs to be fixed, we need an volunteer to pick 
it up and review/test the change and submit it.

Regards,
Ding-Kai


Index: cgemit.cxx
===================================================================
--- cgemit.cxx    (revision 3360)
+++ cgemit.cxx    (working copy)
@@ -1778,10 +1778,13 @@
  {
  #ifdef TARG_X8664
    const char *result[ISA_OPERAND_max_results+1];
+  UINT result_str_offset[ISA_OPERAND_max_results+1];
  #else
    const char *result[ISA_OPERAND_max_results];
+  UINT result_str_offset[ISA_OPERAND_max_results];
  #endif
    const char *opnd[ISA_OPERAND_max_operands];
+  UINT opnd_str_offset[ISA_OPERAND_max_results];
    vstring buf = vstr_begin(LBUF_LEN);
    INT i;
    INT lc = 0;
@@ -1838,7 +1841,7 @@
      }
      // need end-of-string between each operand
      buf = vstr_append(buf, '\0');
-    opnd[i] = vstr_str(buf)+start;
+    opnd_str_offset[i] = start;
    }

    for (i = 0; i < OP_results(op); i++) {
@@ -1871,9 +1874,16 @@
      }
      buf = vstr_concat(buf, rname);
      buf = vstr_append(buf, '\0');    // increment vstr length
-    result[i] = vstr_str(buf)+start;
+    result_str_offset[i] = start;
    }

+  const char* vstr_buf = vstr_str(buf);
+  for (i = 0; i < OP_opnds(op); i++) {
+    opnd[i] = opnd_str_offset[i]+vstr_buf;
+  }
+  for (i = 0; i < OP_results(op); i++) {
+    result[i] = result_str_offset[i]+vstr_buf;
+  }
    fputc ('\t', Asm_File);
  #ifdef TARG_X8664
    lc = CGEMIT_Print_Inst( op, result, opnd, Asm_File );


------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
Open64-devel mailing list
Open64-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/open64-devel

Reply via email to