]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/dist: zero output variables on entry to goc2c functions
authorRuss Cox <rsc@golang.org>
Thu, 27 Mar 2014 18:05:31 +0000 (14:05 -0400)
committerRuss Cox <rsc@golang.org>
Thu, 27 Mar 2014 18:05:31 +0000 (14:05 -0400)
Zeroing the outputs makes sure that during function calls
in those functions we do not let the garbage collector
treat uninitialized values as pointers.

The garbage collector may still see uninitialized values
if a preemption occurs during the function prologue,
before the zeroing has had a chance to run.

This reduces the number of 'bad pointer' messages when
that runtime check is enabled, but it doesn't fix all of them,
so the check is still disabled.

It will also avoid leaks, although I doubt any of these were
particularly serious.

LGTM=iant, khr
R=iant, khr
CC=golang-codereviews
https://golang.org/cl/80850044

src/cmd/dist/goc2c.c

index 36b43f56eb3b9a5de969d20c8740a3f4ebbbb2a2..38627657ec146a9c6dad01878aae1f2092394cc6 100644 (file)
@@ -524,6 +524,7 @@ write_6g_func_header(char *package, char *name, struct params *params,
                     int paramwid, struct params *rets)
 {
        int first, n;
+       struct params *p;
 
        bwritef(output, "void\n");
        if(!contains(name, "ยท"))
@@ -546,6 +547,24 @@ write_6g_func_header(char *package, char *name, struct params *params,
 
        write_params(rets, &first);
        bwritef(output, ")\n{\n");
+       
+       for (p = rets; p != nil; p = p->next) {
+               if(streq(p->name, "..."))
+                       continue;
+               if(streq(p->type, "Slice"))
+                       bwritef(output, "\t%s.array = 0;\n\t%s.len = 0;\n\t%s.cap = 0;\n", p->name, p->name, p->name);
+               else if(streq(p->type, "String"))
+                       bwritef(output, "\t%s.str = 0;\n\t%s.len = 0;\n", p->name, p->name);
+               else if(streq(p->type, "Eface"))
+                       bwritef(output, "\t%s.type = 0;\n\t%s.data = 0;\n", p->name, p->name);
+               else if(streq(p->type, "Iface"))
+                       bwritef(output, "\t%s.tab = 0;\n\t%s.data = 0;\n", p->name, p->name);
+               else if(streq(p->type, "Complex128"))
+                       bwritef(output, "\t%s.real = 0;\n\t%s.imag = 0;\n", p->name, p->name);
+               else
+                       bwritef(output, "\t%s = 0;\n", p->name);
+               bwritef(output, "\tFLUSH(&%s);\n", p->name);
+       }
 }
 
 /* Write a 6g function trailer.  */