1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
12 // The instrument pass modifies the code tree for instrumentation.
14 // For flag_race it modifies the function as follows:
16 // 1. It inserts a call to racefuncenter at the beginning of each function.
17 // 2. It inserts a call to racefuncexit at the end of each function.
18 // 3. It inserts a call to raceread before each memory read.
19 // 4. It inserts a call to racewrite before each memory write.
23 // 1. It inserts a call to msanread before each memory read.
24 // 2. It inserts a call to msanwrite before each memory write.
26 // The rewriting is not yet complete. Certain nodes are not rewritten
29 // TODO(dvyukov): do not instrument initialization as writes:
30 // a := make([]int, 10)
32 // Do not instrument the following packages at all,
33 // at best instrumentation would cause infinite recursion.
34 var omit_pkgs = []string{"runtime/internal/atomic", "runtime/internal/sys", "runtime", "runtime/race", "runtime/msan"}
36 // Only insert racefuncenter/racefuncexit into the following packages.
37 // Memory accesses in the packages are either uninteresting or will cause false positives.
38 var norace_inst_pkgs = []string{"sync", "sync/atomic"}
40 func ispkgin(pkgs []string) bool {
41 if myimportpath != "" {
42 for _, p := range pkgs {
43 if myimportpath == p {
52 func instrument(fn *Node) {
53 if ispkgin(omit_pkgs) || fn.Func.Norace {
57 if flag_race == 0 || !ispkgin(norace_inst_pkgs) {
58 instrumentlist(fn.Nbody, nil)
60 // nothing interesting for race detector in fn->enter
61 instrumentlist(fn.Func.Exit, nil)
65 // nodpc is the PC of the caller as extracted by
66 // getcallerpc. We use -widthptr(FP) for x86.
67 // BUG: this will not work on arm.
68 nodpc := Nod(OXXX, nil, nil)
71 nodpc.Type = Types[TUINTPTR]
72 nodpc.Xoffset = int64(-Widthptr)
73 nd := mkcall("racefuncenter", nil, nil, nodpc)
74 fn.Func.Enter = concat(list1(nd), fn.Func.Enter)
75 nd = mkcall("racefuncexit", nil, nil)
76 fn.Func.Exit = list(fn.Func.Exit, nd)
80 s := fmt.Sprintf("after instrument %v", fn.Func.Nname.Sym)
82 s = fmt.Sprintf("enter %v", fn.Func.Nname.Sym)
83 dumplist(s, fn.Func.Enter)
84 s = fmt.Sprintf("exit %v", fn.Func.Nname.Sym)
85 dumplist(s, fn.Func.Exit)
89 func instrumentlist(l *NodeList, init **NodeList) {
92 for ; l != nil; l = l.Next {
94 instrumentnode(&l.N, &instr, 0, 0)
96 l.N.Ninit = concat(l.N.Ninit, instr)
98 *init = concat(*init, instr)
103 // walkexpr and walkstmt combined
104 // walks the tree and adds calls to the
105 // instrumentation code to top-level (statement) nodes' init
106 func instrumentnode(np **Node, init **NodeList, wr int, skip int) {
114 Dump("instrument-before", n)
118 Fatalf("instrument: bad init list")
120 if init == &n.Ninit {
121 // If init == &n->ninit and n->ninit is non-nil,
122 // instrumentnode might append it to itself.
123 // nil it out and handle it separately before putting it back.
127 instrumentlist(l, nil)
128 instrumentnode(&n, &l, wr, skip) // recurse with nil n->ninit
134 instrumentlist(n.Ninit, nil)
138 Fatalf("instrument: unknown node type %v", Oconv(int(n.Op), 0))
140 case OAS, OASWB, OAS2FUNC:
141 instrumentnode(&n.Left, init, 1, 0)
142 instrumentnode(&n.Right, init, 0, 0)
146 case OCFUNC, OVARKILL:
151 for l := n.List; l != nil; l = l.Next {
153 case OCALLFUNC, OCALLMETH, OCALLINTER:
154 instrumentnode(&l.N, &out, 0, 0)
156 // Scan past OAS nodes copying results off stack.
157 // Those must not be instrumented, because the
158 // instrumentation calls will smash the results.
159 // The assignments are to temporaries, so they cannot
160 // be involved in races and need not be instrumented.
161 for l.Next != nil && l.Next.N.Op == OAS && iscallret(l.Next.N.Right) {
166 instrumentnode(&l.N, &out, 0, 0)
174 instrumentnode(&n.Left, init, 0, 0)
178 instrumentnode(&n.Left, init, 0, 0)
182 instrumentnode(&n.Left, init, 0, 0)
185 // Instrument dst argument of runtime.writebarrier* calls
186 // as we do not instrument runtime code.
187 // typedslicecopy is instrumented in runtime.
189 instrumentnode(&n.Left, init, 0, 0)
199 instrumentnode(&n.Left, init, wr, 0)
203 instrumentnode(&n.Left, init, 0, 0)
207 instrumentnode(&n.Left, init, 0, 1)
208 callinstr(&n, init, wr, skip)
211 case ODOTPTR: // dst = (*x).f with implicit *; otherwise it's ODOT+OIND
212 instrumentnode(&n.Left, init, 0, 0)
214 callinstr(&n, init, wr, skip)
218 instrumentnode(&n.Left, init, 0, 0)
220 callinstr(&n, init, wr, skip)
223 case OSPTR, OLEN, OCAP:
224 instrumentnode(&n.Left, init, 0, 0)
225 if Istype(n.Left.Type, TMAP) {
226 n1 := Nod(OCONVNOP, n.Left, nil)
227 n1.Type = Ptrto(Types[TUINT8])
228 n1 = Nod(OIND, n1, nil)
230 callinstr(&n1, init, 0, skip)
253 instrumentnode(&n.Left, init, wr, 0)
254 instrumentnode(&n.Right, init, wr, 0)
258 instrumentnode(&n.Left, init, wr, 0)
260 // walk has ensured the node has moved to a location where
261 // side effects are safe.
262 // n->right may not be executed,
263 // so instrumentation goes to n->right->ninit, not init.
264 instrumentnode(&n.Right, &n.Right.Ninit, wr, 0)
269 callinstr(&n, init, wr, skip)
273 instrumentnode(&n.Left, init, wr, 0)
277 instrumentnode(&n.Left, init, wr, 0)
281 instrumentnode(&n.Left, init, wr, 0)
282 instrumentnode(&n.Right, init, wr, 0)
286 if !Isfixedarray(n.Left.Type) {
287 instrumentnode(&n.Left, init, 0, 0)
288 } else if !islvalue(n.Left) {
289 // index of unaddressable array, like Map[k][i].
290 instrumentnode(&n.Left, init, wr, 0)
292 instrumentnode(&n.Right, init, 0, 0)
296 instrumentnode(&n.Right, init, 0, 0)
297 if n.Left.Type.Etype != TSTRING {
298 callinstr(&n, init, wr, skip)
302 case OSLICE, OSLICEARR, OSLICE3, OSLICE3ARR, OSLICESTR:
303 instrumentnode(&n.Left, init, 0, 0)
304 instrumentnode(&n.Right, init, 0, 0)
308 instrumentnode(&n.Left, init, 0, 0)
309 instrumentnode(&n.Right, init, 0, 0)
313 instrumentnode(&n.Left, init, 0, 1)
316 // n->left is Type* which is not interesting.
318 instrumentnode(&n.Right, init, 0, 0)
323 instrumentnode(&n.Left, init, 0, 0)
326 // should not appear in AST by now
358 OCLOSURE, // lowered to PTRLIT
359 ORANGE, // lowered to ordinary for loop
360 OARRAYLIT, // lowered to assignments
367 Yyerror("instrument: %v must be lowered by now", Oconv(int(n.Op), 0))
371 // impossible nodes: only appear in backend.
372 case ORROTC, OEXTEND:
373 Yyerror("instrument: %v cannot exist now", Oconv(int(n.Op), 0))
377 Yyerror("instrument: OGETG can happen only in runtime which we don't instrument")
382 instrumentnode(&n.Left, &n.Left.Ninit, 0, 0)
385 instrumentnode(&n.Right, &n.Right.Ninit, 0, 0)
391 instrumentnode(&n.Left, &n.Left.Ninit, 0, 0)
395 // just do generic traversal
408 // does not require instrumentation
409 case OPRINT, // don't bother instrumenting it
410 OPRINTN, // don't bother instrumenting it
411 OCHECKNIL, // always followed by a read.
412 OPARAM, // it appears only in fn->exit to copy heap params back
413 OCLOSUREVAR, // immutable pointer to captured variable
414 ODOTMETH, // either part of CALLMETH or CALLPART (lowered to PTRLIT)
415 OINDREG, // at this stage, only n(SP) nodes from nodarg
416 ODCL, // declarations (without value) cannot be races
422 OTYPESW: // ignored by code generation, do not instrument.
427 if n.Op != OBLOCK { // OBLOCK is handled above in a special way.
428 instrumentlist(n.List, init)
430 instrumentlist(n.Nbody, nil)
431 instrumentlist(n.Rlist, nil)
435 func isartificial(n *Node) bool {
436 // compiler-emitted artificial things that we do not want to instrument,
437 // can't possibly participate in a data race.
438 // can't be seen by C/C++ and therefore irrelevant for msan.
439 if n.Op == ONAME && n.Sym != nil && n.Sym.Name != "" {
440 if n.Sym.Name == "_" {
444 // autotmp's are always local
445 if strings.HasPrefix(n.Sym.Name, "autotmp_") {
449 // statictmp's are read-only
450 if strings.HasPrefix(n.Sym.Name, "statictmp_") {
454 // go.itab is accessed only by the compiler and runtime (assume safe)
455 if n.Sym.Pkg != nil && n.Sym.Pkg.Name != "" && n.Sym.Pkg.Name == "go.itab" {
463 func callinstr(np **Node, init **NodeList, wr int, skip int) bool {
466 //print("callinstr for %+N [ %O ] etype=%E class=%d\n",
467 // n, n->op, n->type ? n->type->etype : -1, n->class);
469 if skip != 0 || n.Type == nil || n.Type.Etype >= TIDEAL {
479 // it skips e.g. stores to ... parameter array
485 // BUG: we _may_ want to instrument PAUTO sometimes
486 // e.g. if we've got a local variable/method receiver
487 // that has got a pointer inside. Whether it points to
488 // the heap or not is impossible to know at compile time
489 if (class&PHEAP != 0) || class == PPARAMREF || class == PEXTERN || b.Op == OINDEX || b.Op == ODOTPTR || b.Op == OIND {
491 foreach(n, hascallspred, &hascalls)
493 n = detachexpr(n, init)
505 // dowidth may not have been called for PEXTERN.
509 Fatalf("instrument: %v badwidth", t)
511 f = mkcall(name, nil, init, uintptraddr(n), Nodintconst(w))
512 } else if flag_race != 0 && (t.Etype == TSTRUCT || Isfixedarray(t)) {
513 name := "racereadrange"
515 name = "racewriterange"
517 // dowidth may not have been called for PEXTERN.
521 Fatalf("instrument: %v badwidth", t)
523 f = mkcall(name, nil, init, uintptraddr(n), Nodintconst(w))
524 } else if flag_race != 0 {
529 f = mkcall(name, nil, init, uintptraddr(n))
532 *init = list(*init, f)
539 // makeaddable returns a node whose memory location is the
540 // same as n, but which is addressable in the Go language
542 // This is different from functions like cheapexpr that may make
543 // a copy of their argument.
544 func makeaddable(n *Node) {
545 // The arguments to uintptraddr technically have an address but
546 // may not be addressable in the Go sense: for example, in the case
547 // of T(v).Field where T is a struct type and v is
548 // an addressable value.
551 if Isfixedarray(n.Left.Type) {
555 // Turn T(v).Field into v.Field
557 if n.Left.Op == OCONVNOP {
570 func uintptraddr(n *Node) *Node {
571 r := Nod(OADDR, n, nil)
573 r = conv(r, Types[TUNSAFEPTR])
574 r = conv(r, Types[TUINTPTR])
578 func detachexpr(n *Node, init **NodeList) *Node {
579 addr := Nod(OADDR, n, nil)
580 l := temp(Ptrto(n.Type))
581 as := Nod(OAS, l, addr)
584 *init = list(*init, as)
585 ind := Nod(OIND, l, nil)
591 func foreachnode(n *Node, f func(*Node, interface{}), c interface{}) {
597 func foreachlist(l *NodeList, f func(*Node, interface{}), c interface{}) {
598 for ; l != nil; l = l.Next {
599 foreachnode(l.N, f, c)
603 func foreach(n *Node, f func(*Node, interface{}), c interface{}) {
604 foreachlist(n.Ninit, f, c)
605 foreachnode(n.Left, f, c)
606 foreachnode(n.Right, f, c)
607 foreachlist(n.List, f, c)
608 foreachlist(n.Nbody, f, c)
609 foreachlist(n.Rlist, f, c)
612 func hascallspred(n *Node, c interface{}) {
614 case OCALL, OCALLFUNC, OCALLMETH, OCALLINTER:
619 // appendinit is like addinit in subr.go
620 // but appends rather than prepends.
621 func appendinit(np **Node, init *NodeList) {
628 // There may be multiple refs to this node;
629 // introduce OCONVNOP to hold init list.
630 case ONAME, OLITERAL:
631 n = Nod(OCONVNOP, n, nil)
638 n.Ninit = concat(n.Ninit, init)