hardened_malloc icon indicating copy to clipboard operation
hardened_malloc copied to clipboard

Do not include bits/functexcept.h when building with libc++

Open 0xC0ncord opened this issue 3 years ago • 6 comments

This header exists only in GCC's libstdc++. If we are building on a system using exclusively LLVM's libc++, compilation will fail.

Add an option to the config files to control using libc++. This will cause this header to not be included if and only if CONFIG_USE_LIBCXX is true and we are not using GCC.

0xC0ncord avatar May 07 '22 15:05 0xC0ncord

Doesn't this break the compilation when used with libc++ and -fnot-exceptions?

jvoisin avatar May 07 '22 16:05 jvoisin

Doesn't this break the compilation when used with libc++ and -fnot-exceptions?

It seems I can reproduce a build failure with -fno-exceptions in the CI on the existing workflows. How should this be tested?

0xC0ncord avatar May 07 '22 17:05 0xC0ncord

The newly added workflow is building successfully now but is currently failing tests, and I can reproduce these on my local machine:

python3 -m unittest discover --start-directory test/
...........................FF..F.....
======================================================================
FAIL: test_uninitialized_free (test_smc.TestSimpleMemoryCorruption)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/concord/Projects/hardened_malloc/test/test_smc.py", line 156, in test_uninitialized_free
    self.assertEqual(stderr.decode("utf-8"),
AssertionError: 'fatal allocator error: invalid uninitialized allocator usage\n' != 'fatal allocator error: invalid free\n'
- fatal allocator error: invalid uninitialized allocator usage
+ fatal allocator error: invalid free


======================================================================
FAIL: test_uninitialized_malloc_usable_size (test_smc.TestSimpleMemoryCorruption)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/concord/Projects/hardened_malloc/test/test_smc.py", line 163, in test_uninitialized_malloc_usable_size
    self.assertEqual(stderr.decode("utf-8"),
AssertionError: 'fatal allocator error: invalid uninitialized allocator usage\n' != 'fatal allocator error: invalid malloc_usable_size\n'
- fatal allocator error: invalid uninitialized allocator usage
+ fatal allocator error: invalid malloc_usable_size


======================================================================
FAIL: test_uninitialized_realloc (test_smc.TestSimpleMemoryCorruption)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/concord/Projects/hardened_malloc/test/test_smc.py", line 169, in test_uninitialized_realloc
    self.assertEqual(stderr.decode("utf-8"),
AssertionError: 'fatal allocator error: invalid uninitialized allocator usage\n' != 'fatal allocator error: invalid realloc\n'
- fatal allocator error: invalid uninitialized allocator usage
?                                ^^^^^^^^^^^ --     ----------
+ fatal allocator error: invalid realloc
?                                ^


----------------------------------------------------------------------
Ran 37 tests in 0.116s

FAILED (failures=3)

0xC0ncord avatar May 07 '22 17:05 0xC0ncord

A first step would be to add a test for -fno-exceptions in the CI as a separate pull-request, then move forward with this one I think.

I might give it a try later this week, but feel free to send one before me :)

jvoisin avatar May 09 '22 12:05 jvoisin

Unfortunately I'm kind of at a stand-still with this one. As this PR stands, hardened_malloc will build and run when linked against libc++ but we definitely should have tests enabled in CI for these builds. It looks like for some reason when linked with libc++, the allocator isn't getting properly initialized and I'm really not sure where to go from here as I'm not familiar with memory allocators.

@jvoisin Would be able to assist with this?

0xC0ncord avatar Jun 05 '22 13:06 0xC0ncord

I don't think I'll be able to find some free time any time soon unfortunately :/

jvoisin avatar Jul 03 '22 16:07 jvoisin

Done by https://github.com/GrapheneOS/hardened_malloc/commit/462c2c5293b834bcbdde1d3a9389202ae6e809ff but not tested with it.

thestinger avatar Jun 10 '23 18:06 thestinger

If you want to make C++ stdlib configuration in the Makefile, it would probably be best to change how CONFIG_CXX_ALLOCATOR is set up. I only used the CXX := $(CC) approach to deal with LTO so that people don't set CC=clang and end up trying to mix clang and g++.

thestinger avatar Jun 10 '23 18:06 thestinger