 Chromium Code Reviews
 Chromium Code Reviews Issue 7379004:
  Add guard pages in front of platform allocations  (Closed) 
  Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
    
  
    Issue 7379004:
  Add guard pages in front of platform allocations  (Closed) 
  Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/| OLD | NEW | 
|---|---|
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. | 
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without | 
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are | 
| 4 // met: | 4 // met: | 
| 5 // | 5 // | 
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright | 
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. | 
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above | 
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following | 
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided | 
| (...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 400 | 400 | 
| 401 void MemoryAllocator::FreeRawMemory(void* mem, | 401 void MemoryAllocator::FreeRawMemory(void* mem, | 
| 402 size_t length, | 402 size_t length, | 
| 403 Executability executable) { | 403 Executability executable) { | 
| 404 #ifdef DEBUG | 404 #ifdef DEBUG | 
| 405 ZapBlock(reinterpret_cast<Address>(mem), length); | 405 ZapBlock(reinterpret_cast<Address>(mem), length); | 
| 406 #endif | 406 #endif | 
| 407 if (isolate_->code_range()->contains(static_cast<Address>(mem))) { | 407 if (isolate_->code_range()->contains(static_cast<Address>(mem))) { | 
| 408 isolate_->code_range()->FreeRawMemory(mem, length); | 408 isolate_->code_range()->FreeRawMemory(mem, length); | 
| 409 } else { | 409 } else { | 
| 410 OS::Free(mem, length); | 410 size_t guardsize = (executable == EXECUTABLE) ? Page::kPageSize : 0; | 
| 411 OS::Free(static_cast<char*>(mem) - guardsize, length + guardsize); | |
| 
Mads Ager (chromium)
2011/07/17 09:47:53
This looks nasty to me. Doesn't this mean that to
 
Cris Neckar
2011/07/18 23:55:12
Done.
 | |
| 411 } | 412 } | 
| 412 isolate_->counters()->memory_allocated()->Decrement(static_cast<int>(length)); | 413 isolate_->counters()->memory_allocated()->Decrement(static_cast<int>(length)); | 
| 413 size_ -= static_cast<int>(length); | 414 size_ -= static_cast<int>(length); | 
| 414 if (executable == EXECUTABLE) size_executable_ -= static_cast<int>(length); | 415 if (executable == EXECUTABLE) size_executable_ -= static_cast<int>(length); | 
| 415 | 416 | 
| 416 ASSERT(size_ >= 0); | 417 ASSERT(size_ >= 0); | 
| 417 ASSERT(size_executable_ >= 0); | 418 ASSERT(size_executable_ >= 0); | 
| 418 } | 419 } | 
| 419 | 420 | 
| 420 | 421 | 
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 496 Page* MemoryAllocator::AllocatePages(int requested_pages, | 497 Page* MemoryAllocator::AllocatePages(int requested_pages, | 
| 497 int* allocated_pages, | 498 int* allocated_pages, | 
| 498 PagedSpace* owner) { | 499 PagedSpace* owner) { | 
| 499 if (requested_pages <= 0) return Page::FromAddress(NULL); | 500 if (requested_pages <= 0) return Page::FromAddress(NULL); | 
| 500 size_t chunk_size = requested_pages * Page::kPageSize; | 501 size_t chunk_size = requested_pages * Page::kPageSize; | 
| 501 | 502 | 
| 502 void* chunk = AllocateRawMemory(chunk_size, &chunk_size, owner->executable()); | 503 void* chunk = AllocateRawMemory(chunk_size, &chunk_size, owner->executable()); | 
| 503 if (chunk == NULL) return Page::FromAddress(NULL); | 504 if (chunk == NULL) return Page::FromAddress(NULL); | 
| 504 LOG(isolate_, NewEvent("PagedChunk", chunk, chunk_size)); | 505 LOG(isolate_, NewEvent("PagedChunk", chunk, chunk_size)); | 
| 505 | 506 | 
| 507 if (owner->executable() == EXECUTABLE) { | |
| 508 OS::Guard(chunk, Page::kPageSize); | |
| 509 chunk_size -= Page::kPageSize; | |
| 510 chunk = static_cast<Address>(chunk) + Page::kPageSize; | |
| 511 } | |
| 506 *allocated_pages = PagesInChunk(static_cast<Address>(chunk), chunk_size); | 512 *allocated_pages = PagesInChunk(static_cast<Address>(chunk), chunk_size); | 
| 507 // We may 'lose' a page due to alignment. | 513 // We may 'lose' a page due to alignment. | 
| 508 ASSERT(*allocated_pages >= kPagesPerChunk - 1); | 514 ASSERT(*allocated_pages >= kPagesPerChunk - 1); | 
| 509 if (*allocated_pages == 0) { | 515 if (*allocated_pages == 0) { | 
| 510 FreeRawMemory(chunk, chunk_size, owner->executable()); | 516 FreeRawMemory(chunk, chunk_size, owner->executable()); | 
| 511 LOG(isolate_, DeleteEvent("PagedChunk", chunk)); | 517 LOG(isolate_, DeleteEvent("PagedChunk", chunk)); | 
| 512 return Page::FromAddress(NULL); | 518 return Page::FromAddress(NULL); | 
| 513 } | 519 } | 
| 514 | 520 | 
| 515 int chunk_id = Pop(); | 521 int chunk_id = Pop(); | 
| (...skipping 2149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2665 } | 2671 } | 
| 2666 | 2672 | 
| 2667 | 2673 | 
| 2668 // ----------------------------------------------------------------------------- | 2674 // ----------------------------------------------------------------------------- | 
| 2669 // LargeObjectChunk | 2675 // LargeObjectChunk | 
| 2670 | 2676 | 
| 2671 LargeObjectChunk* LargeObjectChunk::New(int size_in_bytes, | 2677 LargeObjectChunk* LargeObjectChunk::New(int size_in_bytes, | 
| 2672 Executability executable) { | 2678 Executability executable) { | 
| 2673 size_t requested = ChunkSizeFor(size_in_bytes); | 2679 size_t requested = ChunkSizeFor(size_in_bytes); | 
| 2674 size_t size; | 2680 size_t size; | 
| 2681 size_t guardsize = (executable == EXECUTABLE) ? Page::kPageSize : 0; | |
| 2675 Isolate* isolate = Isolate::Current(); | 2682 Isolate* isolate = Isolate::Current(); | 
| 2676 void* mem = isolate->memory_allocator()->AllocateRawMemory( | 2683 void* mem = isolate->memory_allocator()->AllocateRawMemory( | 
| 2677 requested, &size, executable); | 2684 requested + guardsize, &size, executable); | 
| 2678 if (mem == NULL) return NULL; | 2685 if (mem == NULL) return NULL; | 
| 2679 | 2686 | 
| 2680 // The start of the chunk may be overlayed with a page so we have to | 2687 // The start of the chunk may be overlayed with a page so we have to | 
| 2681 // make sure that the page flags fit in the size field. | 2688 // make sure that the page flags fit in the size field. | 
| 2682 ASSERT((size & Page::kPageFlagMask) == 0); | 2689 ASSERT((size & Page::kPageFlagMask) == 0); | 
| 2683 | 2690 | 
| 2684 LOG(isolate, NewEvent("LargeObjectChunk", mem, size)); | 2691 LOG(isolate, NewEvent("LargeObjectChunk", mem, size)); | 
| 2685 if (size < requested) { | 2692 if (size < requested) { | 
| 
Mads Ager (chromium)
2011/07/17 09:47:53
Shouldn't you add guardsize to requested before yo
 
Cris Neckar
2011/07/18 23:55:12
Done.
 | |
| 2686 isolate->memory_allocator()->FreeRawMemory( | 2693 isolate->memory_allocator()->FreeRawMemory( | 
| 2687 mem, size, executable); | 2694 static_cast<Address>(mem) - guardsize, size + guardsize, executable); | 
| 
Mads Ager (chromium)
2011/07/17 09:47:53
I'm getting confused here. Does this add up? FreeR
 
Cris Neckar
2011/07/18 23:55:12
Yeah this was a mistake, I had initially planned t
 | |
| 2688 LOG(isolate, DeleteEvent("LargeObjectChunk", mem)); | 2695 LOG(isolate, DeleteEvent("LargeObjectChunk", mem)); | 
| 2689 return NULL; | 2696 return NULL; | 
| 2690 } | 2697 } | 
| 2691 | 2698 | 
| 2699 if (guardsize != 0) { | |
| 2700 OS::Guard(mem, guardsize); | |
| 2701 size -= guardsize; | |
| 2702 mem = static_cast<Address>(mem) + guardsize; | |
| 2703 } | |
| 2704 | |
| 2692 ObjectSpace space = (executable == EXECUTABLE) | 2705 ObjectSpace space = (executable == EXECUTABLE) | 
| 2693 ? kObjectSpaceCodeSpace | 2706 ? kObjectSpaceCodeSpace | 
| 2694 : kObjectSpaceLoSpace; | 2707 : kObjectSpaceLoSpace; | 
| 2695 isolate->memory_allocator()->PerformAllocationCallback( | 2708 isolate->memory_allocator()->PerformAllocationCallback( | 
| 2696 space, kAllocationActionAllocate, size); | 2709 space, kAllocationActionAllocate, size); | 
| 2697 | 2710 | 
| 2698 LargeObjectChunk* chunk = reinterpret_cast<LargeObjectChunk*>(mem); | 2711 LargeObjectChunk* chunk = reinterpret_cast<LargeObjectChunk*>(mem); | 
| 2699 chunk->size_ = size; | 2712 chunk->size_ = size; | 
| 2700 Page* page = Page::FromAddress(RoundUp(chunk->address(), Page::kPageSize)); | 2713 Page* page = Page::FromAddress(RoundUp(chunk->address(), Page::kPageSize)); | 
| 2701 page->heap_ = isolate->heap(); | 2714 page->heap_ = isolate->heap(); | 
| (...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3057 for (HeapObject* obj = obj_it.next(); obj != NULL; obj = obj_it.next()) { | 3070 for (HeapObject* obj = obj_it.next(); obj != NULL; obj = obj_it.next()) { | 
| 3058 if (obj->IsCode()) { | 3071 if (obj->IsCode()) { | 
| 3059 Code* code = Code::cast(obj); | 3072 Code* code = Code::cast(obj); | 
| 3060 isolate->code_kind_statistics()[code->kind()] += code->Size(); | 3073 isolate->code_kind_statistics()[code->kind()] += code->Size(); | 
| 3061 } | 3074 } | 
| 3062 } | 3075 } | 
| 3063 } | 3076 } | 
| 3064 #endif // DEBUG | 3077 #endif // DEBUG | 
| 3065 | 3078 | 
| 3066 } } // namespace v8::internal | 3079 } } // namespace v8::internal | 
| OLD | NEW |