Skip to content

[WASM] Integer Overflow on 'size' after stat'ing  #22924

Open
@andrewmd5

Description

@andrewmd5

I've come up against a bit of a wall while porting Perl to WASM (using wasi-sdk/wasi-libc). I've gotten everything working, except for an issue with files over 2GB - when stat'ing them, the file size comes back wrong:

FileStat [WASI C# HOST]: /Users/andrew/Downloads/artifact-33/largefile 3221225472
-1073741824

The negative size (-1073741824) suggests we're hitting a 32-bit integer overflow, which is odd since the build is configured with both use64bitint=define and use64bitall=define. I've double-checked that ivsize is set to 8 and lseeksize is 8 in the config.

Before opening this issue I confirmed it wasn't an upstream issue in clang or wasi-libc. I'm attaching my full perl config output below.

I understand this may not be a priority issue since wasm32-wasi isn't an official target yet, but I'd appreciate any guidance on where I can look within Perl's source to potentially patch this, or if there are any specific flags in my configuration hint file I should try.

Full Configuration
Summary of my perl5 (revision 5 version 41 subversion 7) configuration:
   
  Platform:
    osname=wasi
    osvers=wasi25
    archname=wasm32-wasi
    uname='linux fv-az1253-838 6.5.0-1025-azure #26~22.04.1-ubuntu smp thu jul 11 22:33:04 utc 2024 x86_64 x86_64 x86_64 gnulinux '
    config_args='-sde -Dinc_version_list=none -Ddlsrc=none -Dloclibpth= -Dglibpth= -Dlns=/bin/ln -Dman1dir=none -Dman3dir=none -Dosname=wasi -Darchname=wasm32-wasi -Dosvers=wasi25 -Dmyhostname=objex.ai -Dmydomain=objex.ai -Dperladmin=root -Dcc=wasic -Dld=wasic -Dar=/opt/wasi-sdk/bin/llvm-ar -Dranlib=/opt/wasi-sdk/bin/llvm-ranlib -Doptimize=-O2 -Dlibs=-lm -Dhintfile=wasi -Dhostperl=/home/runner/work/test/test/wasm/../native/miniperl -Dhostgenerate=/home/runner/work/test/test/wasm/../native/generate_uudmap -Dprefix=/home/runner/work/test/test/wasm/prefix -Dsysroot=/opt/wasi-sdk/share/wasi-sysroot -Dusedevel -Dstatic_ext=mro Devel/Peek File/DosGlob File/Glob Sys/Syslog Sys/Hostname PerlIO/via PerlIO/mmap PerlIO/encoding B attributes Unicode/Normalize Unicode/Collate threads threads/shared IPC/SysV re Digest/MD5 Digest/SHA SDBM_File Math/BigInt/FastCalc Data/Dumper I18N/Langinfo Time/Piece IO Hash/Util/FieldHash Hash/Util Filter/Util/Call Encode/Unicode Encode Encode/JP Encode/KR Encode/EBCDIC Encode/CN Encode/Symbol Encode/Byte Encode/TW Compress/Raw/Zlib Compress/Raw/Bzip2 MIME/Base64 Cwd Storable List/Util Fcntl Opcode'
    hint=recommended
    useposix=true
    d_sigaction=undef
    useithreads=undef
    usemultiplicity=undef
    use64bitint=define
    use64bitall=define
    uselongdouble=undef
    usemymalloc=n
    default_inc_excludes_dot=define
  Compiler:
    cc='wasic'
    ccflags ='$ccflags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE -DNO_MATHOMS -mllvm -wasm-enable-sjlj -Wno-implicit-function-declaration -D_WASI_EMULATED_PROCESS_CLOCKS -lwasi-emulated-process-clocks -D_WASI_EMULATED_GETPID -lwasi-emulated-getpid -D_GNU_SOURCE -D_POSIX_C_SOURCE -Wno-null-pointer-arithmetic -D_WASI_EMULATED_SIGNAL -lwasi-emulated-signal -include /opt/wasi-sdk/share/wasi-sysroot/include/wasm32-wasi/fcntl.h -include /opt/wasi-sdk/share/wasi-sysroot/include/wasm32-wasi/__header_sys_stat.h -I/home/runner/work/test/test/stubs'
    optimize='-O2'
    cppflags='-lm -Wno-implicit-function-declaration -DNO_MATHOMS -D_WASI_EMULATED_PROCESS_CLOCKS -lwasi-emulated-process-clocks -D_WASI_EMULATED_GETPID -lwasi-emulated-getpid -D_LARGEFILE64_SOURCE -D_GNU_SOURCE -D_POSIX_C_SOURCE -DSTANDARD_C -DPERL_USE_SAFE_PUTENV -D_WASI_EMULATED_SIGNAL -lwasi-emulated-signal -Wno-null-pointer-arithmetic -fno-strict-aliasing -pipe -fstack-protector-strong -include /opt/wasi-sdk/share/wasi-sysroot/include/wasm32-wasi/fcntl.h -include /opt/wasi-sdk/share/wasi-sysroot/include/wasm32-wasi/__header_sys_stat.h -I/home/runner/work/test/test/stubs -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE -DNO_MATHOMS -Wno-implicit-function-declaration -D_WASI_EMULATED_PROCESS_CLOCKS -lwasi-emulated-process-clocks -D_WASI_EMULATED_GETPID -lwasi-emulated-getpid -D_GNU_SOURCE -D_POSIX_C_SOURCE -Wno-null-pointer-arithmetic -D_WASI_EMULATED_SIGNAL -lwasi-emulated-signal -include /opt/wasi-sdk/share/wasi-sysroot/include/wasm32-wasi/fcntl.h -include /opt/wasi-sdk/share/wasi-sysroot/include/wasm32-wasi/__header_sys_stat.h -I/home/runner/work/test/test/stubs'
    ccversion=''
    gccversion='Ubuntu Clang 14.0.0'
    gccosandvers=''
    intsize=4
    longsize=8
    ptrsize=8
    doublesize=8
    byteorder=1234
    doublekind=3
    d_longlong=define
    longlongsize=8
    d_longdbl=define
    longdblsize=16
    longdblkind=3
    ivtype='long'
    ivsize=8
    nvtype='double'
    nvsize=8
    Off_t='off_t'
    lseeksize=8
    alignbytes=8
    prototype=define
  Linker and Libraries:
    ld='ld'
    ldflags ='-static -mllvm -wasm-enable-sjlj -lwasi-emulated-signal -lwasi-emulated-getpid -lwasi-emulated-process-clocks -lwasi-emulated-mman'
    libpth=/opt/wasi-sdk/share/wasi-sysroot/lib
    libs=-lm
    perllibs=-lm
    libc=
    so=so
    useshrplib=false
    libperl=libperl.a
    gnulibc_version='2.35'
  Dynamic Linking:
    dlsrc=dl_none.xs
    dlext=none
    d_dlsymun=undef
    ccdlflags=''
    cccdlflags=''
    lddlflags=''


Characteristics of this binary (from libperl): 
  Compile-time options:
    HAS_LONG_DOUBLE
    HAS_STRTOLD
    NO_MATHOMS
    PERLIO_LAYERS
    PERL_COPY_ON_WRITE
    PERL_DONT_CREATE_GVSV
    PERL_HASH_FUNC_SIPHASH13
    PERL_HASH_USE_SBOX32
    PERL_MALLOC_WRAP
    PERL_OP_PARENT
    PERL_PRESERVE_IVUV
    PERL_USES_PL_PIDSTATUS
    PERL_USE_DEVEL
    PERL_USE_SAFE_PUTENV
    USE_64_BIT_ALL
    USE_64_BIT_INT
    USE_LARGE_FILES
    USE_LOCALE
    USE_LOCALE_COLLATE
    USE_LOCALE_CTYPE
    USE_LOCALE_NUMERIC
    USE_LOCALE_TIME
    USE_PERLIO
    USE_PERL_ATOF
  Built under wasi
  Compiled at Jan 16 2025 08:07:40
  @INC:
   /prefix/lib/perl5/5.41.7/wasm32-wasi

While the above configuration says 5.41.7, I've also tested on 5.40.0 and the issue persist

The only other relevant issue I could find was this one from 1999.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions