qtiuto / lua-for-android

A high-performance bridge for lua and java or c in Android with mutil-thread and almost all java features supported
BSD 3-Clause "New" or "Revised" License
111 stars 26 forks source link

Error on arm32 #6

Open mingodad opened 5 years ago

mingodad commented 5 years ago

Hello ! Testing your project with with the apk on android arm32 (nexus7) this program fail:

local ffi = require('ffi')
local buf = ffi.new('char[256]')
local c = ffi.C
ffi.cdef('int sprintf(char *buf, const char *fmt, ...);')
print(c.sprintf(buf, '%g', 5.3), ffi.string(buf));

Expected:

3    5.3

Output:

11   2.14609e+41

The above and the following are from the original test.lua and it also fail the following with segfault:

-- unsigned should be ignored for pointer rules
ffi.cdef([=[
int strncmp(const signed char *s1, const unsigned char *s2, size_t n);
]=]);
assert(ffi.C.strncmp("two", "three", 3) ~= 0); -- segfault here
...
-- Test io.tmpfile()
ffi.cdef ([=[
    int fprintf ( FILE * stream, const char * format, ... );
]=]);
local f = io.tmpfile();
ffi.C.fprintf(f, "test: %s\n", "foo"); -- segfault here

f:seek("set", 0);
local str = f:read('*l');
assert(str == 'test: foo', str);
f:close();

Commenting the above mentioned tests all the rest of test.lua pass. Great work !

qtiuto commented 5 years ago

The first is a alignment bug, the second I can not reproduce it,and the third is because io.tempfile only generates a valid pointer for the io library, not for the ffi library. May be fix or not.

Domingo Alvarez Duarte notifications@github.com 于 2019年1月16日周三 05:41写道:

Hello ! Testing your project with with the apk on android arm32 (nexus7) this program fail:

local ffi = require('ffi') local buf = ffi.new('char[256]') local c = ffi.C ffi.cdef('int sprintf(char buf, const char fmt, ...);') print(csprintf(buf, '%g', 5.3), ffi.string(buf));

Expected:

3 5.3

Output:

11 2.14609e+41

The above and the following are from the original test.lua and it also fail the following with segfault:

// unsigned should be ignored for pointer rules ffi.cdef([=[ int strncmp(const signed char s1, const unsigned char s2, size_t n); ]=]); assert(ffi.C.strncmp("two", "three", 3) ~= 0); -- segfault here ... // Test io.tmpfile() ffi.cdef ([=[ int fprintf ( FILE stream, const char format, ... ); ]=]); local f = io.tmpfile(); ffi.C.fprintf(f, "test: %s\n", "foo"); -- segfault here

f:seek("set", 0); local str = f:read('*l'); assert(str == 'test: foo', str); f:close();

Commenting the above mentioned tests all the rest of test.lua pass. Great work !

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/qtiuto/lua-for-android/issues/6, or mute the thread https://github.com/notifications/unsubscribe-auth/AJHwUmkGq-Wf2qIXa_YAfKg5HLUfaZkAks5vDkrsgaJpZM4aB3fT .

mingodad commented 5 years ago

Hello ! After removing the meta method optimisation I've got it to compile again but now it fails here:

assert(c.sprintf(buf, "%d%g", false, 6.7) == 4 && ffi.string(buf) == '06.7');

Output of:

print(c.sprintf(buf, "%d%g", false, 6.7),  ffi.string(buf));
8   03.41612

It was passing before. And it still segfaults here:

assert(ffi.C.strncmp("two", "three", 3) ~= 0);
qtiuto commented 5 years ago

Does it only happen on arm? Does it happen on emulator? Does it happen on other devices? My device has no problem with these bugs. The bug with strncmp seems pretty weird. I will test them on emulator later.

Domingo Alvarez Duarte notifications@github.com 于 2019年1月16日周三 22:02写道:

Hello ! After removing the meta method optimisation I've got it to compile again but now it fails here:

assert(c.sprintf(buf, "%d%g", false, 6.7) == 4 && ffi.string(buf) == '06.7');

Output of:

print(c.sprintf(buf, "%d%g", false, 6.7), ffi.string(buf) == '06.7')); 8 03.41612

I was passing before. And it still segfaults here:

assert(ffi.C.strncmp("two", "three", 3) ~= 0);

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/qtiuto/lua-for-android/issues/6#issuecomment-454788921, or mute the thread https://github.com/notifications/unsubscribe-auth/AJHwUi_NMyIHP-Bwsh7Vhv9EJIcGsdSBks5vDzDYgaJpZM4aB3fT .

mingodad commented 5 years ago

I'm testing it on the device (nexus7) using termux.

mingodad commented 5 years ago

It should not make a difference but the compiler is clang7

mingodad commented 5 years ago

Hello qtiuto !

Here you have attached what I'm using to test your changes to luaffi, it's luaffib/raviffi with your changes but without the meta methods optimization.

The makefile maybe need some adjusts to work for you, as it is it doesn't work on x86 without removing -D__ANDROID_API__=25 from the CFLAGS.

I use it as is on my nexus7 inside termux (bash sell). Cheers ! luaffifb-arm.zip

qtiuto commented 5 years ago

Please apply CHECK_ALIGN in ff.c firstly. It's the final fix for arm alignment. You have three argument before the double value in the the sprintf and it breaks the arm argument passing rule for double/int64. Apply CHECK_ALIGN and it will have the double value offset 4. If you use 'llx' instead of 'g', you will see the higher 4 bytes of the double value is now the lower 4 bytes. Then I will try termux to compile your work.

qtiuto commented 5 years ago

@mingodad

qtiuto commented 5 years ago

I have tested it on my phone by termux in arm32 mode and nothing goes wrong.

Domingo Alvarez Duarte notifications@github.com 于 2019年1月16日周三 23:20写道:

Hello qtiuto !

Here you have attached what I'm using to test your changes to luaffi, it's luaffib/raviffi with your changes but without the meta methods optimization.

The makefile maybe need some adjusts to work for you, as it is it doesn't work on x86 without removing -D__ANDROID_API__=25 from the CFLAGS.

I use it as is on my nexus7 inside termux (bash sell). Cheers ! luaffifb-arm.zip https://github.com/qtiuto/lua-for-android/files/2764706/luaffifb-arm.zip

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/qtiuto/lua-for-android/issues/6#issuecomment-454817319, or mute the thread https://github.com/notifications/unsubscribe-auth/AJHwUnoqZY0TubDkLBztOVcAVjsvCjdWks5vD0M6gaJpZM4aB3fT .

mingodad commented 5 years ago

Hello ! I also just tested on a raspberry pi archlinux and all tests pass. But on my nexus7 this 'assert(ffi.C.strncmp("two", "three", 3) ~= 0);' still segfaults, I'll test on an old netbook running ubuntu x86 to see on 32bits.

qtiuto commented 5 years ago

I think the error may be induce my mmap, which may fail on some devices with limited memory. I test it on my nano pi and it sometimes fails also.

Domingo Alvarez Duarte notifications@github.com 于 2019年1月17日周四 02:01写道:

Hello ! I also just tested on a raspberry pi archlinux and all tests pass. But on my nexus7 this 'assert(ffi.C.strncmp("two", "three", 3) ~= 0);' still segfaults, I'll test on an old netbook running ubuntu x86 to see on 32bits.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/qtiuto/lua-for-android/issues/6#issuecomment-454878765, or mute the thread https://github.com/notifications/unsubscribe-auth/AJHwUsCC7dXO6L5K1q8p5_6dhwnLWZ_sks5vD2kSgaJpZM4aB3fT .

mingodad commented 5 years ago

Hello ! I just tested on the x86 netbook and all tests passed, now I'm trying on a nexus5 but I'm having small problems to get it working, later I'll report it, and maybe test on an OSX and on IOS

qtiuto commented 5 years ago

I solve the problem on my nano pi by calling __builtin_clear_cache, but it's a gcc function. On linux, cacheflush is available. I'm not sure about the compatiblity on other platform. Windows cache has been flush actually.

Domingo Alvarez Duarte notifications@github.com 于 2019年1月17日周四 02:36写道:

Hello ! I just tested on the x86 netbook and all tests passed, now I'm trying on a nexus5 but I'm having small problems to get it working, later I'll report it, and maybe test on an OSX and on IOS

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/qtiuto/lua-for-android/issues/6#issuecomment-454890409, or mute the thread https://github.com/notifications/unsubscribe-auth/AJHwUs9Ry8d3-Aoo-Z61vwCL46-Eu3L8ks5vD3EYgaJpZM4aB3fT .

mingodad commented 5 years ago

I just finished test on the nexus5 and all tests passed, on OSX also all tests passed, now I'm trying on an ipod5 ios8.1 jailbroken to see what happens.

mingodad commented 5 years ago

Hello ! Testing it on the iPOD iOS8.1 arm32 it compiles but gives the same error as before the patch for alignment:

print(c.sprintf(buf, '%g', 5.3), ffi.string(buf));
11  4.66726e-62

But all other tests passed.

LuaJIT 2.1 compiled but when try to run it trash the device, need reboot.

qtiuto commented 5 years ago

replace %g with %llx and send me the result, OK?

Domingo Alvarez Duarte notifications@github.com 于 2019年1月17日周四 04:31写道:

Hello ! Testing it on the iPOD iOS8.1 arm32 it compiles but gives the same error as before the patch for alignment:

print(c.sprintf(buf, '%g', 5.3), ffi.string(buf)); 11 4.66726e-62

LuaJIT 2.1 compiled but when try to run it trash the device, need reboot.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/qtiuto/lua-for-android/issues/6#issuecomment-454929150, or mute the thread https://github.com/notifications/unsubscribe-auth/AJHwUjUbn0CvkftcCGzLJOVlbi10fGkGks5vD4wTgaJpZM4aB3fT .

mingodad commented 5 years ago

Here you have:

lua test.lua 
Running test
11  4.66726e-62
16  3333333316390cdc
4   06.7
Test PASSED

Code changed to show the above output:

print(c.sprintf(buf, "%g", 5.3), ffi.string(buf))
print(c.sprintf(buf, "%llx", 5.3), ffi.string(buf))
--dad assert(c.sprintf(buf, "%g", 5.3) == 3 and ffi.string(buf) == '5.3')
assert(c.sprintf(buf, "%d", false) == 1 and ffi.string(buf) == '0')
print(c.sprintf(buf, "%d%g", false, 6.7), ffi.string(buf))
qtiuto commented 5 years ago

Have you check_align for arm? If you do, the results seems like that the machine may read a double from unaligned address when my align check offsets 4. try fill a integer before to check the alignment.May be a right result will occur. If you can get a right result from that way, then I think I need to handle it specially.

Domingo Alvarez Duarte notifications@github.com 于 2019年1月17日周四 04:36写道:

Here you have:

lua test.lua Running test 11 4.66726e-62 16 3333333316390cdc 4 06.7 Test PASSED

Code changed to show the above output:

print(c.sprintf(buf, "%g", 5.3), ffi.string(buf)) print(c.sprintf(buf, "%llx", 5.3), ffi.string(buf)) --dad assert(c.sprintf(buf, "%g", 5.3) == 3 and ffi.string(buf) == '5.3') assert(c.sprintf(buf, "%d", false) == 1 and ffi.string(buf) == '0') print(c.sprintf(buf, "%d%g", false, 6.7), ffi.string(buf))

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/qtiuto/lua-for-android/issues/6#issuecomment-454931068, or mute the thread https://github.com/notifications/unsubscribe-auth/AJHwUoIjxggPge6XxOzFqlK1I_7GIbY7ks5vD41agaJpZM4aB3fT .

mingodad commented 5 years ago

Hello ! I also isolate the code on one file to see if the interaction with the rest was the cause of the problem but no the problem remain the same:

local ffi = require("ffi")
ffi.cdef("int sprintf(char* buf, const char* format, ...);")

local buf = ffi.new('char[256]')

local c = ffi.C

print(c.sprintf(buf, "%g", 5.3), ffi.string(buf))
print(c.sprintf(buf, "%llx", 5.3), ffi.string(buf))

Output:

11  4.66726e-62
16  33333333181814a8
mingodad commented 5 years ago

Hello ! With what you've told me we get this:

local ffi = require("ffi")
ffi.cdef("int sprintf(char* buf, const char* format, ...);")

local buf = ffi.new('char[256]')

local c = ffi.C

print(c.sprintf(buf, "%d", 5), ffi.string(buf))
print(c.sprintf(buf, "%d:%g", 5, 5.3), ffi.string(buf))
print(c.sprintf(buf, "%g", 5.3), ffi.string(buf))
print(c.sprintf(buf, "%llx", 5.3), ffi.string(buf))

Output:

1   5
5   5:5.3
11  4.66726e-62
16  3333333317043eb4
mingodad commented 5 years ago

And here with more examples:

local ffi = require("ffi")
ffi.cdef("int sprintf(char* buf, const char* format, ...);")

local buf = ffi.new('char[256]')

local c = ffi.C

print(c.sprintf(buf, "%d", 5), ffi.string(buf))
print(c.sprintf(buf, "%d:%g:%g", 5, 5.3, 6.7), ffi.string(buf))
print(c.sprintf(buf, "%g", 5.3), ffi.string(buf))
print(c.sprintf(buf, "%llx", 5.3), ffi.string(buf))
print(c.sprintf(buf, "%g", 6.7), ffi.string(buf))
print(c.sprintf(buf, "%d:%g:%d", 11, 6.7, 22), ffi.string(buf))

Output:

1   5
9   5:5.3:6.7
11  4.66726e-62
16  33333333153806c0
12  -9.25596e+61
9   11:6.7:22

It seems that you are getting close to the problem .

qtiuto commented 5 years ago

I can make sure that this a compiler bug or a library for the platform. The stack doesn't align eight and and the library function var_arg doesn't apply arm argument passing rule and do it as usual like x86. The platform should be handle specially or the gcc version should be handle specially. Any macro defines the platform? @mingodad

qtiuto commented 5 years ago

Just exclude the platform from checking alignment and every thing goes right.

qtiuto commented 5 years ago

I think you can work it out for I'm not familiar with ios macros.

Domingo Alvarez Duarte notifications@github.com 于 2019年1月17日周四 05:10写道:

And here with more examples:

local ffi = require("ffi") ffi.cdef("int sprintf(char buf, const char format, ...);")

local buf = ffi.new('char[256]')

local c = ffi.C

print(c.sprintf(buf, "%d", 5), ffi.string(buf)) print(c.sprintf(buf, "%d:%g:%g", 5, 5.3, 6.7), ffi.string(buf)) print(c.sprintf(buf, "%g", 5.3), ffi.string(buf)) print(c.sprintf(buf, "%llx", 5.3), ffi.string(buf)) print(c.sprintf(buf, "%g", 6.7), ffi.string(buf)) print(c.sprintf(buf, "%d:%g:%d", 11, 6.7, 22), ffi.string(buf))

Output:

1 5 9 5:5.3:6.7 11 4.66726e-62 16 33333333153806c0 12 -9.25596e+61 9 11:6.7:22

It seems that you are getting close to the problem .

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/qtiuto/lua-for-android/issues/6#issuecomment-454942846, or mute the thread https://github.com/notifications/unsubscribe-auth/AJHwUuuSlE_-oyw_EU7QeZNXEw-QdAJzks5vD5VJgaJpZM4aB3fT .

mingodad commented 5 years ago

Here is the assembler generated by this c program:

#include <stdio.h>

int main(int argc, char *argv[])
{
    double d1 = 5.3;
    double d2 = 6.7;
    float f1 = 5.3;
    float f2 = 6.7;
    char buf[256];
    int rc;
    rc = sprintf(buf, "%g", d1);
    printf("%d\t%s\n", rc, buf);
    rc = sprintf(buf, "%g", d2);
    printf("%d\t%s\n", rc, buf);
    rc = sprintf(buf, "%g", f1);
    printf("%d\t%s\n", rc, buf);
    rc = sprintf(buf, "%g", f2);
    printf("%d\t%s\n", rc, buf);

    return 0;
}
    .section    __TEXT,__text,regular,pure_instructions
    .ios_version_min 5, 0
    .syntax unified
    .globl  _main
    .align  1
    .code   16                      @ @main
    .thumb_func _main
_main:
@ BB#0:
    push    {r4, r5, r6, r7, lr}
    add r7, sp, #12
    push.w  {r8, r10, r11}
    sub sp, #268
    movw    r4, :lower16:(L_.str-(LPC0_0+4))
    movw    r10, #13107
    movt    r4, :upper16:(L_.str-(LPC0_0+4))
    movw    r8, :lower16:(L___stack_chk_guard$non_lazy_ptr-(LPC0_1+4))
    movt    r8, :upper16:(L___stack_chk_guard$non_lazy_ptr-(LPC0_1+4))
LPC0_0:
    add r4, pc
LPC0_1:
    add r8, pc
    add r5, sp, #8
    movt    r10, #16405
    mov.w   r0, #858993459
    ldr.w   r8, [r8]
    movs    r1, #0
    mov.w   r2, #256
    mov r3, r4
    ldr.w   r8, [r8]
    str.w   r8, [sp, #264]
    strd    r0, r10, [sp]
    mov r0, r5
    blx ___sprintf_chk
    movw    r6, :lower16:(L_.str.1-(LPC0_2+4))
    mov r1, r0
    movt    r6, :upper16:(L_.str.1-(LPC0_2+4))
    mov r2, r5
LPC0_2:
    add r6, pc
    mov r0, r6
    blx _printf
    movw    r11, #52428
    movw    r0, #52429
    movt    r11, #16410
    movt    r0, #52428
    strd    r0, r11, [sp]
    mov r0, r5
    movs    r1, #0
    mov.w   r2, #256
    mov r3, r4
    blx ___sprintf_chk
    mov r1, r0
    mov r0, r6
    mov r2, r5
    blx _printf
    mov.w   r0, #1073741824
    movs    r1, #0
    strd    r0, r10, [sp]
    mov r0, r5
    mov.w   r2, #256
    mov r3, r4
    blx ___sprintf_chk
    mov r1, r0
    mov r0, r6
    mov r2, r5
    blx _printf
    mov.w   r0, #-1073741824
    movs    r1, #0
    strd    r0, r11, [sp]
    mov r0, r5
    mov.w   r2, #256
    mov r3, r4
    blx ___sprintf_chk
    mov r1, r0
    mov r0, r6
    mov r2, r5
    blx _printf
    ldr r0, [sp, #264]
    subs.w  r0, r8, r0
    itttt   eq
    moveq   r0, #0
    addeq   sp, #268
    popeq.w {r8, r10, r11}
    popeq   {r4, r5, r6, r7, pc}
    blx ___stack_chk_fail

    .section    __TEXT,__cstring,cstring_literals
L_.str:                                 @ @.str
    .asciz  "%g"

L_.str.1:                               @ @.str.1
    .asciz  "%d\t%s\n"

    .section    __DATA,__nl_symbol_ptr,non_lazy_symbol_pointers
    .align  2
L___stack_chk_guard$non_lazy_ptr:
    .indirect_symbol    ___stack_chk_guard
    .long   0

.subsections_via_symbols
qtiuto commented 5 years ago

Can you test whether the problem only happen s on variadic function? I'm not sure if general argument passing needs alignment for ios or not. If not, I think the dasc file need a little fix also. You can define a function with a int and a double as argument and a function with two ints and a double as argument to check whether you can receive right argments.

奥斯陆君王 qtiuto@gmail.com 于 2019年1月17日周四 05:17写道:

I think you can work it out for I'm not familiar with ios macros.

Domingo Alvarez Duarte notifications@github.com 于 2019年1月17日周四 05:10写道:

And here with more examples:

local ffi = require("ffi") ffi.cdef("int sprintf(char buf, const char format, ...);")

local buf = ffi.new('char[256]')

local c = ffi.C

print(c.sprintf(buf, "%d", 5), ffi.string(buf)) print(c.sprintf(buf, "%d:%g:%g", 5, 5.3, 6.7), ffi.string(buf)) print(c.sprintf(buf, "%g", 5.3), ffi.string(buf)) print(c.sprintf(buf, "%llx", 5.3), ffi.string(buf)) print(c.sprintf(buf, "%g", 6.7), ffi.string(buf)) print(c.sprintf(buf, "%d:%g:%d", 11, 6.7, 22), ffi.string(buf))

Output:

1 5 9 5:5.3:6.7 11 4.66726e-62 16 33333333153806c0 12 -9.25596e+61 9 11:6.7:22

It seems that you are getting close to the problem .

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/qtiuto/lua-for-android/issues/6#issuecomment-454942846, or mute the thread https://github.com/notifications/unsubscribe-auth/AJHwUuuSlE_-oyw_EU7QeZNXEw-QdAJzks5vD5VJgaJpZM4aB3fT .

qtiuto commented 5 years ago

The compiler doesn't work wrong. It 's the error from the receiver. It doesn't check stack alignment which will be done by other arm platform. Unfortunately, the stack doesn't align eight for some unkown functions and have the problem occur. I'm not willing to generate extra code to check the stack.

Domingo Alvarez Duarte notifications@github.com 于 2019年1月17日周四 05:24写道:

Here is the assembler generated by this c program:

include

int main(int argc, char *argv[]) { double d1 = 5.3; double d2 = 6.7; float f1 = 5.3; float f2 = 6.7; char buf[256]; int rc; rc = sprintf(buf, "%g", d1); printf("%d\t%s\n", rc, buf); rc = sprintf(buf, "%g", d2); printf("%d\t%s\n", rc, buf); rc = sprintf(buf, "%g", f1); printf("%d\t%s\n", rc, buf); rc = sprintf(buf, "%g", f2); printf("%d\t%s\n", rc, buf);

return 0;

}

.section TEXT,text,regular,pure_instructions .ios_version_min 5, 0 .syntax unified .globl _main .align 1 .code 16 @ @main .thumb_func _main main: @ BB#0: push {r4, r5, r6, r7, lr} add r7, sp, #12 push.w {r8, r10, r11} sub sp, #268 movw r4, :lower16:(L.str-(LPC00+4)) movw r10, #13107 movt r4, :upper16:(L.str-(LPC0_0+4)) movw r8, :lower16:(L_stack_chk_guard$non_lazy_ptr-(LPC0_1+4)) movt r8, :upper16:(L_stack_chk_guard$non_lazy_ptr-(LPC0_1+4)) LPC0_0: add r4, pc LPC01: add r8, pc add r5, sp, #8 movt r10, #16405 mov.w r0, #858993459 ldr.w r8, [r8] movs r1, #0 mov.w r2, #256 mov r3, r4 ldr.w r8, [r8] str.w r8, [sp, #264] strd r0, r10, [sp] mov r0, r5 blx sprintfchk movw r6, :lower16:(L.str.1-(LPC02+4)) mov r1, r0 movt r6, :upper16:(L.str.1-(LPC0_2+4)) mov r2, r5 LPC0_2: add r6, pc mov r0, r6 blx _printf movw r11, #52428 movw r0, #52429 movt r11, #16410 movt r0, #52428 strd r0, r11, [sp] mov r0, r5 movs r1, #0 mov.w r2, #256 mov r3, r4 blx ___sprintf_chk mov r1, r0 mov r0, r6 mov r2, r5 blx printf mov.w r0, #1073741824 movs r1, #0 strd r0, r10, [sp] mov r0, r5 mov.w r2, #256 mov r3, r4 blx sprintf_chk mov r1, r0 mov r0, r6 mov r2, r5 blx _printf mov.w r0, #-1073741824 movs r1, #0 strd r0, r11, [sp] mov r0, r5 mov.w r2, #256 mov r3, r4 blx _sprintf_chk mov r1, r0 mov r0, r6 mov r2, r5 blx printf ldr r0, [sp, #264] subs.w r0, r8, r0 itttt eq moveq r0, #0 addeq sp, #268 popeq.w {r8, r10, r11} popeq {r4, r5, r6, r7, pc} blx stack_chk_fail

.section TEXT,cstring,cstringliterals L.str: @ @.str .asciz "%g"

L_.str.1: @ @.str.1 .asciz "%d\t%s\n"

.section DATA,nl_symbol_ptr,non_lazy_symbol_pointers .align 2 L_stack_chk_guard$non_lazy_ptr: .indirectsymbol stack_chk_guard .long 0

.subsections_via_symbols

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/qtiuto/lua-for-android/issues/6#issuecomment-454947804, or mute the thread https://github.com/notifications/unsubscribe-auth/AJHwUkWOoglnfnIjNDCtbgyLiU_ub8DPks5vD5ilgaJpZM4aB3fT .

mingodad commented 5 years ago

I'm not sure I understand what you are saying:

#include <stdio.h>

void func1(int i1, double d1)
{
    printf("%d : %g\n", i1, d1);
}

void func2(int i1, int i2, double d1)
{
    printf("%d : %d : %g\n", i1, i2, d1);
}

int main(int argc, char *argv[])
{
    double d1 = 5.3;
    int i1 = 2;
    int i2 = 4;

    func1(i1, d1);
    func2(i1, i2, d1);

    return 0;
}
    .section    __TEXT,__text,regular,pure_instructions
    .ios_version_min 5, 0
    .syntax unified
    .globl  _func1
    .align  1
    .code   16                      @ @func1
    .thumb_func _func1
_func1:
@ BB#0:
    push    {r7, lr}
    mov r7, sp
    mov r3, r2
    mov r2, r1
    mov r1, r0
    movw    r0, :lower16:(L_.str-(LPC0_0+4))
    movt    r0, :upper16:(L_.str-(LPC0_0+4))
LPC0_0:
    add r0, pc
    blx _printf
    pop {r7, pc}

    .globl  _func2
    .align  1
    .code   16                      @ @func2
    .thumb_func _func2
_func2:
@ BB#0:
    push    {r7, lr}
    mov r7, sp
    sub sp, #4
    mov r9, r2
    mov r2, r1
    mov r1, r0
    movw    r0, :lower16:(L_.str.1-(LPC1_0+4))
    movt    r0, :upper16:(L_.str.1-(LPC1_0+4))
    str r3, [sp]
LPC1_0:
    add r0, pc
    mov r3, r9
    blx _printf
    add sp, #4
    pop {r7, pc}

    .globl  _main
    .align  3
    .code   16                      @ @main
    .thumb_func _main
_main:
@ BB#0:
    push    {r4, r5, r7, lr}
    add r7, sp, #8
    sub sp, #4
    vldr    d16, LCPI2_0
    movw    r0, :lower16:(L_.str-(LPC2_0+4))
    movt    r0, :upper16:(L_.str-(LPC2_0+4))
    movs    r1, #2
    vmov    r4, r5, d16
LPC2_0:
    add r0, pc
    mov r2, r4
    mov r3, r5
    blx _printf
    movw    r0, :lower16:(L_.str.1-(LPC2_1+4))
    movs    r1, #2
    movt    r0, :upper16:(L_.str.1-(LPC2_1+4))
    movs    r2, #4
LPC2_1:
    add r0, pc
    mov r3, r4
    str r5, [sp]
    blx _printf
    movs    r0, #0
    add sp, #4
    pop {r4, r5, r7, pc}
    .align  3
@ BB#1:
    .data_region
LCPI2_0:
    .long   858993459               @ double 5.2999999999999998
    .long   1075131187
    .end_data_region

    .section    __TEXT,__cstring,cstring_literals
L_.str:                                 @ @.str
    .asciz  "%d : %g\n"

L_.str.1:                               @ @.str.1
    .asciz  "%d : %d : %g\n"

.subsections_via_symbols
mingodad commented 5 years ago

Here is a sample with a variadic function:

#include <stdio.h>
#include <stdarg.h>

void func1(int i1, double d1)
{
    printf("%d : %g\n", i1, d1);
}

void func2(int i1, int i2, double d1)
{
    printf("%d : %d : %g\n", i1, i2, d1);
}

void func3(char *buf, const char *fmt, ...)
{
    double d1 = 0;
    va_list args;
    va_start(args, fmt);
    d1 = va_arg(args, double);
    va_end(args);
    printf("%g\n", d1);
}

int main(int argc, char *argv[])
{
    double d1 = 5.3;
    int i1 = 2;
    int i2 = 4;
    char buf[256];

    func1(i1, d1);
    func2(i1, i2, d1);
    func3(buf, "%g", d1);

    return 0;
}
    .section    __TEXT,__text,regular,pure_instructions
    .ios_version_min 5, 0
    .syntax unified
    .globl  _func1
    .align  1
    .code   16                      @ @func1
    .thumb_func _func1
_func1:
@ BB#0:
    push    {r7, lr}
    mov r7, sp
    mov r3, r2
    mov r2, r1
    mov r1, r0
    movw    r0, :lower16:(L_.str-(LPC0_0+4))
    movt    r0, :upper16:(L_.str-(LPC0_0+4))
LPC0_0:
    add r0, pc
    blx _printf
    pop {r7, pc}

    .globl  _func2
    .align  1
    .code   16                      @ @func2
    .thumb_func _func2
_func2:
@ BB#0:
    push    {r7, lr}
    mov r7, sp
    sub sp, #4
    mov r9, r2
    mov r2, r1
    mov r1, r0
    movw    r0, :lower16:(L_.str.1-(LPC1_0+4))
    movt    r0, :upper16:(L_.str.1-(LPC1_0+4))
    str r3, [sp]
LPC1_0:
    add r0, pc
    mov r3, r9
    blx _printf
    add sp, #4
    pop {r7, pc}

    .globl  _func3
    .align  1
    .code   16                      @ @func3
    .thumb_func _func3
_func3:
@ BB#0:
    sub sp, #8
    push    {r7, lr}
    mov r7, sp
    sub sp, #4
    add.w   r0, r7, #8
    strd    r2, r3, [r7, #8]
    str r0, [sp]
    adds    r0, #8
    str r0, [sp]
    vldr    d16, [r7, #8]
    movw    r0, :lower16:(L_.str.2-(LPC2_0+4))
    movt    r0, :upper16:(L_.str.2-(LPC2_0+4))
    vmov    r1, r2, d16
LPC2_0:
    add r0, pc
    blx _printf
    add sp, #4
    pop.w   {r7, lr}
    add sp, #8
    bx  lr

    .globl  _main
    .align  3
    .code   16                      @ @main
    .thumb_func _main
_main:
@ BB#0:
    push    {r4, r5, r7, lr}
    add r7, sp, #8
    sub sp, #4
    vldr    d16, LCPI3_0
    movw    r0, :lower16:(L_.str-(LPC3_0+4))
    movt    r0, :upper16:(L_.str-(LPC3_0+4))
    movs    r1, #2
    vmov    r4, r5, d16
LPC3_0:
    add r0, pc
    mov r2, r4
    mov r3, r5
    blx _printf
    movw    r0, :lower16:(L_.str.1-(LPC3_1+4))
    movs    r1, #2
    movt    r0, :upper16:(L_.str.1-(LPC3_1+4))
    movs    r2, #4
LPC3_1:
    add r0, pc
    mov r3, r4
    str r5, [sp]
    blx _printf
    mov r2, r4
    mov r3, r5
    bl  _func3
    movs    r0, #0
    add sp, #4
    pop {r4, r5, r7, pc}
    .align  3
@ BB#1:
    .data_region
LCPI3_0:
    .long   858993459               @ double 5.2999999999999998
    .long   1075131187
    .end_data_region

    .section    __TEXT,__cstring,cstring_literals
L_.str:                                 @ @.str
    .asciz  "%d : %g\n"

L_.str.1:                               @ @.str.1
    .asciz  "%d : %d : %g\n"

L_.str.2:                               @ @.str.2
    .asciz  "%g\n"

.subsections_via_symbols
qtiuto commented 5 years ago

Thanks for test. I can make sure that no alignment is applied on ios. Then, you just need to fix two places. The first is CHECK_ALIGN in unpack_vararg and add &&!defined(TARGET_OS_IPHONE) for it. The second is the ALIGNED macro in call_arm.dasc. make it #define ALIGNED(x,align) 1 for TARGET_OS_IPHONE. Or you can see them from my lastest commit.

Domingo Alvarez Duarte notifications@github.com 于 2019年1月17日周四 05:39写道:

I'm not sure I understand what you are saying:

include

void func1(int i1, double d1) { printf("%d : %g\n", i1, d1); }

void func2(int i1, int i2, double d1) { printf("%d : %d : %g\n", i1, i2, d1); }

int main(int argc, char *argv[]) { double d1 = 5.3; int i1 = 2; int i2 = 4;

func1(i1, d1);
func2(i1, i2, d1);

return 0;

}

.section TEXT,text,regular,pure_instructions .ios_version_min 5, 0 .syntax unified .globl _func1 .align 1 .code 16 @ @func1 .thumb_func _func1 func1: @ BB#0: push {r7, lr} mov r7, sp mov r3, r2 mov r2, r1 mov r1, r0 movw r0, :lower16:(L.str-(LPC00+4)) movt r0, :upper16:(L.str-(LPC0_0+4)) LPC0_0: add r0, pc blx _printf pop {r7, pc}

.globl _func2 .align 1 .code 16 @ @func2 .thumb_func _func2 func2: @ BB#0: push {r7, lr} mov r7, sp sub sp, #4 mov r9, r2 mov r2, r1 mov r1, r0 movw r0, :lower16:(L.str.1-(LPC10+4)) movt r0, :upper16:(L.str.1-(LPC1_0+4)) str r3, [sp] LPC1_0: add r0, pc mov r3, r9 blx _printf add sp, #4 pop {r7, pc}

.globl _main .align 3 .code 16 @ @main .thumb_func _main _main: @ BB#0: push {r4, r5, r7, lr} add r7, sp, #8 sub sp, #4 vldr d16, LCPI20 movw r0, :lower16:(L.str-(LPC20+4)) movt r0, :upper16:(L.str-(LPC2_0+4)) movs r1, #2 vmov r4, r5, d16 LPC2_0: add r0, pc mov r2, r4 mov r3, r5 blx printf movw r0, :lower16:(L.str.1-(LPC21+4)) movs r1, #2 movt r0, :upper16:(L.str.1-(LPC2_1+4)) movs r2, #4 LPC2_1: add r0, pc mov r3, r4 str r5, [sp] blx _printf movs r0, #0 add sp, #4 pop {r4, r5, r7, pc} .align 3 @ BB#1: .data_region LCPI2_0: .long 858993459 @ double 5.2999999999999998 .long 1075131187 .end_data_region

.section TEXT,cstring,cstringliterals L.str: @ @.str .asciz "%d : %g\n"

L_.str.1: @ @.str.1 .asciz "%d : %d : %g\n"

.subsections_via_symbols

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/qtiuto/lua-for-android/issues/6#issuecomment-454952965, or mute the thread https://github.com/notifications/unsubscribe-auth/AJHwUgUL_gVjreYh3lrgJjef0H29k_tjks5vD5wBgaJpZM4aB3fT .

qtiuto commented 5 years ago

Moreover, the strncmp error is caused by icache. Merging the change on ffi.h may help you solve the problem.

Domingo Alvarez Duarte notifications@github.com 于 2019年1月17日周四 05:56写道:

Here is a sample with a variadic function:

include

include

void func1(int i1, double d1) { printf("%d : %g\n", i1, d1); }

void func2(int i1, int i2, double d1) { printf("%d : %d : %g\n", i1, i2, d1); }

void func3(char buf, const char fmt, ...) { double d1 = 0; va_list args; va_start(args, fmt); d1 = va_arg(args, double); va_end(args); printf("%g\n", d1); }

int main(int argc, char *argv[]) { double d1 = 5.3; int i1 = 2; int i2 = 4; char buf[256];

func1(i1, d1);
func2(i1, i2, d1);
func3(buf, "%g", d1);

return 0;

}

.section TEXT,text,regular,pure_instructions .ios_version_min 5, 0 .syntax unified .globl _func1 .align 1 .code 16 @ @func1 .thumb_func _func1 func1: @ BB#0: push {r7, lr} mov r7, sp mov r3, r2 mov r2, r1 mov r1, r0 movw r0, :lower16:(L.str-(LPC00+4)) movt r0, :upper16:(L.str-(LPC0_0+4)) LPC0_0: add r0, pc blx _printf pop {r7, pc}

.globl _func2 .align 1 .code 16 @ @func2 .thumb_func _func2 func2: @ BB#0: push {r7, lr} mov r7, sp sub sp, #4 mov r9, r2 mov r2, r1 mov r1, r0 movw r0, :lower16:(L.str.1-(LPC10+4)) movt r0, :upper16:(L.str.1-(LPC1_0+4)) str r3, [sp] LPC1_0: add r0, pc mov r3, r9 blx _printf add sp, #4 pop {r7, pc}

.globl _func3 .align 1 .code 16 @ @func3 .thumb_func _func3 func3: @ BB#0: sub sp, #8 push {r7, lr} mov r7, sp sub sp, #4 add.w r0, r7, #8 strd r2, r3, [r7, #8] str r0, [sp] adds r0, #8 str r0, [sp] vldr d16, [r7, #8] movw r0, :lower16:(L.str.2-(LPC20+4)) movt r0, :upper16:(L.str.2-(LPC2_0+4)) vmov r1, r2, d16 LPC2_0: add r0, pc blx _printf add sp, #4 pop.w {r7, lr} add sp, #8 bx lr

.globl _main .align 3 .code 16 @ @main .thumb_func _main _main: @ BB#0: push {r4, r5, r7, lr} add r7, sp, #8 sub sp, #4 vldr d16, LCPI30 movw r0, :lower16:(L.str-(LPC30+4)) movt r0, :upper16:(L.str-(LPC3_0+4)) movs r1, #2 vmov r4, r5, d16 LPC3_0: add r0, pc mov r2, r4 mov r3, r5 blx printf movw r0, :lower16:(L.str.1-(LPC31+4)) movs r1, #2 movt r0, :upper16:(L.str.1-(LPC3_1+4)) movs r2, #4 LPC3_1: add r0, pc mov r3, r4 str r5, [sp] blx _printf mov r2, r4 mov r3, r5 bl _func3 movs r0, #0 add sp, #4 pop {r4, r5, r7, pc} .align 3 @ BB#1: .data_region LCPI3_0: .long 858993459 @ double 5.2999999999999998 .long 1075131187 .end_data_region

.section TEXT,cstring,cstringliterals L.str: @ @.str .asciz "%d : %g\n"

L_.str.1: @ @.str.1 .asciz "%d : %d : %g\n"

L_.str.2: @ @.str.2 .asciz "%g\n"

.subsections_via_symbols

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/qtiuto/lua-for-android/issues/6#issuecomment-454959062, or mute the thread https://github.com/notifications/unsubscribe-auth/AJHwUjg3O3jtcROAMuoBrFXWA5AwKwRNks5vD5_8gaJpZM4aB3fT .

mingodad commented 5 years ago

Hello ! With the last commits applied it pass all in test.lua, now I'll look at the nexus7 and will tell you.

mingodad commented 5 years ago

On the nexus7 all tests in test.lua passed too. Congratulations ! Well done ! You are the king !

qtiuto commented 5 years ago

All right. I can sleep now. It's 6:00 a.m. in my time zone.

Domingo Alvarez Duarte notifications@github.com 于 2019年1月17日周四 06:21写道:

On the nexus7 all tests in test.lua passed too. Congratulations ! Well done ! You are the king !

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/qtiuto/lua-for-android/issues/6#issuecomment-454966560, or mute the thread https://github.com/notifications/unsubscribe-auth/AJHwUsaqE9asT8fzDo27IuC6HljTlo6qks5vD6YBgaJpZM4aB3fT .

mingodad commented 5 years ago

Good mornig/night ! I'm going to sleep to here is 23:26 in Spain. Cheers ! Again you are the King !