Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions Configurations/15-wasm.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
## -*- mode: perl; -*-
## Build configuration for WebAssembly (WASM) platforms

my %targets = (
# wasm32-wasip1 target for WebAssembly System Interface (WASI) Preview 1
# This configuration is intended for building OpenSSL as a static library
# that can be used in WebAssembly environments with WASI support.
#
# Prerequisites:
# - WASI SDK installed and available in PATH
# - wasm32-wasip1-clang (or wasm32-wasi-clang) compiler
#
# Usage:
# ./Configure wasm32-wasip1 no-threads no-shared no-asm \
# --prefix=/path/to/install
#
# Note: The no-threads, no-shared, and no-asm flags are recommended
# for WASM builds as these features are typically not available or
# not well-supported in WebAssembly environments.

"wasm32-wasip1" => {
inherit_from => [ "BASE_unix" ],
CC => "wasm32-wasip1-clang",
CXX => "wasm32-wasip1-clang++",
CFLAGS => picker(default => "-Wall",
debug => "-O0 -g",
release => "-O3"),
CXXFLAGS => picker(default => "-Wall",
debug => "-O0 -g",
release => "-O3"),
cflags => "",
cxxflags => "-std=c++11",
bn_ops => "BN_LLONG",
sys_id => "WASM",
thread_scheme => "(unknown)",
dso_scheme => "none",
perlasm_scheme => "void",
disable => [ "threads", "shared", "asm", "dso", "engine", "dynamic-engine" ],
# WASM doesn't support shared libraries or dynamic loading
shared_target => "",
shared_cflag => "",
shared_ldflag => "",
AR => "llvm-ar",
ARFLAGS => "rc",
RANLIB => "llvm-ranlib",
},

# Alternative target name for compatibility
"wasm32-wasi" => {
inherit_from => [ "wasm32-wasip1" ],
CC => "wasm32-wasi-clang",
CXX => "wasm32-wasi-clang++",
},
);
199 changes: 199 additions & 0 deletions NOTES-WASM.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
NOTES FOR THE WEBASSEMBLY (WASM) PLATFORMS
===========================================

WebAssembly (WASM) is a binary instruction format for a stack-based virtual
machine. WASI (WebAssembly System Interface) is a system interface for
WebAssembly that provides a standardized API for accessing operating system
features.

Targets
-------

OpenSSL supports the following WebAssembly targets:

- `wasm32-wasip1` - WebAssembly 32-bit target using WASI Preview 1
- `wasm32-wasi` - Alternative name for wasm32-wasip1

Prerequisites
-------------

To build OpenSSL for WebAssembly, you need the WASI SDK:

1. Download and install WASI SDK from:
https://github.com/WebAssembly/wasi-sdk/releases

2. Add the WASI SDK bin directory to your PATH:
```
export WASI_SDK_PATH=/path/to/wasi-sdk
export PATH=$WASI_SDK_PATH/bin:$PATH
```

3. Verify the compiler is available:
```
wasm32-wasip1-clang --version
```
or
```
wasm32-wasi-clang --version
```

Building
--------

Basic build configuration for wasm32-wasip1:

$ ./Configure wasm32-wasip1 \
no-threads \
no-shared \
no-asm \
--prefix=/path/to/install \
--openssldir=/path/to/openssldir

Then build with:

$ make
$ make install

Notes:

- `no-threads`: WASM environments typically don't support threading
- `no-shared`: WASM doesn't support shared libraries
- `no-asm`: Assembly optimizations are not available for WASM
- `no-dso`: Dynamic loading is not supported (automatically set)
- `no-engine`: Engine support requires dynamic loading (automatically set)

Configuration Options
---------------------

You may want to disable additional features that are not needed for your
WASM application to reduce the size of the resulting library:

$ ./Configure wasm32-wasip1 \
no-threads no-shared no-asm \
no-sock no-dgram no-ui-console \
--prefix=/path/to/install

Common additional options:
- `no-sock`: Disable socket support
- `no-dgram`: Disable datagram support
- `no-ui-console`: Disable UI console functions
- `no-stdio`: Disable stdio functions
- `no-deprecated`: Disable deprecated APIs

For a minimal build focused on cryptographic operations:

$ ./Configure wasm32-wasip1 \
no-threads no-shared no-asm \
no-sock no-dgram no-ui-console \
no-engine no-hw no-apps \
--prefix=/path/to/install

Using the WASM Library
----------------------

After building, you'll have static libraries (libcrypto.a and libssl.a) that
can be linked with your WASM application.

To compile and link your WASM application:

$ wasm32-wasip1-clang -O3 \
-I/path/to/install/include \
-L/path/to/install/lib \
-o myapp.wasm \
myapp.c \
-lssl -lcrypto

Running WASM Applications
--------------------------

WASM applications can be run using various runtimes:

1. Wasmtime:
```
wasmtime myapp.wasm
```

2. Wasmer:
```
wasmer run myapp.wasm
```

3. Node.js with WASI support:
```javascript
const { WASI } = require('wasi');
const fs = require('fs');

const wasi = new WASI({
args: process.argv,
env: process.env,
});

const importObject = { wasi_snapshot_preview1: wasi.wasiImport };

WebAssembly.instantiate(
fs.readFileSync('./myapp.wasm'),
importObject
).then((obj) => {
wasi.start(obj.instance);
});
```

Troubleshooting
---------------

### Compiler not found

If you get an error about `wasm32-wasip1-clang` not being found:

1. Verify WASI SDK is installed
2. Check that the bin directory is in your PATH
3. Try using `wasm32-wasi-clang` instead (use the `wasm32-wasi` target)

### Linking errors

If you encounter linking errors:

1. Ensure you're using the matching WASI SDK version for all components
2. Check that all required libraries are linked in the correct order
3. Verify that no forbidden features (threads, shared libs) are enabled

### Size concerns

If the resulting WASM binary is too large:

1. Use release build configuration (`-O3` optimization)
2. Disable unneeded OpenSSL features during configuration
3. Use `wasm-opt` from the Binaryen toolkit to optimize the WASM binary:
```
wasm-opt -O3 myapp.wasm -o myapp-optimized.wasm
```
4. Strip debug information:
```
wasm32-wasip1-strip myapp.wasm
```

### Runtime errors

If you encounter errors when running the WASM application:

1. Ensure the WASM runtime supports the required WASI features
2. Check that sufficient memory is allocated for cryptographic operations
3. Verify that any required files/certificates are accessible to the WASM
environment

Limitations
-----------

When using OpenSSL in WASM environments, be aware of the following limitations:

1. No hardware acceleration: Assembly optimizations are disabled
2. No threading: Multi-threaded operations are not supported
3. No dynamic loading: Engines and dynamic libraries cannot be loaded
4. Limited I/O: Some file system and network operations may be restricted
depending on the WASM runtime environment
5. Memory constraints: WASM environments may have memory limitations

For more information about WASI and WebAssembly, see:
- https://wasi.dev/
- https://webassembly.org/
- https://github.com/WebAssembly/wasi-sdk
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ document.
* [Notes for UNIX-like platforms](NOTES-UNIX.md)
* [Notes for Android platforms](NOTES-ANDROID.md)
* [Notes for Windows platforms](NOTES-WINDOWS.md)
* [Notes for WebAssembly platforms](NOTES-WASM.md)
* [Notes for the DOS platform with DJGPP](NOTES-DJGPP.md)
* [Notes for the OpenVMS platform](NOTES-VMS.md)
* [Notes on Perl](NOTES-PERL.md)
Expand Down
126 changes: 126 additions & 0 deletions build-wasm32-wasip1.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#!/usr/bin/env bash
# Example build script for OpenSSL on wasm32-wasip1
# This script demonstrates how to build OpenSSL for WebAssembly
# using the WASI SDK.
#
# Prerequisites:
# - WASI SDK installed (https://github.com/WebAssembly/wasi-sdk/releases)
# - Perl 5 or later
# - Make
#
# Usage:
# ./build-wasm32-wasip1.sh [install-prefix]
#
# Example:
# ./build-wasm32-wasip1.sh /tmp/openssl-wasm

set -e # Exit on error

# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Configuration
INSTALL_PREFIX="${1:-/tmp/openssl-wasm}"
OPENSSLDIR="${INSTALL_PREFIX}/ssl"

echo -e "${GREEN}OpenSSL WASM32-wasip1 Build Script${NC}"
echo "=================================="
echo ""

# Check if WASI SDK is available
if ! command -v wasm32-wasip1-clang &> /dev/null && ! command -v wasm32-wasi-clang &> /dev/null; then
echo -e "${RED}Error: WASI SDK not found!${NC}"
echo ""
echo "Please install WASI SDK from:"
echo " https://github.com/WebAssembly/wasi-sdk/releases"
echo ""
echo "Then add it to your PATH:"
echo " export WASI_SDK_PATH=/path/to/wasi-sdk"
echo " export PATH=\$WASI_SDK_PATH/bin:\$PATH"
exit 1
fi

# Determine which compiler to use
if command -v wasm32-wasip1-clang &> /dev/null; then
TARGET="wasm32-wasip1"
COMPILER="wasm32-wasip1-clang"
else
TARGET="wasm32-wasi"
COMPILER="wasm32-wasi-clang"
fi

echo -e "${GREEN}Using compiler:${NC} $COMPILER"
echo -e "${GREEN}Install prefix:${NC} $INSTALL_PREFIX"
echo -e "${GREEN}OpenSSL dir:${NC} $OPENSSLDIR"
echo ""

# Clean any previous build
echo -e "${YELLOW}Cleaning previous build...${NC}"
if [ -f Makefile ]; then
make distclean 2>/dev/null || true
fi

# Configure OpenSSL for WASM
echo -e "${YELLOW}Configuring OpenSSL for ${TARGET}...${NC}"
./Configure "$TARGET" \
--prefix="$INSTALL_PREFIX" \
--openssldir="$OPENSSLDIR" \
no-threads \
no-shared \
no-asm \
no-sock \
no-ui-console

echo ""
echo -e "${GREEN}Configuration complete!${NC}"
echo ""

# Display configuration
echo -e "${YELLOW}Configuration summary:${NC}"
perl configdata.pm --dump | grep -E "target =|CC =|disable =|prefix =|openssldir =" || true
echo ""

# Build
echo -e "${YELLOW}Building OpenSSL...${NC}"
make -j$(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || getconf _NPROCESSORS_ONLN 2>/dev/null || echo 1)

echo ""
echo -e "${GREEN}Build complete!${NC}"
echo ""

# Optional: Run tests (some tests may not work in WASM environment)
read -p "Do you want to run tests? (y/N) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo -e "${YELLOW}Running tests...${NC}"
make test || echo -e "${YELLOW}Warning: Some tests may fail in WASM environment${NC}"
fi

# Install
echo ""
read -p "Do you want to install to $INSTALL_PREFIX? (y/N) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo -e "${YELLOW}Installing OpenSSL...${NC}"
make install
echo ""
echo -e "${GREEN}Installation complete!${NC}"
echo ""
echo "OpenSSL has been installed to:"
echo " Libraries: $INSTALL_PREFIX/lib"
echo " Headers: $INSTALL_PREFIX/include"
echo ""
echo "To use in your WASM project:"
echo " ${COMPILER} -O3 \\"
echo " -I$INSTALL_PREFIX/include \\"
echo " -L$INSTALL_PREFIX/lib \\"
echo " -o myapp.wasm \\"
echo " myapp.c \\"
echo " -lssl -lcrypto"
fi

echo ""
echo -e "${GREEN}Done!${NC}"
Loading