Skip to content

Cleanse identifier names to ensure callable from C runtime #100

@bushidocodes

Description

@bushidocodes

It's possible that we might ingest WASI functions exported with names that are not valid C identifiers.

For example, here is a test in the wasm spec test suite:

 (func (export "type-f64-value") (result f64)
    (block (result f64) (f64.neg (br_if 0 (f64.const 4) (i32.const 1))))
  )

This seems to generate valid LLVM bitcode with the same symbol:

; Function Attrs: nounwind
define double @wasmf_type-f64-value() #1 {
entry:
  br label %b_0

exit:                                             ; preds = %b_1
  ret double %0

b_0:                                              ; preds = %entry
  br i1 true, label %b_1, label %b_2

b_1:                                              ; preds = %b_2, %b_0
  %0 = phi double [ 4.000000e+00, %b_0 ], [ -4.000000e+00, %b_2 ]
  br label %exit

b_2:                                              ; preds = %b_0
  br label %b_1
}

However, this fails when we try to link with C runtime code:

dist/br_if_0.c:12:25: error: expected ';' after top level declarator
extern double wasmf_type-f64-value();
                        ^
                        ;

This is because C only supports ASCII characters, numbers, and underscores in symbol names.

Given the way that we resolve exported symbols, I suspect that we need to encode export names. I suggest replacing invalid symbols with ASCII char codes with leading and trailing underscores.

For example, wasmf_type-f64-value would be encoded as wasmf_type_45_f64_45_value

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