[compiler-rt] aeb7580 - Make mmap-munmap interceptor fail earlier (#171295)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Feb 19 21:05:44 PST 2026
Author: Honey Goyal
Date: 2026-02-20T10:35:39+05:30
New Revision: aeb7580d3648efadd9771e9d3b8734b64d78b317
URL: https://github.com/llvm/llvm-project/commit/aeb7580d3648efadd9771e9d3b8734b64d78b317
DIFF: https://github.com/llvm/llvm-project/commit/aeb7580d3648efadd9771e9d3b8734b64d78b317.diff
LOG: Make mmap-munmap interceptor fail earlier (#171295)
If the address range is not covered by shadow memory, make interceptors
like mmap fail earlier.
---------
Signed-off-by: Abhishek Varma <abhvarma at amd.com>
Signed-off-by: Minsoo Choo <minsoochoo0122 at proton.me>
Signed-off-by: hanhanW <hanhan0912 at gmail.com>
Signed-off-by: Nikita B <n2h9z4 at gmail.com>
Signed-off-by: Nick Sarnie <nick.sarnie at intel.com>
Signed-off-by: Ian Wood <ianwood at u.northwestern.edu>
Co-authored-by: Min-Yih Hsu <min.hsu at sifive.com>
Co-authored-by: Sersawy <65075626+Abdelrhmansersawy at users.noreply.github.com>
Co-authored-by: Krzysztof Parzyszek <Krzysztof.Parzyszek at amd.com>
Co-authored-by: Walter Lee <49250218+googlewalt at users.noreply.github.com>
Co-authored-by: cpist (He / Him) <tinyfrog12 at gmail.com>
Co-authored-by: Jonathan Cohen <joncoh at apple.com>
Co-authored-by: Charitha Saumya <136391709+charithaintc at users.noreply.github.com>
Co-authored-by: Mircea Trofin <mtrofin at google.com>
Co-authored-by: Mingming Liu <mingmingl at google.com>
Co-authored-by: Atharv Mane <atharvamane19 at gmail.com>
Co-authored-by: Sam Clegg <sbc at chromium.org>
Co-authored-by: vporpo <vasileios.porpodas at amd.com>
Co-authored-by: Usama Hameed <u_hameed at apple.com>
Co-authored-by: PiJoules <6019989+PiJoules at users.noreply.github.com>
Co-authored-by: Muhammad Bassiouni <60100307+bassiounix at users.noreply.github.com>
Co-authored-by: Roland McGrath <mcgrathr at google.com>
Co-authored-by: Jakub Kuderski <jakub at nod-labs.com>
Co-authored-by: LLVM GN Syncbot <llvmgnsyncbot at gmail.com>
Co-authored-by: Krzysztof Drewniak <Krzysztof.Drewniak at amd.com>
Co-authored-by: Aiden Grossman <aidengrossman at google.com>
Co-authored-by: Kelvin Li <kli at ca.ibm.com>
Co-authored-by: Andrei Elovikov <a.elovikov at gmail.com>
Co-authored-by: Craig Topper <craig.topper at sifive.com>
Co-authored-by: NAKAMURA Takumi <geek4civic at gmail.com>
Co-authored-by: Marco Elver <elver at google.com>
Co-authored-by: Greg Clayton <gclayton at fb.com>
Co-authored-by: Charles Zablit <c_zablit at apple.com>
Co-authored-by: Zachary Yedidia <zyedidia at gmail.com>
Co-authored-by: Michael Spencer <bigcheesegs at gmail.com>
Co-authored-by: Florian Hahn <flo at fhahn.com>
Co-authored-by: Wenju He <wenju.he at intel.com>
Co-authored-by: Andy Kaylor <akaylor at nvidia.com>
Co-authored-by: Luohao Wang <luohaothu at live.com>
Co-authored-by: John Harrison <harjohn at google.com>
Co-authored-by: Ebuka Ezike <yerimyah1 at gmail.com>
Co-authored-by: Jonas Devlieghere <jonas at devlieghere.com>
Co-authored-by: Alexey Bataev <a.bataev at outlook.com>
Co-authored-by: Rahul Joshi <rjoshi at nvidia.com>
Co-authored-by: Jessica Clarke <jrtc27 at jrtc27.com>
Co-authored-by: puneeth_aditya_5656 <myakampuneeth at gmail.com>
Co-authored-by: Claude Sonnet 4.5 <noreply at anthropic.com>
Co-authored-by: Joseph Huber <huberjn at outlook.com>
Co-authored-by: Saina Daneshmand <66389676+SainaDaneshmandjahromi at users.noreply.github.com>
Co-authored-by: Jameson Nash <vtjnash at gmail.com>
Co-authored-by: Fangrui Song <i at maskray.me>
Co-authored-by: Tom Stellard <tstellar at redhat.com>
Co-authored-by: Jie Fu <jiefu at tencent.com>
Co-authored-by: Timm Baeder <tbaeder at redhat.com>
Co-authored-by: Pranav Kant <prka at google.com>
Co-authored-by: woruyu <1214539920 at qq.com>
Co-authored-by: Pradeep Kumar <pradeepku at nvidia.com>
Co-authored-by: Petr Hosek <phosek at google.com>
Co-authored-by: bala-bhargav <penugondabalabharghav at gmail.com>
Co-authored-by: Jhalak Patel <jhalakp at nvidia.com>
Co-authored-by: Utkarsh Saxena <usx at google.com>
Co-authored-by: Carl Ritson <carl.ritson at amd.com>
Co-authored-by: Nikolas Klauser <nikolasklauser at berlin.de>
Co-authored-by: Jay Foad <jay.foad at amd.com>
Co-authored-by: Sudharsan Veeravalli <svs at qti.qualcomm.com>
Co-authored-by: David Spickett <david.spickett at arm.com>
Co-authored-by: Ricardo Jesus <rjj at nvidia.com>
Co-authored-by: David Sherwood <david.sherwood at arm.com>
Co-authored-by: Oleksandr Tarasiuk <oleksandr.tarasiuk at outlook.com>
Co-authored-by: Yingying Wang <3171290993 at qq.com>
Co-authored-by: Sergio Afonso <safonsof at amd.com>
Co-authored-by: Miloš Poletanović <poletanovicmilos97 at gmail.com>
Co-authored-by: Milos Poletanovic <mpoletanovic at syrmia.com>
Co-authored-by: Abhishek Varma <avarma094 at gmail.com>
Co-authored-by: Henry Linjamäki <henry.linjamaki at tuni.fi>
Co-authored-by: Henry Linjamäki <henry.mikael.linjamaki at intel.com>
Co-authored-by: Leandro Lupori <leandro.lupori at linaro.org>
Co-authored-by: Kerry McLaughlin <kerry.mclaughlin at arm.com>
Co-authored-by: Nikita Popov <npopov at redhat.com>
Co-authored-by: Michael Kruse <llvm-project at meinersbur.de>
Co-authored-by: Konrad Kleine <kkleine at redhat.com>
Co-authored-by: EugeneZelenko <eugene.zelenko at gmail.com>
Co-authored-by: Frederik Harwath <frederik.harwath at amd.com>
Co-authored-by: Fateme Hosseini <quic_fhossein at quicinc.com>
Co-authored-by: Simon Pilgrim <llvm-dev at redking.me.uk>
Co-authored-by: Michael Buch <michaelbuch12 at gmail.com>
Co-authored-by: Minsoo Choo <minsoochoo0122 at proton.me>
Co-authored-by: Ryan Cowan <ryan.cowan at arm.com>
Co-authored-by: Matt Arsenault <arsenm2 at gmail.com>
Co-authored-by: Akash Banerjee <akash.banerjee at amd.com>
Co-authored-by: Dark Steve <Prasoon.Mishra at amd.com>
Co-authored-by: Aviral Goel <aviralg at users.noreply.github.com>
Co-authored-by: Osman Yasar <37121924+osmanyasar05 at users.noreply.github.com>
Co-authored-by: Igor Wodiany <igor.wodiany at imgtec.com>
Co-authored-by: Florian Mayer <fmayer at google.com>
Co-authored-by: AidinT <at.aidin at gmail.com>
Co-authored-by: Justin Fargnoli <jfargnoli at nvidia.com>
Co-authored-by: Christopher Di Bella <cjdb at google.com>
Co-authored-by: Han-Chung Wang <hanhan0912 at gmail.com>
Co-authored-by: Wei Xiao <wei3.xiao at intel.com>
Co-authored-by: vangthao95 <vang.thao at amd.com>
Co-authored-by: joaosaffran <joaosaffranllvm at gmail.com>
Co-authored-by: Razvan Lupusoru <razvan.lupusoru at gmail.com>
Co-authored-by: Aman LaChapelle <aman.lachapelle at gmail.com>
Co-authored-by: Jacob Lalonde <jalalonde at fb.com>
Co-authored-by: n2h9 <13541181+n2h9 at users.noreply.github.com>
Co-authored-by: Joao Saffran <jderezende at microsoft.com>
Co-authored-by: Joshua Batista <jbatista at microsoft.com>
Co-authored-by: Balázs Benics <benicsbalazs at gmail.com>
Co-authored-by: Priyanshu Kumar <10b.priyanshu at gmail.com>
Co-authored-by: Osman Yasar <osmanyas05 at gmail.com>
Co-authored-by: Sarah Kuhn <sarahlinhkuhn at gmail.com>
Co-authored-by: Luisa Cicolini <48860705+luisacicolini at users.noreply.github.com>
Co-authored-by: Osama Abdelkader <osama.abdelkader at gmail.com>
Co-authored-by: Andrew Haberlandt <ahaberlandt at apple.com>
Co-authored-by: Rohan Jacob-Rao <rohanjr at google.com>
Co-authored-by: Eugene Epshteyn <eepshteyn at nvidia.com>
Co-authored-by: Ian Anderson <iana at apple.com>
Co-authored-by: adams381 <adams at nvidia.com>
Co-authored-by: Nick Sarnie <nick.sarnie at intel.com>
Co-authored-by: Sadaf Ebrahimi <sadafebrahimi at google.com>
Co-authored-by: RattataKing <46631728+RattataKing at users.noreply.github.com>
Co-authored-by: Chibuoyim (Wilson) Ogbonna <ogbonnachibuoyim12 at gmail.com>
Co-authored-by: Ian Wood <ianwood2024 at u.northwestern.edu>
Co-authored-by: Andres-Salamanca <andrealebarbaritos at gmail.com>
Co-authored-by: Jason Molenda <jmolenda at apple.com>
Co-authored-by: A. Jiang <de34 at live.cn>
Co-authored-by: Maksim Panchenko <maks at fb.com>
Co-authored-by: Peter Collingbourne <peter at pcc.me.uk>
Co-authored-by: Prabhu Rajasekaran <prabhukr at google.com>
Co-authored-by: Dmitry Sidorov <18708689+MrSidims at users.noreply.github.com>
Co-authored-by: Lang Hames <lhames at gmail.com>
Co-authored-by: Daniil Fukalov <dfukalov at gmail.com>
Co-authored-by: paperchalice <liujunchang97 at outlook.com>
Co-authored-by: Viktoria Maximova <viktoria.maksimova at intel.com>
Co-authored-by: wanglei <wanglei at loongson.cn>
Co-authored-by: hev <wangrui at loongson.cn>
Co-authored-by: Zhihui Yang <youngwisdm at gmail.com>
Co-authored-by: Ash <97464181+tnuha at users.noreply.github.com>
Co-authored-by: YunQiang Su <syq at debian.org>
Co-authored-by: Victor Chernyakin <chernyakin.victor.j at outlook.com>
Co-authored-by: Thurston Dang <thurston at google.com>
Co-authored-by: Kewen Meng <Kewen.Meng at amd.com>
Co-authored-by: sohail <150878950+sohail103 at users.noreply.github.com>
Co-authored-by: Louis Dionne <ldionne.2 at gmail.com>
Co-authored-by: azwolski <antoni.zwolski at intel.com>
Co-authored-by: Peter Hawkins <phawkins at google.com>
Co-authored-by: David Green <david.green at arm.com>
Co-authored-by: Sander de Smalen <sander.desmalen at arm.com>
Co-authored-by: Riyaz Ahmad <69200125+riyaz86a at users.noreply.github.com>
Co-authored-by: Riyaz Ahmad <riyaz.ahmad at ibm.com>
Co-authored-by: Honey Goyal <honey.goyal3 at ibm.com>
Added:
compiler-rt/test/asan/TestCases/mmap_shadow_overlap.cpp
compiler-rt/test/asan/TestCases/munmap_shadow_partial.cpp
Modified:
compiler-rt/lib/asan/asan_interceptors.cpp
Removed:
################################################################################
diff --git a/compiler-rt/lib/asan/asan_interceptors.cpp b/compiler-rt/lib/asan/asan_interceptors.cpp
index 02f0ffde72042..6d024e58b27cf 100644
--- a/compiler-rt/lib/asan/asan_interceptors.cpp
+++ b/compiler-rt/lib/asan/asan_interceptors.cpp
@@ -87,6 +87,28 @@ int OnExit() {
return 0;
}
+# if SANITIZER_POSIX
+static inline bool RangeOverlaps(uptr beg, uptr end_excl, uptr seg_beg,
+ uptr seg_end_incl) {
+ if (!seg_beg && !seg_end_incl)
+ return false;
+ uptr seg_end_excl = seg_end_incl + 1;
+ return beg < seg_end_excl && end_excl > seg_beg;
+}
+
+static inline bool IntersectsShadow(uptr beg, uptr end_excl) {
+ // Check shadow regions
+ if (RangeOverlaps(beg, end_excl, kLowShadowBeg, kLowShadowEnd))
+ return true;
+ if (kMidShadowBeg &&
+ RangeOverlaps(beg, end_excl, kMidShadowBeg, kMidShadowEnd))
+ return true;
+ if (RangeOverlaps(beg, end_excl, kHighShadowBeg, kHighShadowEnd))
+ return true;
+ return false;
+}
+# endif // SANITIZER_POSIX
+
} // namespace __asan
// ---------------------- Wrappers ---------------- {{{1
@@ -159,6 +181,25 @@ DECLARE_REAL_AND_INTERCEPTOR(void, free, void*)
template <class Mmap>
static void* mmap_interceptor(Mmap real_mmap, void* addr, SIZE_T length,
int prot, int flags, int fd, OFF64_T offset) {
+# if SANITIZER_POSIX
+ if (length == 0)
+ return real_mmap(addr, length, prot, flags, fd, offset);
+ const uptr start = reinterpret_cast<uptr>(addr);
+ uptr end_excl;
+ if (UNLIKELY(__builtin_add_overflow(start, static_cast<uptr>(length),
+ &end_excl))) {
+ errno = errno_EINVAL;
+ return (void*)-1;
+ }
+ if (flags & map_fixed) {
+ // TODO: shadow gap may need to be checked
+ if (__asan::IntersectsShadow(start, end_excl)) {
+ errno = errno_EINVAL;
+ return (void*)-1;
+ }
+ }
+# endif // SANITIZER_POSIX
+
void* res = real_mmap(addr, length, prot, flags, fd, offset);
if (length && res != (void*)-1) {
const uptr beg = reinterpret_cast<uptr>(res);
@@ -173,14 +214,32 @@ static void* mmap_interceptor(Mmap real_mmap, void* addr, SIZE_T length,
template <class Munmap>
static int munmap_interceptor(Munmap real_munmap, void* addr, SIZE_T length) {
+ const uptr start = reinterpret_cast<uptr>(addr);
+
+# if SANITIZER_POSIX
+ if (length == 0)
+ return real_munmap(addr, length);
+
+ uptr end_excl;
+ if (UNLIKELY(__builtin_add_overflow(start, static_cast<uptr>(length),
+ &end_excl))) {
+ errno = errno_EINVAL;
+ return -1;
+ }
+ // TODO: shadow gap may need to be checked
+ if (__asan::IntersectsShadow(start, end_excl)) {
+ errno = errno_EINVAL;
+ return -1;
+ }
+# endif // SANITIZER_POSIX
+
// We should not tag if munmap fail, but it's to late to tag after
// real_munmap, as the pages could be mmaped by another thread.
- const uptr beg = reinterpret_cast<uptr>(addr);
- if (length && IsAligned(beg, GetPageSize())) {
+ if (length && IsAligned(start, GetPageSize())) {
SIZE_T rounded_length = RoundUpTo(length, GetPageSize());
// Protect from unmapping the shadow.
- if (AddrIsInMem(beg) && AddrIsInMem(beg + rounded_length - 1))
- PoisonShadow(beg, rounded_length, 0);
+ if (AddrIsInMem(start) && AddrIsInMem(start + rounded_length - 1))
+ PoisonShadow(start, rounded_length, 0);
}
return real_munmap(addr, length);
}
@@ -189,13 +248,14 @@ static int munmap_interceptor(Munmap real_munmap, void* addr, SIZE_T length) {
fd, offset) \
do { \
(void)(ctx); \
- return mmap_interceptor(REAL(mmap), addr, sz, prot, flags, fd, off); \
+ return mmap_interceptor(REAL(mmap), addr, length, prot, flags, fd, \
+ offset); \
} while (false)
-# define COMMON_INTERCEPTOR_MUNMAP_IMPL(ctx, addr, length) \
- do { \
- (void)(ctx); \
- return munmap_interceptor(REAL(munmap), addr, sz); \
+# define COMMON_INTERCEPTOR_MUNMAP_IMPL(ctx, addr, length) \
+ do { \
+ (void)(ctx); \
+ return munmap_interceptor(REAL(munmap), addr, length); \
} while (false)
# if CAN_SANITIZE_LEAKS
diff --git a/compiler-rt/test/asan/TestCases/mmap_shadow_overlap.cpp b/compiler-rt/test/asan/TestCases/mmap_shadow_overlap.cpp
new file mode 100644
index 0000000000000..d6f20e643496e
--- /dev/null
+++ b/compiler-rt/test/asan/TestCases/mmap_shadow_overlap.cpp
@@ -0,0 +1,62 @@
+// Test that mmap with MAP_FIXED fails when attempting to overlap shadow memory.
+//
+// RUN: %clangxx_asan %s -o %t
+// RUN: %run %t 2>&1 | FileCheck %s
+//
+// REQUIRES: !windows
+
+#include <errno.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <sys/mman.h>
+
+extern "C" {
+void __asan_get_shadow_mapping(unsigned long *shadow_scale,
+ unsigned long *shadow_offset);
+}
+
+int main() {
+ unsigned long scale = 0;
+ unsigned long offset = 0;
+ __asan_get_shadow_mapping(&scale, &offset);
+
+ if (offset == 0) {
+ fprintf(stderr, "SKIPPED\n");
+ return 0;
+ }
+
+ const size_t map_size = 4096;
+ void *shadow_addr = (void *)offset;
+
+ // MAP_FIXED on shadow memory should fail
+ void *ptr = mmap(shadow_addr, map_size, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
+ if (ptr != MAP_FAILED || errno != EINVAL) {
+ fprintf(stderr, "FAIL\n");
+ return 1;
+ }
+
+ // MAP_FIXED in middle of shadow should fail
+ void *mid_shadow = (void *)(offset + 0x100000);
+ errno = 0;
+ ptr = mmap(mid_shadow, map_size, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
+ if (ptr != MAP_FAILED || errno != EINVAL) {
+ fprintf(stderr, "FAIL\n");
+ return 1;
+ }
+
+ // Normal mmap should succeed
+ errno = 0;
+ ptr = mmap(NULL, map_size, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ if (ptr == MAP_FAILED) {
+ fprintf(stderr, "FAIL\n");
+ return 1;
+ }
+ munmap(ptr, map_size);
+
+ fprintf(stderr, "PASS\n");
+ return 0;
+}
+// CHECK: {{^PASS$|^SKIPPED$}}
\ No newline at end of file
diff --git a/compiler-rt/test/asan/TestCases/munmap_shadow_partial.cpp b/compiler-rt/test/asan/TestCases/munmap_shadow_partial.cpp
new file mode 100644
index 0000000000000..b75462f2586c0
--- /dev/null
+++ b/compiler-rt/test/asan/TestCases/munmap_shadow_partial.cpp
@@ -0,0 +1,33 @@
+// Test that munmap correctly handles application memory regions.
+//
+// RUN: %clangxx_asan %s -o %t
+// RUN: %run %t 2>&1
+//
+// REQUIRES: !windows
+
+#include <errno.h>
+#include <stdio.h>
+#include <sys/mman.h>
+
+int main() {
+ const size_t map_size = 8192;
+
+ // Map application memory
+ void *ptr = mmap(NULL, map_size, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ if (ptr == MAP_FAILED) {
+ fprintf(stderr, "ERROR: mmap failed\n");
+ return 1;
+ }
+
+ // Normal munmap should succeed
+ int res = munmap(ptr, map_size);
+ if (res != 0) {
+ fprintf(stderr, "ERROR: munmap failed with errno=%d\n", errno);
+ return 1;
+ }
+
+ fprintf(stderr, "PASS\n");
+ // CHECK: PASS
+ return 0;
+}
\ No newline at end of file
More information about the llvm-commits
mailing list