Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CFAllocatorSetDefault is per-thread #81

Closed
ramosian-glider opened this issue Aug 31, 2015 · 5 comments
Closed

CFAllocatorSetDefault is per-thread #81

ramosian-glider opened this issue Aug 31, 2015 · 5 comments

Comments

@ramosian-glider
Copy link
Member

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
Copy link
Member Author

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
Copy link
Member Author

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
Copy link
Member Author

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
Copy link
Member Author

Should be fixed in Clang r160630.

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

  • Status changed: Fixed

@ramosian-glider
Copy link
Member Author

Adding Project:AddressSanitizer as part of GitHub migration.

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

  • Labels added: ProjectAddressSanitizer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant