[CDRIVER-4654] aligned_alloc is undefined on Android Created: 30/May/23  Updated: 28/Oct/23  Resolved: 05/Jun/23

Status: Closed
Project: C Driver
Component/s: BSON
Affects Version/s: 1.23.4
Fix Version/s: 1.24.0, 1.23.5

Type: Bug Priority: Major - P3
Reporter: Jean-Marc Le Roux Assignee: Ezra Chung
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: Text File bson-memory.c.patch    

 Description   

Building libbson for Android fails because `aligned_alloc()` is not defined in [`stdlib.h`](https://android.googlesource.com/platform/development/+/13af1d8/ndk/platforms/android-L/include/stdlib.h):

 

  /embedb/crates/embedb-node/target/armv7-linux-androideabi/release/build/mongoc-matcher-450d15ac8d39df22/out/build/libbson/src/libbson/src/libbson/src/bson/bson-memory.c:38:11: error: implicitly declaring library function 'aligned_alloc' with type 'void *(unsigned int, unsigned int)' [-Werror,-Wimplicit-function-declaration]
     return aligned_alloc (alignment, num_bytes);
            ^
  /embedb/crates/embedb-node/target/armv7-linux-androideabi/release/build/mongoc-matcher-450d15ac8d39df22/out/build/libbson/src/libbson/src/libbson/src/bson/bson-memory.c:38:11: note: include the header <stdlib.h> or explicitly provide a declaration for 'aligned_alloc'
  1 error generated.
  make[5]: *** [src/libbson/CMakeFiles/bson_static.dir/build.make:244: src/libbson/CMakeFiles/bson_static.dir/src/bson/bson-memory.c.o] Error 1
  make[5]: *** Waiting for unfinished jobs....
  /embedb/crates/embedb-node/target/armv7-linux-androideabi/release/build/mongoc-matcher-450d15ac8d39df22/out/build/libbson/src/libbson/src/libbson/src/bson/bson-memory.c:38:11: error: implicitly declaring library function 'aligned_alloc' with type 'void *(unsigned int, unsigned int)' [-Werror,-Wimplicit-function-declaration]
     return aligned_alloc (alignment, num_bytes);
            ^
  /embedb/crates/embedb-node/target/armv7-linux-androideabi/release/build/mongoc-matcher-450d15ac8d39df22/out/build/libbson/src/libbson/src/libbson/src/bson/bson-memory.c:38:11: note: include the header <stdlib.h> or explicitly provide a declaration for 'aligned_alloc'

 

 

Corresponding Android NDK issue:

https://github.com/android/ndk/issues/1339

 

Recommended solution:

https://github.com/android/ndk/issues/1339#issuecomment-676821211

 

Right, use `memalign` or `posix_memalign` instead.

  • `memalign` is always available on Android.
  • `posix_memalign` was added in API 17 (but is available on API 16 via libandroid_support.a, which is linked by default when targeting API 16).

The test in `bson-memory.c` will most likely have to be changed to something like this:

 

#if __STDC_VERSION__ >= 201112L && !defined(_WIN32) && !defined(__ANDROID__)
{
   return aligned_alloc (alignment, num_bytes);
}
#elif defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L
{
   void *mem = NULL;
   (void) posix_memalign (&mem, alignment, num_bytes);
   return mem;
}

bson-memory.c.patch

 



 Comments   
Comment by Githook User [ 06/Jun/23 ]

Author:

{'name': 'Jean-Marc Le Roux', 'email': 'jeanmarc.leroux@aerys.in', 'username': 'JMLX42'}

Message: CDRIVER-4654 Fix missing aligned_alloc() on Android (#1286)

  • chore: fix coding style

Co-authored-by: Ezra Chung <88335979+eramongodb@users.noreply.github.com>

---------

Co-authored-by: Jean-Marc Le Roux <jmlx@Enterprise-C>
Co-authored-by: Ezra Chung <88335979+eramongodb@users.noreply.github.com>
Branch: r1.23
https://github.com/mongodb/mongo-c-driver/commit/5f16b6a61bd8138f188a60ce082b3b27bac01b5a

Comment by Githook User [ 05/Jun/23 ]

Author:

{'name': 'Jean-Marc Le Roux', 'email': 'jeanmarc.leroux@aerys.in', 'username': 'JMLX42'}

Message: CDRIVER-4654 Fix missing aligned_alloc() on Android (#1286)

  • chore: fix coding style

Co-authored-by: Ezra Chung <88335979+eramongodb@users.noreply.github.com>

---------

Co-authored-by: Jean-Marc Le Roux <jmlx@Enterprise-C>
Co-authored-by: Ezra Chung <88335979+eramongodb@users.noreply.github.com>
Branch: master
https://github.com/mongodb/mongo-c-driver/commit/7c37e1fe27c16154fb0ef9a1e5a979d0702076aa

Comment by Jean-Marc Le Roux [ 03/Jun/23 ]

PR with a working fix: https://github.com/mongodb/mongo-c-driver/pull/1286

Comment by Kevin Albertson [ 01/Jun/23 ]

The linked commit "Message: CDRIVER-4654 move wire version check after state collection creation (#1287)" has an incorrect ticket number. This ticket is not yet resolved.

Comment by Githook User [ 01/Jun/23 ]

Author:

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

Message: CDRIVER-4654 move wire version check after state collection creation (#1287)

  • copy fle2v2-CreateCollection-OldServer.json

From specifications commit: 106caf3cfd1a92712bbbbc43626f343b619059ab

Comment by PM Bot [ 30/May/23 ]

Hi jeanmarc.leroux@gmail.com, thank you for reporting this issue! The team will look into it and get back to you soon.

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