[CDRIVER-1523] Configuring libbson with --enable-extra-align=no when building with mongoc Created: 06/Sep/16  Updated: 03/Jan/18  Resolved: 18/Oct/16

Status: Closed
Project: C Driver
Component/s: Build, libmongoc
Affects Version/s: None
Fix Version/s: 1.5.0

Type: Bug Priority: Major - P3
Reporter: Hannes Magnusson Assignee: Hannes Magnusson
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

libbson's "EXTRA_ALIGN" (--enable-extra-align=yes) is deprecated and should only be used when maintaining libbson ABI compatibility.

When we configure libbson and install as part of mongoc (since there was no system installed libbson), then there is no ABI compatibility worry, and we should configure the library properly



 Comments   
Comment by Arseny Vakhrushev [ 03/Jan/18 ]

Hi Jesse! Yes, I am aware of the problem extra alignment causes with the current 1.x version, and that you're trying to maintain ABI. I guess you're right about make install in this case. Hope version 2.0 isn't far away.

As to bson_init(), sorry for the confusion! I mean the bug goes away if bson_init() is used instead of bson_init_from_json() to illustrate that it's the nested call to bson_append_document_begin() that crashes the system, not pure initialization.

Comment by A. Jesse Jiryu Davis [ 03/Jan/18 ]

Hi Arseny. You're right, installing libbson from the bundled source code still enables extra alignment. I think that this is the correct behavior, actually: "make install" will install both libmongoc and libbson in the system library directory, so both libmongoc and libbson must maintain ABI with previous versions.

I'm curious about your code example. Why does calling "bson_init" before "bson_init_from_json" prevent a crash? After all, bson_init_from_json also calls bson_init, so I don't understand why calling bson_init before bson_init_from_json would have any effect.

Comment by Arseny Vakhrushev [ 02/Jan/18 ]

I am observing the same bug in libbson when compiled and installed as part of mongoc 1.9.0.

$ tar xf mongo-c-driver-1.9.0.tar.gz && cd mongo-c-driver-1.9.0
$ ./configure --with-libbson=bundled
...
libbson 1.9.0 was configured with the following options:
 
Build configuration:
  Enable debugging (slow)                          : no
  Enable extra alignment (required for 1.0 ABI)    : yes
  Compile with debug symbols (slow)                : no
  Enable GCC build optimization                    : yes
  Code coverage support                            : no
  Cross Compiling                                  : no
  Big endian                                       : no
  Link Time Optimization (experimental)            : no
...

Please note that extra alignment is still ON in bundled mode.

As a result, libbson crashes big time on FreeBSD and macOS (no problem on Linux though):

#include <stdio.h>
#include <bson.h>
 
typedef struct {
	void *p1;
	// void *p2; // No bug when uncommented
	char data[1024];
} Data;
 
int main() {
	Data *p = malloc(sizeof *p);
	bson_t *b = (bson_t *)&p->data;
	printf("%p\n", p);
	printf("%p\n", b);
	// bson_init(b); // No bug with this 'init' istead of 'init_from_json'
	bson_init_from_json(b, "{\"a\":{}}", 8, 0);
	bson_destroy(b);
	free(p);
	return 0;
}

$ ./a.out
0x7fa6f2800000
0x7fa6f2800008
Segmentation fault: 11

P.S. The bug goes away with --enable-debug.

Comment by Hannes Magnusson [ 18/Oct/16 ]

Fixed in https://github.com/mongodb/mongo-c-driver/commit/776a49fad683501d2934cd8b16d887d0d7bba43a

Comment by Bernie Hackett [ 18/Oct/16 ]

Is this a bug or a task?

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