Add sanitizer support to RISCV toolchain

899 views
Skip to first unread message

Jun Sha

unread,
Jul 12, 2020, 9:59:47 PM7/12/20
to RISC-V SW Dev
Hi,

I want to add sanitizer support to the riscv toolchain. Currently, I have ported the RISCV architecture to the AddressSanitizer that has been integrated in GCC, but haven't added support for the AddressSanitizer on LLVM. 

I'm now wondering whether someone else has done this work before. If there is no previous work, I can send my patch to the community. 

I would appreciate it if you could reply to me as soon as possible.

 Best,

Jun

Jim Wilson

unread,
Jul 13, 2020, 12:03:45 AM7/13/20
to Jun Sha, RISC-V SW Dev
On Sun, Jul 12, 2020 at 6:59 PM Jun Sha <js5...@columbia.edu> wrote:
> I want to add sanitizer support to the riscv toolchain. Currently, I have ported the RISCV architecture to the AddressSanitizer that has been integrated in GCC, but haven't added support for the AddressSanitizer on LLVM.

In FSF GCC, asan and ubsan already work. It is only tsan and maybe
lsan that aren't supported yet. Are you looking at current gcc
sources? Since most of the sanitizer is a library that is shared
between gcc and llvm, if asan and ubsan work on gcc they should also
work on llvm.

Note that the FSF requires copyright assignments before it will accept
patches. If you are serious about contributing to GNU projects you
should try to get a copyright assignment.

Jim

Jun Sha

unread,
Jul 13, 2020, 1:42:59 AM7/13/20
to RISC-V SW Dev, ji...@sifive.com, RISC-V SW Dev, Jun Sha
Hi,

Thank you for your interaction. What I'm concerned about is whether asan has worked in RISC-V GCC and RISC-V LLVM. Has RISC-V already been ported to the sanitizer library in both gcc and llvm?

Jun

Andreas Schwab

unread,
Jul 13, 2020, 3:34:34 AM7/13/20
to Jim Wilson, Jun Sha, RISC-V SW Dev
On Jul 12 2020, Jim Wilson wrote:

> In FSF GCC, asan and ubsan already work.

??? There is no asan support in gcc yet.

Andreas.

--
Andreas Schwab, SUSE Labs, sch...@suse.de
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE 1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."

Jim Wilson

unread,
Jul 13, 2020, 12:24:21 PM7/13/20
to Andreas Schwab, Jun Sha, RISC-V SW Dev
On Mon, Jul 13, 2020 at 12:34 AM Andreas Schwab <sch...@suse.de> wrote:
> On Jul 12 2020, Jim Wilson wrote:
> > In FSF GCC, asan and ubsan already work.
> ??? There is no asan support in gcc yet.

II do see RISC-V support in sanitizer_common. And it builds when I
build a toolchain. Why are we building something that doesn't work?
Anyways, I trust Andreas on this matter, so I guess we do need
patches.

Jim

Andreas Schwab

unread,
Jul 13, 2020, 12:57:30 PM7/13/20
to Jim Wilson, Jun Sha, RISC-V SW Dev
On Jul 13 2020, Jim Wilson wrote:

> II do see RISC-V support in sanitizer_common. And it builds when I
> build a toolchain. Why are we building something that doesn't work?

$ echo 'int main(){}' | gcc -fsanitize=address -xc -
cc1: warning: ‘-fsanitize=address’ and ‘-fsanitize=kernel-address’ are not supported for this target

See also <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91441>.

Kito Cheng

unread,
Jul 13, 2020, 9:24:27 PM7/13/20
to Andreas Schwab, Jim Wilson, Jun Sha, RISC-V SW Dev
I guess at least we need to implement TARGET_ASAN_SHADOW_OFFSET on the GCC back-end.

--
You received this message because you are subscribed to the Google Groups "RISC-V SW Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sw-dev+un...@groups.riscv.org.
To view this discussion on the web visit https://groups.google.com/a/groups.riscv.org/d/msgid/sw-dev/875zaric95.fsf%40igel.home.

Jun Sha

unread,
Jul 13, 2020, 10:33:47 PM7/13/20
to RISC-V SW Dev, ji...@sifive.com, Jun Sha, RISC-V SW Dev, Andreas Schwab
Hi Jim,

What do you mean by RISC-V support in sanitizer_common? Has anyone already enabled the address sanitizer for RISC-V GCC toolchain? 

From my perspective, the latest version of gnu toolchain cannot support the flag '-fsanitize=address' for RISC-V compilation yet.

Also, what about things in RISC-V LLVM? It needs asan support too. 

Jun

Kito Cheng

unread,
Jul 13, 2020, 10:56:16 PM7/13/20
to Jun Sha, RISC-V SW Dev, ji...@sifive.com, Andreas Schwab
Hi Jun:

Just remind you, all change of gcc/libsanitizer/ except configure script and Makefile should upstream
to LLVM *AND* then sync to gcc source tree, so the right place to send patch is
LLVM's Phabricator/review system [1]


--
You received this message because you are subscribed to the Google Groups "RISC-V SW Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sw-dev+un...@groups.riscv.org.

Kito Cheng

unread,
Jul 13, 2020, 11:01:14 PM7/13/20
to Jun Sha, RISC-V SW Dev, ji...@sifive.com, Andreas Schwab
Hi all:

Checked with Greentime/RISC-V kernel developer, they use KASAN for a while, 
and seems like work as expected, because the kernel has specified the shadow offset
by option -fasan-shadow-offset, and use the their own sanitizer library, so kernel can
work without any GCC back-end porting.

but for user space address sanitizer, it's not supported yet.

Jim Wilson

unread,
Jul 13, 2020, 11:29:51 PM7/13/20
to Jun Sha, RISC-V SW Dev, Andreas Schwab
On Mon, Jul 13, 2020 at 7:33 PM Jun Sha <js5...@columbia.edu> wrote:
> What do you mean by RISC-V support in sanitizer_common? Has anyone already enabled the address sanitizer for RISC-V GCC toolchain?

Just grep for riscv in the sanitizer sources and there is stuff there.
But obviously some other stuff is still missing. It looks like maybe
ubsan is the only one that currently works.

> From my perspective, the latest version of gnu toolchain cannot support the flag '-fsanitize=address' for RISC-V compilation yet.

Correct. -fsanitize=address is not supported in gcc. I was misled by
the fact that we are building it even though it doesn't work.

> Also, what about things in RISC-V LLVM? It needs asan support too.

I don't know about llvm, but other people can answer that.

Jim

Jun Sha

unread,
Jul 17, 2020, 8:05:23 AM7/17/20
to RISC-V SW Dev, kito....@gmail.com, RISC-V SW Dev, ji...@sifive.com, Andreas Schwab, Jun Sha
Hi Kito,

I'm wondering how KASAN worked by Greentime/RISC-V kernel developer with ASAN not supported? 

According to my understanding, we must first implement support for address sanitizers for user-space applications before adding the -fsanitize=kernel-address flag for the Linux Kernel.

Jun

Kito Cheng

unread,
Jul 20, 2020, 11:40:47 PM7/20/20
to Jun Sha, RISC-V SW Dev, ji...@sifive.com, Andreas Schwab
Hi Jun:

> I'm wondering how KASAN worked by Greentime/RISC-V kernel developer with ASAN not supported? 

KASAN has its own implementation [1-2] which is independent from libsanitizer, the only thing needed to do in GCC or clang/LLVM is insert those instrumentation around memory access. 

> According to my understanding, we must first implement support for address sanitizers for user-space applications before adding the -fsanitize=kernel-address flag for the Linux Kernel.

I was understood that same as you until you raise the mailing thread and I asked linux kernel developer :P but it seems like they don't have so strong dependency, instrumentation generation for ASAN/KASAN is a generic function for GCC and clang/LLVM, so most work is library porting I think.


Anatoly P

unread,
Jul 28, 2020, 12:58:15 AM7/28/20
to RISC-V SW Dev
Hi Jun!

Actually it happened that we were doing asan (and even hwasan). The whole project is here https://github.com/gattaca-lab/llvm-project (please do note that the code was used for the initial prototyping and is quite messy). Right now we are in the process of preparing the patches to push to mainline (for asan part).

Jun Sha

unread,
Aug 11, 2020, 2:26:50 AM8/11/20
to RISC-V SW Dev
Hi,

Has anyone been encounted with a problem that /libsanitizer/sanitizer_common/sanitizer_syscall_generic.inc: error: ‘__NR_open’ was not declared in this scope during the sanitizer_linux compilation? I have added the specific architecture when defining SANITIZER_USES_CANONICAL_LINUX_SYSCALLS in libsanitizer/sanitizer_common/sanitizer_platform.h. Should SANITIZER_USES_CANONICAL_LINUX_SYSCALLS be defined as 1 by default?

Jun

Jun Sha

unread,
Aug 11, 2020, 2:34:18 AM8/11/20
to RISC-V SW Dev
Also, I'm wondering what the difference between (__riscv__) and (__riscv) is when defining a new specific arch.

Kito Cheng

unread,
Aug 11, 2020, 12:17:21 PM8/11/20
to Jun Sha, RISC-V SW Dev
Hi Jun:

> Also, I'm wondering what the difference between (__riscv__) and (__riscv) is when defining a new specific arch.
I would suggest you use __riscv rather than __riscv__, __riscv is
defined in RISC-V C API Specification.

[1] https://github.com/riscv/riscv-c-api-doc/blob/master/riscv-c-api.md

>> Has anyone been encounted with a problem that /libsanitizer/sanitizer_common/sanitizer_syscall_generic.inc: error: ‘__NR_open’ was not declared in this scope during the sanitizer_linux compilation? I have added the specific architecture when defining SANITIZER_USES_CANONICAL_LINUX_SYSCALLS in libsanitizer/sanitizer_common/sanitizer_platform.h. Should SANITIZER_USES_CANONICAL_LINUX_SYSCALLS be defined as 1 by default?

Maybe the comment can give you the answer?

...
// The AArch64 and RISC-V linux ports use the canonical syscall set as
// mandated by the upstream linux community for all new ports. Other ports
// may still use legacy syscalls.
...

https://github.com/llvm/llvm-project/blob/master/compiler-rt/lib/sanitizer_common/sanitizer_platform.h#L264

Anatoly Parshintsev

unread,
Oct 9, 2020, 12:59:15 AM10/9/20
to RISC-V SW Dev, kito....@gmail.com, RISC-V SW Dev, js5...@columbia.edu

Wei Wu (吴伟)

unread,
Oct 9, 2020, 3:45:36 AM10/9/20
to Anatoly Parshintsev, RISC-V SW Dev, kito....@gmail.com, js5...@columbia.edu
Great work. Thanks!

--
You received this message because you are subscribed to the Google Groups "RISC-V SW Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sw-dev+un...@groups.riscv.org.


--
Best wishes,
Wei Wu (吴伟)

Jun Sha

unread,
Nov 21, 2023, 7:00:43 AM11/21/23
to RISC-V SW Dev, lazyp...@gmail.com, RISC-V SW Dev, kito....@gmail.com, Jun Sha, kupokup...@gmail.com
Can lsan work on riscv gcc currently? I cannot find any lsan libraries in the compiled toolchain. 

Tommy Murphy

unread,
Nov 21, 2023, 10:47:48 AM11/21/23
to Jun Sha, RISC-V SW Dev, lazyp...@gmail.com, RISC-V SW Dev, kito....@gmail.com, Jun Sha, kupokup...@gmail.com
This seems to suggest that some level of support for RV64 was previously implemented?

Reply all
Reply to author
Forward
0 new messages