/
unsafe.go
163 lines (135 loc) · 2.76 KB
/
unsafe.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gc
import "cmd/internal/obj"
// look for
// unsafe.Sizeof
// unsafe.Offsetof
// unsafe.Alignof
// rewrite with a constant
func unsafenmagic(nn *Node) *Node {
fn := nn.Left
args := nn.List
if safemode != 0 || fn == nil || fn.Op != ONAME {
return nil
}
s := fn.Sym
if s == nil {
return nil
}
if s.Pkg != unsafepkg {
return nil
}
if args == nil {
Yyerror("missing argument for %v", s)
return nil
}
r := args.N
var v int64
if s.Name == "Sizeof" {
typecheck(&r, Erv)
defaultlit(&r, nil)
tr := r.Type
if tr == nil {
goto bad
}
dowidth(tr)
v = tr.Width
goto yes
}
if s.Name == "Offsetof" {
// must be a selector.
if r.Op != OXDOT {
goto bad
}
// Remember base of selector to find it back after dot insertion.
// Since r->left may be mutated by typechecking, check it explicitly
// first to track it correctly.
typecheck(&r.Left, Erv)
base := r.Left
typecheck(&r, Erv)
switch r.Op {
case ODOT, ODOTPTR:
break
case OCALLPART:
Yyerror("invalid expression %v: argument is a method value", nn)
v = 0
goto ret
default:
goto bad
}
v = 0
// add offsets for inserted dots.
var r1 *Node
for r1 = r; r1.Left != base; r1 = r1.Left {
switch r1.Op {
case ODOT:
v += r1.Xoffset
case ODOTPTR:
Yyerror("invalid expression %v: selector implies indirection of embedded %v", nn, r1.Left)
goto ret
default:
Dump("unsafenmagic", r)
Fatalf("impossible %v node after dot insertion", Oconv(int(r1.Op), obj.FmtSharp))
goto bad
}
}
v += r1.Xoffset
goto yes
}
if s.Name == "Alignof" {
typecheck(&r, Erv)
defaultlit(&r, nil)
tr := r.Type
if tr == nil {
goto bad
}
// make struct { byte; T; }
t := typ(TSTRUCT)
t.Type = typ(TFIELD)
t.Type.Type = Types[TUINT8]
t.Type.Down = typ(TFIELD)
t.Type.Down.Type = tr
// compute struct widths
dowidth(t)
// the offset of T is its required alignment
v = t.Type.Down.Width
goto yes
}
return nil
bad:
Yyerror("invalid expression %v", nn)
v = 0
goto ret
yes:
if args.Next != nil {
Yyerror("extra arguments for %v", s)
}
// any side effects disappear; ignore init
ret:
var val Val
val.U = new(Mpint)
Mpmovecfix(val.U.(*Mpint), v)
n := Nod(OLITERAL, nil, nil)
n.Orig = nn
n.SetVal(val)
n.Type = Types[TUINTPTR]
nn.Type = Types[TUINTPTR]
return n
}
func isunsafebuiltin(n *Node) bool {
if n == nil || n.Op != ONAME || n.Sym == nil || n.Sym.Pkg != unsafepkg {
return false
}
if n.Sym.Name == "Sizeof" {
return true
}
if n.Sym.Name == "Offsetof" {
return true
}
if n.Sym.Name == "Alignof" {
return true
}
return false
}