Cheng Shao pushed to branch wip/terrorjack/asan at Glasgow Haskell Compiler / GHC

Commits:

5 changed files:

Changes:

  • rts/Task.c
    ... ... @@ -183,6 +183,7 @@ freeTask (Task *task)
    183 183
             stgFree(incall);
    
    184 184
         }
    
    185 185
         for (incall = task->spare_incalls; incall != NULL; incall = next) {
    
    186
    +        __ghc_asan_unpoison_memory_region(incall, sizeof(InCall));
    
    186 187
             next = incall->next;
    
    187 188
             stgFree(incall);
    
    188 189
         }
    
    ... ... @@ -252,6 +253,7 @@ newInCall (Task *task)
    252 253
     
    
    253 254
         if (task->spare_incalls != NULL) {
    
    254 255
             incall = task->spare_incalls;
    
    256
    +        __ghc_asan_unpoison_memory_region(incall, sizeof(InCall));
    
    255 257
             task->spare_incalls = incall->next;
    
    256 258
             task->n_spare_incalls--;
    
    257 259
         } else {
    
    ... ... @@ -283,6 +285,7 @@ endInCall (Task *task)
    283 285
             stgFree(incall);
    
    284 286
         } else {
    
    285 287
             incall->next = task->spare_incalls;
    
    288
    +        __ghc_asan_poison_memory_region(incall, sizeof(InCall));
    
    286 289
             task->spare_incalls = incall;
    
    287 290
             task->n_spare_incalls++;
    
    288 291
         }
    

  • rts/include/Stg.h
    ... ... @@ -335,6 +335,7 @@ external prototype return neither of these types to workaround #11395.
    335 335
     #include "stg/MachRegsForHost.h"
    
    336 336
     #include "stg/Regs.h"
    
    337 337
     #include "stg/Ticky.h"
    
    338
    +#include "rts/ASANUtils.h"
    
    338 339
     #include "rts/TSANUtils.h"
    
    339 340
     
    
    340 341
     #if IN_STG_CODE
    

  • rts/include/rts/ASANUtils.h
    1
    +#pragma once
    
    2
    +
    
    3
    +#if defined(__SANITIZE_ADDRESS__)
    
    4
    +#define ASAN_ENABLED
    
    5
    +#elif defined(__has_feature)
    
    6
    +#if __has_feature(address_sanitizer)
    
    7
    +#define ASAN_ENABLED
    
    8
    +#endif
    
    9
    +#endif
    
    10
    +
    
    11
    +#if defined(ASAN_ENABLED)
    
    12
    +#include <sanitizer/asan_interface.h>
    
    13
    +#define USED_IF_ASAN
    
    14
    +#else
    
    15
    +#include <stdlib.h>
    
    16
    +#define USED_IF_ASAN __attribute__((unused))
    
    17
    +#endif
    
    18
    +
    
    19
    +static inline void
    
    20
    +__ghc_asan_poison_memory_region(void const volatile *addr USED_IF_ASAN,
    
    21
    +                                size_t size USED_IF_ASAN) {
    
    22
    +#if defined(ASAN_ENABLED)
    
    23
    +  __asan_poison_memory_region(addr, size);
    
    24
    +#endif
    
    25
    +}
    
    26
    +
    
    27
    +static inline void
    
    28
    +__ghc_asan_unpoison_memory_region(void const volatile *addr USED_IF_ASAN,
    
    29
    +                                  size_t size USED_IF_ASAN) {
    
    30
    +#if defined(ASAN_ENABLED)
    
    31
    +  __asan_unpoison_memory_region(addr, size);
    
    32
    +#endif
    
    33
    +}

  • rts/rts.cabal
    ... ... @@ -289,6 +289,7 @@ library
    289 289
                             -- ^ generated
    
    290 290
                             rts/ghc_ffi.h
    
    291 291
                             rts/Adjustor.h
    
    292
    +                        rts/ASANUtils.h
    
    292 293
                             rts/ExecPage.h
    
    293 294
                             rts/BlockSignals.h
    
    294 295
                             rts/Bytecodes.h
    

  • rts/sm/MBlock.c
    ... ... @@ -579,6 +579,8 @@ getMBlocks(uint32_t n)
    579 579
     
    
    580 580
         ret = getCommittedMBlocks(n);
    
    581 581
     
    
    582
    +    __ghc_asan_unpoison_memory_region(ret, (W_)n * MBLOCK_SIZE);
    
    583
    +
    
    582 584
         debugTrace(DEBUG_gc, "allocated %d megablock(s) at %p",n,ret);
    
    583 585
     
    
    584 586
         mblocks_allocated += n;
    
    ... ... @@ -611,6 +613,8 @@ freeMBlocks(void *addr, uint32_t n)
    611 613
     
    
    612 614
         mblocks_allocated -= n;
    
    613 615
     
    
    616
    +    __ghc_asan_poison_memory_region(addr, (W_)n * MBLOCK_SIZE);
    
    617
    +
    
    614 618
         decommitMBlocks(addr, n);
    
    615 619
     }
    
    616 620