Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,22 @@ type opState struct {
wlog *WireLogRecord
}

// MalformedMessageError reports a FUSE message that could not be converted to
// an operation. Message holds a copy of the raw bytes of the offending
// message, which callers can log to diagnose the corruption.
type MalformedMessageError struct {
Err error
Message []byte
}

func (e *MalformedMessageError) Error() string {
return fmt.Sprintf("convertInMessage: %v", e.Err)
}

func (e *MalformedMessageError) Unwrap() error {
return e.Err
}

// Return the current wirelog record from the context if the MountConfig
// contained a non-nil wireLogger, nil otherwise.
func GetWirelog(ctx context.Context) *WireLogRecord {
Expand Down Expand Up @@ -477,8 +493,15 @@ func (c *Connection) ReadOp() (_ context.Context, op interface{}, _ error) {
outMsg := c.getOutMessage()
op, err = convertInMessage(&c.cfg, inMsg, outMsg, c.protocol)
if err != nil {
// Copy the raw message before recycling the buffer so that the
// caller can log the bytes that failed to convert.
message := append([]byte(nil), inMsg.Bytes()...)
c.putInMessage(inMsg)
c.putOutMessage(outMsg)
return nil, nil, fmt.Errorf("convertInMessage: %v", err)
return nil, nil, &MalformedMessageError{
Err: err,
Message: message,
}
}

// Choose an ID for this operation for the purposes of logging, and log it.
Expand Down
27 changes: 27 additions & 0 deletions connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,39 @@
package fuse

import (
"bytes"
"errors"
"fmt"
"strings"
"testing"

"github.com/jacobsa/fuse/internal/buffer"
)

func TestMalformedMessageError(t *testing.T) {
inner := errors.New("Corrupt OpLookup")
msg := []byte{0x00, 0x01, 0x02, 0xff}
err := &MalformedMessageError{Err: inner, Message: msg}

if got, want := err.Error(), "convertInMessage: Corrupt OpLookup"; got != want {
t.Errorf("Error() = %q, want %q", got, want)
}

// The error unwraps to the underlying conversion error and is recoverable
// via errors.As even when wrapped.
if !errors.Is(err, inner) {
t.Errorf("errors.Is(err, inner) = false, want true")
}

var target *MalformedMessageError
if !errors.As(fmt.Errorf("read op: %w", err), &target) {
t.Fatalf("errors.As failed to recover *MalformedMessageError")
}
if !bytes.Equal(target.Message, msg) {
t.Errorf("Message = %v, want %v", target.Message, msg)
}
}

func TestSanitizeMaxPagesAndWrite(t *testing.T) {
pageSize := uint32(buffer.GetPageSize())
defaultMaxWrite := uint32(buffer.MaxWriteSize)
Expand Down
5 changes: 5 additions & 0 deletions internal/buffer/in_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ func (m *InMessage) Init(r io.Reader) error {
return nil
}

// Bytes returns the complete message read by the most recent call to Init.
func (m *InMessage) Bytes() []byte {
return m.storage[:m.size]
}

// Return a reference to the header read in the most recent call to Init.
func (m *InMessage) Header() *fusekernel.InHeader {
return (*fusekernel.InHeader)(unsafe.Pointer(&m.storage[0]))
Expand Down
44 changes: 44 additions & 0 deletions internal/buffer/in_message_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2026 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package buffer

import (
"bytes"
"encoding/binary"
"testing"

"github.com/jacobsa/fuse/internal/fusekernel"
)

func TestInMessageBytes(t *testing.T) {
// Construct a well-formed message: a header whose Len field matches the
// total size, followed by a small payload.
const payloadLen = 8
total := fusekernel.InHeaderSize + payloadLen
raw := make([]byte, total)
binary.LittleEndian.PutUint32(raw[0:4], uint32(total))
for i := fusekernel.InHeaderSize; i < total; i++ {
raw[i] = byte(i)
}

m := NewInMessage(GetPageSize() + MaxWriteSize)
if err := m.Init(bytes.NewReader(raw)); err != nil {
t.Fatalf("Init: %v", err)
}

if got := m.Bytes(); !bytes.Equal(got, raw) {
t.Errorf("Bytes() = %v, want %v", got, raw)
}
}