dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.35k stars 4.74k forks source link

How can I compile a Native AOT app with AddressSanitizer? #108315

Closed sweemer closed 1 month ago

sweemer commented 1 month ago

Description

I have come across the EnableNativeSanitizers property, but adding it to my Native AOT app's csproj file doesn't seem to actually result in the app being compiled with AddressSanitizer. Is that expected?

If this feature is not yet available, then can I hereby request it? It will be very useful for testing Native AOT apps, especially those that make use of direct P/Invoke and static linking to native libs that have also been built with AddressSanitizer.

Reproduction Steps

1. git clone https://github.com/sweemer/NativeAotASan.git
2. cd NativeAotASan
3. g++ -c main.cpp -fsanitize=address
4. ar qc libnativeaottest.a main.o
5. dotnet publish --verbosity detailed # no sign of -fsanitize=address in the build

Expected behavior

Adding EnableNativeSanitizers and PublishAot to a csproj file results in the app being built with AddressSanitizer.

Actual behavior

The app is not built with AddressSanitizer.

Regression?

No response

Known Workarounds

No response

Configuration

$ dotnet --info
.NET SDK:
 Version:           8.0.401
 Commit:            811edcc344
 Workload version:  8.0.400-manifests.b6724b7a
 MSBuild version:   17.11.4+37eb419ad

Runtime Environment:
 OS Name:     ubuntu
 OS Version:  20.04
 OS Platform: Linux
 RID:         linux-x64
 Base Path:   /usr/share/dotnet/sdk/8.0.401/

.NET workloads installed:
Configured to use loose manifests when installing new manifests.
There are no installed workloads to display.

Host:
  Version:      8.0.8
  Architecture: x64
  Commit:       08338fcaa5

.NET SDKs installed:
  8.0.401 [/usr/share/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 7.0.20 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.8 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 7.0.20 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.8 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

Other architectures found:
  None

Environment variables:
  DOTNET_ROOT       [/usr/share/dotnet]

global.json file:
  Not found

Learn more:
  https://aka.ms/dotnet/info

Download .NET:
  https://aka.ms/dotnet/download

Other information

No response

dotnet-policy-service[bot] commented 1 month ago

Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas See info in area-owners.md if you want to be subscribed.

hez2010 commented 1 month ago

You can try passing -fsanitize=address to the linker manually.

<ItemGroup>
    <LinkerArg Include="-fsanitize=address" />
</ItemGroup>
sweemer commented 1 month ago

Thanks @hez2010 but it's not enough to link with ASan - the sources must be compiled with ASan as well, which must be implemented in the Native AOT compiler as far as I understand.

MichalStrehovsky commented 1 month ago

You have to build the native AOT runtime from source. There's no way to do that currently besides actually cloning this repo and following the instructions. The repo has native support for ASAN so all that should be needed is an extra flag: #74623.

83611 tracks shipping runtime source so that people can build it with any options they desire. Not sure if that's all that's needed for ASAN but it's a prerequisite.

sweemer commented 1 month ago

@MichalStrehovsky Thanks for the explanation