Skip to contentSkip to conversation

linux-user cannot allocate stack memory on riscv64 host due to non-zero guest_base

DISCUSSION Andreas Schwab 4 participants 18 messages Jun 25, 2024 – Jul 2, 2024

lore
Share & tools
lore
#0 lore raw

When running qemu-riscv64 on a riscv64 host executing a ET_EXEC riscv64 binary it cannot allocate memory for the stack:

$ qemu-riscv64 -d page ./hello.riscv64
host mmap_min_addr=0x10000
Locating guest address space @ 0x3ee000
page layout changed following mmap
start            end              size             prot
0000000000010000-0000000000013000 0000000000003000 ---
page layout changed following mmap
start            end              size             prot
0000000000010000-0000000000011000 0000000000001000 r-x
0000000000011000-0000000000013000 0000000000002000 ---
page layout changed following mmap
start            end              size             prot
0000000000010000-0000000000011000 0000000000001000 r-x
0000000000011000-0000000000013000 0000000000002000 rw-
mmap stack: Cannot allocate memory

The issue is that guest_base is non-zero, which turns the target_mmap call with zero base in setup_arg_pages into a host mmap call with non-zero base. On other hosts like x86_64 or aarch64, guest_base remains zero and the issue does not occur.

Signature · 3 lines
Andreas Schwab, SUSE Labs, [email protected]
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."
#1 lore raw
Quoted 19 lines · Andreas Schwab

When running qemu-riscv64 on a riscv64 host executing a ET_EXEC riscv64 binary it cannot allocate memory for the stack:

$ qemu-riscv64 -d page ./hello.riscv64
host mmap_min_addr=0x10000
Locating guest address space @ 0x3ee000
page layout changed following mmap
start            end              size             prot
0000000000010000-0000000000013000 0000000000003000 ---
page layout changed following mmap
start            end              size             prot
0000000000010000-0000000000011000 0000000000001000 r-x
0000000000011000-0000000000013000 0000000000002000 ---
page layout changed following mmap
start            end              size             prot
0000000000010000-0000000000011000 0000000000001000 r-x
0000000000011000-0000000000013000 0000000000002000 rw-
mmap stack: Cannot allocate memory
Quoted 4 lines

The issue is that guest_base is non-zero, which turns the target_mmap call with zero base in setup_arg_pages into a host mmap call with non-zero base. On other hosts like x86_64 or aarch64, guest_base remains zero and the issue does not occur.

You need to be more precise in your bug reports, because it works for me. Everything non-PIE, statically linked:

./qemu-riscv64: ELF 64-bit LSB executable, UCB RISC-V, RVC, double-float ABI, version 1 (SYSV), statically linked, BuildID[sha1]=92e2b4b9a2cbcc91ac029a49ec72eaefe5111f38, for GNU/Linux 4.15.0, with debug_info, not stripped

/home/rth/a.out: ELF 64-bit LSB executable, UCB RISC-V, RVC, double-float ABI, version 1 (SYSV), statically linked, BuildID[sha1]=4c52c576a0452e97d9117b89dd317c88460b0768, for GNU/Linux 4.15.0, not stripped

$ ./qemu-riscv64 -d page ~/a.out
host mmap_min_addr=0x1000
Locating guest address space @ 0x3ff000
page layout changed following mmap
start            end              size             prot
0000000000010000-0000000000084000 0000000000074000 ---
...
end_code    0x0000000000078388
start_code  0x0000000000010000
start_data  0x00000000000795b0
end_data    0x000000000007e8a8
start_stack 0x0000003f812224a0
brk         0x0000000000084000
entry       0x000000000001041c
argv_start  0x0000003f812224a8
env_start   0x0000003f812224b8
auxv_start  0x0000003f81222570
...
Hello, World!

I don't doubt that you see a problem, but I need a reproducer, not a guess as to what the problem might be. Certainly guest_base is *not* it. One can always force the use of a non-zero base with -B or -R.

r~

#2 lore raw
Quoted 1 line · Richard Henderson

can always force the use of a non-zero base with -B or -R.

$ qemu-riscv64 -d page -B 0x3ee000 hello.riscv64
host mmap_min_addr=0x1000 (fallback)
qemu-riscv64: /daten/src/test/hello.riscv64: requires virtual address space that is in use (omit the -B option or choose a different value)
Signature · 3 lines
Andreas Schwab, SUSE Labs, [email protected]
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."
#3 lore raw
Quoted 8 lines · Andreas Schwab
Quoted 1 line · Richard Henderson

can always force the use of a non-zero base with -B or -R.

$ qemu-riscv64 -d page -B 0x3ee000 hello.riscv64
host mmap_min_addr=0x1000 (fallback)
qemu-riscv64: /daten/src/test/hello.riscv64: requires virtual address space that is in use (omit the -B option or choose a different value)

Well, sure, but that obviously is where qemu-riscv64 itself is located. Still not a valid test case.

r~

#4 lore raw
Quoted 5 lines · Richard Henderson
Quoted 4 lines · Andreas Schwab
Quoted 1 line · Richard Henderson

can always force the use of a non-zero base with -B or -R.

Quoted 9 lines
Quoted 3 lines
$ qemu-riscv64 -d page -B 0x3ee000 hello.riscv64
host mmap_min_addr=0x1000 (fallback)
qemu-riscv64: /daten/src/test/hello.riscv64: requires virtual address

space that is in use (omit the -B option or choose a different value)

Quoted 1 line

Well, sure, but that obviously is where qemu-riscv64 itself is located. Still not a valid test case.

Yea, what happens if you say -B 0x3ee000000 or something else that won't conflict?

Warner

#5 lore raw
Quoted 14 lines · Warner Losh
Quoted 11 lines · Richard Henderson
Quoted 7 lines · Andreas Schwab
Quoted 1 line · Richard Henderson

can always force the use of a non-zero base with -B or -R.

$ qemu-riscv64 -d page -B 0x3ee000 hello.riscv64
host mmap_min_addr=0x1000 (fallback)
qemu-riscv64: /daten/src/test/hello.riscv64: requires virtual address

space that is in use (omit the -B option or choose a different value)

Quoted 1 line
Quoted 6 lines
Quoted 3 lines

Well, sure, but that obviously is where qemu-riscv64 itself is located. Still not a valid test case.

Yea, what happens if you say -B 0x3ee000000 or something else that won't conflict?

I didn't chose that number, qemu did. If it doesn't work then qemu must be fixed.

Signature · 3 lines
Andreas Schwab, SUSE Labs, [email protected]
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."
#6 lore raw
Quoted 20 lines · Andreas Schwab
Quoted 18 lines · Warner Losh
Quoted 14 lines · Richard Henderson
Quoted 7 lines · Andreas Schwab
Quoted 1 line · Richard Henderson

can always force the use of a non-zero base with -B or -R.

$ qemu-riscv64 -d page -B 0x3ee000 hello.riscv64
host mmap_min_addr=0x1000 (fallback)
qemu-riscv64: /daten/src/test/hello.riscv64: requires virtual address

space that is in use (omit the -B option or choose a different value)

Quoted 1 line

Well, sure, but that obviously is where qemu-riscv64 itself is located. Still not a valid test case.

Quoted 6 lines
Quoted 2 lines

Yea, what happens if you say -B 0x3ee000000 or something else that won't conflict?

I didn't chose that number, qemu did. If it doesn't work then qemu must be fixed.

And when you are diagnosing the root cause of the bug, the submitter of the bug sometimes needs to do diagnostic tests when requested, not attack the volunteers who are trying to help. If that's all you do, there will be no fix. You can't talk to me like that and expect any reaction but "I have better things to do with my time than deal with this jerk" regardless of the merits of the original complaint.

Warner

Quoted 4 lines

Andreas Schwab, SUSE Labs, [email protected]
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE 1748 E4D4 88E3 0EEA B9D7 "And now for something completely different."

#7 lore raw

Perhaps you should refrain from attacking the volunteers that report bugs.

Signature · 3 lines
Andreas Schwab, SUSE Labs, [email protected]
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."
#8 lore raw
Quoted 2 lines · Andreas Schwab

Perhaps you should refrain from attacking the volunteers that report bugs.

I think the tone of your previous email was extremely terse and didn't actually answer the question, which is quite easy to misinterpret as hostility.

We're all trying to achieve the same thing here, and the underlying problem is that we can't reproduce the fault you're seeing. So to help us with that, you need to provide more precise instructions on exactly how we can get the result you see, or else provide the answers to the questions we're asking to try to get more diagnostic information.

For instance, this might help:
* how exactly did you build the binary (what source, what compiler version, what OS, etc)
* a repro built binary
* exact QEMU command line

Clearly something is different between your setup and Richard's and Warner's, so we need more detail to narrow down what that is. Warner's questions are trying to find out more about what's going on.

thanks
-- PMM

#10 lore raw

Hi Andreas,

Thanks for the test binary.

Now I need to know about your qemu version and build configuration.

Your test binary still works for me. My configuration:

Debian GNU/Linux trixie/sid
gcc version 13.2.0 (Debian 13.2.0-25)

commit 60b4f3aff4e39be04f5d73c65a7e8ef838475c9f (HEAD, tag: v9.0.1, origin/stable-9.0)

../src/configure --static --disable-system --disable-tools --disable-docs

qemu-riscv64: ELF 64-bit LSB executable, UCB RISC-V, RVC, double-float ABI, version 1 (SYSV), statically linked, BuildID[sha1]=86144e2f215344440f3e21b65b2cf2b8cb33c67f, for GNU/Linux 4.15.0, with debug_info, not stripped

LOAD 0x0000000000000000 0x0000000000010000 0x0000000000010000

                 0x000000000035feae 0x000000000035feae  R E    0x1000
  LOAD           0x0000000000360708 0x0000000000370708 0x0000000000370708
                 0x0000000000070ebc 0x00000000000833f0  RW     0x1000

So the end of .bss is at 0x3f3af8.

With -d page, I get

Locating guest address space @ 0x3f4000

which is uncomfortably close to the end of .bss, but seems to work.

r~

#11 lore raw
Quoted 3 lines · Richard Henderson

With -d page, I get

Locating guest address space @ 0x3f4000

Why do you get a different address?

Signature · 3 lines
Andreas Schwab, SUSE Labs, [email protected]
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."
#17 lore raw
In reply to Richard Henderson

Please follow https://en.opensuse.org/openSUSE:RISC-V#QEMU_system_emulation
to start the JeOS-efi image. When logged in:

# zypper rm qemu-linux-user-riscv
# zypper in qemu-linux-user

Then you can run qemu-riscv64 inside:

# qemu-riscv64 -d page hello.riscv64 
host mmap_min_addr=0x10000
Locating guest address space @ 0x3ee000
page layout changed following mmap
start            end              size             prot
0000000000010000-0000000000013000 0000000000003000 ---
page layout changed following mmap
start            end              size             prot
0000000000010000-0000000000011000 0000000000001000 r-x
0000000000011000-0000000000013000 0000000000002000 ---
page layout changed following mmap
start            end              size             prot
0000000000010000-0000000000011000 0000000000001000 r-x
0000000000011000-0000000000013000 0000000000002000 rw-
mmap stack: Cannot allocate memory
Signature · 3 lines
Andreas Schwab, SUSE Labs, [email protected]
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."
#12 lore raw
In reply to Andreas Schwab
Quoted 8 lines · Andreas Schwab
Quoted 3 lines · Richard Henderson

With -d page, I get

Locating guest address space @ 0x3f4000

Why do you get a different address?

Come on, man. Please answer my question:

Quoted 1 line

Now I need to know about your qemu version and build configuration.

r~

#14 lore raw
In reply to Andreas Schwab
Quoted 7 lines · Andreas Schwab
Quoted 3 lines · Richard Henderson

With -d page, I get

Locating guest address space @ 0x3f4000

Why do you get a different address?

Is /proc mounted in your environment?
The guest address base selection depends on /proc/self/maps.

r~

#15 lore raw
In reply to Richard Henderson
Quoted 1 line · Richard Henderson

Is /proc mounted in your environment?

Sure, it's a fully running system on real hardware.

Signature · 3 lines
Andreas Schwab, SUSE Labs, [email protected]
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."
#16 lore raw
Quoted 6 lines · Andreas Schwab
Quoted 1 line · Richard Henderson

Is /proc mounted in your environment?

Sure, it's a fully running system on real hardware.

I mean, you're not running it in some chroot sandbox without /proc.

r~