Пример #1
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));
            }
        }
Пример #2
0
        // 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
            }
        }