[C++] Problems with address sanitizer

38 views
Skip to first unread message

Agustin Gomez

unread,
Sep 10, 2024, 9:19:52 AM9/10/24
to s2geometry-io
Hello there, I'm having some issues after compiling S2geometry with asan flags. The error itself occurs when creating an S2Loop instance. The flags I'm using to compile my app are QMAKE_CXXFLAGS_DEBUG += -fsanitize=address and QMAKE_LFLAGS_DEBUG += -fsanitize=address.  If I compile the app without those flags or if I use the ATTRIBUTE_NO_SANITIZE_ADDRESS macro on the method where the error occurs, asan does not report the problem. I've read that if you compile using D_FORTIFY_SOURCE, asan misbehaves but unfortunately this is not the case. Are you aware of some misbehavior between these two? I can provide more details if needed. Thank you in advance.  

Jesse Rosenstock

unread,
Sep 10, 2024, 9:55:23 AM9/10/24
to Agustin Gomez, s2geometry-io
On Tue, Sep 10, 2024 at 3:19 PM Agustin Gomez <agustin...@gmail.com> wrote:
> Hello there, I'm having some issues after compiling S2geometry with asan flags. The error itself occurs when creating an S2Loop instance. The flags I'm using to compile my app are QMAKE_CXXFLAGS_DEBUG += -fsanitize=address and QMAKE_LFLAGS_DEBUG += -fsanitize=address. If I compile the app without those flags or if I use the ATTRIBUTE_NO_SANITIZE_ADDRESS macro on the method where the error occurs, asan does not report the problem. I've read that if you compile using D_FORTIFY_SOURCE, asan misbehaves but unfortunately this is not the case. Are you aware of some misbehavior between these two? I can provide more details if needed. Thank you in advance.

We'll need more details, ideally a small, self-contained test case
that reproduces the issue.

Agustin Gomez

unread,
Sep 10, 2024, 10:07:12 AM9/10/24
to s2geometry-io
Hi there, I have a C++ application which uses GDAL and S2Geometry to load and process prescription maps. When the application starts processing said maps using S2, asan detects an error regarding S2 and stops the program execution.

My code is something like:

```

S2Polygon* convertOGRPolygonToS2Polygon(OGRPolygon& ogrPolygon) { 

    // Process exterior ring

    ...

    // Create S2 instances

    std::vector<std::unique_ptr<S2Loop>> loops; // This is the line where the error occurs.

    loops.push_back(std::make_unique<S2Loop>(vertices));

    loops.back()->Normalize();

    // Process interior rings (holes)

    ...

 }

```


And when the error occurs the stack trace is as follows:

```

AddressSanitizer:DEADLYSIGNAL

=================================================================

 ==1==ERROR: AddressSanitizer: SEGV on unknown address 0x00000000000e (pc 0x64b86f5fe562 bp 0x7ffc88b15210 sp 0x7ffc88b15200 T0)

 ==1==The signal is caused by a READ memory access.

 ==1==Hint: address points to the zero page.

     #0 0x64b86f5fe562 in absl::lts_20240722::container_internal::btree_node<absl::lts_20240722::container_internal::map_params<S2CellId, S2ShapeIndexCell*, std::less<S2CellId>, std::allocator<std::pair<S2CellId const, S2ShapeIndexCell*> >, 256, false> >::finish() const ../third-party/install/include/absl/container/internal/btree.h:694

    #1 0x64b86f5fafc0 in absl::lts_20240722::container_internal::btree<absl::lts_20240722::container_internal::map_params<S2CellId, S2ShapeIndexCell*, std::less<S2CellId>, std::allocator<std::pair<S2CellId const, S2ShapeIndexCell*> >, 256, false> >::end() const ../third-party/install/include/absl/container/internal/btree.h:1433

    #2 0x64b870b7c60d in absl::lts_20240722::container_internal::btree_container<absl::lts_20240722::container_internal::btree<absl::lts_20240722::container_internal::map_params<S2CellId, S2ShapeIndexCell*, std::less<S2CellId>, std::allocator<std::pair<S2CellId const, S2ShapeIndexCell*> >, 256, false> > >::end() const (app+0x575460d)

     #3 0x64b870b79800 in MutableS2ShapeIndex::Iterator::InitStale(MutableS2ShapeIndex const*, S2ShapeIndex::InitialPosition) (app+0x5751800)

    #4 0x64b870b7027a in MutableS2ShapeIndex::Minimize() (app+0x574827a) 

    #5 0x64b870b709bc in MutableS2ShapeIndex::ReleaseAll() (app+0x57489bc) 

     #6 0x64b870b70a32 in MutableS2ShapeIndex::Clear() (app+0x5748a32) 

     #7 0x64b870bba6c8 in S2Loop::ClearIndex() (app+0x57926c8) 

     #8 0x64b870bba723 in S2Loop::Init(absl::lts_20240722::Span<S2Point const>) (app+0x5792723)

... 

AddressSanitizer can not provide additional info.

SUMMARY: AddressSanitizer: SEGV ../third-party/install/include/absl/container/internal/btree.h:694 in absl::lts_20240722::container_internal::btree_node<absl::lts_20240722::container_internal::map_params<S2CellId, S2ShapeIndexCell*, std::less<S2CellId>, std::allocator<std::pair<S2CellId const, S2ShapeIndexCell*> >, 256, false> >::finish() const 

==1==ABORTING  

```


If I compile the app without using those flags or if I use this macro:

```

#if defined(__clang__) || defined (__GNUC__) 

# define ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address)) 

#else 

# define ATTRIBUTE_NO_SANITIZE_ADDRESS 

#endif

on the convertOGRPolygonToS2Polygon method, asan does not report the problem.

```


I'd rather use the workaround instead of compiling my app without asan and I'm sorry for the bad code formatting. 


Message has been deleted

Agustin Gomez

unread,
Sep 12, 2024, 2:16:44 PM9/12/24
to s2geometry-io
I've been reading about the usage of tcmalloc done by abseil and the incompatibility with address sanitizer. Maybe that is the reason for this error but still I'm not sure. I've deleted my last comment because I need to be sure that the error is not being generated by this library. Thank you for your understanding. 

Agustin Gomez

unread,
Sep 19, 2024, 8:45:03 AM9/19/24
to s2geometry-io
I finally was able to reproduce the error on a small case which is the following:

#include <vector>
#include <s2/s2loop.h>
#include <s2/s2point.h>
#include <s2/s2latlng.h>

int main()
{
    std::vector<S2Point> points;
    points.push_back(S2Point(S2LatLng::FromDegrees(0.392984, -0.743239).ToPoint()));
    points.push_back(S2Point(S2LatLng::FromDegrees(0.392983, -0.743238).ToPoint()));
    points.push_back(S2Point(S2LatLng::FromDegrees(0.392982, -0.743237).ToPoint()));
    points.push_back(S2Point(S2LatLng::FromDegrees(0.39298, -0.743237).ToPoint()));
    points.push_back(S2Point(S2LatLng::FromDegrees(0.392978, -0.743237).ToPoint()));
    points.push_back(S2Point(S2LatLng::FromDegrees(0.392976, -0.743238).ToPoint()));
    points.push_back(S2Point(S2LatLng::FromDegrees(0.392974, -0.743239).ToPoint()));
    points.push_back(S2Point(S2LatLng::FromDegrees(0.392973, -0.743241).ToPoint()));
    points.push_back(S2Point(S2LatLng::FromDegrees(0.392972, -0.743242).ToPoint()));
    points.push_back(S2Point(S2LatLng::FromDegrees(0.392971, -0.743243).ToPoint()));
    points.push_back(S2Point(S2LatLng::FromDegrees(0.392971, -0.743245).ToPoint()));
    points.push_back(S2Point(S2LatLng::FromDegrees(0.392972, -0.743245).ToPoint()));
    points.push_back(S2Point(S2LatLng::FromDegrees(0.392974, -0.743246).ToPoint()));
    points.push_back(S2Point(S2LatLng::FromDegrees(0.392975, -0.743246).ToPoint()));
    points.push_back(S2Point(S2LatLng::FromDegrees(0.392976, -0.743246).ToPoint()));
    points.push_back(S2Point(S2LatLng::FromDegrees(0.392978, -0.743245).ToPoint()));
    points.push_back(S2Point(S2LatLng::FromDegrees(0.39298, -0.743244).ToPoint()));
    points.push_back(S2Point(S2LatLng::FromDegrees(0.392982, -0.743243).ToPoint()));
    points.push_back(S2Point(S2LatLng::FromDegrees(0.392983, -0.743241).ToPoint()));
    points.push_back(S2Point(S2LatLng::FromDegrees(0.392984, -0.74324).ToPoint()));


    std::vector<std::unique_ptr<S2Loop>> loops;
    loops.push_back(std::make_unique<S2Loop>(points)); // line where the error happens
    loops.back()->Normalize();
}

I guess this error can happen with fewer points as well. And these are the compilation flags:

g++ -O0 -ggdb3 -Wall -fsanitize=address -Ithird-party/install/include/ -Lthird-party/install/lib/ -o s2test s2test.cpp -ls2 -labsl_log_internal_message -labsl_examine_stack -labsl_symbolize -labsl_synchronization -labsl_base -labsl_hash -labsl_low_level_hash -labsl_city -labsl_civil_time -labsl_stacktrace -labsl_debugging_internal -labsl_demangle_internal -labsl_demangle_rust -labsl_decode_rust_punycode -labsl_failure_signal_handler -labsl_flags_internal -labsl_flags_commandlineflag -labsl_flags_commandlineflag_internal -labsl_flags_config -labsl_flags_marshalling -labsl_flags_parse -labsl_flags_reflection -labsl_flags_private_handle_accessor -labsl_flags_program_name -labsl_flags_usage -labsl_flags_usage_internal -labsl_graphcycles_internal -labsl_hashtablez_sampler -labsl_int128 -labsl_kernel_timeout_internal -labsl_log_entry -labsl_log_flags -labsl_log_globals -labsl_log_initialize -labsl_log_internal_check_op -labsl_log_internal_fnmatch -labsl_log_internal_format -labsl_log_internal_globals -labsl_log_internal_log_sink_set -labsl_log_sink -labsl_log_internal_nullguard -labsl_log_internal_proto -labsl_malloc_internal -labsl_random_internal_seed_material -labsl_random_seed_gen_exception -labsl_random_seed_sequences -labsl_raw_hash_set -labsl_raw_logging_internal -labsl_spinlock_wait -labsl_strerror -labsl_str_format_internal -labsl_strings -labsl_time -labsl_time_zone -labsl_utf8_for_code_point -lcrypto

I'm a still a newbie using this library so if you see something off, please let me know. Thank you!

Jesse Rosenstock

unread,
Sep 20, 2024, 8:53:08 AM9/20/24
to Agustin Gomez, s2geometry-io
On Thu, Sep 19, 2024 at 2:45 PM Agustin Gomez <agustin...@gmail.com> wrote:
> I finally was able to reproduce the error on a small case which is the following:

Could you file a bug on github with all the commands you used to
build/install abseil-cpp and s2, output of failing asan run, and
versions of abseil-cpp, s2, and gcc?

https://github.com/google/s2geometry/issues

Agustin Gomez

unread,
Sep 23, 2024, 4:31:15 PM9/23/24
to s2geometry-io
Link to the opened issue: https://github.com/google/s2geometry/issues/382 
Reply all
Reply to author
Forward
0 new messages