Don't call libc malloc and free directly from assembly

Reviewed By: strager, javache

Differential Revision: D4484300

fbshipit-source-id: 97b9c2e9525f38c9158cfb499ba93d1af7d84b69
This commit is contained in:
Jakub Woyke 2017-03-16 12:24:11 -07:00 коммит произвёл Facebook Github Bot
Родитель bb266715f1
Коммит b8cc75c613
5 изменённых файлов: 22 добавлений и 25 удалений

Просмотреть файл

@ -153,6 +153,20 @@ static dispatch_group_t RCTProfileGetUnhookGroup(void)
return unhookGroup;
}
// Used by RCTProfileTrampoline assembly file to call libc`malloc
RCT_EXTERN void *RCTProfileMalloc(size_t size);
void *RCTProfileMalloc(size_t size)
{
return malloc(size);
}
// Used by RCTProfileTrampoline assembly file to call libc`free
RCT_EXTERN void RCTProfileFree(void *buf);
void RCTProfileFree(void *buf)
{
free(buf);
}
RCT_EXTERN IMP RCTProfileGetImplementation(id obj, SEL cmd);
IMP RCTProfileGetImplementation(id obj, SEL cmd)
{

Просмотреть файл

@ -35,12 +35,7 @@ SYMBOL_NAME(RCTProfileTrampoline):
* profile
*/
mov r0, #0xc
movw ip, :lower16:(L_malloc-(LPC1_0+4))
movt ip, :upper16:(L_malloc-(LPC1_0+4))
LPC1_0:
add ip, pc
ldr ip, [ip]
blx ip
bl SYMBOL_NAME(RCTProfileMalloc)
/**
* r4 is the callee saved register we'll use to refer to the allocated memory,
* store its initial value, so we can restore it later
@ -92,12 +87,7 @@ LPC1_0:
ldr r1, [r4, #0x8]
ldr r4, [r4]
push {r1} // save the caller on the stack
movw ip, :lower16:(L_free-(LPC1_1+4))
movt ip, :upper16:(L_free-(LPC1_1+4))
LPC1_1:
add ip, pc
ldr ip, [ip]
blx ip
bl SYMBOL_NAME(RCTProfileFree)
pop {lr} // pop the caller
pop {r0} // pop the return value
@ -105,11 +95,4 @@ LPC1_1:
trap
.data
.p2align 2
L_malloc:
.long SYMBOL_NAME(malloc)
L_free:
.long SYMBOL_NAME(free)
#endif

Просмотреть файл

@ -48,7 +48,7 @@ SYMBOL_NAME(RCTProfileTrampoline):
* the implementation and the caller address.
*/
mov x0, #0x10
bl SYMBOL_NAME(malloc)
bl SYMBOL_NAME(RCTProfileMalloc)
// store the initial value of r19, the callee saved register we'll use
str x19, [x0]
mov x19, x0
@ -111,7 +111,7 @@ SYMBOL_NAME(RCTProfileTrampoline):
ldr x10, [x19, #0x8] // load the caller address
ldr x19, [x19] // restore x19
str x10, [sp, #0x18] // store x10 on the stack space allocated above
bl SYMBOL_NAME(free)
bl SYMBOL_NAME(RCTProfileFree)
// Load both return values and link register from the stack
ldr q0, [sp, #0x0]

Просмотреть файл

@ -30,7 +30,7 @@ SYMBOL_NAME(RCTProfileTrampoline):
*/
subl $0x8, %esp // stack padding (16-byte alignment for function calls)
pushl $0xc // allocate 12-bytes
calll SYMBOL_NAME(malloc)
calll SYMBOL_NAME(RCTProfileMalloc)
addl $0xc, %esp // restore stack (8-byte padding + 4-byte argument)
/**
@ -85,7 +85,7 @@ SYMBOL_NAME(RCTProfileTrampoline):
* the stack has already been padded and the first and only argument, the
* memory address, is already in the bottom of the stack.
*/
calll SYMBOL_NAME(free)
calll SYMBOL_NAME(RCTProfileFree)
addl $0x8, %esp
/**

Просмотреть файл

@ -90,7 +90,7 @@ SYMBOL_NAME(RCTProfileTrampoline):
// allocate 16 bytes
movq $0x10, %rdi
callq SYMBOL_NAME_PIC(malloc)
callq SYMBOL_NAME_PIC(RCTProfileMalloc)
// store the initial value of calle saved registers %r13 and %r14
movq %r13, 0x0(%rax)
@ -169,7 +169,7 @@ SYMBOL_NAME(RCTProfileTrampoline):
andq $-0x10, %rsp
// Free the memory allocated to stash callee saved registers
callq SYMBOL_NAME_PIC(free)
callq SYMBOL_NAME_PIC(RCTProfileFree)
// unalign stack and restore %r12
movq %r12, %rsp