Commit d6085636 authored by Robin Cheng's avatar Robin Cheng Committed by Facebook GitHub Bot

Make //folly/tracing/test:static_tracepoint_test work if compiled as PIE.

Summary:
A tracepoint semaphore address is written to the notes section as either the absolute address (if binary is compiled as non-PIE) or the relative address (if binary is compiled as PIE) of the semaphore. In the former case, the test passes; in the latter case, the test as it was could not possibly pass because the binary is mapped to some random base address.

This diff makes a guess: if the addresses are equal, then we're non-PIE; otherwise check if the address offset is valid - that offset is the address at which the binary is mapped, so it always begins with the ELF header, so we can check against that.

If someone knows how to easily figure out that random address or to detect whether the binary is PIE (thereby disabling the test) that'd be great too.

This fixes TSAN for the test, as TSAN binaries are built as PIE.

Reviewed By: yfeldblum

Differential Revision: D22752824

fbshipit-source-id: cd320f9fb8355a20f65e2385f0a1ab1c24ec0b81
parent 4892f1bb
......@@ -32,6 +32,7 @@
#include <folly/portability/Unistd.h>
#include <folly/tracing/StaticTracepoint.h>
#include <folly/tracing/test/StaticTracepointTestModule.h>
#include <sys/auxv.h>
static const std::string kUSDTSubsectionName = FOLLY_SDT_NOTE_NAME;
static const int kUSDTNoteType = FOLLY_SDT_NOTE_TYPE;
......@@ -229,7 +230,14 @@ static bool getTracepointArguments(
align4Bytes(pos);
if (provider == expectedProvider && probe == expectedProbe) {
CHECK_EQ(expectedSemaphore, semaphoreAddr);
// If the binary is not PIE, then the addresses should match.
if (expectedSemaphore == static_cast<uintptr_t>(semaphoreAddr)) {
return true;
}
// If the test is built as PIE, then the semaphore address listed in the
// notes section is relative to the beginning of the binary image.
auto binaryBase = getauxval(AT_PHDR) - 0x40;
CHECK_EQ(expectedSemaphore, binaryBase + semaphoreAddr);
return true;
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment