Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 27 Jun 2020 20:58:35 +0300
From:      Konstantin Belousov <kostikbel@gmail.com>
To:        adr@sdf.org
Cc:        freebsd-arm@freebsd.org
Subject:   Re: calling mmap from assembly, invalid argument
Message-ID:  <20200627175835.GC32126@kib.kiev.ua>
In-Reply-To: <20200627155110.GA22042@SDF.ORG>
References:  <20200627155110.GA22042@SDF.ORG>

next in thread | previous in thread | raw e-mail | index | archive | help
On Sat, Jun 27, 2020 at 03:51:10PM +0000, adr@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@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@freebsd.org mailing list
> https://lists.freebsd.org/mailman/listinfo/freebsd-arm
> To unsubscribe, send any mail to "freebsd-arm-unsubscribe@freebsd.org"



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20200627175835.GC32126>