Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 67 additions & 10 deletions include/modlock/gameinterop/ability_owner_guard.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ struct OwnedAbility {
void* entity = nullptr;
// index is the ability's entity index.
uint32_t index = 0;
// handle is the ability's own handle, index and serial, or the invalid
// handle when its identity cannot prove one.
uint32_t handle = kInvalidEHandleIndex;
// owner is the handle m_hOwnerEntity holds.
uint32_t owner = kInvalidEHandleIndex;
// state is what the handle names.
Expand All @@ -62,22 +65,76 @@ using AbilityClasses = std::unordered_map<const void*, bool>;
const BoundedReader& read,
AbilityClasses& classes);

// HeldAbilities remembers, for each player slot, every ability the guard let
// through to that player's client, by index and serial. Such a client may
// hold the ability: the server counts an entity it sent as held, and clearing
// its bit later makes the client drop an entity the server still updates,
// which closes the client with "CopyExistingEntity: missing client entity".
// A record lasts until the ability leaves the world, another entity takes its
// index, or the slot's connection ends.
class MODLOCK_API HeldAbilities {
public:
// Track starts a pass for slot. A slot no connection occupies, or occupied
// by a connection of another generation than the one recorded, holds
// nothing.
void Track(int32_t slot, bool occupied, uint32_t generation);

// Holds reports whether the client in slot may hold ability. An ability
// whose own handle is unknown is held when anything at its index is.
[[nodiscard]] bool Holds(int32_t slot, const OwnedAbility& ability) const;

// Hold records that the client in slot receives ability.
void Hold(int32_t slot, const OwnedAbility& ability);

// Prune forgets every record whose ability is not in abilities, the
// abilities in the world now, with the same handle.
void Prune(std::span<const OwnedAbility> abilities);

// Count returns how many abilities the client in slot may hold.
[[nodiscard]] size_t Count(int32_t slot) const;

private:
// Slot is one connection's records: the ability handle at each index.
struct Slot {
uint32_t generation = 0;
std::unordered_map<uint32_t, uint32_t> handles;
};

std::unordered_map<int32_t, Slot> slots_;
};

// GuardedAbilities is what one pass of the guard did to one player's set.
struct GuardedAbilities {
// withheld lists the ownerless abilities it kept from the client.
std::vector<OwnedAbility> withheld;
// kept lists the ownerless abilities it still sent, since the client may
// hold them.
std::vector<OwnedAbility> kept;
};

// WithholdOwnerless withholds from set each ability it sends whose owner is
// not a live entity it also sends, and returns those abilities.
// not a live entity it also sends, unless held says the client may already
// hold that ability. It records in held every ability the set still sends.
// held must be tracked for the set's slot first.
//
// The client resolves an ability's owner handle against the entities it
// holds, and its ability code reads through the result without a null check:
// an ability that arrives without its owner crashes the client in the
// ability's post data update. The client creates
// every entity of an update before it runs their post data update callbacks,
// so an owner sent in the same update is enough.
[[nodiscard]] MODLOCK_API std::vector<OwnedAbility> WithholdOwnerless(
TransmitSet& set, std::span<const OwnedAbility> abilities);
// an ability that arrives without its owner crashes the client. The crash
// dumps place the fault in the subclass change callback the client runs as it
// creates the ability. The client creates every entity of an update before it
// runs their post data update callbacks, so an owner sent in the same update
// is enough. An ability the client already holds is not created again, and
// clearing its bit would make the client drop an entity the server still
// counts as held, so it stays.
[[nodiscard]] MODLOCK_API GuardedAbilities
WithholdOwnerless(TransmitSet& set, std::span<const OwnedAbility> abilities, HeldAbilities& held);

// AbilityOwnerGuard keeps every ability from a player whose update does not
// also send its owner, whatever the game or a mod did to the sets. It runs as
// the transmit hook's final handler, after every mod's handler, and only
// clears bits. It logs each ability it starts withholding from a player.
// also send its owner, whatever the game or a mod did to the sets, unless that
// player's client may already hold it. It runs as the transmit hook's final
// handler, after every mod's handler, and only clears bits. It logs each
// ability it starts withholding from a player, and each it keeps sending
// because the client may hold it.
// Install and destruction belong to the engine thread.
class MODLOCK_API AbilityOwnerGuard {
public:
Expand Down
141 changes: 124 additions & 17 deletions src/gameinterop/ability_owner_guard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <unordered_map>
#include <utility>

#include "modlock/gameinterop/connection_tracker.h"
#include "modlock/gameinterop/entity_abi.h"

namespace modlock::gameinterop {
Expand Down Expand Up @@ -94,6 +95,17 @@ OwnerState OwnerStateOf(const void* entity_system, uint32_t handle, const Bounde
return (*flags & kEfDeleteFlags) != 0 ? OwnerState::kDeleting : OwnerState::kLive;
}

// HandleOf reads the handle of the entity at identity, index and serial, even
// while the game deletes it, or the invalid handle when it cannot be read.
uint32_t HandleOf(const uint8_t* identity, const BoundedReader& read) {
const auto stored = ReadValue<uint32_t>(identity, kIdentityEHandleOffset, read);
const auto flags = ReadValue<uint32_t>(identity, kIdentityFlagsOffset, read);
if (!stored || !flags) {
return kInvalidEHandleIndex;
}
return LiveHandleOf(*stored, *flags & ~kEfDeleteFlags).value_or(kInvalidEHandleIndex);
}

// IsAbilityClass reports whether entity_class binds a schema class deriving
// from CCitadelBaseAbility, remembering the answer for the schema class.
bool IsAbilityClass(const void* entity_class, const BoundedReader& read, AbilityClasses& classes) {
Expand Down Expand Up @@ -149,6 +161,7 @@ std::vector<OwnedAbility> FindOwnedAbilities(const void* entity_system, size_t o
abilities.push_back(OwnedAbility{
.entity = identity->instance,
.index = chunk * kMaxEntitiesInList + slot,
.handle = HandleOf(entries + slot * kIdentitySize, read),
.owner = owner,
.state = OwnerStateOf(entity_system, owner, read),
});
Expand All @@ -157,20 +170,97 @@ std::vector<OwnedAbility> FindOwnedAbilities(const void* entity_system, size_t o
return abilities;
}

std::vector<OwnedAbility> WithholdOwnerless(TransmitSet& set,
std::span<const OwnedAbility> abilities) {
std::vector<OwnedAbility> withheld;
void HeldAbilities::Track(int32_t slot, bool occupied, uint32_t generation) {
// A slot without its connection holds nothing.
if (!occupied) {
slots_.erase(slot);
return;
}

// A new connection in the slot starts with nothing.
auto [entry, added] = slots_.try_emplace(slot);
if (!added && entry->second.generation != generation) {
entry->second.handles.clear();
}
entry->second.generation = generation;
}

bool HeldAbilities::Holds(int32_t slot, const OwnedAbility& ability) const {
const auto found = slots_.find(slot);
if (found == slots_.end()) {
return false;
}
const auto held = found->second.handles.find(ability.index);
if (held == found->second.handles.end()) {
return false;
}
return ability.handle == kInvalidEHandleIndex || held->second == ability.handle;
}

void HeldAbilities::Hold(int32_t slot, const OwnedAbility& ability) {
// An untracked slot keeps no records.
const auto found = slots_.find(slot);
if (found == slots_.end()) {
return;
}

// An unknown handle keeps a record of the ability before it.
auto [held, added] = found->second.handles.try_emplace(ability.index, ability.handle);
if (ability.handle != kInvalidEHandleIndex) {
held->second = ability.handle;
}
}

void HeldAbilities::Prune(std::span<const OwnedAbility> abilities) {
// Index the abilities in the world by their entity index.
std::unordered_map<uint32_t, uint32_t> present;
present.reserve(abilities.size());
for (const auto& ability : abilities) {
present.insert_or_assign(ability.index, ability.handle);
}

// Forget each record whose ability left the world or whose index another
// ability took. An unknown handle proves neither.
for (auto& [slot, records] : slots_) {
std::erase_if(records.handles, [&](const auto& record) {
const auto found = present.find(record.first);
if (found == present.end()) {
return true;
}
return found->second != kInvalidEHandleIndex && record.second != kInvalidEHandleIndex &&
found->second != record.second;
});
}
}

size_t HeldAbilities::Count(int32_t slot) const {
const auto found = slots_.find(slot);
return found == slots_.end() ? 0 : found->second.handles.size();
}

GuardedAbilities WithholdOwnerless(TransmitSet& set, std::span<const OwnedAbility> abilities,
HeldAbilities& held) {
GuardedAbilities guarded;
const int32_t slot = set.Slot();
for (const auto& ability : abilities) {
if (!set.Sends(ability.index)) {
continue;
}
if (ability.state == OwnerState::kLive && set.Sends(ability.owner)) {

// Withhold an ownerless ability the client cannot hold yet; one it may
// hold stays, since clearing its bit drops it on the client only.
const bool owned = ability.state == OwnerState::kLive && set.Sends(ability.owner);
if (!owned && !held.Holds(slot, ability)) {
set.Withhold(ability.index);
guarded.withheld.push_back(ability);
continue;
}
set.Withhold(ability.index);
withheld.push_back(ability);
if (!owned) {
guarded.kept.push_back(ability);
}
held.Hold(slot, ability);
}
return withheld;
return guarded;
}

// State is the guard's memory across passes, shared with its handler.
Expand All @@ -187,9 +277,11 @@ struct AbilityOwnerGuard::State {
bool reported_offset = false;
bool reported_system = false;
uint64_t pass = 0;
// seen holds the pass in which each (slot, ability index) withhold last
// seen holds the pass in which each (kind, slot, ability index) report last
// happened.
std::unordered_map<uint64_t, uint64_t> seen;
// held records the abilities each player's client may hold.
HeldAbilities held;
std::chrono::steady_clock::time_point minute{};
int lines = 0;
size_t unlogged = 0;
Expand Down Expand Up @@ -220,10 +312,12 @@ struct AbilityOwnerGuard::State {
return owner_offset;
}

// Log reports one withhold, unless it was logged within the last second or
// the minute's lines are spent.
void Log(int32_t slot, const OwnedAbility& ability) {
const uint64_t key = (uint64_t{static_cast<uint32_t>(slot)} << 32) | ability.index;
// Log reports one withhold, or one ability kept because the client may
// hold it, unless the same report for that player and ability was logged
// within the last second or the minute's lines are spent.
void Log(int32_t slot, const OwnedAbility& ability, bool kept) {
const uint64_t key =
(uint64_t{kept} << 63) | (uint64_t{static_cast<uint32_t>(slot)} << 32) | ability.index;
auto [entry, added] = seen.try_emplace(key, pass);
const bool lapsed = added || pass - entry->second > kRelogPasses;
entry->second = pass;
Expand All @@ -247,9 +341,11 @@ struct AbilityOwnerGuard::State {
++lines;
char owner[16];
std::snprintf(owner, sizeof(owner), "0x%08x", ability.owner);
std::cerr << "[modlock] ability owner guard: withheld " << DesignerName(ability.entity)
<< " (entity " << ability.index << ") from slot " << slot << ": owner " << owner
<< ' ' << OwnerStateName(ability.state)
std::cerr << "[modlock] ability owner guard: " << (kept ? "kept " : "withheld ")
<< DesignerName(ability.entity) << " (entity " << ability.index << ") "
<< (kept ? "for slot " : "from slot ") << slot
<< (kept ? ", whose client may hold it" : "") << ": owner " << owner << ' '
<< OwnerStateName(ability.state)
<< (ability.state == OwnerState::kLive ? ", not sent" : "") << '\n';
}

Expand All @@ -271,9 +367,20 @@ struct AbilityOwnerGuard::State {
return;
}
const auto abilities = FindOwnedAbilities(*system, *offset, &ReadNative, classes);
held.Prune(abilities);
for (auto& set : sets) {
for (const auto& ability : WithholdOwnerless(set, abilities)) {
Log(set.Slot(), ability);
// A slot's records belong to its current connection.
const int32_t slot = set.Slot();
const auto connection = ConnectionTracker::StateForSlot(slot);
held.Track(slot, connection.occupied, connection.generation);

// Withhold what the client cannot hold yet, and report both kinds.
const auto guarded = WithholdOwnerless(set, abilities, held);
for (const auto& ability : guarded.withheld) {
Log(slot, ability, false);
}
for (const auto& ability : guarded.kept) {
Log(slot, ability, true);
}
}

Expand Down
Loading
Loading