Skip to content

Commit

Permalink
Merge pull request #810 from PowerGridModel/bug/AddressSanitizer-issu…
Browse files Browse the repository at this point in the history
…e-bug-re-pro-case

BUG / Memory access violation in buffer span of update data
  • Loading branch information
TonyXiang8787 authored Oct 28, 2024
2 parents 35cc21d + cda6cd4 commit d504b55
Showing 1 changed file with 127 additions and 0 deletions.
127 changes: 127 additions & 0 deletions tests/native_api_tests/test_api_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,133 @@ TEST_CASE("API Model") {
CHECK(batch_node_result_u_angle[1] == doctest::Approx(0.0));
}
}

SUBCASE("Self contained model update error") {
std::vector<ID> const input_node_id{0};
std::vector<double> const input_node_u_rated{100.0};
Buffer input_node_buffer{PGM_def_input_node, 1};
input_node_buffer.set_nan();
input_node_buffer.set_value(PGM_def_input_node_id, input_node_id.data(), -1);
input_node_buffer.set_value(PGM_def_input_node_u_rated, input_node_u_rated.data(), -1);

std::vector<ID> const input_source_id{1};
std::vector<ID> const input_source_node{0};
std::vector<int8_t> const input_source_status{1};
std::vector<double> const input_source_u_ref{1.0};
std::vector<double> const input_source_sk{1000.0};
std::vector<double> const input_source_rx_ratio{0.0};
Buffer input_source_buffer{PGM_def_input_source, 1};
input_source_buffer.set_nan();
input_source_buffer.set_value(PGM_def_input_source_id, input_source_id.data(), -1);
input_source_buffer.set_value(PGM_def_input_source_node, input_source_node.data(), -1);
input_source_buffer.set_value(PGM_def_input_source_status, input_source_status.data(), -1);
input_source_buffer.set_value(PGM_def_input_source_u_ref, input_source_u_ref.data(), -1);
input_source_buffer.set_value(PGM_def_input_source_sk, input_source_sk.data(), -1);
input_source_buffer.set_value(PGM_def_input_source_rx_ratio, input_source_rx_ratio.data(), -1);

std::vector<ID> const input_sym_load_id{2};
std::vector<ID> const input_sym_load_node{0};
std::vector<int8_t> const input_sym_load_status{1};
std::vector<int8_t> const input_sym_load_type{2};
std::vector<double> const input_sym_load_p_specified{0.0};
std::vector<double> const input_sym_load_q_specified{500.0};
Buffer input_sym_load_buffer{PGM_def_input_sym_load, 1};
input_sym_load_buffer.set_nan();
input_sym_load_buffer.set_value(PGM_def_input_sym_load_id, input_sym_load_id.data(), -1);
input_sym_load_buffer.set_value(PGM_def_input_sym_load_node, input_sym_load_node.data(), -1);
input_sym_load_buffer.set_value(PGM_def_input_sym_load_status, input_sym_load_status.data(), -1);
input_sym_load_buffer.set_value(PGM_def_input_sym_load_type, input_sym_load_type.data(), -1);
input_sym_load_buffer.set_value(PGM_def_input_sym_load_p_specified, input_sym_load_p_specified.data(), -1);
input_sym_load_buffer.set_value(PGM_def_input_sym_load_q_specified, input_sym_load_q_specified.data(), -1);

// input dataset - row
DatasetConst input_dataset_row{"input", 0, 1};
input_dataset_row.add_buffer("node", 1, 1, nullptr, input_node_buffer);
input_dataset_row.add_buffer("source", 1, 1, nullptr, input_source_buffer);
input_dataset_row.add_buffer("sym_load", 1, 1, nullptr, input_sym_load_buffer);

// input dataset - col
DatasetConst input_dataset_col{"input", 0, 1};
input_dataset_col.add_buffer("node", 1, 1, nullptr, nullptr);
input_dataset_col.add_attribute_buffer("node", "id", input_node_id.data());
input_dataset_col.add_attribute_buffer("node", "u_rated", input_node_u_rated.data());

input_dataset_col.add_buffer("source", 1, 1, nullptr, nullptr);
input_dataset_col.add_attribute_buffer("source", "id", input_source_id.data());
input_dataset_col.add_attribute_buffer("source", "node", input_source_node.data());
input_dataset_col.add_attribute_buffer("source", "status", input_source_status.data());
input_dataset_col.add_attribute_buffer("source", "u_ref", input_source_u_ref.data());
input_dataset_col.add_attribute_buffer("source", "sk", input_source_sk.data());
input_dataset_col.add_attribute_buffer("source", "rx_ratio", input_source_rx_ratio.data());

input_dataset_col.add_buffer("sym_load", 1, 1, nullptr, nullptr);
input_dataset_col.add_attribute_buffer("sym_load", "id", input_sym_load_id.data());
input_dataset_col.add_attribute_buffer("sym_load", "node", input_sym_load_node.data());
input_dataset_col.add_attribute_buffer("sym_load", "status", input_sym_load_status.data());
input_dataset_col.add_attribute_buffer("sym_load", "type", input_sym_load_type.data());
input_dataset_col.add_attribute_buffer("sym_load", "p_specified", input_sym_load_p_specified.data());
input_dataset_col.add_attribute_buffer("sym_load", "q_specified", input_sym_load_q_specified.data());

// update dataset
std::vector<Idx> source_indptr{0, 1, 1};
std::vector<ID> const update_source_id{1};
std::vector<double> const update_source_u_ref{0.5};
Buffer update_source_buffer{PGM_def_update_source, 1};
update_source_buffer.set_nan();
update_source_buffer.set_value(PGM_def_update_source_id, update_source_id.data(), -1);
update_source_buffer.set_value(PGM_def_update_source_u_ref, update_source_u_ref.data(), -1);

std::vector<Idx> sym_load_indptr{0, 1, 2};
std::vector<ID> const update_sym_load_id{2, 5};
std::vector<double> const update_sym_load_q_specified{100.0, 300.0};
Buffer update_sym_load_buffer{PGM_def_update_sym_load, 2};
update_sym_load_buffer.set_nan();
update_sym_load_buffer.set_value(PGM_def_update_sym_load_id, update_sym_load_id.data(), -1);
update_sym_load_buffer.set_value(PGM_def_update_sym_load_q_specified, update_sym_load_q_specified.data(), -1);

// update dataset - row
DatasetConst update_dataset_row{"update", 1, 2};
update_dataset_row.add_buffer("source", -1, 1, source_indptr.data(), update_source_buffer);
update_dataset_row.add_buffer("sym_load", -1, 2, sym_load_indptr.data(), update_sym_load_buffer);

// update dataset - col
DatasetConst update_dataset_col{"update", 1, 2};

update_dataset_col.add_buffer("source", -1, 1, source_indptr.data(), nullptr);
update_dataset_col.add_attribute_buffer("source", "id", update_source_id.data());
update_dataset_col.add_attribute_buffer("source", "u_ref", update_source_u_ref.data());

update_dataset_col.add_buffer("sym_load", -1, 2, sym_load_indptr.data(), nullptr);
update_dataset_col.add_attribute_buffer("sym_load", "id", update_sym_load_id.data());
update_dataset_col.add_attribute_buffer("sym_load", "q_specified", update_sym_load_q_specified.data());

// output data
Buffer output_node_batch{PGM_def_sym_output_node, 2};
output_node_batch.set_nan();
DatasetMutable output_batch_dataset{"sym_output", 1, 2};
output_batch_dataset.add_buffer("node", 1, 2, nullptr, output_node_batch);

// options
Options const opt{};

SUBCASE("Row-based input dataset") {
Model row_model{50.0, input_dataset_row};

SUBCASE("Row-based update dataset") {
CHECK_THROWS_AS(row_model.calculate(opt, output_batch_dataset, update_dataset_row),
PowerGridBatchError);
}
}

SUBCASE("Columnar input dataset") {
Model col_model{50.0, input_dataset_col};

SUBCASE("Row-based update dataset") {
CHECK_THROWS_AS(col_model.calculate(opt, output_batch_dataset, update_dataset_row),
PowerGridBatchError);
}
}
}
}

} // namespace power_grid_model_cpp

0 comments on commit d504b55

Please sign in to comment.