Skip to content

Commit 4c242eb

Browse files
author
Erik McClure
committed
Fix memory, update tests. fix constant parsing
1 parent b6ad1e0 commit 4c242eb

File tree

10 files changed

+44
-21
lines changed

10 files changed

+44
-21
lines changed

include/innative/innative.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ limitations under the License.
1919

2020
#define INNATIVE_VERSION_MAJOR 0
2121
#define INNATIVE_VERSION_MINOR 1
22-
#define INNATIVE_VERSION_REVISION 0
22+
#define INNATIVE_VERSION_REVISION 1
2323
#define INNATIVE_VERSION(v, m, r, b) (((v|0ULL)<<48) | ((m|0ULL)<<32) | ((r|0ULL)<<16) | (b|0ULL))
2424

2525
// CPU Architecture (possible pre-defined macros found on http://predef.sourceforge.net/prearch.html)

innative-env/internal.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ IN_COMPILER_DLLEXPORT extern IN_COMPILER_NAKED void* _innative_syscall(size_t sy
6969
#else
7070
"movcc pc, lr\n\t"
7171
#endif
72+
);
73+
}
7274
#else
7375
#error unsupported architecture!
7476
#endif

innative-runtime-setup/Product.wxs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<?error Unknown value for Platform variable ?>
1111
<?endif ?>
1212

13-
<?define Version="0.1.0" ?>
13+
<?define Version="0.1.1" ?>
1414
<Product
1515
Id="6A27F179-BADF-4580-821D-2892F229F335"
1616
Name="inNative Runtime v$(var.Version)"

innative-sdk-setup/Product.wxs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<?error Unknown value for Platform variable ?>
1111
<?endif ?>
1212

13-
<?define Version="0.1.0" ?>
13+
<?define Version="0.1.1" ?>
1414
<Product
1515
Id="25A4AE98-4F30-4735-B29B-37B6EDC5A9E9"
1616
Name="inNative SDK v$(var.Version)"

innative/lexer.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,16 @@ namespace innative {
105105
}
106106
s += i;
107107

108-
while(s < end && isxdigit(*s)) ++s;
109-
110108
if(target)
111109
target->assign(begin + 3 + i, s - begin - 3 - i);
112110

111+
while(s < end && (*s == '_' || isxdigit(*s)))
112+
{
113+
if(target && *s != '_')
114+
target->append(1, *s);
115+
++s;
116+
}
117+
113118
return s;
114119
}
115120

@@ -155,12 +160,12 @@ namespace innative {
155160
if(strncasecmp(p, "inf", 3) != 0)
156161
return ERR_WAT_OUT_OF_RANGE;
157162
}
158-
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
159-
errno = 0;
160-
if(std::is_same<double, T>::value && fabs(out) <= 2.2250738585072012e-308)
161-
errno = 0;
162163
}
163164
#endif
165+
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
166+
errno = 0;
167+
if(std::is_same<double, T>::value && fabs(out) <= 2.2250738585072012e-308)
168+
errno = 0;
164169
if(errno == ERANGE)
165170
return ERR_WAT_OUT_OF_RANGE;
166171
// assert(!(errno != 0 || (end - numbuf.c_str()) != length));
@@ -174,7 +179,10 @@ namespace innative {
174179
numbuf.assign("400000"); // Hex for the first bit in the mantissa
175180
if(CheckTokenNAN(token.pos, token.pos + token.len, &numbuf))
176181
{
177-
union { uint32_t i; float f; } u = { 0x7F800000U | strtoul(numbuf.c_str(), &last, 16) };
182+
auto mantissa = strtoul(numbuf.c_str(), &last, 16);
183+
if(mantissa < 0x1 || mantissa > 0x7fffff)
184+
return ERR_WAT_OUT_OF_RANGE;
185+
union { uint32_t i; float f; } u = { 0x7F800000U | mantissa };
178186
if(token.pos[0] == '-')
179187
u.i |= 0x80000000U;
180188

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

innative/parse.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ IN_ERROR innative::ParseInstruction(Stream& s, Instruction& ins, const Environme
311311
ins.immediates[0]._varuint32 = s.ReadVarUInt32(err);
312312

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

316316
break;
317317
case OP_unreachable:

innative/tools.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ Environment* innative::CreateEnvironment(unsigned int modules, unsigned int maxt
4040
env->loglevel = LOG_WARNING;
4141
env->libpath = utility::AllocString(*env, GetProgramPath(arg0).BaseDir().Get());
4242
if(!env->libpath) // Out of memory
43+
{
44+
free(env);
4345
return nullptr;
46+
}
4447

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

6770
ClearEnvironmentCache(env, 0);
68-
6971
for(varuint32 i = 0; i < env->n_modules; ++i)
7072
{
7173
kh_destroy_exports(env->modules[i].exports);
72-
if(env->modules[i].importsection.imports)
73-
free(env->modules[i].importsection.imports);
7474
assert(!env->modules[i].cache);
7575
}
7676

innative/wat.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ int WatParser::AppendImport(Module& m, const Import& i, varuint32* index)
293293
return ERR_WAT_INVALID_IMPORT_ORDER; // If we're trying to insert an import after declaring a table/func/global/memory, fail.
294294

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

299299
// Find the correct index to insert into

innative/wat.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,16 @@ namespace innative {
5252
int ParseElemData(Queue<WatToken>& tokens, varuint32& index, Instruction& op, wat::kh_indexname_t* hash);
5353
int ParseElem(TableInit& e, Queue<WatToken>& tokens);
5454
int ParseData(Queue<WatToken>& tokens);
55+
int AppendImport(Module& m, const Import& i, varuint32* index);
56+
int InlineImportExport(const Environment& env, Module& m, Queue<WatToken>& tokens, varuint32* index, varuint7 kind, Import** out);
5557

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

@@ -78,7 +78,7 @@ namespace innative {
7878
}
7979

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

94+
return ERR_SUCCESS;
95+
}
96+
97+
template<class T>
98+
inline static int AppendArray(const Environment& env, T item, T*& a, varuint32& n)
99+
{
100+
int err;
101+
if((err = ReallocArray(env, a, n)) != ERR_SUCCESS)
102+
return err;
93103
a[n - 1] = item;
94104
return ERR_SUCCESS;
95105
}

0 commit comments

Comments
 (0)