NASM malloc返回NULL

时间:2015-04-09 03:19:41

标签: malloc nasm heap-memory dynamic-memory-allocation

在尝试学习NASM时,我尝试创建一个基本程序来测试malloc功能。我把它贴在下面:

bits 64

extern malloc

section .data

ARRAY_SIZE:             equ 27

array:                  dq 1

error_message:          db "Malloc returned NULL!", 10
error_message_length:   equ $ - error_message

section .text
global _start

_start:
    ; try to allocate an array of bytes in the heap memory using malloc
    push dword ARRAY_SIZE
    call malloc
    ;; if malloc returned NULL, throw an error
    cmp rax, 0
    je err_out
    mov [array], eax                ; save a pointer to the allocated memory in array

    ; fill the array with numbers equal to the indices at that location
    fill_for:
    xor edx, edx                    ; use edx for the for loop index
    .for_start:
    cmp edx, ARRAY_SIZE             ; break if edx >= BUFFER_SIZE
    jg .for_end
        mov [array + edx], edx      ; array[i] = i;
    inc edx
    .for_end:

    ; print the numbers in array
    print_for:
    xor edx, edx                    ; use edx for the for loop index
    .for_start:
    cmp edx, ARRAY_SIZE             ; break if edx >= BUFFER_SIZE
    jg .for_end
        mov eax, [array + edx]      ; print array[i]
        call print_small_number
    .for_end:

print_small_number:
; allocate stack space for the two-digit number string (two digit chars + '\0' = 3b)
;; save the old rbp
mov [rbp], rsp
sub rsp, 8
mov rbp, rsp
;; allocate space for the number string
sub rsp, 3
;; save rdx for the main function
sub rsp, 8
mov [rbp + 3], rdx

    ; turn the (assumed two-digit in base 10) number into a string in number_string
    ;; initialize the number to 0x30, 0x30, 0 (0x30 is the offset to turn a number into a base-10 digit; 10 = '\n')
    mov byte [rbp], 0x30
    mov byte [rbp + 1], 0x30
    mov byte [rbp + 2], 10
                                    ; the number to print is already in eax
    mov bl, 10                      ; divide the message length string by 10 to separate the 10s and 1s digits
    div bl                          ;; the quotient is the 10s digits and the remainder is the 1s
    add [rbp], al                   ; move the resulting char into the first slot in number_string
    add [rbp + 1], ah               ; move the resulting 1s digit char into the second slot in number_string

    ; print the message length string
    mov eax, 4
    mov ebx, 1
    mov rcx, rbp                    ; the string starts at rbp
    mov edx, 3                      ; two digits + \0 = 3 chars = 3b
    int 0x80

; return
;; restore the old rdx for the main function
mov rdx, [rbp + 3]
add rsp, 8
;; deallocate the number string
add rsp, 3
;; restore the old rbp
add rsp, 8
mov rbp, [rsp]
ret

err_out:
    ; print error_message
    mov eax, 4
    mov ebx, 1
    mov ecx, error_message
    mov edx, error_message_length
    int 0x80

    ; end the program
    mov ebx, 0
    mov eax, 1
    int 0x80

然而,当我从这个基本的NASM程序中调用malloc时,它返回NULL(0),通过打印出以下消息显示:

Malloc returned NULL!

我在6GB内存的笔记本电脑上运行它,其他几个应用程序打开,我只是尝试分配27个字节,因此内存耗尽似乎不太可能。

我知道如果给出的size参数是负数,它可能会返回NULL,但是你可以在代码中看到这种情况。

有没有人有任何建议?我不明白为什么这个malloc调用可以返回NULL。

修改

自BASH脚本以来,使用以下命令在64位Linux Mint 17 Qiana中组装并运行此示例:

nasm -f elf64 "$1.asm" -l "$1.lst" &&
ld -s -o "$1" "$1.o" -lc &&
./"$1"

1 个答案:

答案 0 :(得分:2)

malloc()的参数应该进入rdi,而不是被压入堆栈。它可能会失败,因为你传递了它0,或者一些非常高的数字。

此外:

mov [array], eax

应该是:

mov array, rax

因为malloc()能够返回一个不符合四个字节的地址,并且由于array在那时不包含有效的内存地址,所以你不应该是间接的。