Skip to content

Commit

Permalink
wazero-contest
Browse files Browse the repository at this point in the history
  • Loading branch information
faddat committed Jan 11, 2025
1 parent e31ce9e commit b3e5f06
Show file tree
Hide file tree
Showing 3 changed files with 318 additions and 151 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
.idea
.vscode
**combined_code.txt
/vm
/contracts


# no static libraries (35MB+)
/internal/api/lib*.a
Expand Down
18 changes: 9 additions & 9 deletions internal/runtime/hostfunctions.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,13 +454,13 @@ func hostAbort(ctx context.Context, mod api.Module, code uint32) {

for i, r := range ranges {
// Skip reading if `r.start + r.size` might exceed memory bounds
if r.start > mem.Size()*65536 {
fmt.Printf("[range %d] Start offset %d is out of memory bounds (size: %d pages)\n", i, r.start, mem.Size())
if r.start > mem.Size() {
fmt.Printf("[range %d] Start offset %d is out of memory bounds (size: %d bytes)\n", i, r.start, mem.Size())
continue
}
end := r.start + r.size
if end > mem.Size()*65536 {
end = mem.Size() * 65536
if end > mem.Size() {
end = mem.Size()
}
lengthToRead := end - r.start
data, ok := mem.Read(r.start, lengthToRead)
Expand Down Expand Up @@ -977,20 +977,20 @@ func RegisterHostFunctions(runtime wazero.Runtime, env *RuntimeEnvironment) (waz
}

// Calculate required pages for the allocation
currentPages := memory.Size()
currentBytes := memory.Size()
requiredBytes := size
pageSize := uint32(65536) // 64KB
requiredPages := (requiredBytes + pageSize - 1) / pageSize

// Grow memory if needed
if requiredPages > 0 {
if _, ok := memory.Grow(uint32(requiredPages)); !ok {
if requiredBytes > currentBytes {
pagesToGrow := (requiredBytes - currentBytes + pageSize - 1) / pageSize
if _, ok := memory.Grow(uint32(pagesToGrow)); !ok {
panic("failed to grow memory")
}
}

// Return the pointer to the allocated memory
ptr := currentPages * pageSize
ptr := currentBytes
return ptr
}).
WithParameterNames("size").
Expand Down
Loading

0 comments on commit b3e5f06

Please # to comment.