[CDRIVER-2681] bson_context_get_default optimized out when compiling with LTO Created: 02/Jun/18  Updated: 28/Oct/23  Resolved: 07/Jun/18

Status: Closed
Project: C Driver
Component/s: None
Affects Version/s: None
Fix Version/s: 1.11.0

Type: Bug Priority: Major - P3
Reporter: Kevin Albertson Assignee: Kevin Albertson
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
is depended on by SERVER-35184 Enable link-time-optimization for emb... Closed
Related
related to CDRIVER-2805 Add an LTO enabled builder Backlog

 Description   

Drew noticed when we compile the C driver with link time optimization our tests fail. Repro:

cd mongo-c-driver
mkdir cmake-build && cd cmake-build
cmake -DCMAKE_C_FLAGS="-flto" ../
make -j8 test-libmongoc
cd ../
./cmake-build/src/libmongoc/test-libmongoc -d --no-fork -l /bson/bcon/test_oid

fails with a segfault.

It looks like some function definitions are getting optimized out when they shouldn't be. It's a little difficult to debug since -flto removes debug info, but after some printf debugging, it became clear that bson_context_get_default was not getting called. Using nm you can also see there's no symbol in the binary:

nm ./cmake-build/src/libmongoc/test-libmongoc | grep 'bson_context_get_default'

comes up with nothing.

Here's the definition of

BSON_EXPORT (bson_context_t *)
bson_context_get_default (void) BSON_GNUC_CONST;

BSON_GNUC_CONST is defined as _attribute_ ((const)) for gcc/clang. The gcc function attributes docs give this explanation for the const attribute:

Many functions do not examine any values except their arguments, and have no effects except to return a value. Calls to such functions lend themselves to optimization such as common subexpression elimination. The const attribute imposes greater restrictions on a function’s definition than the similar pure attribute below because it prohibits the function from reading global variables. Consequently, the presence of the attribute on a function declaration allows GCC to emit more efficient code for some calls to the function. Decorating the same function with both the const and the pure attribute is diagnosed.

But bson_context_get_default does read from global variables (PTHREAD_ONCE_INIT}} and gContextDefault. Since const functions shouldn't have side effects, the linker thinks it can optimize it out. Sure enough, removing the const attribute results in this test passing.

There are other functions marked as const that probably shouldn't be, e.g. mongoc_ssl_opt_get_default, which returns a global.

 



 Comments   
Comment by Githook User [ 07/Jun/18 ]

Author:

{'username': 'kevinAlbs', 'name': 'Kevin Albertson', 'email': 'kevin.albertson@10gen.com'}

Message: CDRIVER-2681 fix functions with const attr
Branch: master
https://github.com/mongodb/mongo-c-driver/commit/8c2a814b99e08bebede7276dbaa4b7d5f9bba317

Generated at Wed Feb 07 21:16:00 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.