Skip to content

Commit

Permalink
Standardize identifiers, add error handling on malloc()
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik McClure committed May 30, 2019
1 parent 75d1d09 commit 60da02e
Show file tree
Hide file tree
Showing 34 changed files with 226 additions and 214 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -24,7 +24,7 @@ DESTDIR ?=
ifeq ($(MAKECMDGOALS), debug)
CPPFLAGS += -DDEBUG -g
else
CPPFLAGS += -DNDEBUG -march=native -O3
CPPFLAGS += -DNDEBUG -O3
endif

debug: innative-env innative innative-cmd innative-test innative-stub
Expand Down
4 changes: 2 additions & 2 deletions include/innative/errors.h
@@ -1,8 +1,8 @@
// Copyright (c)2019 Black Sphere Studios
// For conditions of distribution and use, see copyright notice in innative.h

#ifndef __ERRORS_H__IN__
#define __ERRORS_H__IN__
#ifndef IN__ERRORS_H
#define IN__ERRORS_H

#ifdef __cplusplus
extern "C" {
Expand Down
8 changes: 4 additions & 4 deletions include/innative/export.h
@@ -1,8 +1,8 @@
// Copyright (c)2019 Black Sphere Studios
// For conditions of distribution and use, see copyright notice in innative.h

#ifndef __EXPORT_H__IN__
#define __EXPORT_H__IN__
#ifndef IN__EXPORT_H
#define IN__EXPORT_H

#include "innative/schema.h"

Expand Down Expand Up @@ -42,7 +42,7 @@ extern "C" {
};

// Allows C code to access a webassembly global, provided it knows the correct type.
typedef union __IN_GLOBAL_TYPE
typedef union IN__GLOBAL_TYPE
{
uint32_t i32;
uint64_t i64;
Expand All @@ -52,7 +52,7 @@ extern "C" {
} IRGlobal;

// Contains pointers to the actual runtime functions
typedef struct __IN_EXPORTS
typedef struct IN__EXPORTS
{
/// Creates an environment with default settings, which must be destroyed using DestroyEnvironment
/// \param modules The number of modules that are expected to be added to the environment - can just be an estimate.
Expand Down
4 changes: 2 additions & 2 deletions include/innative/flags.h
@@ -1,8 +1,8 @@
// Copyright (c)2019 Black Sphere Studios
// For conditions of distribution and use, see copyright notice in innative.h

#ifndef __FLAGS_H__IN__
#define __FLAGS_H__IN__
#ifndef IN__FLAGS_H
#define IN__FLAGS_H

#ifdef __cplusplus
extern "C" {
Expand Down
4 changes: 2 additions & 2 deletions include/innative/innative.h
Expand Up @@ -14,8 +14,8 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

#ifndef __INNATIVE_H__IN__
#define __INNATIVE_H__IN__
#ifndef IN__INNATIVE_H
#define IN__INNATIVE_H

#define INNATIVE_VERSION_MAJOR 0
#define INNATIVE_VERSION_MINOR 1
Expand Down
4 changes: 2 additions & 2 deletions include/innative/opcodes.h
@@ -1,8 +1,8 @@
// Copyright (c)2019 Black Sphere Studios
// For conditions of distribution and use, see copyright notice in innative.h

#ifndef __OPCODES_H__IN__
#define __OPCODES_H__IN__
#ifndef IN__OPCODES_H
#define IN__OPCODES_H

#ifdef __cplusplus
extern "C" {
Expand Down
4 changes: 2 additions & 2 deletions include/innative/path.h
@@ -1,8 +1,8 @@
// Copyright (c)2019 Black Sphere Studios
// For conditions of distribution and use, see copyright notice in innative.h

#ifndef __PATH_H__IN__
#define __PATH_H__IN__
#ifndef IN__PATH_H
#define IN__PATH_H

#include "innative/innative.h"
#include <string>
Expand Down
78 changes: 39 additions & 39 deletions include/innative/schema.h
@@ -1,8 +1,8 @@
// Copyright (c)2019 Black Sphere Studios
// For conditions of distribution and use, see copyright notice in innative.h

#ifndef __SCHEMA_H__IN__
#define __SCHEMA_H__IN__
#ifndef IN__SCHEMA_H
#define IN__SCHEMA_H

#include "innative/innative.h"
#include "innative/khash.h"
Expand Down Expand Up @@ -86,23 +86,23 @@ enum WASM_KIND
WASM_KIND_GLOBAL = 3
};

struct __WASM_ENVIRONMENT;
struct IN_WASM_ENVIRONMENT;

// Represents a binary webassembly byte array structure
typedef struct __WASM_BYTE_ARRAY
typedef struct IN_WASM_BYTE_ARRAY
{
#ifdef __cplusplus
__WASM_BYTE_ARRAY() : bytes(nullptr), n_bytes(0) {}
__WASM_BYTE_ARRAY(uint8_t* b, varuint32 n) : bytes(b), n_bytes(n) {}
IN_WASM_BYTE_ARRAY() : bytes(nullptr), n_bytes(0) {}
IN_WASM_BYTE_ARRAY(uint8_t* b, varuint32 n) : bytes(b), n_bytes(n) {}
inline uint8_t* get() { return bytes; }
inline const uint8_t* get() const { return bytes; }
inline const char* str() const { return (char*)bytes; }
inline varuint32 size() const { return n_bytes; }
void resize(varuint32 sz, bool terminator, const struct __WASM_ENVIRONMENT& env);
void resize(varuint32 sz, bool terminator, const struct IN_WASM_ENVIRONMENT& env);
void discard(varuint32 sz, bool terminator);

bool operator==(const __WASM_BYTE_ARRAY& r) const;
inline bool operator!=(const __WASM_BYTE_ARRAY& r) const { return !operator==(r); }
bool operator==(const IN_WASM_BYTE_ARRAY& r) const;
inline bool operator!=(const IN_WASM_BYTE_ARRAY& r) const { return !operator==(r); }
inline const uint8_t& operator[](varuint32 i) const { assert(i < n_bytes); return bytes[i]; }
inline uint8_t& operator[](varuint32 i) { assert(i < n_bytes); return bytes[i]; }

Expand All @@ -119,15 +119,15 @@ KHASH_DECLARE(cimport, Identifier, char);
KHASH_DECLARE(modules, Identifier, size_t);

// A custom debug info structure used to map webassembly debug information.
typedef struct __WASM_DEBUGINFO
typedef struct IN_WASM_DEBUGINFO
{
unsigned int line;
unsigned int column;
Identifier name; // Stored debug name, if applicable
} DebugInfo;

// A union representing all possible immediate values of a webassembly instruction.
typedef union __WASM_IMMEDIATE
typedef union IN_WASM_IMMEDIATE
{
uint32 _uint32;
varuint1 _varuint1;
Expand All @@ -144,7 +144,7 @@ typedef union __WASM_IMMEDIATE
} Immediate;

// Encodes a single webassembly instruction and it's associated immediate values, plus it's location in the source.
typedef struct __WASM_INSTRUCTION
typedef struct IN_WASM_INSTRUCTION
{
uint8_t opcode;
Immediate immediates[MAX_IMMEDIATES];
Expand All @@ -153,7 +153,7 @@ typedef struct __WASM_INSTRUCTION
} Instruction;

// A webassembly function type signature, encoding the form, parameters, and return values.
typedef struct __WASM_FUNCTION_TYPE
typedef struct IN_WASM_FUNCTION_TYPE
{
varsint7 form;
varsint7* params;
Expand All @@ -163,53 +163,53 @@ typedef struct __WASM_FUNCTION_TYPE
} FunctionType;

// The underlying resizable limits structure used by linear memories and tables.
typedef struct __WASM_RESIZABLE_LIMITS
typedef struct IN_WASM_RESIZABLE_LIMITS
{
varuint32 flags; // WASM_LIMIT_FLAGS
varuint32 minimum;
varuint32 maximum;
} ResizableLimits;

// A single linear memory declaration.
typedef struct __WASM_MEMORY_DESC
typedef struct IN_WASM_MEMORY_DESC
{
ResizableLimits limits;
DebugInfo debug;
} MemoryDesc;

// A single table declaration.
typedef struct __WASM_TABLE_DESC
typedef struct IN_WASM_TABLE_DESC
{
varsint7 element_type;
ResizableLimits resizable;
DebugInfo debug;
} TableDesc;

// A single global description.
typedef struct __WASM_GLOBAL_DESC
typedef struct IN_WASM_GLOBAL_DESC
{
varsint7 type;
varuint1 mutability;
DebugInfo debug;
} GlobalDesc;

// A single global declaration, which is a description plus an initialization instruction.
typedef struct __WASM_GLOBAL_DECL
typedef struct IN_WASM_GLOBAL_DECL
{
GlobalDesc desc;
Instruction init;
} GlobalDecl;

// A single function description, which encodes both a type index telling us the function type signature, and the debug information associated with it.
typedef struct __WASM_FUNCTION_DESC
typedef struct IN_WASM_FUNCTION_DESC
{
varuint32 type_index;
DebugInfo debug;
DebugInfo* param_names; // Always the size of n_params from the signature
} FunctionDesc;

// Represents a single webassembly import definition
typedef struct __WASM_IMPORT
typedef struct IN_WASM_IMPORT
{
Identifier module_name;
Identifier export_name;
Expand All @@ -224,15 +224,15 @@ typedef struct __WASM_IMPORT
} Import;

// Represents a single webassembly export definition
typedef struct __WASM_EXPORT
typedef struct IN_WASM_EXPORT
{
Identifier name;
varuint7 kind; // WASM_KIND
varuint32 index;
} Export;

// Encodes initialization data for a table
typedef struct __WASM_TABLE_INIT
typedef struct IN_WASM_TABLE_INIT
{
varuint32 index;
Instruction offset;
Expand All @@ -241,7 +241,7 @@ typedef struct __WASM_TABLE_INIT
} TableInit;

// Defines the locals, instructions, and debug information for a webassembly function body
typedef struct __WASM_FUNCTION_BODY
typedef struct IN_WASM_FUNCTION_BODY
{
varuint32 body_size;
varuint32 n_locals;
Expand All @@ -254,15 +254,15 @@ typedef struct __WASM_FUNCTION_BODY
} FunctionBody;

// Encodes initialization data for a data section
typedef struct __WASM_DATA_INIT
typedef struct IN_WASM_DATA_INIT
{
varuint32 index;
Instruction offset;
ByteArray data;
} DataInit;

// Represents any custom section defined in the module
typedef struct __WASM_CUSTOM_SECTION
typedef struct IN_WASM_CUSTOM_SECTION
{
varuint7 opcode;
varuint32 payload;
Expand All @@ -276,18 +276,18 @@ namespace innative {
struct Context;
}
}
typedef innative::code::Context __IN_CONTEXT;
typedef innative::code::Context IN_CODE_CONTEXT;
namespace llvm {
class LLVMContext;
}
typedef llvm::LLVMContext __LLVM_CONTEXT;
typedef llvm::LLVMContext LLVM_LLVM_CONTEXT;
#else
typedef void __IN_CONTEXT;
typedef void __LLVM_CONTEXT;
typedef void IN_CODE_CONTEXT;
typedef void LLVM_LLVM_CONTEXT;
#endif

// Represents a single webassembly module
typedef struct __WASM_MODULE
typedef struct IN_WASM_MODULE
{
uint32 magic_cookie;
uint32 version;
Expand Down Expand Up @@ -368,25 +368,25 @@ typedef struct __WASM_MODULE

struct kh_exports_s* exports;
const char* path; // For debugging purposes, store path to source .wat file, if it exists.
__IN_CONTEXT* cache; // If non-zero, points to a cached compilation of this module
IN_CODE_CONTEXT* cache; // If non-zero, points to a cached compilation of this module
} Module;

// Represents a single validation error node in a singly-linked list.
typedef struct __WASM_VALIDATION_ERROR
typedef struct IN_WASM_VALIDATION_ERROR
{
int code;
char* error;
ptrdiff_t m;
struct __WASM_VALIDATION_ERROR* next;
struct IN_WASM_VALIDATION_ERROR* next;
} ValidationError;

// Encodes a single webassembly embedding used by the environment in the linking process.
typedef struct __WASM_EMBEDDING
typedef struct IN_WASM_EMBEDDING
{
const void* data;
uint64_t size; // If size is 0, data points to a null terminated UTF8 file path
int tag; // defines the type of embedding data included, determined by the runtime. 0 is always a static library file for the current platform.
struct __WASM_EMBEDDING* next;
struct IN_WASM_EMBEDDING* next;
} Embedding;

enum WASM_LOG_LEVEL
Expand All @@ -401,10 +401,10 @@ enum WASM_LOG_LEVEL

KHASH_DECLARE(modulepair, kh_cstr_t, FunctionType);

struct __WASM_ALLOCATOR;
struct IN_WASM_ALLOCATOR;

// Represents a collection of webassembly modules and configuration options that will be compiled into a single binary
typedef struct __WASM_ENVIRONMENT
typedef struct IN_WASM_ENVIRONMENT
{
size_t n_modules; // number of completely loaded modules (for multithreading)
size_t size; // Size of loaded or loading modules
Expand All @@ -420,15 +420,15 @@ typedef struct __WASM_ENVIRONMENT
const char* objpath; // Path to store intermediate results. If NULL, intermediate results are stored in the output folder
const char* linker; // If nonzero, attempts to execute this path as a linker instead of using the built-in LLD linker
const char* system; // prefix for the "system" module, which simply attempts to link the function name as a C function. Defaults to a blank string.
struct __WASM_ALLOCATOR* alloc; // Stores a pointer to the internal allocator
struct IN_WASM_ALLOCATOR* alloc; // Stores a pointer to the internal allocator
int loglevel; // WASM_LOG_LEVEL
FILE* log; // Output stream for log messages
void(*wasthook)(void*); // Optional hook for WAST debugging cases

struct kh_modules_s* modulemap;
struct kh_modulepair_s* whitelist;
struct kh_cimport_s* cimports;
__LLVM_CONTEXT* context;
LLVM_LLVM_CONTEXT* context;
} Environment;

#ifdef __cplusplus
Expand Down
2 changes: 1 addition & 1 deletion innative-cmd/main.cpp
Expand Up @@ -400,7 +400,7 @@ int main(int argc, char* argv[])

#ifdef IN_PLATFORM_WIN32
if(generate)
env->alloc = (__WASM_ALLOCATOR*)BeginUpdateResourceA(out.c_str(), TRUE);
env->alloc = (IN_WASM_ALLOCATOR*)BeginUpdateResourceA(out.c_str(), TRUE);
if(!env->alloc)
{
std::cout << "Failed to begin resource update!" << std::endl;
Expand Down
3 changes: 3 additions & 0 deletions innative-stub/stub.c
Expand Up @@ -36,6 +36,9 @@ wchar_t* GetRuntimeVersion(wchar_t* buf, size_t sz, uint16_t major, uint16_t min
DWORD len;
RegQueryValueExW(hKey, L"runtime", 0, 0, 0, &len);
LPBYTE runtime = malloc(sizeof(wchar_t)*((len + 1) / 2));
if(!runtime)
return 0;

if(RegQueryValueExW(hKey, L"runtime", 0, 0, runtime, &len) != ERROR_SUCCESS)
{
free(runtime);
Expand Down

1 comment on commit 60da02e

@elfring
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you mix changes for different issues in such a commit?

Please sign in to comment.