Fix 32-bit overflow in Jacobian size checks BlockJacobianWriter and CompressedRowJacobianWriter both computed num_residuals * TangentSize() in 32-bit int and accumulated entry counts in 32-bit unsigned int before checking against INT_MAX. In BlockJacobianWriter::BuildJacobianLayout, count both E-block and total entries in int64_t during the first pass and reject Jacobians over INT_MAX before allocating layout storage. Once the first pass succeeds, the second pass stays entirely in int. In CompressedRowJacobianWriter::CreateJacobian, accumulate num_jacobian_nonzeros in int64_t. Update evaluator_test to: - Exceed INT_MAX total entries using 10,000 blocks of size 500x500 (avoiding a 32-bit overflow in Program::MaxDerivativesPerResidualBlock) and cover all-F, split E+F, and all-E overflow in BlockJacobianWriter. - Test a single 46341x46345 block directly on both Jacobian writers so the 64-bit per-block product check stays covered under UBSan. Change-Id: I4fbb774d6605c315abc79b351a5c1fee61ddcd63
diff --git a/internal/ceres/block_jacobian_writer.cc b/internal/ceres/block_jacobian_writer.cc index 01946dd..51ae321 100644 --- a/internal/ceres/block_jacobian_writer.cc +++ b/internal/ceres/block_jacobian_writer.cc
@@ -31,7 +31,11 @@ #include "ceres/block_jacobian_writer.h" #include <algorithm> +#include <cstddef> +#include <cstdint> +#include <limits> #include <memory> +#include <utility> #include <vector> #include "absl/log/check.h" @@ -72,23 +76,33 @@ const std::vector<ResidualBlock*>& residual_blocks = program.residual_blocks(); - // Iterate over all the active residual blocks and determine how many E blocks - // are there. This will determine where the F blocks start in the jacobian - // matrix. Also compute the number of jacobian blocks. - unsigned int f_block_pos = 0; - unsigned int num_jacobian_blocks = 0; + // Iterate over all the active residual blocks to count the number of + // jacobian blocks, the total number of entries, and the number of E-block + // entries (which determines where the F blocks start). Count in int64_t so + // we reject Jacobians that exceed INT_MAX entries or blocks before + // allocating layout storage or computing int offsets. + int64_t num_e_block_nonzeros = 0; + int64_t total_num_nonzeros = 0; + int64_t num_jacobian_blocks = 0; for (auto* residual_block : residual_blocks) { - const int num_residuals = residual_block->NumResiduals(); + const int64_t num_residuals = residual_block->NumResiduals(); const int num_parameter_blocks = residual_block->NumParameterBlocks(); - // Advance f_block_pos over each E block for this residual. for (int j = 0; j < num_parameter_blocks; ++j) { ParameterBlock* parameter_block = residual_block->parameter_blocks()[j]; if (!parameter_block->IsConstant()) { // Only count blocks for active parameters. - num_jacobian_blocks++; + ++num_jacobian_blocks; + const int64_t block_size = + num_residuals * parameter_block->TangentSize(); + total_num_nonzeros += block_size; + if (total_num_nonzeros > std::numeric_limits<int>::max()) { + LOG(ERROR) + << "Overflow error. Too many entries in the Jacobian matrix."; + return false; + } if (parameter_block->index() < num_eliminate_blocks) { - f_block_pos += num_residuals * parameter_block->TangentSize(); + num_e_block_nonzeros += block_size; } } } @@ -99,15 +113,16 @@ } } - // We now know that the E blocks are laid out starting at zero, and the F - // blocks are laid out starting at f_block_pos. Iterate over the residual - // blocks again, and this time fill the jacobian_layout array with the - // position information. - + // We now know that the E blocks are laid out starting at zero, the F blocks + // are laid out starting at num_e_block_nonzeros, and all block sizes and + // offsets fit in int. Iterate over the residual blocks again and fill the + // jacobian_layout array with the position information. jacobian_layout->resize(program.NumResidualBlocks()); - jacobian_layout_storage->resize(num_jacobian_blocks); + jacobian_layout_storage->resize( + static_cast<std::size_t>(num_jacobian_blocks)); int e_block_pos = 0; + int f_block_pos = static_cast<int>(num_e_block_nonzeros); int* jacobian_pos = jacobian_layout_storage->data(); std::vector<std::pair<int, int>> active_parameter_blocks; for (int i = 0; i < residual_blocks.size(); ++i) { @@ -152,13 +167,8 @@ jacobian_pos[k] = e_block_pos; e_block_pos += jacobian_block_size; } else { - jacobian_pos[k] = static_cast<int>(f_block_pos); + jacobian_pos[k] = f_block_pos; f_block_pos += jacobian_block_size; - if (f_block_pos > std::numeric_limits<int>::max()) { - LOG(ERROR) - << "Overflow error. Too many entries in the Jacobian matrix."; - return false; - } } } jacobian_pos += active_parameter_blocks.size();
diff --git a/internal/ceres/compressed_row_jacobian_writer.cc b/internal/ceres/compressed_row_jacobian_writer.cc index 95076b7..776775f 100644 --- a/internal/ceres/compressed_row_jacobian_writer.cc +++ b/internal/ceres/compressed_row_jacobian_writer.cc
@@ -31,7 +31,9 @@ #include "ceres/compressed_row_jacobian_writer.h" #include <algorithm> +#include <cstdint> #include <iterator> +#include <limits> #include <memory> #include <string> #include <utility> @@ -95,13 +97,12 @@ const int total_num_residuals = program_->NumResiduals(); const int total_num_effective_parameters = program_->NumEffectiveParameters(); - // Count the number of jacobian nonzeros. - // - // We used an unsigned int here, so that we can compare it INT_MAX without - // triggering overflow behaviour. - unsigned int num_jacobian_nonzeros = total_num_effective_parameters; + // Count the number of jacobian nonzeros. Use int64_t so that the running + // total and per-block products can be checked against INT_MAX without + // overflowing. + int64_t num_jacobian_nonzeros = total_num_effective_parameters; for (auto* residual_block : residual_blocks) { - const int num_residuals = residual_block->NumResiduals(); + const int64_t num_residuals = residual_block->NumResiduals(); const int num_parameter_blocks = residual_block->NumParameterBlocks(); for (int j = 0; j < num_parameter_blocks; ++j) { auto parameter_block = residual_block->parameter_blocks()[j];
diff --git a/internal/ceres/evaluator_test.cc b/internal/ceres/evaluator_test.cc index 12e717d..85f8b2c 100644 --- a/internal/ceres/evaluator_test.cc +++ b/internal/ceres/evaluator_test.cc
@@ -40,7 +40,9 @@ #include "absl/log/log.h" #include "absl/log/vlog_is_on.h" #include "absl/strings/str_format.h" +#include "ceres/block_jacobian_writer.h" #include "ceres/casts.h" +#include "ceres/compressed_row_jacobian_writer.h" #include "ceres/cost_function.h" #include "ceres/crs_matrix.h" #include "ceres/eigen.h" @@ -675,6 +677,14 @@ } } +class LargeCostFunction : public SizedCostFunction<500, 500> { + bool Evaluate(double const* const* parameters, + double* residuals, + double** jacobians) const override { + return true; + } +}; + class HugeCostFunction : public SizedCostFunction<46341, 46345> { bool Evaluate(double const* const* parameters, double* residuals, @@ -685,27 +695,39 @@ TEST(Evaluator, LargeProblemDoesNotCauseCrashBlockJacobianWriter) { ProblemImpl problem; - std::vector<double> x(46345); + std::vector<double> x(500); + std::vector<double> y(500); - problem.AddResidualBlock(new HugeCostFunction, nullptr, x.data()); - Evaluator::Options options; - options.linear_solver_type = SPARSE_NORMAL_CHOLESKY; - options.context = problem.context(); - options.num_eliminate_blocks = 0; - options.dynamic_sparsity = false; - std::string error; + // 5000 blocks on x (1.25e9 entries) and 5000 on y (1.25e9 entries) give + // 2.5e9 > INT_MAX total Jacobian entries while each block has 250,000 + // derivatives (fits in int). Testing num_eliminate_blocks = 0, 1, 2 covers + // all-F, split E+F (where E and F each fit in int), and all-E overflow. + for (int i = 0; i < 5000; ++i) { + problem.AddResidualBlock(new LargeCostFunction, nullptr, x.data()); + problem.AddResidualBlock(new LargeCostFunction, nullptr, y.data()); + } auto program = problem.mutable_program(); program->SetParameterOffsetsAndIndex(); - auto evaluator = Evaluator::Create(options, program, &error); - auto jacobian = evaluator->CreateJacobian(); - EXPECT_EQ(jacobian, nullptr); + for (int num_eliminate_blocks : {0, 1, 2}) { + Evaluator::Options options; + options.linear_solver_type = SPARSE_NORMAL_CHOLESKY; + options.context = problem.context(); + options.num_eliminate_blocks = num_eliminate_blocks; + options.dynamic_sparsity = false; + std::string error; + auto evaluator = Evaluator::Create(options, program, &error); + auto jacobian = evaluator->CreateJacobian(); + EXPECT_EQ(jacobian, nullptr); + } } TEST(Evaluator, LargeProblemDoesNotCauseCrashCompressedRowJacobianWriter) { ProblemImpl problem; - std::vector<double> x(46345); + std::vector<double> x(500); - problem.AddResidualBlock(new HugeCostFunction, nullptr, x.data()); + for (int i = 0; i < 10000; ++i) { + problem.AddResidualBlock(new LargeCostFunction, nullptr, x.data()); + } Evaluator::Options options; // CGNR on CUDA_SPARSE is the only combination that triggers a // CompressedRowJacobianWriter. @@ -721,5 +743,24 @@ EXPECT_EQ(jacobian, nullptr); } +TEST(Evaluator, SingleHugeBlockDoesNotOverflowJacobianWriters) { + ProblemImpl problem; + std::vector<double> x(46345); + problem.AddResidualBlock(new HugeCostFunction, nullptr, x.data()); + auto program = problem.mutable_program(); + program->SetParameterOffsetsAndIndex(); + + for (int num_eliminate_blocks : {0, 1}) { + Evaluator::Options options; + options.num_eliminate_blocks = num_eliminate_blocks; + BlockJacobianWriter writer(options, program); + EXPECT_EQ(writer.CreateJacobian(), nullptr); + } + + Evaluator::Options options; + CompressedRowJacobianWriter writer(options, program); + EXPECT_EQ(writer.CreateJacobian(), nullptr); +} + } // namespace internal } // namespace ceres