-
Notifications
You must be signed in to change notification settings - Fork 291
/
memory.go
345 lines (303 loc) · 8.31 KB
/
memory.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
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
/*
* Copyright (C) 2018 The ontology Authors
* This file is part of The ontology library.
*
* The ontology is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ontology is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The ontology. If not, see <http://www.gnu.org/licenses/>.
*/
package memory
import (
"encoding/binary"
"errors"
"reflect"
"github.com/ontio/ontology/vm/wasmvm/util"
)
type PType int
const (
PInt8 PType = iota
PInt16
PInt32
PInt64
PFloat32
PFloat64
PString
PStruct
PUnkown
)
const (
//VM_NIL_POINTER = math.MaxInt64
VM_NIL_POINTER = 0
)
type TypeLength struct {
Ptype PType
Length int
}
type VMmemory struct {
Memory []byte
AllocedMemIdex int
PointedMemIndex int
ParamIndex int //args analyze pointer
MemPoints map[uint64]*TypeLength
}
//Alloc memory for base types, return the address in memory
func (vm *VMmemory) Malloc(size int) (int, error) {
if vm.Memory == nil || len(vm.Memory) == 0 {
return 0, errors.New("memory is not initialized")
}
if vm.AllocedMemIdex+size+1 > len(vm.Memory) {
return 0, errors.New("memory out of bound")
}
if vm.AllocedMemIdex+size+1 > vm.PointedMemIndex {
return 0, errors.New("memory out of bound")
}
offset := vm.AllocedMemIdex + 1
vm.AllocedMemIdex += size
return offset, nil
}
//Alloc memory for pointer types, return the address in memory
func (vm *VMmemory) MallocPointer(size int, p_type PType) (int, error) {
if vm.Memory == nil || len(vm.Memory) == 0 {
return 0, errors.New("memory is not initialized")
}
if vm.PointedMemIndex+size > len(vm.Memory) {
return 0, errors.New("memory out of bound")
}
offset := vm.PointedMemIndex + 1
vm.PointedMemIndex += size
//save the point and length
vm.MemPoints[uint64(offset)] = &TypeLength{Ptype: p_type, Length: size}
return offset, nil
}
func (vm *VMmemory) copyMemAndGetIdx(b []byte, p_type PType) (int, error) {
idx, err := vm.MallocPointer(len(b), p_type)
if err != nil {
return 0, err
}
copy(vm.Memory[idx:idx+len(b)], b)
return idx, nil
}
//return pointed memory size
func (vm *VMmemory) GetPointerMemSize(addr uint64) int {
//nil case
if addr == uint64(VM_NIL_POINTER) {
return 0
}
v, ok := vm.MemPoints[addr]
if ok {
return v.Length
} else {
return 0
}
}
//return pointed memory
//when wasm returns a pointer, call this function to get the pointed memory
func (vm *VMmemory) GetPointerMemory(addr uint64) ([]byte, error) {
//nil case
if addr == uint64(VM_NIL_POINTER) {
return nil, nil
}
length := vm.GetPointerMemSize(addr)
if length == 0 {
return nil, nil
}
if int(addr)+length > len(vm.Memory) {
return nil, errors.New("memory out of bound")
} else {
return vm.Memory[int(addr) : int(addr)+length], nil
}
}
//set pointer types into memory, return address of memory
func (vm *VMmemory) SetPointerMemory(val interface{}) (int, error) {
////nil case
if val == nil {
return VM_NIL_POINTER, nil
}
switch reflect.TypeOf(val).Kind() {
case reflect.String:
b := []byte(val.(string))
return vm.copyMemAndGetIdx(b, PString)
case reflect.Array, reflect.Struct, reflect.Ptr:
//todo implement
return 0, nil
case reflect.Slice:
switch val.(type) {
case []byte:
return vm.copyMemAndGetIdx(val.([]byte), PString)
case []int:
intBytes := make([]byte, len(val.([]int))*4)
for i, v := range val.([]int) {
tmp := make([]byte, 4)
binary.LittleEndian.PutUint32(tmp, uint32(v))
copy(intBytes[i*4:(i+1)*4], tmp)
}
return vm.copyMemAndGetIdx(intBytes, PInt32)
case []int64:
intBytes := make([]byte, len(val.([]int64))*8)
for i, v := range val.([]int64) {
tmp := make([]byte, 8)
binary.LittleEndian.PutUint64(tmp, uint64(v))
copy(intBytes[i*8:(i+1)*8], tmp)
}
return vm.copyMemAndGetIdx(intBytes, PInt64)
case []float32:
floatBytes := make([]byte, len(val.([]float32))*4)
for i, v := range val.([]float32) {
tmp := util.Float32ToBytes(v)
copy(floatBytes[i*4:(i+1)*4], tmp)
}
return vm.copyMemAndGetIdx(floatBytes, PFloat32)
case []float64:
floatBytes := make([]byte, len(val.([]float64))*4)
for i, v := range val.([]float64) {
tmp := util.Float64ToBytes(v)
copy(floatBytes[i*8:(i+1)*8], tmp)
}
return vm.copyMemAndGetIdx(floatBytes, PFloat64)
case []string:
sbytes := make([]byte, len(val.([]string))*4) //address is 4 bytes
for i, s := range val.([]string) {
idx, err := vm.SetPointerMemory(s)
if err != nil {
return 0, err
}
tmp := make([]byte, 4)
binary.LittleEndian.PutUint32(tmp, uint32(idx))
copy(sbytes[i*4:(i+1)*4], tmp)
}
return vm.copyMemAndGetIdx(sbytes, PInt32)
case [][]byte:
bbytes := make([]byte, len(val.([][]byte))*4) //address is 4 bytes
for i, b := range val.([][]byte) {
idx, err := vm.SetPointerMemory(b)
if err != nil {
return 0, err
}
tmp := make([]byte, 4)
binary.LittleEndian.PutUint32(tmp, uint32(idx))
copy(bbytes[i*4:(i+1)*4], tmp)
}
return vm.copyMemAndGetIdx(bbytes, PInt32)
default:
return 0, errors.New("Not supported slice type")
}
default:
return 0, errors.New("not supported type")
}
}
//set struct into memory , return address of memory
func (vm *VMmemory) SetStructMemory(val interface{}) (int, error) {
if reflect.TypeOf(val).Kind() != reflect.Struct {
return 0, errors.New("SetStructMemory :input is not a struct")
}
valref := reflect.ValueOf(val)
//var totalsize = 0
var index = 0
for i := 0; i < valref.NumField(); i++ {
field := valref.Field(i)
//nested struct case
if reflect.TypeOf(field.Type()).Kind() == reflect.Struct {
idx, err := vm.SetStructMemory(field)
if err != nil {
return 0, err
} else {
if i == 0 && index == 0 {
index = idx
}
}
} else {
var fieldVal interface{}
//todo how to determine the value is int or int64
var idx int
var err error
switch field.Kind() {
case reflect.Int, reflect.Int32, reflect.Uint, reflect.Uint32:
fieldVal = int(field.Int())
idx, err = vm.SetMemory(fieldVal)
case reflect.Int64, reflect.Uint64:
fieldVal = field.Int()
idx, err = vm.SetMemory(fieldVal)
case reflect.Float32, reflect.Float64:
fieldVal = field.Float()
idx, err = vm.SetMemory(fieldVal)
case reflect.String:
fieldVal = field.String()
tmp, err := vm.SetPointerMemory(fieldVal)
if err != nil {
return 0, err
}
//add the point address to memory
idx, err = vm.SetMemory(tmp)
case reflect.Slice:
//fieldVal = field.Interface()
//TODO note the struct field MUST be public
tmp, err := vm.SetPointerMemory(fieldVal)
if err != nil {
return 0, err
}
//add the point address to memory
idx, err = vm.SetMemory(tmp)
}
if err != nil {
return 0, err
} else {
if i == 0 && index == 0 {
index = idx
}
}
}
}
return index, nil
}
//set base types into memory, return address of memory
func (vm *VMmemory) SetMemory(val interface{}) (int, error) {
switch val.(type) {
case string: //use SetPointerMemory for string
return vm.SetPointerMemory(val.(string))
case int:
tmp := make([]byte, 4)
binary.LittleEndian.PutUint32(tmp, uint32(val.(int)))
idx, err := vm.Malloc(len(tmp))
if err != nil {
return 0, err
}
copy(vm.Memory[idx:idx+len(tmp)], tmp)
return idx, nil
case int64:
tmp := make([]byte, 8)
binary.LittleEndian.PutUint64(tmp, uint64(val.(int64)))
idx, err := vm.Malloc(len(tmp))
if err != nil {
return 0, err
}
copy(vm.Memory[idx:idx+len(tmp)], tmp)
return idx, nil
case float32:
tmp := util.Float32ToBytes(val.(float32))
idx, err := vm.Malloc(len(tmp))
if err != nil {
return 0, err
}
copy(vm.Memory[idx:idx+len(tmp)], tmp)
return idx, nil
case float64:
tmp := util.Float64ToBytes(val.(float64))
idx, err := vm.Malloc(len(tmp))
if err != nil {
return 0, err
}
copy(vm.Memory[idx:idx+len(tmp)], tmp)
return idx, nil
default:
return 0, errors.New("not supported type")
}
}