Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ASAN issue in lib/cstring. #4865

Merged
merged 4 commits into from
Aug 12, 2024
Merged

Fix ASAN issue in lib/cstring. #4865

merged 4 commits into from
Aug 12, 2024

Conversation

matthewtlam
Copy link
Contributor

@matthewtlam matthewtlam commented Aug 12, 2024

Signed-off-by: Matthew Lam matthew.lam.qwerty@gmail.com

@matthewtlam
Copy link
Contributor Author

For context, running the ASAN tool on one of our tests revealed an initialization_order_fiasco runtime error.

SanitizerError
AddressSanitizer: initialization-order-fiasco [third_party/p4lang_p4c/lib/json.h:110]:23 in JsonValue;
Details
=================================================================
==4779==ERROR: AddressSanitizer: initialization-order-fiasco on address 0x7f2eaf3ee3c0 at pc 0x7f2eaefdbe2b bp 0x7ffdb7f50ba0 sp 0x7ffdb7f50b98
READ of size 8 at 0x7f2eaf3ee3c0 thread T0
    #0 0x7f2eaefdbe2a in JsonValue [third_party/p4lang_p4c/lib/json.h:110]:23
    #1 0x7f2eaefdbe2a in __cxx_global_var_init.2 [third_party/p4lang_p4c/lib/json.cpp:36]:34
    #2 0x7f2eaefdbe2a in _GLOBAL__sub_I_json.cpp third_party/p4lang_p4c/lib/json.cpp
    #3 0x7f2ecbbade29 in call_init (/usr/grte/v5/lib64/ld-linux-x86-64.so.2+0x1be29) (BuildId: 3c4ff280c4f5e542ce622870f20d94f2)
    #4 0x7f2ecbbadc8a in _dl_init (/usr/grte/v5/lib64/ld-linux-x86-64.so.2+0x1bc8a) (BuildId: 3c4ff280c4f5e542ce622870f20d94f2)
    #5 0x7f2ecbb99659 in _dl_start_user (/usr/grte/v5/lib64/ld-linux-x86-64.so.2+0x7659) (BuildId: 3c4ff280c4f5e542ce622870f20d94f2)

0x7f2eaf3ee3c0 is located 32 bytes before global variable '(anonymous namespace)::cache()::g_cache' defined in 'third_party/p4lang_p4c/lib/cstring.cpp' (0x7f2eaf3ee3e0) of size 56
  registered at:
    #0 0x55fa54125a58 in __asan_register_globals [third_party/llvm/llvm-project/compiler-rt/lib/asan/asan_globals.cpp:369]:3
    #1 0x55fa54126b6e in __asan_register_elf_globals [third_party/llvm/llvm-project/compiler-rt/lib/asan/asan_globals.cpp:352]:3
    #2 0x7f2eaef73247 in asan.module_ctor alloc_trace.cpp
    #3 0x7f2ecbbade29 in call_init (/usr/grte/v5/lib64/ld-linux-x86-64.so.2+0x1be29) (BuildId: 3c4ff280c4f5e542ce622870f20d94f2)
    #4 0x7f2ecbbadc8a in _dl_init (/usr/grte/v5/lib64/ld-linux-x86-64.so.2+0x1bc8a) (BuildId: 3c4ff280c4f5e542ce622870f20d94f2)
    #5 0x7f2ecbb99659 in _dl_start_user (/usr/grte/v5/lib64/ld-linux-x86-64.so.2+0x7659) (BuildId: 3c4ff280c4f5e542ce622870f20d94f2)

0x7f2eaf3ee3c0 is located 0 bytes inside of global variable 'cstring::empty' defined in 'third_party/p4lang_p4c/lib/cstring.cpp' (0x7f2eaf3ee3c0) of size 8
  registered at:
    #0 0x55fa54125a58 in __asan_register_globals [third_party/llvm/llvm-project/compiler-rt/lib/asan/asan_globals.cpp:369]:3
    #1 0x55fa54126b6e in __asan_register_elf_globals [third_party/llvm/llvm-project/compiler-rt/lib/asan/asan_globals.cpp:352]:3
    #2 0x7f2eaef73247 in asan.module_ctor alloc_trace.cpp
    #3 0x7f2ecbbade29 in call_init (/usr/grte/v5/lib64/ld-linux-x86-64.so.2+0x1be29) (BuildId: 3c4ff280c4f5e542ce622870f20d94f2)
    #4 0x7f2ecbbadc8a in _dl_init (/usr/grte/v5/lib64/ld-linux-x86-64.so.2+0x1bc8a) (BuildId: 3c4ff280c4f5e542ce622870f20d94f2)
    #5 0x7f2ecbb99659 in _dl_start_user (/usr/grte/v5/lib64/ld-linux-x86-64.so.2+0x7659) (BuildId: 3c4ff280c4f5e542ce622870f20d94f2)

SUMMARY: AddressSanitizer: initialization-order-fiasco [third_party/p4lang_p4c/lib/json.h:110]:23 in JsonValue

@matthewtlam
Copy link
Contributor Author

@jonathan-dilorenzo, @smolkaj for visibility

@fruffy fruffy added the core Topics concerning the core segments of the compiler (frontend, midend, parser) label Aug 12, 2024
Signed-off-by: Matthew Lam matthew.lam.qwerty@gmail.com
@asl
Copy link
Contributor

asl commented Aug 12, 2024

I actually do have patch for this downstream. This is not a complete solution. It only fixes the problem in the single file. However, the order of static initializers is not defined across the translation units. So the cstring literal could initialized somewhere else.

@qobilidop
Copy link
Member

Do we understand why this wasn't caught by the existing sanitizer CI workflow on GitHub?

@asl
Copy link
Contributor

asl commented Aug 12, 2024

Do we understand why this wasn't caught by the existing sanitizer CI workflow on GitHub?

Yes. Though I was able to reproduce it with asan on downstream project. The order of static initializers do matter. And this depends on linker.

@asl
Copy link
Contributor

asl commented Aug 12, 2024

I submitted downstream solution in #4866 that should fix the underlying problem

@matthewtlam will you please double-check it fixes asan issues for you?

@matthewtlam
Copy link
Contributor Author

@asl I will double-check that #4866 fixes the ASAN issues on my side

@asl
Copy link
Contributor

asl commented Aug 12, 2024

Do we understand why this wasn't caught by the existing sanitizer CI workflow on GitHub?

To add more on this. We might want to try ASAN_OPTIONS=check_initialization_order=true:strict_init_order=true. Though it might be overly-aggressive :)

@matthewtlam
Copy link
Contributor Author

@asl Unfortunately, the ASAN check failed with the changes in #4866 as shown below. I believe we use the most strict form of checking for ASAN.

SanitizerError
AddressSanitizer: initialization-order-fiasco [third_party/p4lang_p4c/lib/json.h:110]:23 in JsonValue (//third_party/pins_infra/p4testgen/bmv2:p4testgen_bmv2_integration_test); see [http://go/sanitizer-blaze-warning]
Details
=================================================================
==1324==ERROR: AddressSanitizer: initialization-order-fiasco on address 0x7fd667be8800 at pc 0x7fd6677baceb bp 0x7ffe123e3380 sp 0x7ffe123e3378
READ of size 8 at 0x7fd667be8800 thread T0
    #0 0x7fd6677bacea in JsonValue [third_party/p4lang_p4c/lib/json.h:110]:23
    #1 0x7fd6677bacea in __cxx_global_var_init.2 [third_party/p4lang_p4c/lib/json.cpp:36]:34
    #2 0x7fd6677bacea in _GLOBAL__sub_I_json.cpp third_party/p4lang_p4c/lib/json.cpp
    #3 0x7fd683b9ae29 in call_init (/usr/grte/v5/lib64/ld-linux-x86-64.so.2+0x1be29) (BuildId: 3c4ff280c4f5e542ce622870f20d94f2)
    #4 0x7fd683b9ac8a in _dl_init (/usr/grte/v5/lib64/ld-linux-x86-64.so.2+0x1bc8a) (BuildId: 3c4ff280c4f5e542ce622870f20d94f2)
    #5 0x7fd683b86659 in _dl_start_user (/usr/grte/v5/lib64/ld-linux-x86-64.so.2+0x7659) (BuildId: 3c4ff280c4f5e542ce622870f20d94f2)

0x7fd667be8800 is located 32 bytes before global variable '(anonymous namespace)::cache()::g_cache' defined in 'third_party/p4lang_p4c/lib/cstring.cpp' (0x7fd667be8820) of size 8
  registered at:
    #0 0x558a85125c98 in __asan_register_globals [third_party/llvm/llvm-project/compiler-rt/lib/asan/asan_globals.cpp:392]:3
    #1 0x558a85126ece in __asan_register_elf_globals [third_party/llvm/llvm-project/compiler-rt/lib/asan/asan_globals.cpp:375]:3
    #2 0x7fd6677803b7 in asan.module_ctor alloc_trace.cpp
    #3 0x7fd683b9ae29 in call_init (/usr/grte/v5/lib64/ld-linux-x86-64.so.2+0x1be29) (BuildId: 3c4ff280c4f5e542ce622870f20d94f2)
    #4 0x7fd683b9ac8a in _dl_init (/usr/grte/v5/lib64/ld-linux-x86-64.so.2+0x1bc8a) (BuildId: 3c4ff280c4f5e542ce622870f20d94f2)
    #5 0x7fd683b86659 in _dl_start_user (/usr/grte/v5/lib64/ld-linux-x86-64.so.2+0x7659) (BuildId: 3c4ff280c4f5e542ce622870f20d94f2)

0x7fd667be8800 is located 0 bytes inside of global variable 'cstring::empty' defined in 'third_party/p4lang_p4c/lib/cstring.cpp' (0x7fd667be8800) of size 8
  registered at:
    #0 0x558a85125c98 in __asan_register_globals [third_party/llvm/llvm-project/compiler-rt/lib/asan/asan_globals.cpp:392]:3
    #1 0x558a85126ece in __asan_register_elf_globals [third_party/llvm/llvm-project/compiler-rt/lib/asan/asan_globals.cpp:375]:3
    #2 0x7fd6677803b7 in asan.module_ctor alloc_trace.cpp
    #3 0x7fd683b9ae29 in call_init (/usr/grte/v5/lib64/ld-linux-x86-64.so.2+0x1be29) (BuildId: 3c4ff280c4f5e542ce622870f20d94f2)
    #4 0x7fd683b9ac8a in _dl_init (/usr/grte/v5/lib64/ld-linux-x86-64.so.2+0x1bc8a) (BuildId: 3c4ff280c4f5e542ce622870f20d94f2)
    #5 0x7fd683b86659 in _dl_start_user (/usr/grte/v5/lib64/ld-linux-x86-64.so.2+0x7659) (BuildId: 3c4ff280c4f5e542ce622870f20d94f2)

SUMMARY: AddressSanitizer: initialization-order-fiasco [third_party/p4lang_p4c/lib/json.h:110]:23 in JsonValue

@asl
Copy link
Contributor

asl commented Aug 12, 2024

@asl Unfortunately, the ASAN check failed with the changes in #4866 as shown below. I believe we use the most strict form of checking for ASAN.

Ah, actually this is a different issue!

#4866 fixes the problem when cstring cache is not live before any of cstring literals around.
#4865 fixes the problem where cstrings (cstring::empty and cstring::newline) are not live before json

So, it looks like we need to use both. And actually reduce the number of static objects around, this makes p4c-as-a-library usage hard :(

@vlstill Should we sunset cstring::empty and cstring::newline in the presence of _cs literals?

lib/cstring.h Outdated Show resolved Hide resolved
DCO Remediation Commit for Matthew Lam <matthew.lam.qwerty@gmail.com>

I, Matthew Lam <matthew.lam.qwerty@gmail.com>, hereby add my Signed-off-by to this commit: a19e22e
I, Matthew Lam <matthew.lam.qwerty@gmail.com>, hereby add my Signed-off-by to this commit: 6ccde62
I, Matthew Lam <matthew.lam.qwerty@gmail.com>, hereby add my Signed-off-by to this commit: fe76037

Signed-off-by: Matthew Lam <matthew.lam.qwerty@gmail.com>

Signed-off-by: Matthew Lam <matthew.lam.qwerty@gmail.com>
lib/cstring.h Show resolved Hide resolved
Copy link
Member

@smolkaj smolkaj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@asl asl added this pull request to the merge queue Aug 12, 2024
Merged via the queue into p4lang:main with commit e89f843 Aug 12, 2024
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Topics concerning the core segments of the compiler (frontend, midend, parser)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants