This is an archive of the discontinued LLVM Phabricator instance.

[RISCV][Lsan] Set allocator for AP64
ClosedPublic

Authored by hauhsu on Jun 14 2023, 3:29 AM.

Details

Summary

This patch uses similar allocator configuration to Asan, i.e. dynamic
allocator start address (~(uptr)0) and use VeryDenseSizeClassMap.
Since there are small memory spaces for SV39, using the
DefaultSizeClassMap causes error:

sanitizer_allocator_primary64.h:639:18: error: static assertion failed due to requirement '(kRegionSize) >= (1ULL << (64 / 2))':

639 |   COMPILER_CHECK((kRegionSize) >= (1ULL << (SANITIZER_WORDSIZE / 2)));
    |   ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

sanitizer_common/sanitizer_allocator_primary64.h:639:32: note: expression evaluates to '2147483648 >= 4294967296'

Diff Detail

Event Timeline

hauhsu created this revision.Jun 14 2023, 3:29 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 14 2023, 3:29 AM
hauhsu requested review of this revision.Jun 14 2023, 3:29 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 14 2023, 3:29 AM
Herald added subscribers: Restricted Project, pcwang-thead, eopXD. · View Herald Transcript
hauhsu changed the visibility from "Public (No Login Required)" to "No One".Jun 14 2023, 3:33 AM
hauhsu updated this revision to Diff 531607.Jun 14 2023, 9:58 PM

Separate commits and add dependency.

hauhsu edited the summary of this revision. (Show Details)Jun 14 2023, 10:06 PM
hauhsu changed the visibility from "No One" to "Public (No Login Required)".

Hi @luismarques @vitalybuka @MaskRay these patches might need your attention.
Thanks :)

Can lsan work for riscv64 now? I can run binaries compiled with -fsanitize=address adding ASAN_OPTIONS=detect_leaks=1, but no memory-leak error is reported.

Can lsan work for riscv64 now? I can run binaries compiled with -fsanitize=address adding ASAN_OPTIONS=detect_leaks=1, but no memory-leak error is reported.

This file applies to standalone lsan, without asan.

sanitizer_common/sanitizer_allocator_primary64.h:639:32: note: expression evaluates to '2147483648 >= 4294967296'

How do we get 2147483648 there?

vitalybuka added inline comments.Jun 21 2023, 11:41 PM
compiler-rt/lib/lsan/lsan_allocator.h
74–76

why this can be like SANITIZER_FUCHSIA
DefaultSizeClassMap
and
kAllocatorSize = 0x40000000000ULL; // 4T.

MaskRay added inline comments.Jun 23 2023, 2:25 PM
compiler-rt/lib/lsan/lsan_allocator.h
74–76

sanitizer_allocator_primary64.h:639:18: error: static assertion failed due to requirement '(kRegionSize) >= (1ULL << (64 / 2))': is due to an overly restrictive static_assert. D153664 shall relax this restriction and allow DefaultSizeClassMap to be usable.

kAllocatorSize = 0x40000000000ULL cannot be used as SV39 https://docs.kernel.org/riscv/vm-layout.html#risc-v-linux-kernel-sv39 cannot accomoodate such a large mmap request.

Can lsan work for riscv64 now? I can run binaries compiled with -fsanitize=address adding ASAN_OPTIONS=detect_leaks=1, but no memory-leak error is reported.

This file applies to standalone lsan, without asan.

So only x86 supports lsan with asan now? Are there extra work to support lsan with asan than standalone?

Can lsan work for riscv64 now? I can run binaries compiled with -fsanitize=address adding ASAN_OPTIONS=detect_leaks=1, but no memory-leak error is reported.

Asan and Lsan should work on SV39 virtual memory mapping mode without applying this patchset.
There was an Asan/Lsan porting done by @EccoTheDolphin (https://reviews.llvm.org/D87580) and @luismarques (https://reviews.llvm.org/D92403).

What Kito and I tries to do is to enable Asan and Lsan under different virtual memory mapping mode. I.e. SV39, SV48 and SV57.
The commits are:

So only x86 supports lsan with asan now? Are there extra work to support lsan with asan than standalone?

Lsan with asan and standalone lsan for SV39 should work without any changes.

Lsan with asan for SV39/48 should be supported once asan commits are applied, that is with the first 2 patches (D139823, D139827).
standalone lsan for SV39/48 needs the first 3 patches (D139823, D139827, D152895)

Lsan with asan and standalone lsan for SV39/48/57 need 4 patches (D139823, D139827, D152895, D152990)

Can lsan work for riscv64 now? I can run binaries compiled with -fsanitize=address adding ASAN_OPTIONS=detect_leaks=1, but no memory-leak error is reported.

Asan and Lsan should work on SV39 virtual memory mapping mode without applying this patchset.
There was an Asan/Lsan porting done by @EccoTheDolphin (https://reviews.llvm.org/D87580) and @luismarques (https://reviews.llvm.org/D92403).

Only with this two patches, I can run binaries compiled with -fsanitize=address adding ASAN_OPTIONS=detect_leaks=1, but no memory-leak error is reported.

Can lsan work for riscv64 now? I can run binaries compiled with -fsanitize=address adding ASAN_OPTIONS=detect_leaks=1, but no memory-leak error is reported.

Asan and Lsan should work on SV39 virtual memory mapping mode without applying this patchset.
There was an Asan/Lsan porting done by @EccoTheDolphin (https://reviews.llvm.org/D87580) and @luismarques (https://reviews.llvm.org/D92403).

Only with this two patches, I can run binaries compiled with -fsanitize=address adding ASAN_OPTIONS=detect_leaks=1, but no memory-leak error is reported.

Hmmm That's weird, I can use Lsan with our old toolchain (built in 2022.08):

$ cat lsan.c
// Source:
// https://github.com/google/sanitizers/wiki/AddressSanitizerLeakSanitizer
//
#include <stdlib.h>

void *p;

int main() {
  p = malloc(7);
  p = 0; // The memory is leaked here.
  return 0;
}

$ riscv64-unknown-linux-gnu-clang -mcpu=sifive-x280n -fsanitize=address lsan.c -o lsan -static-libsan 

(I added -static-libsan to make sure the old librt is used.)

Then copy the binary lsan to QEMU (rv39 mode) and run it:

root@qemuriscv64:~# ./lsan

=================================================================
==750==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 7 byte(s) in 1 object(s) allocated from:
    #0 0x2ae4e0edcc in __interceptor_malloc (/home/root/lsan+0xc5dcc)
    #1 0x2ae4e4242e in main (/home/root/lsan+0xf942e)
    #2 0x3f8fe19700 in __libc_start_call_main libc-start.c

SUMMARY: AddressSanitizer: 7 byte(s) leaked in 1 allocation(s).

@hauhsu There are many folks who care about the sanitizers but don't have much experience setting up RISC-V environments, especially the more nuanced variants like Sv39, Sv48.
It'd be useful if you can provide the environment setup instructions somewhere, with concrete examples (raw commands).

Have you tested that D153664 makes typedef VeryDenseSizeClassMap SizeClassMap; unnecessary?

@hauhsu There are many folks who care about the sanitizers but don't have much experience setting up RISC-V environments, especially the more nuanced variants like Sv39, Sv48.
It'd be useful if you can provide the environment setup instructions somewhere, with concrete examples (raw commands).

Actually build bot would be even better, otherwise no guaranty that any fixes will continue to work.

Have you tested that D153664 makes typedef VeryDenseSizeClassMap SizeClassMap; unnecessary?

hauhsu added a comment.Jul 6 2023, 2:33 AM

@hauhsu There are many folks who care about the sanitizers but don't have much experience setting up RISC-V environments, especially the more nuanced variants like Sv39, Sv48.
It'd be useful if you can provide the environment setup instructions somewhere, with concrete examples (raw commands).

That make sense. After discussing with @kito-cheng I, 'll try to produce an environment for building and testing RISC-V LLVM. But it might take some time.
Our plane is to use QEMU system mode + Fedora image.

Actually build bot would be even better, otherwise no guaranty that any fixes will continue to work.

Sadly we can't promise to have resources for a build bot. But we'd love to see others to provide one.

Have you tested that D153664 makes typedef VeryDenseSizeClassMap SizeClassMap; unnecessary?

I'll test it later and update my commits if necessary. Thanks for reminding me.

Have you tested that D153664 makes typedef VeryDenseSizeClassMap SizeClassMap; unnecessary?

That commit works. I'll update this patch later.

hauhsu updated this revision to Diff 545086.Jul 28 2023, 3:14 AM

There is no need to use VeryDenseSizeClassMap after applying D153664.

MaskRay accepted this revision.Jul 30 2023, 8:01 PM
This revision is now accepted and ready to land.Jul 30 2023, 8:01 PM
This revision was landed with ongoing or failed builds.Aug 8 2023, 3:18 AM
Closed by commit rGe7191fbec3ba: [RISCV][Lsan] Set allocator for AP64 (authored by hauhsu, committed by hau-hsu). · Explain Why
This revision was automatically updated to reflect the committed changes.