Best Syzkaller code snippet using ipc.readUint64
ipc.go
Source:ipc.go  
1package ipc2import (3	"encoding/binary"4	"fmt"5	"io"6	"math/rand"7	"os"8	"runtime"9	"runtime/debug"10	"syscall"11	"unsafe"12	"github.com/pkg/errors"13	"github.com/oov/aviutl_rampreview/src/go/ods"14)15type IPC struct {16	cache   map[int][]byte17	storage map[string][]byte18	mappedFile syscall.Handle19	mappedView uintptr20	reply      chan error21	replyDone  chan struct{}22}23func (ipc *IPC) dispatch(cmd string) error {24	switch cmd {25	case "HELO":26		return writeUint32(0x80000000)27	case "FMO ":28		h, err := readUInt64()29		if err != nil {30			return err31		}32		if ipc.mappedView != 0 {33			if err = syscall.UnmapViewOfFile(ipc.mappedView); err != nil {34				return err35			}36			ipc.mappedView = 037		}38		if ipc.mappedFile != 0 {39			if err = syscall.CloseHandle(ipc.mappedFile); err != nil {40				return err41			}42			ipc.mappedFile = 043		}44		ipc.mappedFile = syscall.Handle(h)45		ipc.mappedView, err = syscall.MapViewOfFile(ipc.mappedFile, syscall.FILE_MAP_WRITE, 0, 0, 0)46		if err != nil {47			syscall.CloseHandle(ipc.mappedFile)48			ipc.mappedFile = 049			return err50		}51		return writeUint32(0x80000000)52	case "PUT ":53		key, err := readInt32()54		if err != nil {55			return err56		}57		size, err := readInt32()58		if err != nil {59			return err60		}61		if ipc.mappedFile == 0 || ipc.mappedView == 0 {62			return errors.New("not initialized yet")63		}64		buf := make([]byte, size)65		copy(buf, ((*[1 << 49]byte)(unsafe.Pointer(ipc.mappedView)))[:size:size])66		ipc.cache[key] = buf67		return writeUint32(0x80000000)68	case "GET ":69		key, err := readInt32()70		if err != nil {71			return err72		}73		if ipc.mappedFile == 0 || ipc.mappedView == 0 {74			return errors.New("not initialized yet")75		}76		if err = writeUint32(0x80000000); err != nil {77			return err78		}79		if buf, ok := ipc.cache[key]; ok {80			copy(((*[1 << 49]byte)(unsafe.Pointer(ipc.mappedView)))[:], buf)81			err = writeInt32(int32(len(buf)))82		} else {83			err = writeInt32(0)84		}85		return err86	case "CLR ":87		ipc.cache = map[int][]byte{}88		runtime.GC()89		debug.FreeOSMemory()90		return writeUint32(0x80000000)91	case "PUTS":92		key, err := readString()93		if err != nil {94			return err95		}96		size, err := readInt32()97		if err != nil {98			return err99		}100		if ipc.mappedFile == 0 || ipc.mappedView == 0 {101			return errors.New("not initialized yet")102		}103		buf := make([]byte, size)104		copy(buf, ((*[1 << 49]byte)(unsafe.Pointer(ipc.mappedView)))[:size:size])105		ipc.storage[key] = buf106		if rand.Float32() > 0.97 {107			runtime.GC()108			debug.FreeOSMemory()109		}110		return writeUint32(0x80000000)111	case "DELS":112		key, err := readString()113		if err != nil {114			return err115		}116		delete(ipc.storage, key)117		return writeUint32(0x80000000)118	case "GETS":119		key, err := readString()120		if err != nil {121			return err122		}123		if ipc.mappedFile == 0 || ipc.mappedView == 0 {124			return errors.New("not initialized yet")125		}126		if err = writeUint32(0x80000000); err != nil {127			return err128		}129		if buf, ok := ipc.storage[key]; ok {130			copy(((*[1 << 49]byte)(unsafe.Pointer(ipc.mappedView)))[:], buf)131			err = writeInt32(int32(len(buf)))132		} else {133			err = writeInt32(0)134		}135		return err136	case "CLRS":137		ipc.storage = map[string][]byte{}138		runtime.GC()139		debug.FreeOSMemory()140		return writeUint32(0x80000000)141	case "STAT":142		var m runtime.MemStats143		runtime.ReadMemStats(&m)144		if err := writeUint32(0x80000000); err != nil {145			return err146		}147		return writeUint64(m.Alloc)148	}149	return errors.New("unknown command")150}151func (ipc *IPC) readCommand(r chan string) {152	cmd := make([]byte, 4)153	for {154		ods.ODS("wait next command...")155		if read, err := io.ReadFull(os.Stdin, cmd); err != nil || read != 4 {156			r <- fmt.Sprintf("error: %v", err)157			return158		}159		l := binary.LittleEndian.Uint32(cmd)160		if l&0x80000000 == 0 {161			break162		}163		l &= 0x7fffffff164		if l == 0 {165			ods.ODS("readCommand: reply no error")166			ipc.reply <- nil167		} else {168			buf := make([]byte, l)169			read, err := io.ReadFull(os.Stdin, buf)170			if err != nil {171				r <- fmt.Sprintf("error: %v", err)172				return173			}174			if read != int(l) {175				r <- fmt.Sprintf("error: %v", errors.New("unexcepted read size"))176				return177			}178			ods.ODS("readCommand: reply %s", string(buf))179			ipc.reply <- errors.New(string(buf))180		}181		<-ipc.replyDone182	}183	ods.ODS("readCommand: cmd %s", string(cmd))184	r <- string(cmd)185}186func (ipc *IPC) gc() {187}188func (ipc *IPC) Main(exitCh chan<- struct{}) {189	defer func() {190		if err := recover(); err != nil {191			ods.Recover(err)192		}193		if ipc.mappedView != 0 {194			syscall.UnmapViewOfFile(ipc.mappedView)195			ipc.mappedView = 0196		}197		if ipc.mappedFile != 0 {198			syscall.CloseHandle(ipc.mappedFile)199			ipc.mappedFile = 0200		}201		close(exitCh)202	}()203	cmdCh := make(chan string)204	go ipc.readCommand(cmdCh)205	for {206		select {207		case cmd := <-cmdCh:208			if len(cmd) != 4 {209				ods.ODS("%s", cmd) // report error210				return211			}212			ods.ODS("%s", cmd)213			if err := ipc.dispatch(cmd); err != nil {214				ods.ODS("error: %v", err)215				if err = writeReply(err); err != nil {216					return217				}218			}219			ods.ODS("%s END", cmd)220			go ipc.readCommand(cmdCh)221		}222	}223}224func New() *IPC {225	r := &IPC{226		cache:   map[int][]byte{},227		storage: map[string][]byte{},228		reply:     make(chan error),229		replyDone: make(chan struct{}),230	}231	return r232}...encoding.go
Source:encoding.go  
1// This Source Code Form is subject to the terms of the MIT License.2// If a copy of the MIT License was not distributed with this3// file, you can obtain one at https://opensource.org/licenses/MIT.4//5// Copyright (c) DUSK NETWORK. All rights reserved.6package utils7import (8	"bytes"9	"encoding/binary"10	"fmt"11	"io"12	"math"13	"github.com/dusk-network/dusk-blockchain/pkg/core/data/ipc/transactions"14	"github.com/dusk-network/dusk-blockchain/pkg/core/database"15	"github.com/dusk-network/dusk-blockchain/pkg/p2p/wire/encoding"16	"github.com/dusk-network/dusk-protobuf/autogen/go/rusk"17	"google.golang.org/protobuf/proto"18)19var byteOrder = binary.LittleEndian20// EncodeBlockTx tries to serialize type, index and encoded value of transactions.ContractCall.21func EncodeBlockTx(tx transactions.ContractCall, txIndex uint32) ([]byte, error) {22	buf := new(bytes.Buffer)23	// Write tx type as first field24	if err := buf.WriteByte(byte(tx.Type())); err != nil {25		return nil, err26	}27	// Write index value as second field.28	// golevedb is ordering keys lexicographically. That said, the order of the29	// stored KV is not the order of inserting30	if err := WriteUint32(buf, txIndex); err != nil {31		return nil, err32	}33	// Write tx gas spent as third field34	if err := WriteUint64(buf, tx.GasSpent()); err != nil {35		return nil, err36	}37	// Write transactions.ContractCall bytes38	if err := transactions.Marshal(buf, tx); err != nil {39		return nil, err40	}41	txErrBytes := make([]byte, 0)42	if tx.TxError() != nil {43		txerr, err := proto.Marshal(tx.TxError())44		if err != nil {45			return nil, err46		}47		txErrBytes = txerr48	}49	if err := encoding.WriteVarBytesUint32(buf, txErrBytes); err != nil {50		return nil, err51	}52	return buf.Bytes(), nil53}54// DecodeBlockTx tries to deserialize the type, index and decoded value of a tx.55func DecodeBlockTx(data []byte, typeFilter transactions.TxType) (transactions.ContractCall, uint32, error) {56	txIndex := uint32(math.MaxUint32)57	tx := transactions.NewTransaction()58	reader := bytes.NewBuffer(data)59	// Peek the type from the first byte60	typeBytes, err := reader.ReadByte()61	if err != nil {62		return nil, txIndex, err63	}64	txReadType := transactions.TxType(typeBytes)65	if typeFilter != database.AnyTxType {66		// Do not read and decode the rest of the bytes if the transaction type67		// is not same as typeFilter68		if typeFilter != txReadType {69			return nil, txIndex, fmt.Errorf("tx of type %d not found", typeFilter)70		}71	}72	// Read tx index field73	if e := ReadUint32(reader, &txIndex); e != nil {74		return nil, txIndex, e75	}76	// Read gasSpent field77	var gasSpent uint6478	if e := ReadUint64(reader, &gasSpent); e != nil {79		return nil, txIndex, e80	}81	if e := transactions.Unmarshal(reader, tx); e != nil {82		return tx, txIndex, err83	}84	var buferr []byte85	if e := encoding.ReadVarBytesUint32LE(reader, &buferr); e != nil {86		return tx, txIndex, err87	}88	if len(buferr) > 0 {89		decodeError := new(rusk.ExecutedTransaction_Error)90		if err = proto.Unmarshal(buferr, decodeError); err != nil {91			return tx, txIndex, err92		}93		tx.Error = decodeError94	}95	cc, err := transactions.UpdateTransaction(tx, gasSpent, tx.Error)96	if err != nil {97		return tx, txIndex, err98	}99	return cc, txIndex, err100}101// WriteUint32 Tx utility to use a Tx byteOrder on internal encoding.102func WriteUint32(w io.Writer, value uint32) error {103	var b [4]byte104	byteOrder.PutUint32(b[:], value)105	_, err := w.Write(b[:])106	return err107}108// ReadUint32 will read four bytes and convert them to a uint32 from the Tx109// byteOrder. The result is put into v.110func ReadUint32(r io.Reader, v *uint32) error {111	var b [4]byte112	n, err := r.Read(b[:])113	if err != nil || n != len(b) {114		return err115	}116	*v = byteOrder.Uint32(b[:])117	return nil118}119// WriteUint64 Tx utility to use a common byteOrder on internal encoding.120func WriteUint64(w io.Writer, value uint64) error {121	var b [8]byte122	byteOrder.PutUint64(b[:], value)123	_, err := w.Write(b[:])124	return err125}126// ReadUint64 will read four bytes and convert them to a uint64 from the Tx127// byteOrder. The result is put into v.128func ReadUint64(r io.Reader, v *uint64) error {129	var b [8]byte130	n, err := r.Read(b[:])131	if err != nil || n != len(b) {132		return err133	}134	*v = byteOrder.Uint64(b[:])135	return nil136}...readUint64
Using AI Code Generation
1import (2func main() {3    ipc, err := rpc.Dial("/home/ethereum/.ethereum/geth.ipc")4    if err != nil {5        panic(err)6    }7    if err := ipc.Call(&result, "admin_nodeInfo"); err != nil {8        panic(err)9    }10    fmt.Println(result)11}Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
