Crash when a Lua Hit handler reads attack.weapon for a thrown weapon

Issue Description

When an actor is hit by a thrown weapon (throwing star, dart, knife), any local script whose I.Combat hit handler reads attack.weapon crashes the game (signal 11). No Lua error is logged. The same happens with an arrow or bolt whose launcher was unequipped while the projectile was flying.

This follows up on #8664 (closed) / !4879 (merged). The description of !4879 (merged) says that for thrown weapons "weapon is always going to be an invalid object", but the object is not invalid. It dangles:

  • In ProjectileManager::moveProjectiles (apps/openmw/mwworld/projectilemanager.cpp), bow is projectileRef.getPtr(), a ManualRef on the stack, unless the launcher is still equipped. For thrown weapons it always is the ManualRef. It is passed as both weapon and projectile to MWMechanics::projectileHit, and then to LuaManager::onHit.
  • onHit wraps it in LObject(weapon). The SafePtr constructor stores the raw Ptr together with the current getPtrRegistryRevision().
  • The ManualRef is destroyed at the end of the loop iteration, before the queued local Hit event reaches the handlers.
  • In the handler, SafePtr::update() only re-resolves the Ptr if the registry revision has changed. Usually it has not, so ptrOrEmpty() returns the dangling Ptr, and any access (recordId, types.Weapon.objectIsInstance, types.Weapon.record) is a use-after-free.

The engine's own _onHit is safe because it reads ammo first (apps/openmw/mwlua/types/actor.cpp). Mods that look at the weapon of a ranged hit, for example to find its skill or type, crash instead.

A possible fix: in LuaManager::onHit, don't pass weapon when it is the projectile itself (weapon == ammo, which covers both thrown weapons and ammunition without a launcher). Alternatively, pass it as a record id, as is already done for ammo.

Steps to Reproduce

  1. Add a content file with this script, attached to actors:
    • test.omwscripts: NPC, CREATURE: scripts/hitcrash.lua
    • scripts/hitcrash.lua:
      local I = require('openmw.interfaces')
      I.Combat.addOnHitHandler(function(attack)
          if attack.weapon then print(attack.weapon.recordId) end
      end)
  2. Equip a stack of throwing stars (e.g. player->additem chitin throwing star 20).
  3. Throw one at an NPC or a creature and hit it.
  4. The game crashes with signal 11.

Melee hits, and arrows or bolts shot from a launcher that is still equipped, print the weapon id as expected.

System Information

  • OpenMW Version: 0.52.0 RC1 (4ff261bf). The same code is on master (77eec047), and has been since !4879 (merged) (0.50).
  • Operating System & Version: Linux (Ubuntu)
  • GPU: Intel Graphics
  • Morrowind Version: CD
  • Language: English
  • Addons: Tribunal, Bloodmoon, Expanded Vanilla

Mods

  • Are you using mods? Yes. Found with a Lua mod (Natural Character Growth) whose hit handler read attack.weapon. The script above reproduces it without that mod.
  • Are you using any shader mods? Yes

Logs and Settings

openmw.log ends with no error. The crash handler only reports Fatal Error (signal 11), Address: (nil), with no backtrace (gdb timed out while downloading debug info).

openmw-crash.log

openmw.cfg

settings.cfg