@@ -23,6 +23,68 @@ bool BytecodeUtil::checkWasmHeader(std::string_view bytecode) {
2323 return bytecode.size () < 8 || !::memcmp (bytecode.data (), wasm_magic_number, 4 );
2424}
2525
26+ bool BytecodeUtil::getAbiVersion (std::string_view bytecode, proxy_wasm::AbiVersion &ret) {
27+ ret = proxy_wasm::AbiVersion::Unknown;
28+ // Check Wasm header.
29+ if (!checkWasmHeader (bytecode)) {
30+ return false ;
31+ }
32+ // Skip the Wasm header.
33+ const char *pos = bytecode.data () + 8 ;
34+ const char *end = bytecode.data () + bytecode.size ();
35+ while (pos < end) {
36+ if (pos + 1 > end) {
37+ return false ;
38+ }
39+ const auto section_type = *pos++;
40+ uint32_t section_len = 0 ;
41+ if (!parseVarint (pos, end, section_len) || pos + section_len > end) {
42+ return false ;
43+ }
44+ if (section_type == 7 /* export section */ ) {
45+ uint32_t export_vector_size = 0 ;
46+ if (!parseVarint (pos, end, export_vector_size) || pos + export_vector_size > end) {
47+ return false ;
48+ }
49+ // Search thourgh exports.
50+ for (uint32_t i = 0 ; i < export_vector_size; i++) {
51+ // Parse name of the export.
52+ uint32_t export_name_size = 0 ;
53+ if (!parseVarint (pos, end, export_name_size) || pos + export_name_size > end) {
54+ return false ;
55+ }
56+ const std::string export_name = {pos, export_name_size};
57+ pos += export_name_size;
58+ if (pos + 1 > end) {
59+ return false ;
60+ }
61+ // Check if it is a function type export
62+ if (*pos++ == 0x00 ) {
63+ // Check the name of the function.
64+ if (export_name == " proxy_abi_version_0_1_0" ) {
65+ ret = AbiVersion::ProxyWasm_0_1_0;
66+ return true ;
67+ } else if (export_name == " proxy_abi_version_0_2_0" ) {
68+ ret = AbiVersion::ProxyWasm_0_2_0;
69+ return true ;
70+ } else if (export_name == " proxy_abi_version_0_2_1" ) {
71+ ret = AbiVersion::ProxyWasm_0_2_1;
72+ return true ;
73+ }
74+ }
75+ // Skip export's index.
76+ if (!parseVarint (pos, end, export_name_size)) {
77+ return false ;
78+ }
79+ }
80+ return false ;
81+ } else {
82+ pos += section_len;
83+ }
84+ }
85+ return true ;
86+ }
87+
2688bool BytecodeUtil::getCustomSection (std::string_view bytecode, std::string_view name,
2789 std::string_view &ret) {
2890 // Check Wasm header.
0 commit comments