Skip to content

Commit

Permalink
Fix memory, update tests. fix constant parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik McClure committed Jun 8, 2019
1 parent b6ad1e0 commit 4c242eb
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 21 deletions.
2 changes: 1 addition & 1 deletion include/innative/innative.h
Expand Up @@ -19,7 +19,7 @@ limitations under the License.

#define INNATIVE_VERSION_MAJOR 0
#define INNATIVE_VERSION_MINOR 1
#define INNATIVE_VERSION_REVISION 0
#define INNATIVE_VERSION_REVISION 1
#define INNATIVE_VERSION(v, m, r, b) (((v|0ULL)<<48) | ((m|0ULL)<<32) | ((r|0ULL)<<16) | (b|0ULL))

// CPU Architecture (possible pre-defined macros found on http://predef.sourceforge.net/prearch.html)
Expand Down
2 changes: 2 additions & 0 deletions innative-env/internal.c
Expand Up @@ -69,6 +69,8 @@ IN_COMPILER_DLLEXPORT extern IN_COMPILER_NAKED void* _innative_syscall(size_t sy
#else
"movcc pc, lr\n\t"
#endif
);
}
#else
#error unsupported architecture!
#endif
Expand Down
2 changes: 1 addition & 1 deletion innative-runtime-setup/Product.wxs
Expand Up @@ -10,7 +10,7 @@
<?error Unknown value for Platform variable ?>
<?endif ?>

<?define Version="0.1.0" ?>
<?define Version="0.1.1" ?>
<Product
Id="6A27F179-BADF-4580-821D-2892F229F335"
Name="inNative Runtime v$(var.Version)"
Expand Down
2 changes: 1 addition & 1 deletion innative-sdk-setup/Product.wxs
Expand Up @@ -10,7 +10,7 @@
<?error Unknown value for Platform variable ?>
<?endif ?>

<?define Version="0.1.0" ?>
<?define Version="0.1.1" ?>
<Product
Id="25A4AE98-4F30-4735-B29B-37B6EDC5A9E9"
Name="inNative SDK v$(var.Version)"
Expand Down
27 changes: 19 additions & 8 deletions innative/lexer.cpp
Expand Up @@ -105,11 +105,16 @@ namespace innative {
}
s += i;

while(s < end && isxdigit(*s)) ++s;

if(target)
target->assign(begin + 3 + i, s - begin - 3 - i);

while(s < end && (*s == '_' || isxdigit(*s)))
{
if(target && *s != '_')
target->append(1, *s);
++s;
}

return s;
}

Expand Down Expand Up @@ -155,12 +160,12 @@ namespace innative {
if(strncasecmp(p, "inf", 3) != 0)
return ERR_WAT_OUT_OF_RANGE;
}
if(std::is_same<float, T>::value && fabs(out) <= 1.1754942e-38f) // glibc incorrectly considers subnormal numbers as underflow and sets ERANGE in this case
errno = 0;
if(std::is_same<double, T>::value && fabs(out) <= 2.2250738585072012e-308)
errno = 0;
}
#endif
if(std::is_same<float, T>::value && fabs(out) <= 1.1754942e-38f) // WebAssembly never considers an underflow to be a range error, it rounds to zero
errno = 0;
if(std::is_same<double, T>::value && fabs(out) <= 2.2250738585072012e-308)
errno = 0;
if(errno == ERANGE)
return ERR_WAT_OUT_OF_RANGE;
// assert(!(errno != 0 || (end - numbuf.c_str()) != length));
Expand All @@ -174,7 +179,10 @@ namespace innative {
numbuf.assign("400000"); // Hex for the first bit in the mantissa
if(CheckTokenNAN(token.pos, token.pos + token.len, &numbuf))
{
union { uint32_t i; float f; } u = { 0x7F800000U | strtoul(numbuf.c_str(), &last, 16) };
auto mantissa = strtoul(numbuf.c_str(), &last, 16);
if(mantissa < 0x1 || mantissa > 0x7fffff)
return ERR_WAT_OUT_OF_RANGE;
union { uint32_t i; float f; } u = { 0x7F800000U | mantissa };
if(token.pos[0] == '-')
u.i |= 0x80000000U;

Expand All @@ -197,7 +205,10 @@ namespace innative {
numbuf.assign("8000000000000"); // Hex for the first bit in the mantissa
if(CheckTokenNAN(token.pos, token.pos + token.len, &numbuf))
{
union { uint64_t i; double f; } u = { 0x7FF0000000000000ULL | strtoull(numbuf.c_str(), &last, 16) };
auto mantissa = strtoull(numbuf.c_str(), &last, 16);
if(mantissa < 0x1 || mantissa > 0xfffffffffffff)
return ERR_WAT_OUT_OF_RANGE;
union { uint64_t i; double f; } u = { 0x7FF0000000000000ULL | mantissa };
if(token.pos[0] == '-')
u.i |= 0x8000000000000000ULL;

Expand Down
2 changes: 1 addition & 1 deletion innative/parse.cpp
Expand Up @@ -311,7 +311,7 @@ IN_ERROR innative::ParseInstruction(Stream& s, Instruction& ins, const Environme
ins.immediates[0]._varuint32 = s.ReadVarUInt32(err);

if(err >= 0)
ins.immediates[1]._varuptr = s.ReadVarUInt64(err);
ins.immediates[1]._varuptr = s.ReadVarUInt32(err); // Currently 32-bit because all memories are 32-bit

break;
case OP_unreachable:
Expand Down
6 changes: 3 additions & 3 deletions innative/tools.cpp
Expand Up @@ -40,7 +40,10 @@ Environment* innative::CreateEnvironment(unsigned int modules, unsigned int maxt
env->loglevel = LOG_WARNING;
env->libpath = utility::AllocString(*env, GetProgramPath(arg0).BaseDir().Get());
if(!env->libpath) // Out of memory
{
free(env);
return nullptr;
}

env->objpath = 0;
env->system = "";
Expand All @@ -65,12 +68,9 @@ void innative::DestroyEnvironment(Environment* env)
return;

ClearEnvironmentCache(env, 0);

for(varuint32 i = 0; i < env->n_modules; ++i)
{
kh_destroy_exports(env->modules[i].exports);
if(env->modules[i].importsection.imports)
free(env->modules[i].importsection.imports);
assert(!env->modules[i].cache);
}

Expand Down
2 changes: 1 addition & 1 deletion innative/wat.cpp
Expand Up @@ -293,7 +293,7 @@ int WatParser::AppendImport(Module& m, const Import& i, varuint32* index)
return ERR_WAT_INVALID_IMPORT_ORDER; // If we're trying to insert an import after declaring a table/func/global/memory, fail.

*index = 0;
if(!(m.importsection.imports = trealloc<Import>(m.importsection.imports, ++m.importsection.n_import)))
if(ReallocArray(env, m.importsection.imports, m.importsection.n_import) != ERR_SUCCESS)
return ERR_FATAL_OUT_OF_MEMORY;

// Find the correct index to insert into
Expand Down
18 changes: 14 additions & 4 deletions innative/wat.h
Expand Up @@ -52,16 +52,16 @@ namespace innative {
int ParseElemData(Queue<WatToken>& tokens, varuint32& index, Instruction& op, wat::kh_indexname_t* hash);
int ParseElem(TableInit& e, Queue<WatToken>& tokens);
int ParseData(Queue<WatToken>& tokens);
int AppendImport(Module& m, const Import& i, varuint32* index);
int InlineImportExport(const Environment& env, Module& m, Queue<WatToken>& tokens, varuint32* index, varuint7 kind, Import** out);

static int ParseBlockType(Queue<WatToken>& tokens, varsint7& out);
static int ParseModule(Environment& env, Module& m, Queue<WatToken>& tokens, utility::StringRef name, WatToken& internalname);
static int ParseName(const Environment& env, ByteArray& name, const WatToken& t);
static int AppendImport(Module& m, const Import& i, varuint32* index);
static int AddWatValType(const Environment& env, WatTokenID id, varsint7*& a, varuint32& n);
static int WatString(const Environment& env, ByteArray& str, utility::StringRef t);
static void WriteUTF32(uint32_t ch, ByteArray& str, varuint32& index);
static varsint7 WatValType(WatTokenID id);
static int InlineImportExport(const Environment& env, Module& m, Queue<WatToken>& tokens, varuint32* index, varuint7 kind, Import** out);
static int ParseLocalAppend(const Environment& env, FunctionBody& body, Queue<WatToken>& tokens);
static int AddName(wat::kh_indexname_t* h, WatToken t, varuint32 index);

Expand All @@ -78,7 +78,7 @@ namespace innative {
}

template<class T>
inline static int AppendArray(const Environment& env, T item, T*& a, varuint32& n)
inline static int ReallocArray(const Environment& env, T*& a, varuint32& n)
{
// We only allocate power of two chunks from our greedy allocator
varuint32 i = utility::NextPow2(n++);
Expand All @@ -87,9 +87,19 @@ namespace innative {
T* old = a;
if(!(a = utility::tmalloc<T>(env, n * 2)))
return ERR_FATAL_OUT_OF_MEMORY;
utility::tmemcpy<T>(a, n * 2, old, n - 1); // Don't free old because it was from a greedy allocator.
if(old != nullptr)
utility::tmemcpy<T>(a, n * 2, old, n - 1); // Don't free old because it was from a greedy allocator.
}

return ERR_SUCCESS;
}

template<class T>
inline static int AppendArray(const Environment& env, T item, T*& a, varuint32& n)
{
int err;
if((err = ReallocArray(env, a, n)) != ERR_SUCCESS)
return err;
a[n - 1] = item;
return ERR_SUCCESS;
}
Expand Down
2 changes: 1 addition & 1 deletion spec
Submodule spec updated 42 files
+6 −1 document/core/appendix/custom.rst
+195 −190 document/core/appendix/embedding.rst
+4 −4 document/core/appendix/implementation.rst
+1 −0 document/core/appendix/index-rules.rst
+1 −1 document/core/appendix/index-types.rst
+1 −0 document/core/binary/modules.rst
+1 −1 document/core/binary/values.rst
+2 −1 document/core/conf.py
+11 −6 document/core/exec/instructions.rst
+9 −5 document/core/exec/modules.rst
+16 −16 document/core/exec/numerics.rst
+5 −5 document/core/exec/runtime.rst
+1 −1 document/core/index.bs
+6 −1 document/core/index.rst
+17 −2 document/core/intro/introduction.rst
+1 −1 document/core/syntax/instructions.rst
+4 −0 document/core/syntax/modules.rst
+6 −6 document/core/syntax/values.rst
+1 −1 document/core/text/conventions.rst
+2 −2 document/core/text/lexical.rst
+2 −2 document/core/text/values.rst
+2 −2 document/core/util/bikeshed/conf.py
+11 −1 document/core/util/bikeshed_fixup.py
+37 −34 document/core/util/macros.def
+1 −1 document/core/util/mathjax2katex.py
+65 −0 document/core/valid/types.rst
+60 −53 document/js-api/index.bs
+1 −1 interpreter/Makefile
+4 −3 interpreter/exec/f32.ml
+21 −15 interpreter/exec/f32_convert.ml
+4 −3 interpreter/exec/f64.ml
+6 −5 interpreter/exec/f64_convert.ml
+121 −10 interpreter/exec/float.ml
+3 −3 interpreter/exec/i64_convert.ml
+25 −8 interpreter/exec/int.ml
+8 −0 interpreter/util/lib.ml
+1 −0 interpreter/util/lib.mli
+8 −0 test/core/align.wast
+963 −0 test/core/binary-leb128.wast
+1 −244 test/core/binary.wast
+665 −0 test/core/const.wast
+10 −0 test/core/conversions.wast

0 comments on commit 4c242eb

Please sign in to comment.