From 22bc020dcdd8e552aba60c0b4488e08816a7b178 Mon Sep 17 00:00:00 2001 From: Ryan Hill <38674843+RyanUnderhill@users.noreply.github.com> Date: Mon, 15 May 2023 10:35:38 -0700 Subject: [PATCH] Ryanunderhill/ortapibase enforce (#15940) ### Description To prevent people from accidentally changing OrtApiBase, these static_asserts will fire if anyone adds a method, rearranges the method ordering, or changes the signature of any of the methods. ### Motivation and Context People have submitted changes that have done these things and there was no mechanism to stop them besides someone noticing in a code review. An automated way to let people know is needed. --- onnxruntime/core/session/onnxruntime_c_api.cc | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/onnxruntime/core/session/onnxruntime_c_api.cc b/onnxruntime/core/session/onnxruntime_c_api.cc index f2a241db27..53af75a2c6 100644 --- a/onnxruntime/core/session/onnxruntime_c_api.cc +++ b/onnxruntime/core/session/onnxruntime_c_api.cc @@ -2728,6 +2728,13 @@ static constexpr OrtApi ort_api_1_to_16 = { &OrtApis::GetBuildInfoString}; // End of Version 15 - DO NOT MODIFY ABOVE (see above text for more information) +// OrtApiBase can never change as there is no way to know what version of OrtApiBase is returned by OrtGetApiBase. +static_assert(sizeof(OrtApiBase) == sizeof(void*) * 2, "New methods can't be added to OrtApiBase as it is not versioned"); +static_assert(offsetof(OrtApiBase, GetApi) / sizeof(void*) == 0, "These functions cannot be reordered"); +static_assert(offsetof(OrtApiBase, GetVersionString) / sizeof(void*) == 1, "These functions cannot be reordered"); +static_assert(std::is_same_v, "This function's signature can never change"); +static_assert(std::is_same_v, "This function's signature can never change"); + // Asserts to do a some checks to ensure older Versions of the OrtApi never change (will detect an addition or deletion but not if they cancel out each other) // If any of these asserts hit, read the above 'Rules on how to add a new Ort API version' static_assert(offsetof(OrtApi, ReleaseCustomOpDomain) / sizeof(void*) == 101, "Size of version 1 API cannot change");