0

I coded a simple asm binary file that get the arg of the user and then prints it on the screen using the C func printf.

So the code is compile using:

nasm -f elf64 ...
gcc -m64 -no-pie ....

The program works very well. I want to use it with python and doing so, i can launch it with subprocess.Popen(cmd) (this works well too)

Buuuut when i want to capture the output of my asm compiled program in order to parse it with python, the output is always empty ...

I tried:

subprocess.Popen(cmd, stdout=PIPE, stderr=PIPE)
subprocess.call(...)
subprocess.run(...)
subprocess.check_output(...)
os.system(...)
os.popen(...)

Anyway i think i tested all the ways ... I really don't understand why the program works well on my python script except if i try to capture the output.

If someone have an idea ...., please let me know.

The asm code is:

; nasm -f elf64 -o test.o test.asm
; gcc -m elf64 test.o -o test

extern printf

section .text
global main
main:
    mov rdi, fmt
    mov rsi, s_val
    xor rax, rax
    call printf WRT ..plt

    ; exit
    xor rdi, rdi,
    mov rax, 0x3c
    syscall


section .data
    s_val db 'hello world', 0
    fmt db '%s',0x0a, 0

The python code is:

#!/usr/bin/python3

import subprocess
from subprocess import Popen

p = Popen(['/home/test'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

r = p.communicate()

print(r)

thx

ginko
  • 47
  • 5
  • 1
    You should show your code. Make sure it's actually writing to `stdout` or `stderr`. Many people make a mistake of writing to `stdin` which happens to work for a terminal. – Jester Nov 18 '19 at 15:24
  • 1
    Use the [edit link under your post](https://stackoverflow.com/posts/58917796/edit) to add new information into your question. – Jester Nov 18 '19 at 18:31

1 Answers1

1

This is a classic: you use stdio functions from the libc and then terminate your program with a raw exit system call, never giving it a chance to flush the output on exit.

Fix this by exiting your program using the exit function instead of a raw exit system call:

call exit wrt ..plt

Alternatively, simply return from main to let the C runtime startup code exit the program for you.

fuz
  • 88,405
  • 25
  • 200
  • 352