示例#1
0
        public static void Main(string[] args)
        {
            // Create an empty WebAssembly file.
            var file = new WasmFile();

            // Define a type section.
            var typeSection = new TypeSection();

            file.Sections.Add(typeSection);

            // Write the file to a (memory) stream.
            var stream = new MemoryStream();

            file.WriteBinaryTo(stream);
            stream.Seek(0, SeekOrigin.Begin);

            // Read the file from a (memory) stream.
            file = WasmFile.ReadBinary(stream);
            stream.Seek(0, SeekOrigin.Begin);

            // Define a memory section if it doesn't exist already.
            var memSection = file.GetFirstSectionOrNull <MemorySection>();

            if (memSection == null)
            {
                // The file doesn't specify a memory section, so we'll
                // have to create one and add it to the file.
                memSection = new MemorySection();
                file.Sections.Add(memSection);
            }

            memSection.Memories.Clear();
            // Memory sizes are specified in WebAssembly pages,
            // which are regions of storage with size 64KiB.
            // `new ResizableLimits(1, 1)` creates a memory description
            // that is initially one page (first argument) in size and
            // is capped at one page of memory (second argument), so
            // there will always be exactly one page of linear memory.
            memSection.Memories.Add(
                new MemoryType(new ResizableLimits(1, 1)));

            // Print the memory size.
            List <MemoryType> memSections =
                file.GetFirstSectionOrNull <MemorySection>()
                .Memories;

            Console.WriteLine(
                "Memory size: {0}",
                memSections
                .Single <MemoryType>()
                .Limits);

            // Save the file again.
            file.WriteBinaryTo(stream);
            stream.Seek(0, SeekOrigin.Begin);
        }
示例#2
0
        public async Task <IActionResult> DeleteTypeSection(int?id)
        {
            if (id != null)
            {
                TypeSection typeSection = await db.TypeSections.FirstOrDefaultAsync(u => u.Id == id);

                if (typeSection != null)
                {
                    db.TypeSections.Remove(typeSection);
                    await db.SaveChangesAsync();

                    return(RedirectToAction("Index"));
                }
            }
            return(NotFound());
        }
示例#3
0
        public async Task <IActionResult> CreateTypeSection(EditSectionViewModel model)
        {
            TypeSection typeSection = await db.TypeSections.FirstOrDefaultAsync(u => u.Name == model.Name);

            if (typeSection == null)
            {
                TypeSection _typeSection = new TypeSection()
                {
                    Name = model.Name
                };
                db.TypeSections.Add(_typeSection);
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(NotFound());
        }
示例#4
0
        public async Task <IActionResult> ConfirmDeleteTypeSection(int?id)
        {
            if (id != null)
            {
                TypeSection typeSection = await db.TypeSections.FirstOrDefaultAsync(u => u.Id == id);

                if (typeSection != null)
                {
                    TypeSectionViewModel model = new TypeSectionViewModel()
                    {
                        Id         = (int)id,
                        Name       = typeSection.Name,
                        YearEvents = await db.YearEvents.Where(ye => ye.TypeSection == typeSection.Name).ToListAsync()
                    };
                    return(View(model));
                }
            }
            return(NotFound());
        }
示例#5
0
        /// <summary>
        /// Reads the non-custom section with the given header.
        /// </summary>
        /// <param name="Header">The section header.</param>
        /// <returns>The parsed section.</returns>
        protected Section ReadKnownSectionPayload(SectionHeader Header)
        {
            switch (Header.Name.Code)
            {
            case SectionCode.Type:
                return(TypeSection.ReadSectionPayload(Header, this));

            case SectionCode.Import:
                return(ImportSection.ReadSectionPayload(Header, this));

            case SectionCode.Function:
                return(FunctionSection.ReadSectionPayload(Header, this));

            case SectionCode.Table:
                return(TableSection.ReadSectionPayload(Header, this));

            case SectionCode.Memory:
                return(MemorySection.ReadSectionPayload(Header, this));

            case SectionCode.Global:
                return(GlobalSection.ReadSectionPayload(Header, this));

            case SectionCode.Export:
                return(ExportSection.ReadSectionPayload(Header, this));

            case SectionCode.Start:
                return(StartSection.ReadSectionPayload(Header, this));

            case SectionCode.Element:
                return(ElementSection.ReadSectionPayload(Header, this));

            case SectionCode.Code:
                return(CodeSection.ReadSectionPayload(Header, this));

            case SectionCode.Data:
                return(DataSection.ReadSectionPayload(Header, this));

            default:
                return(ReadUnknownSectionPayload(Header));
            }
        }
        // strict parse means all sections must come in order
        public void ParseAsWASM(string filename, bool strict_parse = true)
        {
            if (!BitConverter.IsLittleEndian)
            {
                throw new NotImplementedException("LEB128 implementation only handles little endian systems");
            }

            using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
            {
                BinaryReader reader = new BinaryReader(fs);
                uint         magic  = reader.ReadUInt32();
                if (magic != MAGIC)
                {
                    throw new Exception("Not a compiled Web Assembly file.");
                }

                uint version = reader.ReadUInt32();
                if (version > SUPPORTED_VERSION)
                {
                    throw new Exception($"Unsupported version. Expected version <= {SUPPORTED_VERSION}, received {version}.");
                }

                int last_parsed_module = int.MinValue;

                /* Read in each module */

                while (true)
                {
                    int id = reader.PeekChar();

                    // EOF
                    if (id == -1)
                    {
                        break;
                    }

                    if (strict_parse && id < last_parsed_module)
                    {
                        throw new Exception("File contains out of order sections.");
                    }
                    last_parsed_module = id;

                    switch (id)
                    {
                    case (int)WebAssemblyModuleID.Custom:
                        if (strict_parse && custom != null)
                        {
                            throw new Exception("File contains a duplicate custom section.");
                        }
                        custom = new CustomSection(reader);
                        break;

                    case (int)WebAssemblyModuleID.Type:
                        if (strict_parse && type != null)
                        {
                            throw new Exception("File contains a duplicate type section.");
                        }
                        type = new TypeSection(reader);
                        break;

                    case (int)WebAssemblyModuleID.Import:
                        if (strict_parse && import != null)
                        {
                            throw new Exception("File contains a duplicate import section.");
                        }
                        import = new ImportSection(reader);
                        break;

                    case (int)WebAssemblyModuleID.Function:
                        if (strict_parse && function != null)
                        {
                            throw new Exception("File contains a duplicate function section.");
                        }
                        function = new FunctionSection(reader);
                        break;

                    case (int)WebAssemblyModuleID.Table:
                        if (strict_parse && table != null)
                        {
                            throw new Exception("File contains a duplicate table section.");
                        }
                        table = new TableSection(reader);
                        break;

                    case (int)WebAssemblyModuleID.Memory:
                        if (strict_parse && memory != null)
                        {
                            throw new Exception("File contains a duplicate memory section.");
                        }
                        memory = new MemorySection(reader);
                        break;

                    case (int)WebAssemblyModuleID.Global:
                        if (strict_parse && global != null)
                        {
                            throw new Exception("File contains a duplicate global section.");
                        }
                        global = new GlobalSection(reader);
                        break;

                    case (int)WebAssemblyModuleID.Export:
                        if (strict_parse && export != null)
                        {
                            throw new Exception("File contains a duplicate export section.");
                        }
                        export = new ExportSection(reader);
                        break;

                    case (int)WebAssemblyModuleID.Start:
                        if (strict_parse && start != null)
                        {
                            throw new Exception("File contains a duplicate start section.");
                        }
                        start = new StartSection(reader);
                        break;

                    case (int)WebAssemblyModuleID.Element:
                        if (strict_parse && element != null)
                        {
                            throw new Exception("File contains a duplicate element section.");
                        }
                        element = new ElementSection(reader);
                        break;

                    case (int)WebAssemblyModuleID.Code:
                        if (strict_parse && code != null)
                        {
                            throw new Exception("File contains a duplicate code section.");
                        }
                        code = new CodeSection(reader);
                        break;

                    case (int)WebAssemblyModuleID.Data:
                        if (strict_parse && data != null)
                        {
                            throw new Exception("File contains a duplicate data section.");
                        }
                        data = new DataSection(reader);
                        break;

                    // Error
                    default:
                        throw new Exception($"Unknown section {id}.");
                    }
                }

                /* Additional validation */

                // The lengths of vectors produced by the (possibly empty) function and code section must match up.
                if ((function != null && code == null) || (function == null && code != null))
                {
                    throw new Exception("File corrupt. Must include both function and code sections.");
                }
                if (function.types.Length != code.bodies.Length)
                {
                    throw new Exception("File corrupt. Function and code sections do not match up.");
                }

                // TODO: I don't actually check if data overlaps

                // TODO: Validate everything in this list
                // https://webassembly.github.io/spec/core/valid/modules.html
            }
        }