ramosian-glider / sanitizer-issues

test
0 stars 0 forks source link

CFAllocatorSetDefault is per-thread #81

Closed ramosian-glider closed 9 years ago

ramosian-glider commented 9 years ago

Originally reported on Google Code with ID 81

CFAllocatorSetDefault seems to work on a per-thread basis. E.g.:

$ cat p.mm
//===============================
#import <Foundation/Foundation.h>
#include <pthread.h>

void do_bad_stuff() {
  NSURL *base = [[NSURL alloc] initWithString:@"file://localhost/Users/glider/Library/"];
  NSURL *u = [[NSURL alloc] initWithString:@"Saved Application State" relativeToURL:base];
}

void *worker(void *_) {
#ifndef REPLACE_IN_MAIN_THREAD
  CFAllocatorSetDefault(kCFAllocatorMallocZone);
#endif
  do_bad_stuff();
  return NULL;
}

int main() {
#ifdef REPLACE_IN_MAIN_THREAD
  CFAllocatorSetDefault(kCFAllocatorMallocZone);
#endif
  pthread_t th;
  pthread_create(&th, NULL, worker, NULL);
  pthread_join(th, NULL);
  return 0;
}
//===============================
$ clang++ p.mm -o p -framework Foundation -DREPLACE_IN_MAIN_THREAD && ./p   # Everything
fine
$ clang++ p.mm -o p -framework Foundation -UREPLACE_IN_MAIN_THREAD && ./p
p(54794,0x107281000) malloc: *** error for object 0x7ff7c8e002f8: pointer being freed
was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6

(this is an example from issue 70, which may stop working under ASan at once)

We need to reset the default CF allocator upon each thread creation.

Reported by ramosian.glider on 2012-06-19 20:08:31

ramosian-glider commented 9 years ago
Moreover, this means we need to keep _all_ the CFAllocators created by ASan and check
against them in the case of every invalid free (see issue 70)

Reported by glider@chromium.org on 2012-06-21 16:40:37

ramosian-glider commented 9 years ago
For example, a CF object may be allocated on one thread and deallocated on another one.

Reported by glider@chromium.org on 2012-06-21 19:28:23

ramosian-glider commented 9 years ago
NB: this is not a bug in CFAllocator itself, it's just how it works.

Reported by ramosian.glider on 2012-06-27 07:41:58

ramosian-glider commented 9 years ago
Should be fixed in Clang r160630.

Reported by ramosian.glider on 2012-07-23 14:18:12

ramosian-glider commented 9 years ago
Adding Project:AddressSanitizer as part of GitHub migration.

Reported by ramosian.glider on 2015-07-30 09:12:59