wasm-metering
Version:
injects metering into webassembly binaries
245 lines (201 loc) • 6.37 kB
Plain Text
/*
* Copyright 2016 WebAssembly Community Group participants
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* TODO(binji): nice way to define these with WASM_ prefix? */
/* Whether <alloca.h> is available */
#cmakedefine01 HAVE_ALLOCA_H
/* Whether <unistd.h> is available */
#cmakedefine01 HAVE_UNISTD_H
/* Whether snprintf is defined by stdio.h */
#cmakedefine01 HAVE_SNPRINTF
/* Whether sysconf is defined by unistd.h */
#cmakedefine01 HAVE_SYSCONF
/* Whether ssize_t is defined by stddef.h */
#cmakedefine01 HAVE_SSIZE_T
/* Whether strcasecmp is defined by strings.h */
#cmakedefine01 HAVE_STRCASECMP
#cmakedefine01 COMPILER_IS_CLANG
#cmakedefine01 COMPILER_IS_GNU
#cmakedefine01 COMPILER_IS_MSVC
__inline unsigned long wasm_clz_u32(unsigned long mask) {
unsigned long index;
_BitScanReverse(&index, mask);
return sizeof(unsigned long) * 8 - (index + 1);
}
__inline unsigned long wasm_clz_u64(unsigned __int64 mask) {
unsigned long index;
_BitScanReverse64(&index, mask);
return sizeof(unsigned __int64) * 8 - (index + 1);
unsigned long index;
unsigned long high_mask;
memcpy(&high_mask, (unsigned char*)&mask + sizeof(unsigned long),
sizeof(unsigned long));
if (_BitScanReverse(&index, high_mask)) {
return sizeof(unsigned long) * 8 - (index + 1);
}
unsigned long low_mask;
memcpy(&low_mask, &mask, sizeof(unsigned long));
_BitScanReverse(&index, low_mask);
return sizeof(unsigned __int64) * 8 - (index + 1);
}
__inline unsigned long wasm_ctz_u32(unsigned long mask) {
unsigned long index;
_BitScanForward(&index, mask);
return index;
}
__inline unsigned long wasm_ctz_u64(unsigned __int64 mask) {
unsigned long index;
_BitScanForward64(&index, mask);
return index;
unsigned long low_mask = (unsigned long)mask;
if (low_mask) {
return wasm_ctz_u32(low_mask);
}
unsigned long high_mask;
memcpy(&high_mask, (unsigned char*)&mask + sizeof(unsigned long),
sizeof(unsigned long));
return sizeof(unsigned long) * 8 + wasm_ctz_u32(high_mask);
}
__inline unsigned __int64 __popcnt64(unsigned __int64 value) {
unsigned long high_value;
unsigned long low_value;
memcpy(&high_value, (unsigned char*)&value + sizeof(unsigned long),
sizeof(unsigned long));
memcpy(&low_value, &value, sizeof(unsigned long));
return wasm_popcount_u32(high_value) + wasm_popcount_u32(low_value);
}
/* Check For MINGW first, because it is also a GNU compiler */
/* print format specifier for size_t */
/* print format specifier for size_t */
/* can't just use _snprintf because it doesn't always null terminate */
int wasm_snprintf(char* str, size_t size, const char* format, ...);
/* can't just use vsnprintf because it doesn't always null terminate */
int wasm_vsnprintf(char* str, size_t size, const char* format, va_list ap);
typedef int ssize_t;