Skip to content

Commit

Permalink
Add missing frees in cgo interface
Browse files Browse the repository at this point in the history
  • Loading branch information
PlasmaPower authored and hkalodner committed Nov 30, 2021
1 parent 2ddae98 commit c8c8efd
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
12 changes: 4 additions & 8 deletions packages/arb-avm-cpp/cmachine/arbcore.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,28 +90,24 @@ func (ac *ArbCore) PrintCoreThreadBacktrace() {
C.arbCorePrintCoreThreadBacktrace(ac.c)
}

// Note: the slices field of the returned struct needs manually freed by C.free
func sequencerBatchItemsToByteSliceArray(batchItems []inbox.SequencerBatchItem) C.struct_ByteSliceArrayStruct {
return bytesArrayToByteSliceArray(encodeSequencerBatchItems(batchItems))
}

// Note: the slices field of the returned struct needs manually freed by C.free
func delayedMessagesToByteSliceArray(delayedMessages []inbox.DelayedMessage) C.struct_ByteSliceArrayStruct {
return bytesArrayToByteSliceArray(encodeDelayedMessages(delayedMessages))
}

func u256ArrayToByteSliceArray(nums []*big.Int) C.struct_ByteSliceArrayStruct {
var data [][]byte
for _, num := range nums {
data = append(data, math.U256Bytes(num))
}
return bytesArrayToByteSliceArray(data)
}

func (ac *ArbCore) DeliverMessages(previousMessageCount *big.Int, previousSeqBatchAcc common.Hash, seqBatchItems []inbox.SequencerBatchItem, delayedMessages []inbox.DelayedMessage, reorgSeqBatchItemCount *big.Int) bool {
defer runtime.KeepAlive(ac)
previousMessageCountPtr := unsafeDataPointer(math.U256Bytes(previousMessageCount))
previousSeqBatchAccPtr := unsafeDataPointer(previousSeqBatchAcc.Bytes())
seqBatchItemsSlice := sequencerBatchItemsToByteSliceArray(seqBatchItems)
defer C.free(seqBatchItemsSlice.slices)
delayedMessagesSlice := delayedMessagesToByteSliceArray(delayedMessages)
defer C.free(delayedMessagesSlice.slices)

var cReorgSeqBatchItemCount unsafe.Pointer
if reorgSeqBatchItemCount != nil {
Expand Down
4 changes: 2 additions & 2 deletions packages/arb-avm-cpp/cmachine/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ func (m *Machine) ExecuteAssertion(
)
}

// Note: the slices field of the returned struct needs manually freed by C.free
func bytesArrayToByteSliceArray(bytes [][]byte) C.struct_ByteSliceArrayStruct {
byteSlices := encodeByteSliceList(bytes)
sliceArrayData := C.malloc(C.size_t(C.sizeof_struct_ByteSliceStruct * len(byteSlices)))
Expand All @@ -190,15 +191,14 @@ func (m *Machine) ExecuteAssertionAdvanced(
) (*protocol.ExecutionAssertion, []value.Value, uint64, error) {
defer runtime.KeepAlive(m)
conf := C.machineExecutionConfigCreate()
defer C.machineExecutionConfigDestroy(conf)

C.machineExecutionConfigSetMaxGas(conf, C.uint64_t(maxGas), boolToCInt(goOverGas))

msgData := bytesArrayToByteSliceArray(encodeMachineInboxMessages(messages))
defer C.free(msgData.slices)
C.machineExecutionConfigSetInboxMessages(conf, msgData)

C.machineExecutionConfigSetInboxMessages(conf, msgData)

sideloadsData := bytesArrayToByteSliceArray(encodeInboxMessages(sideloads))
defer C.free(sideloadsData.slices)
C.machineExecutionConfigSetSideloads(conf, sideloadsData)
Expand Down

0 comments on commit c8c8efd

Please sign in to comment.