Skip to content

Commit 9164a8c

Browse files
committed
soft fail when normalization fix is not applied
1 parent e1f826a commit 9164a8c

File tree

3 files changed

+55
-8
lines changed

3 files changed

+55
-8
lines changed

packages/orchestrator/internal/sandbox/sandbox.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,11 @@ func pauseProcessMemory(
835835

836836
err = header.ValidateMappings(memfileHeader.Mapping, memfileHeader.Metadata.Size, memfileHeader.Metadata.BlockSize)
837837
if err != nil {
838-
return nil, nil, fmt.Errorf("invalid memfile header mappings: %w", err)
838+
if memfileHeader.IsNormalizeFixApplied() {
839+
return nil, nil, fmt.Errorf("invalid memfile header mappings: %w", err)
840+
}
841+
842+
zap.L().Warn("memfile header mappings are invalid, but normalize fix is not applied", zap.Error(err))
839843
}
840844

841845
return memfileDiff, memfileHeader, nil
@@ -896,7 +900,11 @@ func pauseProcessRootfs(
896900

897901
err = header.ValidateMappings(rootfsHeader.Mapping, rootfsHeader.Metadata.Size, rootfsHeader.Metadata.BlockSize)
898902
if err != nil {
899-
return nil, nil, fmt.Errorf("invalid rootfs header mappings: %w", err)
903+
if rootfsHeader.IsNormalizeFixApplied() {
904+
return nil, nil, fmt.Errorf("invalid rootfs header mappings: %w", err)
905+
}
906+
907+
zap.L().Warn("rootfs header mappings are invalid, but normalize fix is not applied", zap.Error(err))
900908
}
901909

902910
return rootfsDiff, rootfsHeader, nil

packages/shared/pkg/storage/header/header.go

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ import (
55

66
"github.com/bits-and-blooms/bitset"
77
"github.com/google/uuid"
8+
"go.uber.org/zap"
89
)
910

11+
const NormalizeFixVersion = 3
12+
1013
type Header struct {
1114
Metadata *Metadata
1215
blockStarts *bitset.BitSet
@@ -49,6 +52,12 @@ func NewHeader(metadata *Metadata, mapping []*BuildMap) (*Header, error) {
4952
}, nil
5053
}
5154

55+
// IsNormalizeFixApplied is a helper method to soft fail for older versions of the header where fix for normalization was not applied.
56+
// This should be removed in the future.
57+
func (t *Header) IsNormalizeFixApplied() bool {
58+
return t.Metadata.Version >= NormalizeFixVersion
59+
}
60+
5261
func (t *Header) GetShiftedMapping(offset int64) (mappedOffset int64, mappedLength int64, buildID *uuid.UUID, err error) {
5362
mapping, shift, err := t.getMapping(offset)
5463
if err != nil {
@@ -60,18 +69,39 @@ func (t *Header) GetShiftedMapping(offset int64) (mappedOffset int64, mappedLeng
6069
buildID = &mapping.BuildId
6170

6271
if mappedLength < 0 {
63-
return 0, 0, nil, fmt.Errorf("mapped length for offset %d is negative: %d", offset, mappedLength)
72+
if t.IsNormalizeFixApplied() {
73+
return 0, 0, nil, fmt.Errorf("mapped length for offset %d is negative: %d", offset, mappedLength)
74+
}
75+
76+
zap.L().Warn("mapped length is negative, but normalize fix is not applied",
77+
zap.Int64("offset", offset),
78+
zap.Int64("mappedLength", mappedLength),
79+
)
6480
}
6581

6682
return mappedOffset, mappedLength, buildID, nil
6783
}
6884

6985
func (t *Header) getMapping(offset int64) (*BuildMap, int64, error) {
7086
if offset < 0 || offset >= int64(t.Metadata.Size) {
71-
return nil, 0, fmt.Errorf("offset %d is out of bounds (size: %d)", offset, t.Metadata.Size)
87+
if t.IsNormalizeFixApplied() {
88+
return nil, 0, fmt.Errorf("offset %d is out of bounds (size: %d)", offset, t.Metadata.Size)
89+
}
90+
91+
zap.L().Warn("offset is out of bounds, but normalize fix is not applied",
92+
zap.Int64("offset", offset),
93+
zap.Int64("size", int64(t.Metadata.Size)),
94+
)
7295
}
7396
if offset%int64(t.Metadata.BlockSize) != 0 {
74-
return nil, 0, fmt.Errorf("offset %d is not aligned to block size %d", offset, t.Metadata.BlockSize)
97+
if t.IsNormalizeFixApplied() {
98+
return nil, 0, fmt.Errorf("offset %d is not aligned to block size %d", offset, t.Metadata.BlockSize)
99+
}
100+
101+
zap.L().Warn("offset is not aligned to block size, but normalize fix is not applied",
102+
zap.Int64("offset", offset),
103+
zap.Int64("blockSize", int64(t.Metadata.BlockSize)),
104+
)
75105
}
76106

77107
block := BlockIdx(offset, int64(t.Metadata.BlockSize))
@@ -90,8 +120,17 @@ func (t *Header) getMapping(offset int64) (*BuildMap, int64, error) {
90120

91121
// Verify that the offset falls within this mapping's range
92122
if shift >= int64(mapping.Length) {
93-
return nil, 0, fmt.Errorf("offset %d (block %d) is beyond the end of mapping at offset %d (ends at %d)",
94-
offset, block, mapping.Offset, mapping.Offset+mapping.Length)
123+
if t.IsNormalizeFixApplied() {
124+
return nil, 0, fmt.Errorf("offset %d (block %d) is beyond the end of mapping at offset %d (ends at %d)",
125+
offset, block, mapping.Offset, mapping.Offset+mapping.Length)
126+
}
127+
128+
zap.L().Warn("offset is beyond the end of mapping, but normalize fix is not applied",
129+
zap.Int64("offset", offset),
130+
zap.Int64("block", block),
131+
zap.Uint64("mappingOffset", mapping.Offset),
132+
zap.Uint64("mappingEnd", mapping.Offset+mapping.Length),
133+
)
95134
}
96135

97136
return mapping, shift, nil

packages/shared/pkg/storage/header/serialization.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"github.com/e2b-dev/infra/packages/shared/pkg/storage"
1414
)
1515

16-
const metadataVersion = 2
16+
const metadataVersion = 3
1717

1818
type Metadata struct {
1919
Version uint64

0 commit comments

Comments
 (0)