From a439da789d0a362cff2f24df2c59674ed73d0035 Mon Sep 17 00:00:00 2001 From: Jonas Perolini Date: Fri, 6 Nov 2026 18:43:44 +0100 Subject: [PATCH] fix(navigator): move geofence polygon scratch storage off the stack (#29649) * fix(navigator): move geofence polygon scratch buffer to .bss A maximum-size 64-vertex polygon consumes 512 bytes of temporary stack storage. Reserve the bounded scratch array in .bss to protect navigator stack headroom without heap allocation or callback indirection. On px4_fmu-v6c_default with GCC 13.2.1 (-Os), the function's own frame at 64 vertices falls from 608 to 80 bytes. Static RAM increases by 512 bytes and firmware flash decreases by 40 bytes. Projection and geometry remain unchanged; document serialized access and reject oversized input. Signed-off-by: Jonas Perolini Assisted-by: Codex * refactor(navigator): move vertice array as class member * refactor(navigator): clean comment --------- Signed-off-by: Jonas Perolini --- .../GeofenceAvoidancePlannerTest.cpp | 121 ++++++++++++++++++ .../RTLPlanner/geofence_avoidance_planner.cpp | 11 +- .../RTLPlanner/geofence_avoidance_planner.h | 3 + 3 files changed, 131 insertions(+), 4 deletions(-) diff --git a/src/modules/navigator/RTLPlanner/GeofenceAvoidancePlannerTest.cpp b/src/modules/navigator/RTLPlanner/GeofenceAvoidancePlannerTest.cpp index ab3bf936257..97c60cb0027 100644 --- a/src/modules/navigator/RTLPlanner/GeofenceAvoidancePlannerTest.cpp +++ b/src/modules/navigator/RTLPlanner/GeofenceAvoidancePlannerTest.cpp @@ -202,6 +202,127 @@ TEST_F(GeofenceAvoidancePlannerTest, PathAroundExclusionZone) EXPECT_NEAR(last_wp(1), vertices[2](1), 1e-3); } +TEST_F(GeofenceAvoidancePlannerTest, RebuildingDifferentFenceRestoresOriginalPath) +{ + FakeGeofence original(kExclusionVertices, 4, NAV_CMD_FENCE_POLYGON_VERTEX_EXCLUSION); + _planner.updateGraphFromGeofence(original, 0.f); + _planner.updateDestination(kExclusionDestination); + ASSERT_EQ(_planner.updateStartAndFillPath(kExclusionStart), 2); + const matrix::Vector2d expected[] = {_planner.waypointAtIndex(0), _planner.waypointAtIndex(1)}; + + // Rebuild with a different graph and projection reference, away from the flight path. + matrix::Vector2d shifted[4]; + + for (int i = 0; i < 4; ++i) { + shifted[i] = kExclusionVertices[i] + matrix::Vector2d{0.01, 0.01}; + } + + FakeGeofence different(shifted, 4, NAV_CMD_FENCE_POLYGON_VERTEX_EXCLUSION); + _planner.updateGraphFromGeofence(different, 0.f); + _planner.updateDestination(kExclusionDestination); + ASSERT_EQ(_planner.status(), GeofenceAvoidancePlanner::Status::Success); + ASSERT_EQ(_planner.updateStartAndFillPath(kExclusionStart), 0); + EXPECT_FALSE(_planner.needsStraightLineFallback()); + + _planner.updateGraphFromGeofence(original, 0.f); + _planner.updateDestination(kExclusionDestination); + ASSERT_EQ(_planner.status(), GeofenceAvoidancePlanner::Status::Success); + ASSERT_EQ(_planner.updateStartAndFillPath(kExclusionStart), 2); + EXPECT_FALSE(_planner.needsStraightLineFallback()); + + for (int i = 0; i < 2; ++i) { + EXPECT_DOUBLE_EQ(_planner.waypointAtIndex(i)(0), expected[i](0)); + EXPECT_DOUBLE_EQ(_planner.waypointAtIndex(i)(1), expected[i](1)); + } +} + +TEST_F(GeofenceAvoidancePlannerTest, RebuildingAfterRejectedFenceRestoresOriginalPath) +{ + FakeGeofence original(kExclusionVertices, 4, NAV_CMD_FENCE_POLYGON_VERTEX_EXCLUSION); + _planner.updateGraphFromGeofence(original, 0.f); + _planner.updateDestination(kExclusionDestination); + ASSERT_EQ(_planner.updateStartAndFillPath(kExclusionStart), 2); + const matrix::Vector2d expected[] = {_planner.waypointAtIndex(0), _planner.waypointAtIndex(1)}; + + const matrix::Vector2d duplicate[] = { + kExclusionVertices[0], kExclusionVertices[1], kExclusionVertices[1], kExclusionVertices[3] + }; + FakeGeofence invalid(duplicate, 4, NAV_CMD_FENCE_POLYGON_VERTEX_EXCLUSION); + _planner.updateGraphFromGeofence(invalid, 0.f); + ASSERT_EQ(_planner.status(), GeofenceAvoidancePlanner::Status::Degenerate); + _planner.updateDestination(kExclusionDestination); + ASSERT_EQ(_planner.updateStartAndFillPath(kExclusionStart), 0); + EXPECT_TRUE(_planner.needsStraightLineFallback()); + + _planner.updateGraphFromGeofence(original, 0.f); + _planner.updateDestination(kExclusionDestination); + ASSERT_EQ(_planner.status(), GeofenceAvoidancePlanner::Status::Success); + ASSERT_EQ(_planner.updateStartAndFillPath(kExclusionStart), 2); + EXPECT_FALSE(_planner.needsStraightLineFallback()); + + for (int i = 0; i < 2; ++i) { + EXPECT_DOUBLE_EQ(_planner.waypointAtIndex(i)(0), expected[i](0)); + EXPECT_DOUBLE_EQ(_planner.waypointAtIndex(i)(1), expected[i](1)); + } +} + +TEST_F(GeofenceAvoidancePlannerTest, PathAroundMaximumVertexPolygon) +{ + const matrix::Vector2d center{47.355, 8.519}; + constexpr double radius_degrees = 0.001; + matrix::Vector2d vertices[DM_KEY_FENCE_POINTS_MAX]; + + for (int i = 0; i < DM_KEY_FENCE_POINTS_MAX; ++i) { + const double angle = 2.0 * M_PI * i / DM_KEY_FENCE_POINTS_MAX; + vertices[i] = center + matrix::Vector2d{radius_degrees * cos(angle), radius_degrees * sin(angle)}; + } + + FakeGeofence fence(vertices, DM_KEY_FENCE_POINTS_MAX, NAV_CMD_FENCE_POLYGON_VERTEX_EXCLUSION); + _planner.updateGraphFromGeofence(fence, 5.f); + ASSERT_EQ(_planner.status(), GeofenceAvoidancePlanner::Status::Success); + + // The direct route between these points crosses the exclusion polygon. + const matrix::Vector2d start = center - matrix::Vector2d{2.0 * radius_degrees, 0.0}; + const matrix::Vector2d destination = center + matrix::Vector2d{2.0 * radius_degrees, 0.0}; + _planner.updateDestination(destination); + ASSERT_EQ(_planner.status(), GeofenceAvoidancePlanner::Status::Success); + EXPECT_GT(_planner.updateStartAndFillPath(start), 0); + EXPECT_FALSE(_planner.needsStraightLineFallback()); +} + +TEST_F(GeofenceAvoidancePlannerTest, OversizedPolygonRejectsPreviousPathAndAllowsRecovery) +{ + FakeGeofence original(kExclusionVertices, 4, NAV_CMD_FENCE_POLYGON_VERTEX_EXCLUSION); + _planner.updateGraphFromGeofence(original, 0.f); + _planner.updateDestination(kExclusionDestination); + ASSERT_EQ(_planner.updateStartAndFillPath(kExclusionStart), 2); + + constexpr int num_vertices = DM_KEY_FENCE_POINTS_MAX + 1; + const matrix::Vector2d center{47.355, 8.519}; + constexpr double radius_degrees = 0.001; + matrix::Vector2d vertices[num_vertices]; + + for (int i = 0; i < num_vertices; ++i) { + const double angle = 2.0 * M_PI * i / num_vertices; + vertices[i] = center + matrix::Vector2d{radius_degrees * cos(angle), radius_degrees * sin(angle)}; + } + + FakeGeofence oversized(vertices, num_vertices, NAV_CMD_FENCE_POLYGON_VERTEX_EXCLUSION); + _planner.updateGraphFromGeofence(oversized, 0.f); + // The graph budget fits; the input exceeds the maximum storable polygon size. + ASSERT_LE(_planner.requiredNodes(), _planner.maxNodes()); + ASSERT_EQ(_planner.status(), GeofenceAvoidancePlanner::Status::BudgetExceeded); + _planner.updateDestination(kExclusionDestination); + EXPECT_EQ(_planner.updateStartAndFillPath(kExclusionStart), 0); + EXPECT_TRUE(_planner.needsStraightLineFallback()); + + _planner.updateGraphFromGeofence(original, 0.f); + _planner.updateDestination(kExclusionDestination); + ASSERT_EQ(_planner.status(), GeofenceAvoidancePlanner::Status::Success); + EXPECT_EQ(_planner.updateStartAndFillPath(kExclusionStart), 2); + EXPECT_FALSE(_planner.needsStraightLineFallback()); +} + TEST_F(GeofenceAvoidancePlannerTest, PathInsideInclusionZone) { using namespace matrix; diff --git a/src/modules/navigator/RTLPlanner/geofence_avoidance_planner.cpp b/src/modules/navigator/RTLPlanner/geofence_avoidance_planner.cpp index 277faee06c1..6be8e773cf8 100644 --- a/src/modules/navigator/RTLPlanner/geofence_avoidance_planner.cpp +++ b/src/modules/navigator/RTLPlanner/geofence_avoidance_planner.cpp @@ -201,16 +201,19 @@ bool GeofenceAvoidancePlanner::updatePolygonsFromGeofence( if (info.fence_type == NAV_CMD_FENCE_POLYGON_VERTEX_INCLUSION || info.fence_type == NAV_CMD_FENCE_POLYGON_VERTEX_EXCLUSION) { - // Could skip this local copy and pass e.g. a reference to geofence into addPolygon, so it could access directly - matrix::Vector2f local_in[info.vertex_count]; + if (info.vertex_count > DM_KEY_FENCE_POINTS_MAX) { + _status = Status::BudgetExceeded; + perf_cancel(_update_polygons_perf); + return false; + } for (int vertex_idx = 0; vertex_idx < info.vertex_count; vertex_idx++) { - local_in[vertex_idx] = get_vertex_local_position(poly_index, vertex_idx, geofence, _reference); + _vertices_in[vertex_idx] = get_vertex_local_position(poly_index, vertex_idx, geofence, _reference); } const bool is_inclusion = (info.fence_type == NAV_CMD_FENCE_POLYGON_VERTEX_INCLUSION); - add_result = _polygons.addPolygon(local_in, info.vertex_count, is_inclusion, margin); + add_result = _polygons.addPolygon(_vertices_in, info.vertex_count, is_inclusion, margin); } else if (info.fence_type == NAV_CMD_FENCE_CIRCLE_INCLUSION || info.fence_type == NAV_CMD_FENCE_CIRCLE_EXCLUSION) { diff --git a/src/modules/navigator/RTLPlanner/geofence_avoidance_planner.h b/src/modules/navigator/RTLPlanner/geofence_avoidance_planner.h index 5e003403afe..ecc60870b7f 100644 --- a/src/modules/navigator/RTLPlanner/geofence_avoidance_planner.h +++ b/src/modules/navigator/RTLPlanner/geofence_avoidance_planner.h @@ -202,6 +202,9 @@ private: perf_counter_t _plan_path_perf{perf_alloc(PC_ELAPSED, "rtl_planner: plan path")}; perf_counter_t _lookup_path_perf{perf_alloc(PC_ELAPSED, "rtl_planner: lookup path")}; + // Scratch storage for the vertices of the polygon being added, off the stack + matrix::Vector2f _vertices_in[DM_KEY_FENCE_POINTS_MAX]; + // Sets _status on failure (matching planPath); returns false to abort the build. bool updatePolygonsFromGeofence(GeofenceInterface &geofence, float margin); void updateEdgeCosts();