Skip to content

Instantly share code, notes, and snippets.

@gnh1201
Last active March 8, 2022 03:03
Show Gist options
  • Select an option

  • Save gnh1201/b9c4ee4a98fbb369237b3e447a9550a2 to your computer and use it in GitHub Desktop.

Select an option

Save gnh1201/b9c4ee4a98fbb369237b3e447a9550a2 to your computer and use it in GitHub Desktop.
Build the WASM runtime on CentOS 6.9/GLIBC 2.12

Build the WASM runtime on CentOS 6.9/GLIBC 2.12

1. Get virtual image

2. Change the repos to vault.centos.org

  • /etc/yum.repos.d/* - replace base URL to vault.centos.org

3. Update packages

  • yum update

4. Install 'Development Tools'

  • yum groupinstall 'Development Tools'

5. Install devtoolset-3(g++)

  1. curl http://linuxsoft.cern.ch/cern/scl/slc6-scl.repo > /etc/yum.repos.d/slc6-scl.repo
  2. /etc/yum.repos.d/slc6-scl.repo - replace gpgcheck=1 to gpgcheck=0
  3. yum update
  4. yum install devtoolset-3
  5. scl enable devtoolset-3 bash

6. Install openssl-devel

  1. yum install openssl-devel

7. Install CMake (latest)

  1. cd /usr/src
  2. wget https://github.com/Kitware/CMake/releases/download/v3.23.0-rc1/cmake-3.23.0-rc1.tar.gz
  3. tar xvfz cmake-3.23.0-rc1.tar.gz
  4. ./configure
  5. make
  6. make install

8. Clone wasm-micro-runtime and edit CMakeLists.txt

  1. git clone https://github.com/bytecodealliance/wasm-micro-runtime

  2. cd product-mini/platforms/linux/

  3. Add -lrt flag to CMakeList.txt

    set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security -Wshadow -lrt")
    
  4. Disable -mindirect-branch-register flag

    #set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mindirect-branch-register")
    

9. Build WAMR vmcore (for Linux)

cd product-mini/platforms/linux/
mkdir build
cd build
cmake .. -DWAMR_DISABLE_HW_BOUND_CHECK=1 -DWAMR_BUILD_REF_TYPES=1
make

See also: https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/doc/build_wamr.md

10. Check: ./iwasm

$ ./iwasm
Usage: iwasm [-options] wasm_file [args...]
options:
  -f|--function name     Specify a function name of the module to run rather
                         than main
  -v=n                   Set log verbose level (0 to 5, default is 2) larger
                         level with more log
  --stack-size=n         Set maximum stack size in bytes, default is 16 KB
  --heap-size=n          Set maximum heap size in bytes, default is 16 KB
  --repl                 Start a very simple REPL (read-eval-print-loop) mode
                         that runs commands in the form of "FUNC ARG..."
  --xip                  Enable XIP (Execution In Place) mode to run AOT file
                         generated with "--enable-indirect-mode" flag
  --env=<env>            Pass wasi environment variables with "key=value"
                         to the program, for example:
                           --env="key1=value1" --env="key2=value2"
  --dir=<dir>            Grant wasi access to the given host directories
                         to the program, for example:
                           --dir=<dir1> --dir=<dir2>

11. Complie test.c

  1. Download wasi-sdk: https://github.com/WebAssembly/wasi-sdk/releases

  2. Create test.c file

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    char *buf;

    printf("Hello world!\n");

    buf = malloc(1024);
    if (!buf) {
        printf("malloc buf failed\n");
        return -1;
    }

    printf("buf ptr: %p\n", buf);

    sprintf(buf, "%s", "1234\n");
    printf("buf: %s", buf);

    free(buf);
    return 0;
}

See also: https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/doc/build_wasm_app.md

  1. Complile to test.wasm
/opt/wasi-sdk/bin/clang -v test.c --sysroot=/opt/wasi-sdk/share/wasi-sysroot -o test.wasm

See also: WebAssembly/wasi-sdk#67

12. Done: run test.wasm

$ ./iwasm test.wasm
Hello world!
but ptr: 0x11500
buf: 1234

See also: Pre-compiled binary

https://github.com/gnh1201/iwasm-binary-glibc-2.12

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment