Skip to content

Commit

Permalink
go/runtime/bundle: Avoid loading all data into memory
Browse files Browse the repository at this point in the history
Since the bundles may become larger with TDX VMs, we should make sure
that we are not loading entire bundles into memory.
  • Loading branch information
kostko committed Sep 25, 2024
1 parent 5af2670 commit 14d309d
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 279 deletions.
10 changes: 10 additions & 0 deletions go/common/crypto/hash/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"encoding/hex"
"errors"
"hash"
"io"

cmtbytes "github.com/cometbft/cometbft/libs/bytes"

Expand Down Expand Up @@ -149,6 +150,15 @@ func NewFromBytes(data ...[]byte) (h Hash) {
return
}

// NewFromReader creates a new hash by hashing data from the provided reader until EOF.
func NewFromReader(reader io.Reader) (Hash, error) {
b := NewBuilder()
if _, err := io.Copy(b, reader); err != nil {
return Hash{}, err
}
return b.Build(), nil
}

// LoadFromHexBytes creates a new hash by loading it from the given CometBFT
// HexBytes byte array.
func LoadFromHexBytes(data cmtbytes.HexBytes) (h Hash) {
Expand Down
19 changes: 2 additions & 17 deletions go/common/sgx/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,10 @@ func (m *MrEnclave) UnmarshalHex(text string) error {
// FromSgxs derives a MrEnclave from r, under the assumption that r will
// provide the entire `.sgxs` file.
func (m *MrEnclave) FromSgxs(r io.Reader) error {
// A `.sgxs` file's SHA256 digest is conveniently the MRENCLAVE.
var buf [32768]byte

h := sha256.New()
readLoop:
for {
l, err := r.Read(buf[:])
if l > 0 {
_, _ = h.Write(buf[:l])
}
switch err {
case nil:
case io.EOF:
break readLoop
default:
return fmt.Errorf("sgx: failed to read .sgxs: %w", err)
}
if _, err := io.Copy(h, r); err != nil {
return fmt.Errorf("sgx: failed to read sgxs: %w", err)
}

sum := h.Sum(nil)
return m.UnmarshalBinary(sum)
}
Expand Down
201 changes: 0 additions & 201 deletions go/oasis-node/cmd/debug/bundle/bundle.go

This file was deleted.

2 changes: 0 additions & 2 deletions go/oasis-node/cmd/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/spf13/cobra"

"github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/debug/beacon"
"github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/debug/bundle"
"github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/debug/byzantine"
"github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/debug/control"
"github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/debug/dumpdb"
Expand All @@ -27,7 +26,6 @@ func Register(parentCmd *cobra.Command) {
control.Register(debugCmd)
dumpdb.Register(debugCmd)
beacon.Register(debugCmd)
bundle.Register(debugCmd)

parentCmd.AddCommand(debugCmd)
}
4 changes: 2 additions & 2 deletions go/oasis-test-runner/oasis/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func (rt *Runtime) toRuntimeBundle(deploymentIndex int) (*bundle.Bundle, error)
if err != nil {
return nil, fmt.Errorf("oasis/runtime: failed to read ELF binary: %w", err)
}
_ = bnd.Add(elfBin, binBuf)
_ = bnd.Add(elfBin, bundle.NewBytesData(binBuf))

comp := &bundle.Component{
Kind: compCfg.Kind,
Expand All @@ -289,7 +289,7 @@ func (rt *Runtime) toRuntimeBundle(deploymentIndex int) (*bundle.Bundle, error)
comp.SGX = &bundle.SGXMetadata{
Executable: sgxBin,
}
_ = bnd.Add(sgxBin, binBuf)
_ = bnd.Add(sgxBin, bundle.NewBytesData(binBuf))
}

bnd.Manifest.Components = append(bnd.Manifest.Components, comp)
Expand Down
Loading

0 comments on commit 14d309d

Please sign in to comment.