package wasm

import (
	
	
	
	
	
	
	
	
	

	
	
	
	
)

const (
	// MemoryPageSize is the unit of memory length in WebAssembly,
	// and is defined as 2^16 = 65536.
	// See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#memory-instances%E2%91%A0
	MemoryPageSize = uint32(65536)
	// MemoryLimitPages is maximum number of pages defined (2^16).
	// See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#grow-mem
	MemoryLimitPages = uint32(65536)
	// MemoryPageSizeInBits satisfies the relation: "1 << MemoryPageSizeInBits == MemoryPageSize".
	MemoryPageSizeInBits = 16
)

// compile-time check to ensure MemoryInstance implements api.Memory
var _ api.Memory = &MemoryInstance{}

type waiters struct {
	mux sync.Mutex
	l   *list.List
}

// MemoryInstance represents a memory instance in a store, and implements api.Memory.
//
// Note: In WebAssembly 1.0 (20191205), there may be up to one Memory per store, which means the precise memory is always
// wasm.Store Memories index zero: `store.Memories[0]`
// See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#memory-instances%E2%91%A0.
type MemoryInstance struct {
	internalapi.WazeroOnlyType

	Buffer        []byte
	Min, Cap, Max uint32
	Shared        bool
	// definition is known at compile time.
	definition api.MemoryDefinition

	// Mux is used in interpreter mode to prevent overlapping calls to atomic instructions,
	// introduced with WebAssembly threads proposal, and in compiler mode to make memory modifications
	// within Grow non-racy for the Go race detector.
	Mux sync.Mutex

	// waiters implements atomic wait and notify. It is implemented similarly to golang.org/x/sync/semaphore,
	// with a fixed weight of 1 and no spurious notifications.
	waiters sync.Map

	// ownerModuleEngine is the module engine that owns this memory instance.
	ownerModuleEngine ModuleEngine

	expBuffer experimental.LinearMemory
}

// NewMemoryInstance creates a new instance based on the parameters in the SectionIDMemory.
func ( *Memory,  experimental.MemoryAllocator,  ModuleEngine) *MemoryInstance {
	 := MemoryPagesToBytesNum(.Min)
	 := MemoryPagesToBytesNum(.Cap)
	 := MemoryPagesToBytesNum(.Max)

	var  []byte
	var  experimental.LinearMemory
	if  != nil {
		 = .Allocate(, )
		 = .Reallocate()
		_ = [:] // Bounds check that the minimum was allocated.
	} else if .IsShared {
		// Shared memory needs a fixed buffer, so allocate with the maximum size.
		//
		// The rationale as to why we can simply use make([]byte) to a fixed buffer is that Go's GC is non-relocating.
		// That is not a part of Go spec, but is well-known thing in Go community (wazero's compiler heavily relies on it!)
		// 	* https://github.com/go4org/unsafe-assume-no-moving-gc
		//
		// Also, allocating Max here isn't harmful as the Go runtime uses mmap for large allocations, therefore,
		// the memory buffer allocation here is virtual and doesn't consume physical memory until it's used.
		// 	* https://github.com/golang/go/blob/8121604559035734c9677d5281bbdac8b1c17a1e/src/runtime/malloc.go#L1059
		//	* https://github.com/golang/go/blob/8121604559035734c9677d5281bbdac8b1c17a1e/src/runtime/malloc.go#L1165
		 = make([]byte, , )
	} else {
		 = make([]byte, , )
	}
	return &MemoryInstance{
		Buffer:            ,
		Min:               .Min,
		Cap:               memoryBytesNumToPages(uint64(cap())),
		Max:               .Max,
		Shared:            .IsShared,
		expBuffer:         ,
		ownerModuleEngine: ,
	}
}

// Definition implements the same method as documented on api.Memory.
func ( *MemoryInstance) () api.MemoryDefinition {
	return .definition
}

// Size implements the same method as documented on api.Memory.
func ( *MemoryInstance) () uint32 {
	return uint32(len(.Buffer))
}

// ReadByte implements the same method as documented on api.Memory.
func ( *MemoryInstance) ( uint32) (byte, bool) {
	if !.hasSize(, 1) {
		return 0, false
	}
	return .Buffer[], true
}

// ReadUint16Le implements the same method as documented on api.Memory.
func ( *MemoryInstance) ( uint32) (uint16, bool) {
	if !.hasSize(, 2) {
		return 0, false
	}
	return binary.LittleEndian.Uint16(.Buffer[ : +2]), true
}

// ReadUint32Le implements the same method as documented on api.Memory.
func ( *MemoryInstance) ( uint32) (uint32, bool) {
	return .readUint32Le()
}

// ReadFloat32Le implements the same method as documented on api.Memory.
func ( *MemoryInstance) ( uint32) (float32, bool) {
	,  := .readUint32Le()
	if ! {
		return 0, false
	}
	return math.Float32frombits(), true
}

// ReadUint64Le implements the same method as documented on api.Memory.
func ( *MemoryInstance) ( uint32) (uint64, bool) {
	return .readUint64Le()
}

// ReadFloat64Le implements the same method as documented on api.Memory.
func ( *MemoryInstance) ( uint32) (float64, bool) {
	,  := .readUint64Le()
	if ! {
		return 0, false
	}
	return math.Float64frombits(), true
}

// Read implements the same method as documented on api.Memory.
func ( *MemoryInstance) (,  uint32) ([]byte, bool) {
	if !.hasSize(, uint64()) {
		return nil, false
	}
	return .Buffer[ : + : +], true
}

// WriteByte implements the same method as documented on api.Memory.
func ( *MemoryInstance) ( uint32,  byte) bool {
	if !.hasSize(, 1) {
		return false
	}
	.Buffer[] = 
	return true
}

// WriteUint16Le implements the same method as documented on api.Memory.
func ( *MemoryInstance) ( uint32,  uint16) bool {
	if !.hasSize(, 2) {
		return false
	}
	binary.LittleEndian.PutUint16(.Buffer[:], )
	return true
}

// WriteUint32Le implements the same method as documented on api.Memory.
func ( *MemoryInstance) (,  uint32) bool {
	return .writeUint32Le(, )
}

// WriteFloat32Le implements the same method as documented on api.Memory.
func ( *MemoryInstance) ( uint32,  float32) bool {
	return .writeUint32Le(, math.Float32bits())
}

// WriteUint64Le implements the same method as documented on api.Memory.
func ( *MemoryInstance) ( uint32,  uint64) bool {
	return .writeUint64Le(, )
}

// WriteFloat64Le implements the same method as documented on api.Memory.
func ( *MemoryInstance) ( uint32,  float64) bool {
	return .writeUint64Le(, math.Float64bits())
}

// Write implements the same method as documented on api.Memory.
func ( *MemoryInstance) ( uint32,  []byte) bool {
	if !.hasSize(, uint64(len())) {
		return false
	}
	copy(.Buffer[:], )
	return true
}

// WriteString implements the same method as documented on api.Memory.
func ( *MemoryInstance) ( uint32,  string) bool {
	if !.hasSize(, uint64(len())) {
		return false
	}
	copy(.Buffer[:], )
	return true
}

// MemoryPagesToBytesNum converts the given pages into the number of bytes contained in these pages.
func ( uint32) ( uint64) {
	return uint64() << MemoryPageSizeInBits
}

// Grow implements the same method as documented on api.Memory.
func ( *MemoryInstance) ( uint32) ( uint32,  bool) {
	if .Shared {
		.Mux.Lock()
		defer .Mux.Unlock()
	}

	 := .Pages()
	if  == 0 {
		return , true
	}

	 :=  + 
	if  > .Max || int32() < 0 {
		return 0, false
	} else if .expBuffer != nil {
		 := .expBuffer.Reallocate(MemoryPagesToBytesNum())
		if  == nil {
			// Allocator failed to grow.
			return 0, false
		}
		if .Shared {
			if unsafe.SliceData() != unsafe.SliceData(.Buffer) {
				panic("shared memory cannot move, this is a bug in the memory allocator")
			}
			// We assume grow is called under a guest lock.
			// But the memory length is accessed elsewhere,
			// so use atomic to make the new length visible across threads.
			atomicStoreLengthAndCap(&.Buffer, uintptr(len()), uintptr(cap()))
			.Cap = memoryBytesNumToPages(uint64(cap()))
		} else {
			.Buffer = 
			.Cap = 
		}
	} else if  > .Cap { // grow the memory.
		if .Shared {
			panic("shared memory cannot be grown, this is a bug in wazero")
		}
		.Buffer = append(.Buffer, make([]byte, MemoryPagesToBytesNum())...)
		.Cap = 
	} else { // We already have the capacity we need.
		if .Shared {
			// We assume grow is called under a guest lock.
			// But the memory length is accessed elsewhere,
			// so use atomic to make the new length visible across threads.
			atomicStoreLength(&.Buffer, uintptr(MemoryPagesToBytesNum()))
		} else {
			.Buffer = .Buffer[:MemoryPagesToBytesNum()]
		}
	}
	.ownerModuleEngine.MemoryGrown()
	return , true
}

// Pages implements the same method as documented on api.Memory.
func ( *MemoryInstance) () ( uint32) {
	return memoryBytesNumToPages(uint64(len(.Buffer)))
}

// PagesToUnitOfBytes converts the pages to a human-readable form similar to what's specified. e.g. 1 -> "64Ki"
//
// See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#memory-instances%E2%91%A0
func ( uint32) string {
	 :=  * 64
	if  < 1024 {
		return fmt.Sprintf("%d Ki", )
	}
	 :=  / 1024
	if  < 1024 {
		return fmt.Sprintf("%d Mi", )
	}
	 :=  / 1024
	if  < 1024 {
		return fmt.Sprintf("%d Gi", )
	}
	return fmt.Sprintf("%d Ti", /1024)
}

// Below are raw functions used to implement the api.Memory API:

// Uses atomic write to update the length of a slice.
func atomicStoreLengthAndCap( *[]byte,  uintptr,  uintptr) {
	//nolint:staticcheck
	 := (*reflect.SliceHeader)(unsafe.Pointer())
	 := (*uintptr)(unsafe.Pointer(&.Cap))
	atomic.StoreUintptr(, )
	 := (*uintptr)(unsafe.Pointer(&.Len))
	atomic.StoreUintptr(, )
}

// Uses atomic write to update the length of a slice.
func atomicStoreLength( *[]byte,  uintptr) {
	//nolint:staticcheck
	 := (*reflect.SliceHeader)(unsafe.Pointer())
	 := (*uintptr)(unsafe.Pointer(&.Len))
	atomic.StoreUintptr(, )
}

// memoryBytesNumToPages converts the given number of bytes into the number of pages.
func memoryBytesNumToPages( uint64) ( uint32) {
	return uint32( >> MemoryPageSizeInBits)
}

// hasSize returns true if Len is sufficient for byteCount at the given offset.
//
// Note: This is always fine, because memory can grow, but never shrink.
func ( *MemoryInstance) ( uint32,  uint64) bool {
	return uint64()+ <= uint64(len(.Buffer)) // uint64 prevents overflow on add
}

// readUint32Le implements ReadUint32Le without using a context. This is extracted as both ints and floats are stored in
// memory as uint32le.
func ( *MemoryInstance) ( uint32) (uint32, bool) {
	if !.hasSize(, 4) {
		return 0, false
	}
	return binary.LittleEndian.Uint32(.Buffer[ : +4]), true
}

// readUint64Le implements ReadUint64Le without using a context. This is extracted as both ints and floats are stored in
// memory as uint64le.
func ( *MemoryInstance) ( uint32) (uint64, bool) {
	if !.hasSize(, 8) {
		return 0, false
	}
	return binary.LittleEndian.Uint64(.Buffer[ : +8]), true
}

// writeUint32Le implements WriteUint32Le without using a context. This is extracted as both ints and floats are stored
// in memory as uint32le.
func ( *MemoryInstance) ( uint32,  uint32) bool {
	if !.hasSize(, 4) {
		return false
	}
	binary.LittleEndian.PutUint32(.Buffer[:], )
	return true
}

// writeUint64Le implements WriteUint64Le without using a context. This is extracted as both ints and floats are stored
// in memory as uint64le.
func ( *MemoryInstance) ( uint32,  uint64) bool {
	if !.hasSize(, 8) {
		return false
	}
	binary.LittleEndian.PutUint64(.Buffer[:], )
	return true
}

// Wait32 suspends the caller until the offset is notified by a different agent.
func ( *MemoryInstance) ( uint32,  uint32,  int64,  func( *MemoryInstance,  uint32) uint32) uint64 {
	 := .getWaiters()
	.mux.Lock()

	 := (, )
	if  !=  {
		.mux.Unlock()
		return 1
	}

	return .wait(, )
}

// Wait64 suspends the caller until the offset is notified by a different agent.
func ( *MemoryInstance) ( uint32,  uint64,  int64,  func( *MemoryInstance,  uint32) uint64) uint64 {
	 := .getWaiters()
	.mux.Lock()

	 := (, )
	if  !=  {
		.mux.Unlock()
		return 1
	}

	return .wait(, )
}

func ( *MemoryInstance) ( *waiters,  int64) uint64 {
	if .l == nil {
		.l = list.New()
	}

	// The specification requires a trap if the number of existing waiters + 1 == 2^32, so we add a check here.
	// In practice, it is unlikely the application would ever accumulate such a large number of waiters as it
	// indicates several GB of RAM used just for the list of waiters.
	// https://github.com/WebAssembly/threads/blob/main/proposals/threads/Overview.md#wait
	if uint64(.l.Len()+1) == 1<<32 {
		.mux.Unlock()
		panic(wasmruntime.ErrRuntimeTooManyWaiters)
	}

	 := make(chan struct{})
	 := .l.PushBack()
	.mux.Unlock()

	if  < 0 {
		<-
		return 0
	} else {
		select {
		case <-:
			return 0
		case <-time.After(time.Duration()):
			// While we could see if the channel completed by now and ignore the timeout, similar to x/sync/semaphore,
			// the Wasm spec doesn't specify this behavior, so we keep things simple by prioritizing the timeout.
			.mux.Lock()
			.l.Remove()
			.mux.Unlock()
			return 2
		}
	}
}

func ( *MemoryInstance) ( uint32) *waiters {
	,  := .waiters.Load()
	if ! {
		// The first time an address is waited on, simultaneous waits will cause extra allocations.
		// Further operations will be loaded above, which is also the general pattern of usage with
		// mutexes.
		, _ = .waiters.LoadOrStore(, &waiters{})
	}

	return .(*waiters)
}

// Notify wakes up at most count waiters at the given offset.
func ( *MemoryInstance) ( uint32,  uint32) uint32 {
	,  := .waiters.Load()
	if ! {
		return 0
	}
	 := .(*waiters)

	.mux.Lock()
	defer .mux.Unlock()
	if .l == nil {
		return 0
	}

	 := uint32(0)
	for  := .l.Len();  > 0 &&  < ;  = .l.Len() {
		 := .l.Remove(.l.Front()).(chan struct{})
		close()
		++
	}

	return 
}