This is an archive of the discontinued LLVM Phabricator instance.

[msan] Intercept dladdr1, and refactor dladdr
ClosedPublic

Authored by thurston on Jun 30 2023, 4:08 PM.

Details

Summary

This patch adds an msan interceptor for dladdr1 (with support for RTLD_DL_LINKMAP and RTLD_DL_SYMENT) and an accompanying test. It also adds a helper file, msan_dl.cpp, that contains UnpoisonDllAddrInfo (refactored out of the dladdr interceptor) and UnpoisonDllAddr1ExtraInfo.

Diff Detail

Event Timeline

thurston created this revision.Jun 30 2023, 4:08 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 30 2023, 4:08 PM
Herald added a subscriber: Enna1. · View Herald Transcript
thurston requested review of this revision.Jun 30 2023, 4:08 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 30 2023, 4:08 PM
Herald added a subscriber: Restricted Project. · View Herald Transcript
thurston added a subscriber: tra.
vitalybuka added inline comments.Jun 30 2023, 4:25 PM
compiler-rt/lib/msan/msan_interceptors.cpp
1551–1567

what if we move these into a separate cpp file and include headers with them
same for link_map
we don't want header in the interceptor file, other cpp should work

And add most of interceptor body into UnpoisonDllAddr1ExtraInfo()

1551–1567

e.g. msan_dl.cpp

compiler-rt/test/msan/dladdr1_test.c
4

do you need FileCheck here, or asserts is enough?

vitalybuka added inline comments.Jun 30 2023, 4:27 PM
compiler-rt/lib/msan/msan_interceptors.cpp
1551–1567

same struct dlinfo

thurston added inline comments.Jun 30 2023, 4:40 PM
compiler-rt/lib/msan/msan_interceptors.cpp
1551–1567

Good idea, I will make those changes and update this CL.

compiler-rt/test/msan/dladdr1_test.c
4
// CHECK: libc.so
// CHECK: dladdr1_test

are inconvenient to implement as asserts, because they're not exact matches. The output on my machine is:

0x563819032000: '', 0x563819035dc8
0x7ffe4b330000: 'linux-vdso.so.1', 0x7ffe4b3303e0
0x7fb9237d7000: '/lib/x86_64-linux-gnu/libc.so.6', 0x7fb9239a8b60
0x7fb9239d2000: '/lib64/ld-linux-x86-64.so.2', 0x7fb923a03e20
0x7fb9239cb000: '/tmp/dl-so.so', 0x7fb9239cee20

but I expect subtle differences on build bots and other machines that aren't precisely the same 64-bit Linux.

thurston added inline comments.Jun 30 2023, 4:42 PM
compiler-rt/test/msan/dladdr1_test.c
4

Oops, that last line of the output should be

0x7f96e35b6000: '/usr/local/google/home/thurston/llvm-projectD/build/projects/compiler-rt/test/msan/lld-X86_64/Output/dladdr1_test.c.tmp-so.so', 0x7f96e35b86a8

(the previous output was from my working copy in /tmp)

thurston updated this revision to Diff 536481.Jun 30 2023, 4:53 PM

Adds a new header file, msan_dl.h, per Vitaly's suggestion.

thurston planned changes to this revision.Jun 30 2023, 4:56 PM
vitalybuka added inline comments.Jun 30 2023, 4:59 PM
compiler-rt/lib/msan/msan_dl.h
13

that's not what I suggested

suggestion is to not define these structs but:
#include <dlfcn.h> and friends in a cpp file
and export function like UnpoisonDllAddr1ExtraInfo

thurston updated this revision to Diff 536484.Jun 30 2023, 5:17 PM

Refactor into UnpoisonDllAddr1ExtraInfo(), per Vitaly's suggestion

thurston planned changes to this revision.Jun 30 2023, 5:18 PM
thurston added inline comments.
compiler-rt/lib/msan/msan_dl.h
13

Ah, I see. Let me fix that.

thurston updated this revision to Diff 536485.Jun 30 2023, 5:25 PM

"#include <dlfcn.h> and friends in a cpp file and export function like UnpoisonDllAddr1ExtraInfo"

vitalybuka added inline comments.Jun 30 2023, 6:52 PM
compiler-rt/lib/msan/msan_dl.cpp
2

please add standard file headers

5

#include "msan_dl.h"

40

Same for void UnpoisonDllAddrInfo?

compiler-rt/lib/msan/msan_interceptors.cpp
22

.h is not in the patch

vitalybuka accepted this revision.Jun 30 2023, 6:59 PM
vitalybuka added inline comments.
compiler-rt/lib/msan/msan_dl.cpp
22–28
This revision is now accepted and ready to land.Jun 30 2023, 6:59 PM
thurston added inline comments.Jun 30 2023, 7:47 PM
compiler-rt/lib/msan/msan_interceptors.cpp
1551–1567

If I #include dlfcn.h to get the definition of struct Dl_info, it will also drag in the declaration of dladdr/dladdr1/dlerror, conflicting with the interceptors. I suspect that's why msan_interceptors.cpp had defined their own struct dlinfo.

vitalybuka added inline comments.Jun 30 2023, 7:50 PM
compiler-rt/lib/msan/msan_interceptors.cpp
1551–1567

you will include dlfcn.h only into msan_dl.cpp

thurston updated this revision to Diff 536499.Jun 30 2023, 8:12 PM

Change order of headers

vitalybuka added inline comments.Jun 30 2023, 8:12 PM
compiler-rt/lib/msan/msan_dl.cpp
40

can map be nullptr? we may crash on l_prev then

thurston added inline comments.Jun 30 2023, 8:12 PM
compiler-rt/lib/msan/msan_interceptors.cpp
1551–1567

I ended up also having to replace Dl_info* with void*, in everywhere except msan_dl.cpp.

vitalybuka accepted this revision.Jun 30 2023, 8:14 PM
vitalybuka added inline comments.
compiler-rt/lib/msan/msan_dl.cpp
2

still no header?

//===-- msan_interceptors.cpp ---------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
compiler-rt/lib/msan/msan_dl.h
2

also

//===-- msan_interceptors.cpp ---------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
thurston added inline comments.Jun 30 2023, 8:16 PM
compiler-rt/lib/msan/msan_dl.cpp
2

Sorry, I misinterpreted "add standard file headers" as "add #include <x.h>". I'll fix.

vitalybuka added inline comments.Jun 30 2023, 8:19 PM
compiler-rt/lib/msan/msan_dl.cpp
2–5
thurston updated this revision to Diff 536500.Jun 30 2023, 8:23 PM

Fix null pointer case
Add license to top of header files

vitalybuka accepted this revision.Jun 30 2023, 8:39 PM
vitalybuka added inline comments.
compiler-rt/lib/msan/msan_dl.cpp
43–52
thurston updated this revision to Diff 536506.Jun 30 2023, 8:53 PM
thurston marked 14 inline comments as done.

Made 'if (! map) return;' change

thurston retitled this revision from [msan] Intercept dladdr1, and add lit test to [msan] Intercept dladdr1, and refactor dladdr.Jun 30 2023, 8:54 PM
thurston edited the summary of this revision. (Show Details)
vitalybuka accepted this revision.Jun 30 2023, 10:07 PM
This revision was landed with ongoing or failed builds.Jul 1 2023, 12:28 PM
This revision was automatically updated to reflect the committed changes.
thurston reopened this revision.Jul 5 2023, 2:11 PM
This revision is now accepted and ready to land.Jul 5 2023, 2:11 PM
thurston updated this revision to Diff 537494.Jul 5 2023, 2:11 PM

Fix 'cast from 'void ' to 'const Elf64_Sym ' must have all intermediate pointers const qualified to be safe [-Werror,-Wcast-qual]'

This revision was landed with ongoing or failed builds.Jul 6 2023, 9:19 AM
This revision was automatically updated to reflect the committed changes.
brooks added a subscriber: brooks.Jul 6 2023, 4:38 PM

This change broke the build of msan on FreeBSD. dladdr1 seems to be a glibc-ism and is not present in FreeBSD or musl. All this functionality needs to be gated by a CMake check.

thurston added a comment.EditedJul 6 2023, 5:24 PM

This change broke the build of msan on FreeBSD. dladdr1 seems to be a glibc-ism and is not present in FreeBSD or musl. All this functionality needs to be gated by a CMake check.

@brooks Thanks for the heads up, and apologies for the inconvenience. To fix the build, I've reverted my patch (https://reviews.llvm.org/rG015dabd7672f936cdb5bdcad20fe80b17f05c9ca) and will gate it appropriately before I reland it.

thurston reopened this revision.Jul 11 2023, 10:53 AM
This revision is now accepted and ready to land.Jul 11 2023, 10:53 AM
thurston updated this revision to Diff 539195.Jul 11 2023, 10:53 AM

Add '#if SANITIZER_GLIBC' guards to avoid breaking FreeBSD

Herald added a project: Restricted Project. · View Herald TranscriptJul 11 2023, 10:53 AM
This revision was landed with ongoing or failed builds.Jul 11 2023, 1:13 PM
This revision was automatically updated to reflect the committed changes.