Skip to content

[Clang][Driver] Use-after free in CreateOffloadingDeviceToolChains #131325

@Meinersbur

Description

@Meinersbur

For deducing offload triples, the driver creates some temporary ToolChains here:

auto TempTC = std::make_unique<toolchains::CudaToolChain>(
*this, *NVPTXTriple, *HostTC, C.getInputArgs());
for (StringRef Arch : getOffloadArchs(
C, C.getArgs(), Action::OFK_OpenMP, &*TempTC, true))

auto TempTC = std::make_unique<toolchains::AMDGPUOpenMPToolChain>(
*this, *AMDTriple, *HostTC, C.getInputArgs());
for (StringRef Arch : getOffloadArchs(
C, C.getArgs(), Action::OFK_OpenMP, &*TempTC, true))

The problem is that the ToolChain is used to index into a cache here:

DerivedArgList *&Entry = TCArgs[{TC, BoundArch, DeviceOffloadKind}];

It is possible that the temporary ToolChains are allocated at the same address. In that case the derived arguments for CudaToolChain are reused for AMDGPUOpenMPToolChain.

Causes the flakiness of the offload-Xarch.c test case here: https://lab.llvm.org/buildbot/#/builders/81/builds/5233
Other complains here: #126248

Temporay Toolchains introduced here: a17ab7a

Btw, it is super risky to use StringRef as map index type. Instantiating temporary toolchains is not common, but temporary strings definitely are.

C.getArgsForToolChain(&TC, Triple.getArchName(), Action::OFK_None);
would be one. More bugs be waiting.

Metadata

Metadata

Assignees

Labels

clang:driver'clang' and 'clang++' user-facing binaries. Not 'clang-cl'

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions