calling mmap from assembly, invalid argument

Konstantin Belousov kostikbel at gmail.com
Sat Jun 27 17:58:53 UTC 2020


On Sat, Jun 27, 2020 at 03:51:10PM +0000, adr at SDF.ORG wrote:
> Hello,
> 
> I can't make a simple call to mmap.
> 
> I'm not using system calls because I've to use other c functions
> in the real code, so there is not gain following the possible
> changes on the syscalls.
> 
> I should be missing something.
> I've used clang in openbsd without a problem.
> 
> I'll appreciate any help.
> 
> adr
> ============================================
> % uname -a
> FreeBSD fbsd 13.0-CURRENT FreeBSD 13.0-CURRENT #0 r362037: Thu Jun 11 05:06:50 UTC 2020
> root at releng1.nyi.freebsd.org:/usr/obj/usr/src/arm.armv7/sys/GENERIC  arm
> 
> It doesn't matter if I assemble the code with gas.
> 
> % clang -o test test.s
> % ./test
> 
> Invalid argument
> mmap addr: 0xffffffff
> 
> ==============================
> .syntax unified
> 
> @ /usr/include/sys/mman.h
> PROT_READ  = 1
> PROT_WRITE = 2
> MAP_PRIVATE = 2
> MAP_ANONYMOUS = 0x1000
> 
>   .data
> ostr:
>   .string "mmap addr: %#x\n"
>   .align
> 
>   .text
>   .global main
> 
> main:
>   mov r5, 0     @ offset
>   mov r4, -1    @ fd
>   push {r4, r5}
Offset has off_t type which is 64 bit and requires proper alignment.
In this code, some random garbage on the stack is interpreted as offset.

Our mmap(2) is strict by requiring offset equal to zero for MAP_ANON.

You would see it yourself with either ktrace/kdump or truss.

>   mov r3, MAP_ANONYMOUS|MAP_PRIVATE
>   mov r2, PROT_READ|PROT_WRITE
>   mov r1, 4096 @ len
>   mov r0, 0 @ addr
>   bl mmap
>   mov r5, r0
>   mov r0, 0
>   bl perror
>   ldr r0, =ostr
>   mov r1, r5
>   bl printf
>   mov r0, 0
>   bl fflush 
>   b exit
> _______________________________________________
> freebsd-arm at freebsd.org mailing list
> https://lists.freebsd.org/mailman/listinfo/freebsd-arm
> To unsubscribe, send any mail to "freebsd-arm-unsubscribe at freebsd.org"


More information about the freebsd-arm mailing list