# Untrusted files, long sessions and speed

What the library promises when it is fed files it cannot trust, when it runs for hours while avatars come and go,
and what it is held to for speed on each class of machine. Use this page when you take avatar files, manifests or
custom items from players, run a server, or set a frame budget.

## Untrusted input

Every decoder takes its bytes as untrusted. A file that is cut short, padded, or built to mislead gives an error
code, not a crash, and a part that decodes keeps its promises: indices inside the vertices, finite positions and
uvs.

```c
sona_manifest m;
if (sona_manifest_parse(manifest, &m) != SONA_OK)   /* any 1,000 bytes from a player */
  puts("not an avatar");                            /* SONA_E_MANIFEST */
else
  sona_avatar_add(ctx, manifest, NULL, &avatar);    /* its items load through your sources */
```

```cpp
sona::Manifest m;
if (sona::Manifest::parse(manifest, m) != SONA_OK)   // any 1,000 bytes from a player
  std::puts("not an avatar");                        // SONA_E_MANIFEST
else
  avatar = ctx.add(manifest);                        // its items load through your sources
```

```csharp
try
{
    SonaManifests.Parse(manifest);            // any 1,000 bytes from a player
    avatar = ctx.Add(manifest);               // its items load through your sources
}
catch (SonaException) { Console.WriteLine("not an avatar"); }   // SONA_E_MANIFEST
```

```rust
match Manifest::parse(&manifest) {           // any 1,000 bytes from a player
    Ok(_) => { ctx.add(&manifest, &AddOptions::default())?; }   // its items load through your sources
    Err(_) => println!("not an avatar"),     // SONA_E_MANIFEST
}
```

```python
try:
    sona.Manifest(manifest)                  # any 1,000 bytes from a player
    avatar = ctx.add(manifest)               # its items load through your sources
except sona.SonaError:
    print("not an avatar")                   # SONA_E_MANIFEST
```

| Input | Read by | What a bad one gives |
|---|---|---|
| A manifest, as bytes or base64 | `sona_manifest_parse`, `sona_avatar_add_base64` | `SONA_E_MANIFEST`; one that parses writes back byte for byte |
| Item files from a source (models, textures, clips, shape overrides) | The loader, inside `sona_update` | The avatar is `SONA_AVATAR_FAILED` with its item named in `SONA_EVENT_FAILED`; other avatars are not touched |
| Console storage (STFS packages, FATX images, profile files) | `SONA_SOURCE_CONSOLE` | A read error for that file |
| A custom item (`.sonaitem`) | `sona_custom_item_check`, `sona_custom_item_add` | An error from the call; nothing is added |
| An imported animation (BVH, glTF, glb, FBX) | `sona_anim_file_open` | An error from the open; no clip is made |
| A prop's model and pictures (glTF, PNG, JPEG) | `sona_prop_mesh_load`, `sona_prop_mesh_open` | An error from the call; no mesh is made |
| A saved state | `sona_avatar_load_state` | `SONA_E_CORRUPT` for bytes that are not a state, `SONA_E_UNSUPPORTED` for a newer format |
| A network packet | `sona_net_read` | An error from the call; the receiver keeps its state |

The decoders are fuzzed: containers, models, clips, textures, manifests and custom items each have a target that
feeds them mutated files under the address and undefined-behaviour sanitisers.

## Long sessions

Memory stays flat as avatars are added and removed. What grows are bounded caches, each inside its budget in
`sona_config.budget`: the decoded items and clips that no avatar uses now, and the face sheets (every new mix of a
face and an expression is a sheet). Removing an avatar frees what only it used; `sona_stats` reports the library's
CPU and GPU memory at any time.

`sona-bench`, in your download's `bench/` folder, has a soak scene that swaps a crowd's avatars for others for as
long as you say and fails (exit 4) when memory, less what the caches hold, grows from one cycle of swaps to the
next:

```text
sona-bench --scene soak --backend vk --source dir:<folder of avatar files> --outfits random \
  --catalogue sona-catalogue.bin --counts 1000 --seconds 1800 --verbose
```

`--budget-mb N` sets every cache budget at once; `--backend none` soaks the core without drawing; `--verbose`
prints each sample with the core's memory per tag. The soak reads its avatar files from a folder (`dir:`), so the
network does not enter the numbers; the other scenes also take `--source gate:<url>`.

## Speed targets

The library is held to these on each class of machine, with every avatar in its own random outfit and body:

| Class | Machine | Targets (each a set limit) |
|---|---|---|
| Desktop | A mid desktop GPU (RTX 4000 Ada, about an RTX 4060 Ti) | 1,000 different avatars under 8 ms a frame, ready under 10 s, under 1.5 MB each; 10,000 in a stadium with LOD under 16 ms |
| Integrated GPU | Core Ultra 9 185H with its Arc iGPU | 1,000 different avatars under 16 ms; 10,000 with LOD under 16 ms and ready under 30 s; one more avatar ready and drawn from memory under 50 ms |
| Handheld | Steam Deck | 200 at 60 fps, 1,000 with LOD at 30 fps |
| Phone | A low-end Android phone | 20 at 60 fps on the low [tier](tiers.md) |
| Xbox 360 | The console | 10 at 30 fps |

[Performance](../performance.md) has the measured figures for the first two and the command to measure your own
machine.

What the renderer does to meet them:

- **Vertex cache order.** Opaque meshes are uploaded in an order that runs the vertex shader about once per
  vertex; blended meshes keep their order.
- **Simplified meshes for small avatars.** With `sona_lod_set`, an opaque mesh of an avatar under 120 pixels tall
  draws about half its triangles (edge collapses by quadric error; every vertex, seam and open border kept). The
  lists are made on the workers. Without `sona_lod_set` every avatar draws all its triangles
  ([crowds](crowd.md)).
- **Faces bake on the workers,** in rows, to the same bytes as on one thread.
- **Vulkan without resizable BAR.** Vertex and index data are uploaded through staging into memory the CPU does not
  map, so draws do not read them across the bus. `SONA_VK_STAGE_GEOMETRY=0` or `1` in the environment overrides
  the choice.

An integrated GPU shares its power budget with the CPU: measure it with nothing else running.
