|
| 1 | +package wasm |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "reflect" |
| 7 | + "slices" |
| 8 | + "strings" |
| 9 | + "sync" |
| 10 | + "unsafe" |
| 11 | + |
| 12 | + "github.com/tetratelabs/wazero" |
| 13 | + "github.com/tetratelabs/wazero/api" |
| 14 | + |
| 15 | + "github.com/quay/claircore" |
| 16 | +) |
| 17 | + |
| 18 | +// PtrMember is a helper to take a pointer to a Go struct, then return a |
| 19 | +// pointer that's contained as a field. |
| 20 | +func ptrMember(off uintptr) api.GoModuleFunc { |
| 21 | + return func(ctx context.Context, mod api.Module, stack []uint64) { |
| 22 | + // Take in *A, which has a *B at offset "off". |
| 23 | + ref := unsafe.Pointer(api.DecodeExternref(stack[0])) // Shouldn't be nil. |
| 24 | + ptrField := unsafe.Add(ref, off) // This pointer can't be nil. |
| 25 | + ptr := *(*unsafe.Pointer)(ptrField) // Can be nil. |
| 26 | + stack[0] = api.EncodeExternref(uintptr(ptr)) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +// PtrToMember is a helper to take a pointer to a Go struct, then return a |
| 31 | +// pointer to a contained field. |
| 32 | +func ptrToMember(off uintptr) api.GoModuleFunc { |
| 33 | + return func(ctx context.Context, mod api.Module, stack []uint64) { |
| 34 | + // Take in *A, which has a B at offset "off". |
| 35 | + ref := unsafe.Pointer(api.DecodeExternref(stack[0])) // Shouldn't be nil. |
| 36 | + ptr := unsafe.Add(ref, off) // This pointer can't be nil. |
| 37 | + stack[0] = api.EncodeExternref(uintptr(ptr)) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +// StringMember is a helper to take a pointer to a Go struct, then return a |
| 42 | +// copy of a string member to a caller-allocated buffer. |
| 43 | +func stringMember(off uintptr) api.GoModuleFunc { |
| 44 | + return func(ctx context.Context, mod api.Module, stack []uint64) { |
| 45 | + // Unsure of another way to get this length information. |
| 46 | + h := (*reflect.StringHeader)(unsafe.Pointer(api.DecodeExternref(stack[0]) + off)) |
| 47 | + offset := api.DecodeU32(stack[1]) |
| 48 | + lim := int(api.DecodeU32(stack[2])) |
| 49 | + s := unsafe.String((*byte)(unsafe.Pointer(h.Data)), h.Len) |
| 50 | + sz := min(lim, len(s)) |
| 51 | + if sz == 0 { |
| 52 | + stack[0] = api.EncodeI32(0) |
| 53 | + return |
| 54 | + } |
| 55 | + s = s[:sz] |
| 56 | + mem := mod.ExportedMemory("memory") |
| 57 | + if mem.WriteString(offset, s) { |
| 58 | + stack[0] = api.EncodeI32(int32(sz)) |
| 59 | + } else { |
| 60 | + stack[0] = api.EncodeI32(0) |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +// StringerMember is a helper to take a pointer to a Go struct, then place the |
| 66 | +// string representation of a member into a caller-allocated buffer. |
| 67 | +func stringerMember(off uintptr) api.GoModuleFunc { |
| 68 | + return func(ctx context.Context, mod api.Module, stack []uint64) { |
| 69 | + iface := (any)(unsafe.Pointer(api.DecodeExternref(stack[0]) + off)).(fmt.Stringer) |
| 70 | + offset := api.DecodeU32(stack[1]) |
| 71 | + lim := int(api.DecodeU32(stack[2])) |
| 72 | + s := iface.String() |
| 73 | + sz := min(lim, len(s)) |
| 74 | + if mod.ExportedMemory("memory").WriteString(offset, s[:sz]) { |
| 75 | + stack[0] = api.EncodeI32(int32(sz)) |
| 76 | + } else { |
| 77 | + stack[0] = api.EncodeI32(0) |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// NotNil checks that the passed externref is not-nil. |
| 83 | +// |
| 84 | +// This is needed because externrefs are unobservable from within WASM; they |
| 85 | +// can only be handed back to the host and not manipulated in any way. |
| 86 | +func notNil(ctx context.Context, mod api.Module, stack []uint64) { |
| 87 | + if api.DecodeExternref(stack[0]) != 0 { |
| 88 | + stack[0] = api.EncodeI32(1) |
| 89 | + } else { |
| 90 | + stack[0] = api.EncodeI32(0) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +type methodSpec struct { |
| 95 | + Name string |
| 96 | + Func api.GoModuleFunc |
| 97 | + Params []api.ValueType |
| 98 | + ParamNames []string |
| 99 | + Results []api.ValueType |
| 100 | + ResultNames []string |
| 101 | +} |
| 102 | + |
| 103 | +func (s *methodSpec) Build(b wazero.HostModuleBuilder) wazero.HostModuleBuilder { |
| 104 | + return b.NewFunctionBuilder(). |
| 105 | + WithName(s.Name). |
| 106 | + WithParameterNames(s.ParamNames...). |
| 107 | + WithResultNames(s.ResultNames...). |
| 108 | + WithGoModuleFunction(s.Func, s.Params, s.Results). |
| 109 | + Export(s.Name) |
| 110 | +} |
| 111 | + |
| 112 | +func gettersFor[T any]() []methodSpec { |
| 113 | + t := reflect.TypeFor[T]() |
| 114 | + recv := strings.ToLower(t.Name()) |
| 115 | + out := make([]methodSpec, 0, t.NumField()) |
| 116 | + |
| 117 | + switch t { |
| 118 | + // These types are passed-in and always valid. |
| 119 | + case reflect.TypeFor[claircore.IndexRecord](), |
| 120 | + reflect.TypeFor[claircore.Vulnerability](): |
| 121 | + default: |
| 122 | + out = append(out, methodSpec{ |
| 123 | + Name: fmt.Sprintf("%s_valid", recv), |
| 124 | + Func: notNil, |
| 125 | + Params: []api.ValueType{api.ValueTypeExternref}, |
| 126 | + Results: []api.ValueType{api.ValueTypeI32}, |
| 127 | + ParamNames: []string{recv + "Ref"}, |
| 128 | + ResultNames: []string{"ok"}, |
| 129 | + }) |
| 130 | + } |
| 131 | + for i := 0; i < t.NumField(); i++ { |
| 132 | + sf := t.Field(i) |
| 133 | + if !sf.IsExported() { |
| 134 | + continue |
| 135 | + } |
| 136 | + if sf.Name == "ID" { // Skip "id" fields. |
| 137 | + continue |
| 138 | + } |
| 139 | + |
| 140 | + ft := sf.Type |
| 141 | + tgt := strings.ToLower(sf.Name) |
| 142 | + // Do some fixups: |
| 143 | + switch tgt { |
| 144 | + case "dist": |
| 145 | + tgt = "distribution" |
| 146 | + case "arch": |
| 147 | + tgt = "architecture" |
| 148 | + case "repo": |
| 149 | + tgt = "repository" |
| 150 | + } |
| 151 | + mi := len(out) |
| 152 | + out = append(out, methodSpec{}) |
| 153 | + m := &out[mi] |
| 154 | + m.Name = fmt.Sprintf("%s_get_%s", recv, tgt) |
| 155 | + switch ft.Kind() { |
| 156 | + case reflect.Pointer: |
| 157 | + m.Func = ptrMember(sf.Offset) |
| 158 | + m.Params = []api.ValueType{api.ValueTypeExternref} |
| 159 | + m.Results = []api.ValueType{api.ValueTypeExternref} |
| 160 | + m.ParamNames = []string{recv + "Ref"} |
| 161 | + m.ResultNames = []string{tgt + "Ref"} |
| 162 | + case reflect.String: |
| 163 | + m.Func = stringMember(sf.Offset) |
| 164 | + m.Params = []api.ValueType{api.ValueTypeExternref, api.ValueTypeI32, api.ValueTypeI32} |
| 165 | + m.Results = []api.ValueType{api.ValueTypeI32} |
| 166 | + m.ParamNames = []string{recv + "Ref", "buf", "buf_len"} |
| 167 | + m.ResultNames = []string{"len"} |
| 168 | + case reflect.Struct: |
| 169 | + switch { |
| 170 | + case ft == reflect.TypeFor[claircore.Version](): |
| 171 | + m.Func = ptrToMember(sf.Offset) |
| 172 | + m.Params = []api.ValueType{api.ValueTypeExternref} |
| 173 | + m.Results = []api.ValueType{api.ValueTypeExternref} |
| 174 | + m.ParamNames = []string{recv + "Ref"} |
| 175 | + m.ResultNames = []string{tgt + "Ref"} |
| 176 | + case ft.Implements(reflect.TypeFor[fmt.Stringer]()): |
| 177 | + m.Func = stringerMember(sf.Offset) |
| 178 | + m.Params = []api.ValueType{api.ValueTypeExternref, api.ValueTypeI32, api.ValueTypeI32} |
| 179 | + m.Results = []api.ValueType{api.ValueTypeI32} |
| 180 | + m.ParamNames = []string{recv + "Ref", "buf", "buf_len"} |
| 181 | + m.ResultNames = []string{"len"} |
| 182 | + default: |
| 183 | + out = out[:mi] |
| 184 | + } |
| 185 | + default: |
| 186 | + out = out[:mi] |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + return slices.Clip(out) |
| 191 | +} |
| 192 | + |
| 193 | +var hostV1Interface = sync.OnceValue(func() []methodSpec { |
| 194 | + return slices.Concat( |
| 195 | + gettersFor[claircore.IndexRecord](), |
| 196 | + gettersFor[claircore.Detector](), |
| 197 | + gettersFor[claircore.Distribution](), |
| 198 | + gettersFor[claircore.Package](), |
| 199 | + gettersFor[claircore.Range](), |
| 200 | + gettersFor[claircore.Repository](), |
| 201 | + gettersFor[claircore.Version](), |
| 202 | + gettersFor[claircore.Vulnerability](), |
| 203 | + ) |
| 204 | +}) |
| 205 | + |
| 206 | +func buildHostV1Interface(rt wazero.Runtime) wazero.HostModuleBuilder { |
| 207 | + b := rt.NewHostModuleBuilder("claircore_matcher_1") |
| 208 | + for _, spec := range hostV1Interface() { |
| 209 | + b = spec.Build(b) |
| 210 | + } |
| 211 | + return b |
| 212 | +} |
0 commit comments