This is an archive of the discontinued LLVM Phabricator instance.

[asan] Disable stack malloc on functions containing inline assembly
ClosedPublic

Authored by zaks.anna on Jun 24 2015, 5:17 PM.

Details

Reviewers
glider
samsonov
Summary

Don't run stack malloc on functions containing inline assembly even on 64-bit platforms. It makes LLVM run out of registers.

It fails with the following test case on darwin, 64 bit.

clang -cc1 -O0 -triple x86_64-apple-macosx10.10.0 -emit-obj -fsanitize=address -mstackrealign -o ~/tmp/ex.o -x c ex.c
error: inline assembly requires more registers than available

#define PTR_REG(val) "r" val
void TestInlineAssembly(const unsigned char *S, unsigned int pS, unsigned char *D, unsigned int pD, unsigned int h) {

unsigned int sr = 4, pDiffD = pD - 5;
unsigned int pDiffS = (pS << 1) - 5;
char flagSA = ((pS & 15) == 0),
flagDA = ((pD & 15) == 0);
asm volatile (
  "mov %0,  %%"PTR_REG("si")"\n"
  "mov %2,  %%"PTR_REG("cx")"\n"
  "mov %1,  %%"PTR_REG("di")"\n"
  "mov %8,  %%"PTR_REG("ax")"\n"
  :
  : "m" (S), "m" (D), "m" (pS), "m" (pDiffS), "m" (pDiffD), "m" (sr), "m" (flagSA), "m" (flagDA), "m" (h)
  : "%"PTR_REG("si"), "%"PTR_REG("di"), "%"PTR_REG("ax"), "%"PTR_REG("cx"), "%"PTR_REG("dx"), "memory"

);
}

This functionality has already been disabled for 32-bit in http://reviews.llvm.org/D8790, r233979.

Diff Detail

Event Timeline

zaks.anna updated this revision to Diff 28423.Jun 24 2015, 5:17 PM
zaks.anna retitled this revision from to [asan] Disable stack malloc on functions containing inline assembly.
zaks.anna updated this object.
zaks.anna edited the test plan for this revision. (Show Details)
zaks.anna added reviewers: samsonov, glider.
zaks.anna added subscribers: kcc, Unknown Object (MLST).
samsonov accepted this revision.Jun 24 2015, 5:37 PM
samsonov edited edge metadata.

LGTM

lib/Transforms/Instrumentation/AddressSanitizer.cpp
1757

Please update the comment.

This revision is now accepted and ready to land.Jun 24 2015, 5:37 PM
glider accepted this revision.Jun 25 2015, 2:44 AM
glider edited edge metadata.

LGTM
Doesn't this denote a problem with the register allocator?
In theory, 1 additional register should be sufficient, although suboptimal.

I couldn't try this with a different allocator, because -mregalloc doesn't work with -O0.
However the problem isn't reproducible with -O1, so it might be a good idea to file a regalloc bug.

Alex, I don't fully understand why we think this could be a problem with register allocator. Could you explain this in more detail?

Doesn't this denote a problem with the register allocator?
In theory, 1 additional register should be sufficient, although suboptimal.

zaks.anna closed this revision.Jun 25 2015, 6:15 PM

in r240722

Alex, I don't fully understand why we think this could be a problem with register allocator. Could you explain this in more detail?

Well, IIUC the piece of assembly code you've provided requires only 5 general-purpose registers, while x86_64 has 16 of them. With register spilling it should be possible to run using a single register for the rest of the function (which isn't really optimal). Given we have more than one register left, I consider the regallocator to be a bit too lazy in this case (not to downplay its value in many other cases).