-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyzer.go
More file actions
357 lines (319 loc) · 8.67 KB
/
analyzer.go
File metadata and controls
357 lines (319 loc) · 8.67 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"go/types"
"reflect"
"unsafe"
)
// FieldLayout represents the memory layout of a single struct field
type FieldLayout struct {
Name string `json:"name"`
Type string `json:"type"`
Offset uintptr `json:"offset"`
Size uintptr `json:"size"`
Alignment uintptr `json:"alignment"`
}
// StructLayout represents the complete memory layout of a struct
type StructLayout struct {
Name string `json:"name"`
TotalSize uintptr `json:"total_size"`
Alignment uintptr `json:"alignment"`
Fields []FieldLayout `json:"fields"`
Padding []PaddingInfo `json:"padding"`
}
// PaddingInfo represents padding bytes in the struct
type PaddingInfo struct {
Offset uintptr `json:"offset"`
Size uintptr `json:"size"`
After string `json:"after"` // Field name after which padding occurs
}
// AnalyzeStruct analyzes a Go struct type and returns its memory layout
func AnalyzeStruct(structType reflect.Type) (*StructLayout, error) {
if structType.Kind() != reflect.Struct {
return nil, fmt.Errorf("expected struct type, got %v", structType.Kind())
}
layout := &StructLayout{
Name: structType.Name(),
TotalSize: structType.Size(),
Alignment: uintptr(structType.Align()),
Fields: make([]FieldLayout, 0, structType.NumField()),
Padding: make([]PaddingInfo, 0),
}
// Analyze each field
for i := 0; i < structType.NumField(); i++ {
field := structType.Field(i)
fieldLayout := FieldLayout{
Name: field.Name,
Type: field.Type.String(),
Offset: field.Offset,
Size: field.Type.Size(),
Alignment: uintptr(field.Type.Align()),
}
layout.Fields = append(layout.Fields, fieldLayout)
}
// Calculate padding
layout.Padding = calculatePadding(layout.Fields, layout.TotalSize)
return layout, nil
}
// calculatePadding identifies padding bytes in the struct
func calculatePadding(fields []FieldLayout, totalSize uintptr) []PaddingInfo {
padding := make([]PaddingInfo, 0)
for i := 0; i < len(fields); i++ {
currentField := fields[i]
nextOffset := currentField.Offset + currentField.Size
// Check for padding between current and next field
if i < len(fields)-1 {
nextField := fields[i+1]
if nextOffset < nextField.Offset {
padding = append(padding, PaddingInfo{
Offset: nextOffset,
Size: nextField.Offset - nextOffset,
After: currentField.Name,
})
}
} else {
// Check for trailing padding
if nextOffset < totalSize {
padding = append(padding, PaddingInfo{
Offset: nextOffset,
Size: totalSize - nextOffset,
After: currentField.Name,
})
}
}
}
return padding
}
// AnalyzeStructFromSource analyzes a struct from Go source code
func AnalyzeStructFromSource(sourceCode, structName string) (*StructLayout, error) {
fset := token.NewFileSet()
// Parse the source code
file, err := parser.ParseFile(fset, "", sourceCode, 0)
if err != nil {
return nil, fmt.Errorf("failed to parse source: %w", err)
}
// Find the struct declaration
var structType *ast.StructType
var foundName string
ast.Inspect(file, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.TypeSpec:
if x.Name.Name == structName {
if st, ok := x.Type.(*ast.StructType); ok {
structType = st
foundName = x.Name.Name
return false
}
}
}
return true
})
if structType == nil {
return nil, fmt.Errorf("struct %s not found in source", structName)
}
// Create type checker configuration
conf := types.Config{Importer: nil}
info := &types.Info{
Types: make(map[ast.Expr]types.TypeAndValue),
Defs: make(map[*ast.Ident]types.Object),
Uses: make(map[*ast.Ident]types.Object),
}
// Type check the file
pkg, err := conf.Check("main", fset, []*ast.File{file}, info)
if err != nil {
// Continue even with type errors, as we can still extract basic structure
}
// Build layout from AST
layout := &StructLayout{
Name: foundName,
Fields: make([]FieldLayout, 0),
}
// Get the type from the package
if pkg != nil {
obj := pkg.Scope().Lookup(structName)
if obj != nil {
if named, ok := obj.Type().(*types.Named); ok {
if st, ok := named.Underlying().(*types.Struct); ok {
return analyzeStructFromTypes(foundName, st)
}
}
}
}
return layout, nil
}
// analyzeStructFromTypes analyzes a types.Struct and calculates layout
func analyzeStructFromTypes(name string, st *types.Struct) (*StructLayout, error) {
layout := &StructLayout{
Name: name,
Fields: make([]FieldLayout, 0, st.NumFields()),
}
var offset uintptr
var maxAlign uintptr = 1
// Calculate offsets and sizes for each field
for i := 0; i < st.NumFields(); i++ {
field := st.Field(i)
fieldType := field.Type()
// Get size and alignment
size := getSizeOfType(fieldType)
align := getAlignOfType(fieldType)
if align > maxAlign {
maxAlign = align
}
// Align the offset
if offset%align != 0 {
offset = (offset + align - 1) / align * align
}
fieldLayout := FieldLayout{
Name: field.Name(),
Type: fieldType.String(),
Offset: offset,
Size: size,
Alignment: align,
}
layout.Fields = append(layout.Fields, fieldLayout)
offset += size
}
// Align total size to struct alignment
if offset%maxAlign != 0 {
offset = (offset + maxAlign - 1) / maxAlign * maxAlign
}
layout.TotalSize = offset
layout.Alignment = maxAlign
layout.Padding = calculatePadding(layout.Fields, layout.TotalSize)
return layout, nil
}
// getSizeOfType returns the size of a types.Type
func getSizeOfType(t types.Type) uintptr {
switch t := t.(type) {
case *types.Basic:
info := t.Info()
switch {
case info&types.IsBoolean != 0:
return 1
case info&types.IsInteger != 0:
switch t.Kind() {
case types.Int8, types.Uint8:
return 1
case types.Int16, types.Uint16:
return 2
case types.Int32, types.Uint32:
return 4
case types.Int64, types.Uint64:
return 8
case types.Int, types.Uint:
return unsafe.Sizeof(int(0))
case types.Uintptr:
return unsafe.Sizeof(uintptr(0))
}
case info&types.IsFloat != 0:
switch t.Kind() {
case types.Float32:
return 4
case types.Float64:
return 8
}
case info&types.IsComplex != 0:
switch t.Kind() {
case types.Complex64:
return 8
case types.Complex128:
return 16
}
case info&types.IsString != 0:
return unsafe.Sizeof("")
}
case *types.Pointer, *types.Chan, *types.Map, *types.Signature:
return unsafe.Sizeof(uintptr(0))
case *types.Slice:
return unsafe.Sizeof([]int{})
case *types.Array:
elemSize := getSizeOfType(t.Elem())
return uintptr(t.Len()) * elemSize
case *types.Struct:
// Recursively calculate struct size
var offset uintptr
var maxAlign uintptr = 1
for i := 0; i < t.NumFields(); i++ {
field := t.Field(i)
size := getSizeOfType(field.Type())
align := getAlignOfType(field.Type())
if align > maxAlign {
maxAlign = align
}
if offset%align != 0 {
offset = (offset + align - 1) / align * align
}
offset += size
}
if offset%maxAlign != 0 {
offset = (offset + maxAlign - 1) / maxAlign * maxAlign
}
return offset
case *types.Interface:
return unsafe.Sizeof((*interface{})(nil))
}
return unsafe.Sizeof(uintptr(0))
}
// getAlignOfType returns the alignment of a types.Type
func getAlignOfType(t types.Type) uintptr {
switch t := t.(type) {
case *types.Basic:
info := t.Info()
switch {
case info&types.IsBoolean != 0:
return 1
case info&types.IsInteger != 0:
switch t.Kind() {
case types.Int8, types.Uint8:
return 1
case types.Int16, types.Uint16:
return 2
case types.Int32, types.Uint32:
return 4
case types.Int64, types.Uint64:
return 8
case types.Int, types.Uint:
return uintptr(unsafe.Alignof(int(0)))
case types.Uintptr:
return uintptr(unsafe.Alignof(uintptr(0)))
}
case info&types.IsFloat != 0:
switch t.Kind() {
case types.Float32:
return 4
case types.Float64:
return 8
}
case info&types.IsComplex != 0:
switch t.Kind() {
case types.Complex64:
return 4
case types.Complex128:
return 8
}
case info&types.IsString != 0:
return uintptr(unsafe.Alignof(""))
}
case *types.Pointer, *types.Chan, *types.Map, *types.Signature:
return uintptr(unsafe.Alignof(uintptr(0)))
case *types.Slice:
return uintptr(unsafe.Alignof([]int{}))
case *types.Array:
return getAlignOfType(t.Elem())
case *types.Struct:
var maxAlign uintptr = 1
for i := 0; i < t.NumFields(); i++ {
align := getAlignOfType(t.Field(i).Type())
if align > maxAlign {
maxAlign = align
}
}
return maxAlign
case *types.Interface:
return uintptr(unsafe.Alignof((*interface{})(nil)))
}
return uintptr(unsafe.Alignof(uintptr(0)))
}