[SERVER-44372] Shim declarations on same line in different TUs can lead to linker errors Created: 01/Nov/19  Updated: 29/Oct/23  Resolved: 07/Nov/19

Status: Closed
Project: Core Server
Component/s: Build
Affects Version/s: None
Fix Version/s: 4.3.1

Type: Bug Priority: Major - P3
Reporter: Andrew Morrow (Inactive) Assignee: Billy Donahue
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible
Operating System: ALL
Sprint: Dev Tools 2019-11-18
Participants:

 Description   

The MONGO_DECLARE_SHIM macro uses token pasting to attach the current line number to the internal class it defines: https://github.com/mongodb/mongo/blob/8010c51d3f1cc1020959f8b351f8242d141d42a3/src/mongo/base/shim.h#L136-L139

If two files declare shims within the same namespace on the same line, that will be an ODR violation:

The DECLARE_SHIM and related macros must find a way to generate names which cannot collide like this, probably by folding some of the file name into the generated name, or by requiring a stringy argument to the macro to disambiguate.



 Comments   
Comment by Githook User [ 07/Nov/19 ]

Author:

{'name': 'Billy Donahue', 'username': 'BillyDonahue', 'email': 'billy.donahue@mongodb.com'}

Message: SERVER-44372 WeakFunction: a simplification of SHIM_ macros
Branch: master
https://github.com/mongodb/mongo/commit/b2493375543cb89b020f224a1759c1f4d40aa638

Comment by Githook User [ 07/Nov/19 ]

Author:

{'username': 'BillyDonahue', 'email': 'billy.donahue@mongodb.com', 'name': 'Billy Donahue'}

Message: SERVER-44372 Shim -> WeakFunctionRegistry (enterprise)
Branch: master
https://github.com/10gen/mongo-enterprise-modules/commit/7cf5fe824a6e6832ee9f1623873bd1a32c2f719d

Comment by Billy Donahue [ 05/Nov/19 ]

Enterprise CR: https://mongodbcr.appspot.com/524640017

Comment by Billy Donahue [ 05/Nov/19 ]

After an hour or so, I could not figure out how to safely replace the line numbers with better identifiers.
I also didn't want to leave this code in place, because it takes too long to understand enough to do any work on it.

The shim.h macros are a very difficult implementation of a pretty basic idea of having a function registry to disassociate function providers from function callers in the link graph.

The better course of action here is to replace the extremely complex meta-programming thing with a conventional C++ based mechanism, based on a string-keyed map of type erased generalized function pointers, and dynamic_cast. The 13 or so functions that are being changed out with the shim framework are never performance critical. They are registering factory functions for the most part, or they are at the frequency of once per operation.

CR: https://mongodbcr.appspot.com/536220009/

Comment by Billy Donahue [ 04/Nov/19 ]

Good news is that there are only 13 instances of MONGO_DECLARE_SHIM in the codebase.
Which opens up the options for solving this.

src/mongo/db/auth/authorization_manager.h:99:    static MONGO_DECLARE_SHIM(()->std::unique_ptr<AuthorizationManager>) create;
src/mongo/db/auth/authorization_session.h:75:    static MONGO_DECLARE_SHIM(
src/mongo/db/auth/authz_manager_external_state.h:62:    static MONGO_DECLARE_SHIM(()->std::unique_ptr<AuthzManagerExternalState>) create;
src/mongo/db/auth/authz_session_external_state.h:53:    static MONGO_DECLARE_SHIM(
src/mongo/db/pipeline/mongo_process_interface.h:101:    static MONGO_DECLARE_SHIM(
src/mongo/db/read_concern.h:54:extern MONGO_DECLARE_SHIM((OperationContext * opCtx,
src/mongo/db/read_concern.h:68:extern MONGO_DECLARE_SHIM((OperationContext * opCtx,
src/mongo/db/read_concern.h:80:extern MONGO_DECLARE_SHIM((OperationContext * opCtx, const int readConcernTimeout)->Status)
src/mongo/db/read_concern.h:88:extern MONGO_DECLARE_SHIM((OperationContext * opCtx,
src/mongo/db/s/transaction_coordinator_curop.h:34:extern MONGO_DECLARE_SHIM((OperationContext * opCtx, bool includeIdle, std::vector<BSONObj>* ops)
src/mongo/db/s/transaction_coordinator_factory.h:38:extern MONGO_DECLARE_SHIM((OperationContext * opCtx, TxnNumber clientTxnNumber)->void)
src/mongo/db/s/transaction_coordinator_worker_curop_repository.h:68:extern MONGO_DECLARE_SHIM(()->std::shared_ptr<TransactionCoordinatorWorkerCurOpRepository>)
src/mongo/shell/bench.h:162:    static MONGO_DECLARE_SHIM((const BenchRunConfig&)->std::unique_ptr<DBClientBase>)

Generated at Thu Feb 08 05:05:48 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.