Fix Navigation agent callback wild pointer crash

Fixes crash in sanitizer builds when callback agent or object are already freed.
This commit is contained in:
smix8 2022-12-05 23:05:56 +01:00
parent f3e6750a7e
commit 194c1c44e0
10 changed files with 25 additions and 25 deletions

View File

@ -40,12 +40,12 @@
<method name="agent_set_callback" qualifiers="const"> <method name="agent_set_callback" qualifiers="const">
<return type="void" /> <return type="void" />
<param index="0" name="agent" type="RID" /> <param index="0" name="agent" type="RID" />
<param index="1" name="receiver" type="Object" /> <param index="1" name="object_id" type="int" />
<param index="2" name="method" type="StringName" /> <param index="2" name="method" type="StringName" />
<param index="3" name="userdata" type="Variant" default="null" /> <param index="3" name="userdata" type="Variant" default="null" />
<description> <description>
Callback called at the end of the RVO process. If a callback is created manually and the agent is placed on a navigation map it will calculate avoidance for the agent and dispatch the calculated [code]safe_velocity[/code] to the [param receiver] object with a signal to the chosen [param method] name. Sets the callback [param object_id] and [param method] that gets called after each avoidance processing step for the [param agent]. The calculated [code]safe_velocity[/code] will be dispatched with a signal to the object just before the physics calculations.
[b]Note:[/b] Created callbacks are always processed independently of the SceneTree state as long as the agent is on a navigation map and not freed. To disable the dispatch of a callback from an agent use [method agent_set_callback] again with a [code]null[/code] object as the [param receiver]. [b]Note:[/b] Created callbacks are always processed independently of the SceneTree state as long as the agent is on a navigation map and not freed. To disable the dispatch of a callback from an agent use [method agent_set_callback] again with a [code]0[/code] ObjectID as the [param object_id].
</description> </description>
</method> </method>
<method name="agent_set_map" qualifiers="const"> <method name="agent_set_map" qualifiers="const">

View File

@ -40,12 +40,12 @@
<method name="agent_set_callback" qualifiers="const"> <method name="agent_set_callback" qualifiers="const">
<return type="void" /> <return type="void" />
<param index="0" name="agent" type="RID" /> <param index="0" name="agent" type="RID" />
<param index="1" name="receiver" type="Object" /> <param index="1" name="object_id" type="int" />
<param index="2" name="method" type="StringName" /> <param index="2" name="method" type="StringName" />
<param index="3" name="userdata" type="Variant" default="null" /> <param index="3" name="userdata" type="Variant" default="null" />
<description> <description>
Callback called at the end of the RVO process. If a callback is created manually and the agent is placed on a navigation map it will calculate avoidance for the agent and dispatch the calculated [code]safe_velocity[/code] to the [param receiver] object with a signal to the chosen [param method] name. Sets the callback [param object_id] and [param method] that gets called after each avoidance processing step for the [param agent]. The calculated [code]safe_velocity[/code] will be dispatched with a signal to the object just before the physics calculations.
[b]Note:[/b] Created callbacks are always processed independently of the SceneTree state as long as the agent is on a navigation map and not freed. To disable the dispatch of a callback from an agent use [method agent_set_callback] again with a [code]null[/code] object as the [param receiver]. [b]Note:[/b] Created callbacks are always processed independently of the SceneTree state as long as the agent is on a navigation map and not freed. To disable the dispatch of a callback from an agent use [method agent_set_callback] again with a [code]0[/code] ObjectID as the [param object_id].
</description> </description>
</method> </method>
<method name="agent_set_map" qualifiers="const"> <method name="agent_set_map" qualifiers="const">

View File

@ -676,14 +676,14 @@ bool GodotNavigationServer::agent_is_map_changed(RID p_agent) const {
return agent->is_map_changed(); return agent->is_map_changed();
} }
COMMAND_4(agent_set_callback, RID, p_agent, Object *, p_receiver, StringName, p_method, Variant, p_udata) { COMMAND_4(agent_set_callback, RID, p_agent, ObjectID, p_object_id, StringName, p_method, Variant, p_udata) {
RvoAgent *agent = agent_owner.get_or_null(p_agent); RvoAgent *agent = agent_owner.get_or_null(p_agent);
ERR_FAIL_COND(agent == nullptr); ERR_FAIL_COND(agent == nullptr);
agent->set_callback(p_receiver == nullptr ? ObjectID() : p_receiver->get_instance_id(), p_method, p_udata); agent->set_callback(p_object_id, p_method, p_udata);
if (agent->get_map()) { if (agent->get_map()) {
if (p_receiver == nullptr) { if (p_object_id == ObjectID()) {
agent->get_map()->remove_agent_as_controlled(agent); agent->get_map()->remove_agent_as_controlled(agent);
} else { } else {
agent->get_map()->set_agent_as_controlled(agent); agent->get_map()->set_agent_as_controlled(agent);

View File

@ -167,7 +167,7 @@ public:
COMMAND_2(agent_set_position, RID, p_agent, Vector3, p_position); COMMAND_2(agent_set_position, RID, p_agent, Vector3, p_position);
COMMAND_2(agent_set_ignore_y, RID, p_agent, bool, p_ignore); COMMAND_2(agent_set_ignore_y, RID, p_agent, bool, p_ignore);
virtual bool agent_is_map_changed(RID p_agent) const override; virtual bool agent_is_map_changed(RID p_agent) const override;
COMMAND_4_DEF(agent_set_callback, RID, p_agent, Object *, p_receiver, StringName, p_method, Variant, p_udata, Variant()); COMMAND_4_DEF(agent_set_callback, RID, p_agent, ObjectID, p_object_id, StringName, p_method, Variant, p_udata, Variant());
COMMAND_1(free, RID, p_object); COMMAND_1(free, RID, p_object);

View File

@ -197,9 +197,9 @@ NavigationAgent2D::~NavigationAgent2D() {
void NavigationAgent2D::set_avoidance_enabled(bool p_enabled) { void NavigationAgent2D::set_avoidance_enabled(bool p_enabled) {
avoidance_enabled = p_enabled; avoidance_enabled = p_enabled;
if (avoidance_enabled) { if (avoidance_enabled) {
NavigationServer2D::get_singleton()->agent_set_callback(agent, this, "_avoidance_done"); NavigationServer2D::get_singleton()->agent_set_callback(agent, get_instance_id(), "_avoidance_done");
} else { } else {
NavigationServer2D::get_singleton()->agent_set_callback(agent, nullptr, "_avoidance_done"); NavigationServer2D::get_singleton()->agent_set_callback(agent, ObjectID(), "_avoidance_done");
} }
} }
@ -209,7 +209,7 @@ bool NavigationAgent2D::get_avoidance_enabled() const {
void NavigationAgent2D::set_agent_parent(Node *p_agent_parent) { void NavigationAgent2D::set_agent_parent(Node *p_agent_parent) {
// remove agent from any avoidance map before changing parent or there will be leftovers on the RVO map // remove agent from any avoidance map before changing parent or there will be leftovers on the RVO map
NavigationServer2D::get_singleton()->agent_set_callback(agent, nullptr, "_avoidance_done"); NavigationServer2D::get_singleton()->agent_set_callback(agent, ObjectID(), "_avoidance_done");
if (Object::cast_to<Node2D>(p_agent_parent) != nullptr) { if (Object::cast_to<Node2D>(p_agent_parent) != nullptr) {
// place agent on navigation map first or else the RVO agent callback creation fails silently later // place agent on navigation map first or else the RVO agent callback creation fails silently later
agent_parent = Object::cast_to<Node2D>(p_agent_parent); agent_parent = Object::cast_to<Node2D>(p_agent_parent);

View File

@ -204,9 +204,9 @@ NavigationAgent3D::~NavigationAgent3D() {
void NavigationAgent3D::set_avoidance_enabled(bool p_enabled) { void NavigationAgent3D::set_avoidance_enabled(bool p_enabled) {
avoidance_enabled = p_enabled; avoidance_enabled = p_enabled;
if (avoidance_enabled) { if (avoidance_enabled) {
NavigationServer3D::get_singleton()->agent_set_callback(agent, this, "_avoidance_done"); NavigationServer3D::get_singleton()->agent_set_callback(agent, get_instance_id(), "_avoidance_done");
} else { } else {
NavigationServer3D::get_singleton()->agent_set_callback(agent, nullptr, "_avoidance_done"); NavigationServer3D::get_singleton()->agent_set_callback(agent, ObjectID(), "_avoidance_done");
} }
} }
@ -216,7 +216,7 @@ bool NavigationAgent3D::get_avoidance_enabled() const {
void NavigationAgent3D::set_agent_parent(Node *p_agent_parent) { void NavigationAgent3D::set_agent_parent(Node *p_agent_parent) {
// remove agent from any avoidance map before changing parent or there will be leftovers on the RVO map // remove agent from any avoidance map before changing parent or there will be leftovers on the RVO map
NavigationServer3D::get_singleton()->agent_set_callback(agent, nullptr, "_avoidance_done"); NavigationServer3D::get_singleton()->agent_set_callback(agent, ObjectID(), "_avoidance_done");
if (Object::cast_to<Node3D>(p_agent_parent) != nullptr) { if (Object::cast_to<Node3D>(p_agent_parent) != nullptr) {
// place agent on navigation map first or else the RVO agent callback creation fails silently later // place agent on navigation map first or else the RVO agent callback creation fails silently later
agent_parent = Object::cast_to<Node3D>(p_agent_parent); agent_parent = Object::cast_to<Node3D>(p_agent_parent);

View File

@ -140,10 +140,6 @@ static Transform3D trf2_to_trf3(const Transform2D &d) {
return Transform3D(b, o); return Transform3D(b, o);
} }
static Object *obj_to_obj(Object *d) {
return d;
}
static StringName sn_to_sn(const StringName &d) { static StringName sn_to_sn(const StringName &d) {
return d; return d;
} }
@ -152,6 +148,10 @@ static Variant var_to_var(const Variant &d) {
return d; return d;
} }
static ObjectID id_to_id(const ObjectID &id) {
return id;
}
static Ref<NavigationMesh> poly_to_mesh(Ref<NavigationPolygon> d) { static Ref<NavigationMesh> poly_to_mesh(Ref<NavigationPolygon> d) {
if (d.is_valid()) { if (d.is_valid()) {
return d->get_mesh(); return d->get_mesh();
@ -289,7 +289,7 @@ void NavigationServer2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("agent_set_target_velocity", "agent", "target_velocity"), &NavigationServer2D::agent_set_target_velocity); ClassDB::bind_method(D_METHOD("agent_set_target_velocity", "agent", "target_velocity"), &NavigationServer2D::agent_set_target_velocity);
ClassDB::bind_method(D_METHOD("agent_set_position", "agent", "position"), &NavigationServer2D::agent_set_position); ClassDB::bind_method(D_METHOD("agent_set_position", "agent", "position"), &NavigationServer2D::agent_set_position);
ClassDB::bind_method(D_METHOD("agent_is_map_changed", "agent"), &NavigationServer2D::agent_is_map_changed); ClassDB::bind_method(D_METHOD("agent_is_map_changed", "agent"), &NavigationServer2D::agent_is_map_changed);
ClassDB::bind_method(D_METHOD("agent_set_callback", "agent", "receiver", "method", "userdata"), &NavigationServer2D::agent_set_callback, DEFVAL(Variant())); ClassDB::bind_method(D_METHOD("agent_set_callback", "agent", "object_id", "method", "userdata"), &NavigationServer2D::agent_set_callback, DEFVAL(Variant()));
ClassDB::bind_method(D_METHOD("free_rid", "rid"), &NavigationServer2D::free); ClassDB::bind_method(D_METHOD("free_rid", "rid"), &NavigationServer2D::free);
@ -408,7 +408,7 @@ void FORWARD_2_C(agent_set_ignore_y, RID, p_agent, bool, p_ignore, rid_to_rid, b
bool FORWARD_1_C(agent_is_map_changed, RID, p_agent, rid_to_rid); bool FORWARD_1_C(agent_is_map_changed, RID, p_agent, rid_to_rid);
void FORWARD_4_C(agent_set_callback, RID, p_agent, Object *, p_receiver, StringName, p_method, Variant, p_udata, rid_to_rid, obj_to_obj, sn_to_sn, var_to_var); void FORWARD_4_C(agent_set_callback, RID, p_agent, ObjectID, p_object_id, StringName, p_method, Variant, p_udata, rid_to_rid, id_to_id, sn_to_sn, var_to_var);
void FORWARD_1_C(free, RID, p_object, rid_to_rid); void FORWARD_1_C(free, RID, p_object, rid_to_rid);

View File

@ -218,7 +218,7 @@ public:
virtual bool agent_is_map_changed(RID p_agent) const; virtual bool agent_is_map_changed(RID p_agent) const;
/// Callback called at the end of the RVO process /// Callback called at the end of the RVO process
virtual void agent_set_callback(RID p_agent, Object *p_receiver, StringName p_method, Variant p_udata = Variant()) const; virtual void agent_set_callback(RID p_agent, ObjectID p_object_id, StringName p_method, Variant p_udata = Variant()) const;
virtual void query_path(const Ref<NavigationPathQueryParameters2D> &p_query_parameters, Ref<NavigationPathQueryResult2D> p_query_result) const; virtual void query_path(const Ref<NavigationPathQueryParameters2D> &p_query_parameters, Ref<NavigationPathQueryResult2D> p_query_result) const;

View File

@ -109,7 +109,7 @@ void NavigationServer3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("agent_set_target_velocity", "agent", "target_velocity"), &NavigationServer3D::agent_set_target_velocity); ClassDB::bind_method(D_METHOD("agent_set_target_velocity", "agent", "target_velocity"), &NavigationServer3D::agent_set_target_velocity);
ClassDB::bind_method(D_METHOD("agent_set_position", "agent", "position"), &NavigationServer3D::agent_set_position); ClassDB::bind_method(D_METHOD("agent_set_position", "agent", "position"), &NavigationServer3D::agent_set_position);
ClassDB::bind_method(D_METHOD("agent_is_map_changed", "agent"), &NavigationServer3D::agent_is_map_changed); ClassDB::bind_method(D_METHOD("agent_is_map_changed", "agent"), &NavigationServer3D::agent_is_map_changed);
ClassDB::bind_method(D_METHOD("agent_set_callback", "agent", "receiver", "method", "userdata"), &NavigationServer3D::agent_set_callback, DEFVAL(Variant())); ClassDB::bind_method(D_METHOD("agent_set_callback", "agent", "object_id", "method", "userdata"), &NavigationServer3D::agent_set_callback, DEFVAL(Variant()));
ClassDB::bind_method(D_METHOD("free_rid", "rid"), &NavigationServer3D::free); ClassDB::bind_method(D_METHOD("free_rid", "rid"), &NavigationServer3D::free);

View File

@ -233,7 +233,7 @@ public:
virtual bool agent_is_map_changed(RID p_agent) const = 0; virtual bool agent_is_map_changed(RID p_agent) const = 0;
/// Callback called at the end of the RVO process /// Callback called at the end of the RVO process
virtual void agent_set_callback(RID p_agent, Object *p_receiver, StringName p_method, Variant p_udata = Variant()) const = 0; virtual void agent_set_callback(RID p_agent, ObjectID p_object_id, StringName p_method, Variant p_udata = Variant()) const = 0;
/// Destroy the `RID` /// Destroy the `RID`
virtual void free(RID p_object) const = 0; virtual void free(RID p_object) const = 0;