Skip to content

Free, open-source 3D sculpting for Linux, macOS and Windows: dense meshes, remeshing, painting, built-in AI (image/text to 3D), Send to Unreal, game-ready LODs, Gaussian splats and OpenUSD.

Fix a use-after-free in Tool > Texture Map > Material Channels; memory readouts from atomic counters

islandmed18d646ea8963
diff --git a/src/app/PanelPbr.cpp b/src/app/PanelPbr.cpp
index cf0641ba..c9358c61 100644
--- a/src/app/PanelPbr.cpp
+++ b/src/app/PanelPbr.cpp
@@ -122,6 +122,16 @@ void App::drawTextureChannelsContents() {
const Mesh& m = doc_.mesh;
if (!hasTexture(m)) return;
const PaintTexture& t = *m.texture;
+ // Commands that edit the texture replace it (captureTexture: the old one goes into the undo step and may be
+ // freed), so `t` is only valid until one runs: they wait in `pending` until everything is drawn.
+ std::vector<std::pair<std::string, Args>> pending;
+ struct RunPending {
+ App& app;
+ std::vector<std::pair<std::string, Args>>& list;
+ ~RunPending() {
+ for (auto& [name, args] : list) app.runCommand(name, args);
+ }
+ } runPending{*this, pending};
const ImGuiStyle& style = ImGui::GetStyle();
ImGui::SeparatorText("Material Channels");
// Channel toggles, two per row.
@@ -132,7 +142,7 @@ void App::drawTextureChannelsContents() {
if (column) ImGui::SameLine(half + style.ItemSpacing.x + ImGui::GetCursorStartPos().x);
const std::string label = std::string(channelFormat(c).label) + "##texchan";
if (ImGui::Checkbox(label.c_str(), &on))
- runCommand("texture_channels", {{on ? "enable" : "disable", std::string(channelFormat(c).id)}});
+ pending.push_back({"texture_channels", {{on ? "enable" : "disable", std::string(channelFormat(c).id)}}});
ui::HelpText(channelFormat(c).label, nullptr,
(channelHelp(c) + " Turning a channel on costs no memory until it is painted; turning it off "
"discards its paint (undoable).")
@@ -149,12 +159,12 @@ void App::drawTextureChannelsContents() {
ImGui::SetNextItemWidth(-1);
if (ImGui::BeginCombo("##channelview", viewLabel)) {
if (ImGui::Selectable("Normal view", doc_.channelView == Channel::BaseColor))
- runCommand("channel_view", {{"channel", std::string("off")}});
+ pending.push_back({"channel_view", {{"channel", std::string("off")}}});
for (Channel c : kMaterialChannels) {
if (!channelEnabled(t, c)) continue;
const std::string label = std::string("Solo ") + channelFormat(c).label;
if (ImGui::Selectable(label.c_str(), doc_.channelView == c))
- runCommand("channel_view", {{"channel", std::string(channelFormat(c).id)}});
+ pending.push_back({"channel_view", {{"channel", std::string(channelFormat(c).id)}}});
}
ImGui::EndCombo();
}
diff --git a/src/core/Benchmark.cpp b/src/core/Benchmark.cpp
index 88c6c817..af588672 100644
--- a/src/core/Benchmark.cpp
+++ b/src/core/Benchmark.cpp
@@ -75,8 +75,7 @@ MemoryUse documentMemory(const Document& doc) {
u.mesh += m.memoryBytes();
if (m.texture && std::find(seen.begin(), seen.end(), m.texture.get()) == seen.end()) {
seen.push_back(m.texture.get());
- u.textures += m.texture->bytes();
- u.channels += channelBytes(*m.texture);
+ u.textures += m.texture->colourBytes();
}
u.bvh += (active ? doc.bvh : st.bvh).memoryBytes();
u.levels += subdivBytes(m);
@@ -88,6 +87,10 @@ MemoryUse documentMemory(const Document& doc) {
u.gpu += m.vertexCount() * (12 + (m.hasColors() ? 4 : 0) + (m.hasMask() ? 1 : 0)) * 11 / 10 +
m.triangleCount() * (6 + (m.hasPolygroups() ? 2 : 0));
}
+ // Material channel tiles from the atomic count (every texture, copies and undo steps): no tile walk here, so
+ // the readout is safe whatever else is painting.
+ u.channels = TileImage::liveBytes();
+ u.textures += u.channels;
u.paintCaches = texturePaintCacheBytes();
return u;
}
diff --git a/src/core/Benchmark.h b/src/core/Benchmark.h
index 797fcd3f..52735f9e 100644
--- a/src/core/Benchmark.h
+++ b/src/core/Benchmark.h
@@ -84,7 +84,7 @@ struct MemoryUse {
size_t undo = 0; // undo history (UndoStack::undoBytes)
size_t gpu = 0; // vertex and index buffers the renderer keeps for the visible SubTools (an estimate)
size_t textures = 0; // painted textures (core/Texture.h): colour, layers, material channels (each texture once)
- size_t channels = 0; // of `textures`, the material channel tiles (core/TextureChannels.h)
+ size_t channels = 0; // of `textures`, every material channel tile alive (copies and undo steps too; TileImage::liveBytes)
size_t paintCaches = 0; // texture painting caches of dense meshes (UV tile bins, texel maps), shared per UV layout
size_t triangles = 0, vertices = 0;
size_t total() const { return mesh + bvh + levels + undo + textures + paintCaches; }
diff --git a/src/core/Texture.cpp b/src/core/Texture.cpp
index f6cd3efd..6c3d6610 100644
--- a/src/core/Texture.cpp
+++ b/src/core/Texture.cpp
@@ -621,12 +621,14 @@ std::vector<uint8_t>& PaintTexture::target(int layer) {
return layers[size_t(layer)].rgba;
}
-size_t PaintTexture::bytes() const {
+size_t PaintTexture::colourBytes() const {
size_t n = pixels.size() + base.size() + coverage.size() + sizeof(PaintTexture);
for (const auto& l : layers) n += l.rgba.size() + sizeof(TextureLayer);
- return n + channelBytes(*this);
+ return n;
}
+size_t PaintTexture::bytes() const { return colourBytes() + channelBytes(*this); }
+
std::shared_ptr<PaintTexture> makeTexture(int width, int height) {
auto t = std::make_shared<PaintTexture>();
t->width = std::max(width, 1);
@@ -840,6 +842,7 @@ struct UvTileIndex {
std::vector<uint8_t> triTiles; // 4 per triangle: tile x0, y0, x1, y1 (x0 > x1: none); tiles per side <= 256
std::vector<uint32_t> binStart; // tilesX * tilesY + 1
std::vector<uint32_t> bins;
+ size_t bytes = 0; // set once built (texturePaintCacheBytes)
};
// One tile's texels: the triangle and barycentrics (b1, b2 as 16 bits) of each, kNoTri where no island reaches.
@@ -861,8 +864,15 @@ struct TexelMapCache {
std::vector<std::unique_ptr<TexelMapTile>> tiles;
size_t live = 0;
uint64_t clock = 0;
+ // Bytes of the tiles' maps, kept as they are built and dropped, so memory readouts never walk `tiles`.
+ std::atomic<size_t> bytes{0};
};
+static size_t texelMapBytes(const TexelMapTile& t) {
+ return sizeof(TexelMapTile) + t.tri.size() * sizeof(uint32_t) + t.bary.size() * sizeof(uint16_t) +
+ t.extras.size() * sizeof(TexelMapTile::Extra);
+}
+
namespace {
uint64_t paintKey(const Mesh& m, int w, int h) {
@@ -914,7 +924,7 @@ std::shared_ptr<const UvTileIndex> buildUvIndex(const Mesh& m, int width, int he
uint32_t running = 0;
for (size_t k = 0; k < tiles; ++k) {
idx->binStart[k] = running;
- for (size_t b = 0; b < blocks; ++b) {
+ for (size_t b = 0; b < blocks; ++b) { // (per tile, the blocks in order)
const uint32_t n = counts[b * tiles + k];
counts[b * tiles + k] = running; // now: where block b writes into bin k
running += n;
@@ -926,6 +936,7 @@ std::shared_ptr<const UvTileIndex> buildUvIndex(const Mesh& m, int width, int he
uint32_t* at = &counts[b * tiles];
for (size_t i = b * per; i < std::min(nt, (b + 1) * per); ++i) forTiles(i, [&](size_t k) { idx->bins[at[k]++] = uint32_t(i); });
});
+ idx->bytes = sizeof(UvTileIndex) + idx->triTiles.size() + (idx->binStart.size() + idx->bins.size()) * sizeof(uint32_t);
return idx;
}
@@ -1152,12 +1163,10 @@ void clearTexturePaintCaches() {
size_t texturePaintCacheBytes() {
std::lock_guard lock(gPaintCacheMutex);
size_t n = 0;
- for (const PaintCacheEntry& e : gPaintCache) {
- if (e.index) n += e.index->triTiles.size() + (e.index->binStart.size() + e.index->bins.size()) * sizeof(uint32_t);
+ for (const PaintCacheEntry& e : gPaintCache) { // (counters only: safe while a dab builds texel maps)
+ if (e.index) n += e.index->bytes;
if (e.coverage) n += e.coverage->size();
- if (e.maps)
- for (const auto& t : e.maps->tiles)
- if (t) n += t->tri.size() * 4 + t->bary.size() * 2 + t->extras.size() * sizeof(TexelMapTile::Extra);
+ if (e.maps) n += e.maps->bytes.load(std::memory_order_relaxed);
}
return n;
}
@@ -1231,6 +1240,7 @@ size_t TexturePainter::dabTiles(const DabContext& c, const Paint& paint, bool co
std::sort(old.begin(), old.end());
for (const auto& [used, i] : old) {
if (cache.live + missing <= kTexelMapBudget) break;
+ cache.bytes.fetch_sub(texelMapBytes(*cache.tiles[i]), std::memory_order_relaxed);
cache.tiles[i].reset();
--cache.live;
}
@@ -1248,6 +1258,7 @@ size_t TexturePainter::dabTiles(const DabContext& c, const Paint& paint, bool co
if (!slot) {
slot = buildTexelMap(mesh, c.tex, idx, ti);
slot->used = cache.clock;
+ cache.bytes.fetch_add(texelMapBytes(*slot), std::memory_order_relaxed);
}
const TexelMapTile& map = *slot;
if (c.s <= 0.0f) return; // prewarm: only the texel maps
diff --git a/src/core/Texture.h b/src/core/Texture.h
index 86a4d0d1..8e7e4c9c 100644
--- a/src/core/Texture.h
+++ b/src/core/Texture.h
@@ -121,6 +121,8 @@ struct PaintTexture {
// The array painting writes into (layer -1 = Base: `pixels` without layers).
std::vector<uint8_t>& target(int layer);
size_t bytes() const;
+ // Without the material channel tiles (TileImage::liveBytes counts those, safe from any thread).
+ size_t colourBytes() const;
};
// Bytes of the texture-painting caches shared by every texture on the same UVs (UV tile bins, coverage, texel
diff --git a/src/core/TextureChannels.h b/src/core/TextureChannels.h
index f480a7a7..366de1d8 100644
--- a/src/core/TextureChannels.h
+++ b/src/core/TextureChannels.h
@@ -152,7 +152,9 @@ void channelViewTexels(const PaintTexture& tex, Channel c, int x0, int y0, int x
void syncChannelView(PaintTexture& tex, Channel c);
// --- Memory ---
-// Bytes of material channel images (bases, composites and layer channels).
+// Bytes of material channel images (bases, composites and layer channels). Walks the tiles: call it only where
+// the texture is edited (the main thread), never while a dab may run elsewhere; TileImage::liveBytes is the
+// thread-safe total.
size_t channelBytes(const PaintTexture& tex);
// texture_channels, paint_material, channel_view, texture_layer_channel.
diff --git a/src/core/TileImage.cpp b/src/core/TileImage.cpp
index ea0078f8..ca632760 100644
--- a/src/core/TileImage.cpp
+++ b/src/core/TileImage.cpp
@@ -3,6 +3,7 @@
#include "core/TileImage.h"
#include <algorithm>
+#include <atomic>
#include <cmath>
#include <cstring>
@@ -10,6 +11,8 @@ namespace sf {
namespace {
+std::atomic<size_t> gLiveTileBytes{0}; // TileImage::liveBytes
+
// Writes `value` (component units) as one raw texel of `comps` components of `bits`.
void putTexel(uint8_t* out, const std::array<uint16_t, 4>& value, int comps, int bits) {
if (bits == 16) {
@@ -31,6 +34,10 @@ std::array<uint16_t, 4> readTexel(const uint8_t* p, int comps, int bits) {
} // namespace
+TileImage::Tile::~Tile() { gLiveTileBytes.fetch_sub(counted, std::memory_order_relaxed); }
+
+size_t TileImage::liveBytes() { return gLiveTileBytes.load(std::memory_order_relaxed); }
+
std::array<uint16_t, 4> TileImage::toUnits(int bits, float r, float g, float b, float a) {
const float m = bits == 16 ? 65535.0f : 255.0f;
auto u = [m](float x) { return uint16_t(std::lround(std::clamp(x, 0.0f, 1.0f) * m)); };
@@ -62,6 +69,8 @@ uint8_t* TileImage::writableTile(size_t index) {
const int tb = texelBytes();
for (size_t i = 0; i < size_t(kTileImageTexels); ++i) std::memcpy(&made->data[i * size_t(tb)], px, size_t(tb));
}
+ made->counted = sizeof(Tile) + made->data.size();
+ gLiveTileBytes.fetch_add(made->counted, std::memory_order_relaxed);
t = std::move(made);
return t->data.data();
}
@@ -76,6 +85,8 @@ void TileImage::fillTile(size_t index, const std::array<uint16_t, 4>& value) {
auto t = std::make_shared<Tile>();
t->uniform = true;
t->value = value;
+ t->counted = sizeof(Tile);
+ gLiveTileBytes.fetch_add(t->counted, std::memory_order_relaxed);
tiles_[index] = std::move(t);
}
diff --git a/src/core/TileImage.h b/src/core/TileImage.h
index cca41aa5..19f144ce 100644
--- a/src/core/TileImage.h
+++ b/src/core/TileImage.h
@@ -36,6 +36,11 @@ public:
bool uniform = false;
std::array<uint16_t, 4> value{}; // the uniform tile's texel (in component units: 0-255 or 0-65535)
std::vector<uint8_t> data; // a dense tile: kTileImageTexels * texelBytes(), rows of 64 texels
+ size_t counted = 0; // what this tile added to liveBytes() (taken back when it goes)
+ Tile() = default;
+ Tile(const Tile&) = delete;
+ Tile& operator=(const Tile&) = delete;
+ ~Tile();
};
using TilePtr = std::shared_ptr<Tile>;
@@ -102,6 +107,11 @@ public:
// Same size, format and texels (tiles compared by value).
bool sameTexels(const TileImage& other) const;
+ // Bytes of every tile alive in the process (all images, their copies and undo steps), kept in an atomic
+ // counter as tiles are made and freed: safe to read from any thread at any time (memory readouts), unlike
+ // bytes(), which walks an image's tiles and must run where the image is edited.
+ static size_t liveBytes();
+
// A fill value from components in 0..1.
static std::array<uint16_t, 4> toUnits(int bits, float r, float g = 0, float b = 0, float a = 0);
diff --git a/tests/test_texture_channels.cpp b/tests/test_texture_channels.cpp
index 64c07ec6..255a67e6 100644
--- a/tests/test_texture_channels.cpp
+++ b/tests/test_texture_channels.cpp
@@ -18,7 +18,9 @@
#include <cmath>
#include <cstdio>
#include <cstdlib>
+#include <atomic>
#include <filesystem>
+#include <thread>
using namespace sf;
@@ -687,3 +689,70 @@ TEST(texture_dab_tile_path_matches_triangle_path) {
CHECK(islandPainted > 10000);
CHECK(worst <= 1);
}
+
+TEST(texture_memory_readouts_are_safe_while_painting) {
+ // Memory readouts (TileImage::liveBytes, texturePaintCacheBytes) run while a worker paints material dabs on a
+ // dense mesh (texel tiles, texel maps built on the fly) and edits channels: the counters are atomics, so the
+ // readers never walk tiles that are being replaced. Afterwards every tile is accounted for.
+ const size_t before = TileImage::liveBytes();
+ {
+ Mesh base = makeIcosphere(6);
+ sphericalUvs(base);
+ BVH bvh;
+ bvh.build(base);
+ std::atomic<bool> done{false};
+ std::atomic<size_t> reads{0}, maxSeen{0};
+ std::thread reader([&] {
+ while (!done.load()) {
+ const size_t live = TileImage::liveBytes();
+ const size_t caches = texturePaintCacheBytes();
+ maxSeen = std::max(maxSeen.load(), live + caches);
+ ++reads;
+ }
+ });
+ std::thread painter([&] {
+ for (int round = 0; round < 6; ++round) {
+ Mesh m = base;
+ m.texture = makeTexture(1024, 1024);
+ m.paintToTexture = true;
+ for (Channel c : {Channel::Roughness, Channel::Height, Channel::Normal}) setChannelEnabled(*m.texture, c, true);
+ if (round % 2) addTextureLayer(*m.texture, "Layer");
+ TexturePainter::setPath(round % 3 == 2 ? TexturePainter::Path::Triangles : TexturePainter::Path::Tiles);
+ BrushMaterial mat = rustyMetal();
+ mat.channels = channelBit(Channel::BaseColor) | channelBit(Channel::Roughness) | channelBit(Channel::Height) |
+ channelBit(Channel::Normal);
+ TexturePainter p;
+ CHECK(p.begin(m, mat.channels));
+ DabParams d = dab(glm::vec3(0, 0, 1), 0.3f, 0.7f);
+ d.viewDir = glm::vec3(0, 0, -1);
+ for (int i = 0; i < 8; ++i) {
+ d.center = glm::normalize(glm::vec3(-0.5f + 0.13f * float(i), 0.1f * float(round), 1.0f));
+ p.dab(m, bvh, d, mat, Symmetry{true, false, false});
+ }
+ UndoRecord rec;
+ p.end(rec);
+ if (rec.textureTiles) rec.textureTiles->swapWith(m); // undo swaps tile pointers
+ setChannelEnabled(*m.texture, Channel::Height, false);
+ }
+ TexturePainter::setPath(TexturePainter::Path::Auto);
+ done = true;
+ });
+ painter.join();
+ reader.join();
+ CHECK(reads.load() > 10);
+ CHECK(maxSeen.load() > before);
+ }
+ CHECK(TileImage::liveBytes() == before); // every tile made was freed and taken off the count
+}
+
+TEST(texture_edits_replace_the_texture_object) {
+ // Why the UI defers texture commands until it has drawn: a command that edits the texture gives the mesh a
+ // copy and keeps the old one only as an undo step's packed tiles, so a reference held across the command
+ // points at freed memory (the Texture Map panel crashed this way under load).
+ Document doc = makeDoc(texturedPlane(8, 256));
+ std::weak_ptr<PaintTexture> old = doc.mesh.texture;
+ CommandRegistry reg;
+ CHECK(reg.execute(doc, "texture_channels", {{"enable", std::string("roughness")}}).ok);
+ CHECK(old.expired());
+ CHECK(channelEnabled(*doc.mesh.texture, Channel::Roughness));
+}