Skip to content

Commit

Permalink
engine: make the external reference ID random and unique
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Feb 29, 2024
1 parent 017945c commit 19925e5
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions engine/references.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package engine

import "unsafe"
import (
"math/rand"
"unsafe"
)

// ExternalReferences manages external references to be used by the Interpreter
// to store references that can be passed to guest modules.
type ExternalReferences struct {
nextid int32
refs map[int32]uintptr
refs map[int32]uintptr
}

// NewReferences creates a new ExternalReferences store.
Expand All @@ -18,7 +20,7 @@ func NewReferences() *ExternalReferences {

// Add adds a reference to the ExternalReferences store and returns the new reference id.
func (r *ExternalReferences) Add(thing unsafe.Pointer) int32 {
id := r.referenceid()
id := r.newReferenceId()
r.refs[id] = uintptr(thing)
return id
}
Expand All @@ -33,8 +35,17 @@ func (r *ExternalReferences) Remove(id int32) {
delete(r.refs, id)
}

// TODO: generate a safe reference id
func (r *ExternalReferences) referenceid() int32 {
r.nextid++
return r.nextid
// generates a random reference id that is not already in use.
func (r *ExternalReferences) newReferenceId() int32 {
for {
id := int32(randomInt(1, 0x7FFFFFFF))
if _, ok := r.refs[id]; !ok {
return id
}
}
}

// Returns an int >= min, < max
func randomInt(min, max int) int {
return min + rand.Intn(max-min)
}

0 comments on commit 19925e5

Please sign in to comment.