Пример #1
0
        public override byte[] Execute(byte[] arg)
        {
            try
            {
                if (arg == null || arg.Length <= 0)
                {
                    MyProgram.PrintToConsole("no data was received");
                    return(EmptyBytes);
                }

                var text = Encoding.UTF8.GetString(arg);

                MyProgram.PrintToConsole($"data received, {text.Length} chars long");

                var taggedText = text.ToTaggedString();

                MyProgram.PrintToConsole($"Part-Of-Speech tagger complete");

                var bufferout = Encoding.UTF8.GetBytes(taggedText);

                WriteOutputToDisk(bufferout, ".txt");

                return(bufferout);
            }
            catch (Exception ex)
            {
                MyProgram.PrintToConsole(ex);
                return(EmptyBytes);
            }
        }
Пример #2
0
        public override byte[] Execute(byte[] arg)
        {
            MyProgram.PrintToConsole();
            MyProgram.PrintToConsole($"{nameof(GetTokenPageRank)} invoked");
            MyProgram.ProgressMessageState = null;
            if (arg == null || !arg.Any())
            {
                MyProgram.PrintToConsole("no tokens were passed up on the socket");
                return(JsonEncodedResponse(
                           new TokenPageRankResponse
                {
                    Msg = "no tokens were passed up on the socket",
                    St = MetadataTokenStatus.Error
                }));
            }

            try
            {
                var json      = Encoding.UTF8.GetString(arg);
                var allTokens = JsonConvert.DeserializeObject <TokenIdResponse>(json);
                if (allTokens?.Tokens == null || !allTokens.Tokens.Any())
                {
                    return(JsonEncodedResponse(new TokenPageRankResponse
                    {
                        Msg = "parse from JSON failed",
                        St = MetadataTokenStatus.Error
                    }));
                }

                var pageRank = ((IaaProgram)MyProgram).UtilityMethods.ResolveTokenPageRanks(allTokens);
                Console.Write('\n');

                return(JsonEncodedResponse(new TokenPageRankResponse {
                    Ranks = pageRank
                }));
            }
            catch (Exception ex)
            {
                MyProgram.PrintToConsole(ex);
                return(JsonEncodedResponse(new TokenPageRankResponse
                {
                    Msg = string.Format(ex.Message),
                    St = MetadataTokenStatus.Error
                }));
            }
        }
Пример #3
0
        public override byte[] Execute(byte[] arg)
        {
            MyProgram.PrintToConsole("GetFlattenAssembly invoked");
            MyProgram.ProgressMessageState = null;

            try
            {
                if (arg == null || arg.Length <= 0)
                {
                    throw new ItsDeadJim("No Path to an assembly was passed to the GetFlattenAssembly command.");
                }

                var asmPath = Encoding.UTF8.GetString(arg);

                if (!File.Exists(asmPath))
                {
                    throw new ItsDeadJim("There isn't a file at the location: " + asmPath);
                }

                NfConfig.AssemblySearchPaths.Add(Path.GetDirectoryName(asmPath));

                var asm = NfConfig.UseReflectionOnlyLoad
                    ? Util.Binary.Asm.NfReflectionOnlyLoadFrom(asmPath)
                    : Util.Binary.Asm.NfLoadFrom(asmPath);
                Action <ProgressMessage> myProgress = message => MyProgram.PrintToConsole(message);
                var flatAsm = Flatten.GetFlattenedAssembly(new FlattenLineArgs {
                    Assembly = asm, Depth = _maxDepth
                }, myProgress);
                flatAsm.Path = asmPath;

                return(JsonEncodedResponse(flatAsm));
            }
            catch (Exception ex)
            {
                Console.WriteLine('\n');
                MyProgram.PrintToConsole(ex);
                return(JsonEncodedResponse(
                           new FlattenAssembly {
                    AllLines = new List <FlattenedLine> {
                        new NullFlattenedLine(ex)
                    }
                }));
            }
        }
Пример #4
0
        public override byte[] Execute(byte[] arg)
        {
            MyProgram.PrintToConsole();
            MyProgram.PrintToConsole($"{nameof(ReassignTokenNames)} invoked");
            MyProgram.ProgressMessageState = null;
            try
            {
                var json = Encoding.UTF8.GetString(arg);
                var rqst = JsonConvert.DeserializeObject <TokenReassignRequest>(json);
                var subj = rqst.SubjectTokenNames;

                //assign this for writing the output to file
                _reassignAssemblySubjectName = rqst.AsmName;
                var frgn = rqst.ForeignTokenNames;
                var typs = rqst.ForeignTokenTypes;
                if (subj == null || typs == null)
                {
                    return(JsonEncodedResponse(new TokenReassignResponse
                    {
                        Msg = "Both the SubjectTokenNames and ForeignTokenTypes are required.",
                        St = MetadataTokenStatus.Error
                    }));
                }

                subj.ReassignAllInterfaceTokens(typs, MyProgram.PrintToConsole, frgn);
                var rspn = new TokenReassignResponse {
                    Names = subj.Items
                };
                return(JsonEncodedResponse(rspn));
            }
            catch (Exception ex)
            {
                MyProgram.PrintToConsole(ex);
                return(JsonEncodedResponse(new TokenReassignResponse
                {
                    Msg = string.Format(ex.Message),
                    St = MetadataTokenStatus.Error
                }));
            }
        }
Пример #5
0
 public override byte[] Execute(byte[] arg)
 {
     try
     {
         if (arg == null || arg.Length <= 0)
         {
             throw new ItsDeadJim("No Type Name given to parse.");
         }
         var nm = Encoding.UTF8.GetString(arg);
         MyProgram.PrintToConsole(nm);
         var f = TypeNameParseTree.ParseIl(nm);
         return(JsonEncodedResponse(f));
     }
     catch (Exception ex)
     {
         Console.WriteLine('\n');
         MyProgram.PrintToConsole(ex);
         return(JsonEncodedResponse(new NfTypeNameParseItem {
             Error = ex
         }));
     }
 }
Пример #6
0
        public override byte[] Execute(byte[] arg)
        {
            MyProgram.PrintToConsole();
            MyProgram.PrintToConsole($"{nameof(GetTokenTypes)} invoked");
            MyProgram.ProgressMessageState = null;
            try
            {
                //expect that the caller has init'ed this with some target assembly(ies)
                if (((IaaProgram)MyProgram).AsmInited != true)
                {
                    MyProgram.PrintToConsole("no assemblies are loaded - call GetAsmIndices");
                    return(JsonEncodedResponse(
                               new TokenTypeResponse
                    {
                        Msg = "no assemblies are loaded - call GetAsmIndices",
                        St = MetadataTokenStatus.Error
                    }));
                }

                var json = Encoding.UTF8.GetString(arg);
                var rqst = JsonConvert.DeserializeObject <TokenTypeRequest>(json);
                if (!string.IsNullOrWhiteSpace(rqst.ResolveAllNamedLike))
                {
                    ((IaaProgram)MyProgram).AssemblyNameRegexPattern = rqst.ResolveAllNamedLike;
                }

                if (!string.IsNullOrWhiteSpace(((IaaProgram)MyProgram).RootAssemblyPath))
                {
                    _rootDir = System.IO.Path.GetDirectoryName(((IaaProgram)MyProgram).RootAssemblyPath);
                }

                //get all the assemblies of this app domain
                _asmIndices = ((IaaProgram)MyProgram).AsmIndicies;

                MyProgram.PrintToConsole($"There are {_asmIndices.Asms.Length} assemblies in scope.");

                var allTypes = GetAllTypes();
                if (allTypes == null || !allTypes.Any())
                {
                    var msg = "Could not resolve any types from any of the assemblies.";
                    MyProgram.PrintToConsole(msg);
                    return(JsonEncodedResponse(
                               new TokenTypeResponse
                    {
                        Msg = msg,
                        St = MetadataTokenStatus.Error
                    }));
                }

                MyProgram.PrintToConsole();
                MyProgram.PrintToConsole($"There are {allTypes.Length} types in all assemblies.");
                var tokenTypes = new List <MetadataTokenType>();
                var totalTypes = allTypes.Length;
                for (var i = 0; i < totalTypes; i++)
                {
                    var cType = allTypes[i];

                    ((IaaProgram)MyProgram).ReportProgress(new ProgressMessage
                    {
                        Activity        = $"{cType}",
                        ProcName        = Process.GetCurrentProcess().ProcessName,
                        ProgressCounter = Etc.CalcProgressCounter(i, totalTypes),
                        Status          = "Resolving all type names"
                    });

                    var tt = GetMetadataTokenType(cType);
                    if (tt != null)
                    {
                        tokenTypes.Add(tt);
                    }
                }

                var tokenTypeRspn = new TokenTypeResponse {
                    Types = tokenTypes.ToArray()
                };
                //keep a copy of this in memory like AsmIndices
                ((IaaProgram)MyProgram).TokenTypeResponse = tokenTypeRspn;
                return(JsonEncodedResponse(tokenTypeRspn));
            }
            catch (Exception ex)
            {
                Console.Write('\n');
                MyProgram.PrintToConsole(ex);
                return(JsonEncodedResponse(
                           new TokenTypeResponse
                {
                    Msg = ex.Message,
                    St = MetadataTokenStatus.Error
                }));
            }
        }
Пример #7
0
        public override byte[] Execute(byte[] arg)
        {
            MyProgram.PrintToConsole();
            MyProgram.PrintToConsole($"{nameof(GetTokenIds)} invoked");
            MyProgram.ProgressMessageState = null;
            try
            {
                var cProc = Process.GetCurrentProcess();
                var asm   = GetAssembly(arg);

                var asmTypes = asm.NfGetTypes(false, MyProgram.LogFile);
                var tokens   = new List <MetadataTokenId>();
                var counter  = 0;
                var total    = asmTypes.Length;
                MyProgram.PrintToConsole(string.Format("There are {0} top-level types in this assembly", total));
                foreach (var asmType in asmTypes)
                {
                    counter += 1;
                    ((IaaProgram)MyProgram).ReportProgress(new ProgressMessage
                    {
                        Activity        = asmType.FullName,
                        ProcName        = cProc.ProcessName,
                        ProgressCounter = Etc.CalcProgressCounter(counter, total),
                        Status          = "Getting top-level types"
                    });
                    tokens.Add(AssemblyAnalysis.GetMetadataToken(asmType, 0, MyProgram.LogFile));
                }

                Console.Write('\n');
                if (string.IsNullOrWhiteSpace(((IaaProgram)MyProgram).AssemblyNameRegexPattern))
                {
                    return(JsonEncodedResponse(
                               new TokenIdResponse
                    {
                        Tokens = tokens.ToArray()
                    }));
                }

                var countDepth = 0;
                var callTokens = tokens.SelectMany(x => x.Items.SelectMany(y => y.Items)).ToArray();
                counter = 0;
                total   = callTokens.Length;
                MyProgram.PrintToConsole(string.Format("There are {0} call-of-call tokens", total));
                foreach (var iToken in callTokens)
                {
                    counter += 1;
                    ((IaaProgram)MyProgram).ReportProgress(new ProgressMessage
                    {
                        Activity        = string.Format("{0}.{1}", iToken.RslvAsmIdx, iToken.Id),
                        ProcName        = cProc.ProcessName,
                        ProgressCounter = Etc.CalcProgressCounter(counter, total),
                        Status          = "Getting call-of-calls"
                    });
                    var stackTrc = new Stack <MetadataTokenId>();
                    var msg      = new StringBuilder();
                    ResolveCallOfCall(iToken, ref countDepth, stackTrc, msg);
                    if (msg.Length > 0)
                    {
                        MyProgram.PrintToLog(msg.ToString());
                    }
                }
                Console.Write('\n');
                var tokenIdRspn = new TokenIdResponse
                {
                    Tokens = tokens.ToArray()
                };
                ((IaaProgram)MyProgram).TokenIdResponse = tokenIdRspn;
                return(JsonEncodedResponse(tokenIdRspn));
            }
            catch (Exception ex)
            {
                Console.Write('\n');
                MyProgram.PrintToConsole(ex);
                return(JsonEncodedResponse(
                           new TokenIdResponse
                {
                    Msg = ex.Message,
                    St = MetadataTokenStatus.Error
                }));
            }
        }
Пример #8
0
        /// <summary>
        /// Resolves the outbound call stack for the given <see cref="token"/>
        /// </summary>
        /// <param name="token"></param>
        /// <param name="depth"></param>
        /// <param name="stackTrc">For detecting recursive call patterns</param>
        /// <param name="msgOut">For getting details on recursion.</param>
        internal void ResolveCallOfCall(MetadataTokenId token, ref int depth, Stack <MetadataTokenId> stackTrc, StringBuilder msgOut)
        {
            if (msgOut != null)
            {
                if (depth > 0)
                {
                    msgOut.Append(new string(' ', depth));
                }

                msgOut.AppendFormat("Depth:{0}", depth);
                msgOut.AppendFormat(", Token:{0}.0x{1}", token.RslvAsmIdx, token.Id.ToString("X4"));
            }
            //detect if we are in a recursive call
            if (stackTrc.Any(x => x.Equals(token)))
            {
                if (msgOut != null)
                {
                    msgOut.AppendLine(", Message:'present in stack trace'");
                }
                return;
            }

            stackTrc.Push(new MetadataTokenId {
                Id = token.Id, RslvAsmIdx = token.RslvAsmIdx
            });

            //increment the current depth
            depth += 1;

            //abort if max depth has been reached
            if (depth > ((IaaProgram)MyProgram).MaxRecursionDepth)
            {
                depth -= 1;
                MyProgram.PrintToConsole(
                    String.Format("Max Recursion Depth @ {0}.{1}\n", token.RslvAsmIdx, token.Id));
                if (msgOut != null)
                {
                    msgOut.AppendLine(", Message:'max recursion depth'");
                }
                return;
            }

            //when there are already Items then leave them as is
            if (token.Items != null && token.Items.Length > 0)
            {
                depth -= 1;
                if (msgOut != null)
                {
                    msgOut.AppendLine(", Message:'Items already present'");
                }
                return;
            }

            //don't waste clock cycles on Ignore types
            if (((IaaProgram)MyProgram).DisolutionCache.Contains(token))
            {
                depth -= 1;
                if (msgOut != null)
                {
                    msgOut.AppendLine(", Message:'token in DisolutionCache'");
                }
                return;
            }

            //resolve token to name
            MetadataTokenName tokenName = null;

            if (((IaaProgram)MyProgram).TokenId2NameCache.ContainsKey(token))
            {
                tokenName = ((IaaProgram)MyProgram).TokenId2NameCache[token];
            }
            else
            {
                // the token must be resolvable with the its manifest module
                var resolveRslt = ((IaaProgram)MyProgram).UtilityMethods.ResolveSingleTokenName(token, out tokenName, msgOut);
                if (!resolveRslt || tokenName == null)
                {
                    ((IaaProgram)MyProgram).DisolutionCache.Add(token);
                    depth -= 1;
                    if (msgOut != null)
                    {
                        msgOut.AppendLine(", Message:'ResolveSingleTokenName failed'");
                    }
                    return;
                }
                ((IaaProgram)MyProgram).TokenId2NameCache.Add(token, tokenName);
            }

            //only proceed to find calls of calls, types are resolved elsewhere
            if (!tokenName.IsMethodName())
            {
                depth -= 1;
                if (msgOut != null)
                {
                    msgOut.AppendLine(", Message:'token is not a method'");
                }
                return;
            }

            //match is on Asm Name, not type nor member name
            var owningAsmName = ((IaaProgram)MyProgram).AsmIndicies.Asms.FirstOrDefault(x => x.IndexId == tokenName.OwnAsmIdx);

            if (owningAsmName == null)
            {
                ((IaaProgram)MyProgram).DisolutionCache.Add(token);
                depth -= 1;
                if (msgOut != null)
                {
                    msgOut.AppendLine(string.Format(", Message:'owning assembly idx {0} has no match'", tokenName.OwnAsmIdx));
                }
                return;
            }

            //check for match of asm name to user defined regex
            if (!Regex.IsMatch(owningAsmName.AssemblyName, ((IaaProgram)MyProgram).AssemblyNameRegexPattern))
            {
                ((IaaProgram)MyProgram).DisolutionCache.Add(token);
                if (msgOut != null)
                {
                    msgOut.AppendLine(string.Format(", Message:'assembly name [{1}] does not match regex [{0}]'",
                                                    ((IaaProgram)MyProgram).AssemblyNameRegexPattern, owningAsmName.AssemblyName));
                }

                depth -= 1;
                return;
            }

            //resolve token to runtime member info
            MemberInfo mi;
            var        rtMiRslt =
                ((IaaProgram)MyProgram).UtilityMethods.TryResolveRtMemberInfo(((IaaProgram)MyProgram).AsmIndicies.GetAssemblyByIndex(owningAsmName.IndexId),
                                                                              tokenName.Name, out mi, msgOut);

            if (!rtMiRslt)
            {
                ((IaaProgram)MyProgram).DisolutionCache.Add(token);
                depth -= 1;
                return;
            }

            //get the token as it is called in its own assembly
            var r = AssemblyAnalysis.GetMetadataToken(mi, true, tokenName.OwnAsmIdx);

            if (stackTrc.Any(x => x.Equals(r)))
            {
                depth        -= 1;
                token.IsByRef = 1;
                if (msgOut != null)
                {
                    msgOut.AppendLine(string.Format(", Message:'name resolved token id {0},{1} found in stack trace'",
                                                    r.RslvAsmIdx, r.Id.ToString("X4")));
                }
                return;
            }

            //this token differs from the method arg 'token'
            stackTrc.Push(new MetadataTokenId {
                Id = r.Id, RslvAsmIdx = r.RslvAsmIdx
            });

            //unwind when its a terminal token
            if (r.Items == null || r.Items.Length <= 0)
            {
                depth -= 1;
                if (msgOut != null)
                {
                    msgOut.AppendLine(string.Format(", Message:'name resolved token id {0},{1} is a terminal node'",
                                                    r.RslvAsmIdx, r.Id.ToString("X4")));
                }

                return;
            }

            //these token ids are only resolvable to the asm who owns MemberInfo(mi)
            token.Items = r.Items;

            //recurse each of these calls-of-calls[...]-of-calls
            foreach (var iToken in token.Items)
            {
                ResolveCallOfCall(iToken, ref depth, stackTrc, msgOut);
            }

            depth -= 1;
        }
Пример #9
0
        public override byte[] Execute(byte[] arg)
        {
            MyProgram.PrintToConsole();
            MyProgram.PrintToConsole($"{nameof(GetAsmIndices)} invoked");
            try
            {
                if (arg == null || arg.Length <= 0)
                {
                    MyProgram.PrintToConsole("the arg for GetAsmIndices is null or empty");
                    return(JsonEncodedResponse(
                               new AsmIndexResponse
                    {
                        Msg = "the arg for GetAsmIndices is null or empty",
                        St = MetadataTokenStatus.Error
                    }));
                }

                var json    = Encoding.UTF8.GetString(arg);
                var rqst    = JsonConvert.DeserializeObject <AsmIndexRequest>(json);
                var asmPath = rqst.AssemblyFilePath.Replace("\"", string.Empty);
                if (asmPath.Contains(WAK_WAK) && !asmPath.StartsWith(WAK_WAK))
                {
                    asmPath = asmPath.Replace(WAK_WAK, new string(new[] { (char)0x5C }));
                }

                if (!File.Exists(asmPath))
                {
                    MyProgram.PrintToConsole(string.Format("invalid path [{0}]", asmPath));
                    return(JsonEncodedResponse(
                               new AsmIndexResponse
                    {
                        Msg = string.Format("invalid path [{0}]", asmPath),
                        St = MetadataTokenStatus.Error
                    }));
                }

                MyProgram.PrintToConsole($"Assembly: {Path.GetFileName(asmPath)}");
                ((IaaProgram)MyProgram).RootAssemblyPath = asmPath;

                NfConfig.AssemblySearchPaths.Add(Path.GetDirectoryName(asmPath));

                var asm = NfConfig.UseReflectionOnlyLoad
                    ? Util.Binary.Asm.NfReflectionOnlyLoadFrom(asmPath)
                    : Util.Binary.Asm.NfLoadFrom(asmPath);

                if (asm == null)
                {
                    MyProgram.PrintToConsole("cannot load assembly");
                    return(JsonEncodedResponse(
                               new AsmIndexResponse
                    {
                        Msg = "cannot load assembly",
                        St = MetadataTokenStatus.Error
                    }));
                }

                ((IaaProgram)MyProgram).Init(asm);
                ((IaaProgram)MyProgram).AssignAsmIndicies(asm);

                return(JsonEncodedResponse(((IaaProgram)MyProgram).AsmIndicies));
            }
            catch (Exception ex)
            {
                MyProgram.PrintToConsole(ex);
                return(JsonEncodedResponse(
                           new AsmIndexResponse
                {
                    Msg = string.Format(ex.Message),
                    St = MetadataTokenStatus.Error
                }));
            }
        }
Пример #10
0
        public override byte[] Execute(byte[] arg)
        {
            MyProgram.PrintToConsole();
            MyProgram.PrintToConsole($"{nameof(GetTokenNames)} invoked");
            MyProgram.ProgressMessageState = null;
            try
            {
                var myIaaProgram = ((IaaProgram)MyProgram);
                if (myIaaProgram.AsmInited != true)
                {
                    MyProgram.PrintToConsole("no assemblies are loaded - call GetAsmIndices");
                    return(JsonEncodedResponse(
                               new TokenNameResponse
                    {
                        Msg = "no assemblies are loaded - call GetAsmIndices",
                        St = MetadataTokenStatus.Error
                    }));
                }

                var json   = Encoding.UTF8.GetString(arg);
                var rqst   = JsonConvert.DeserializeObject <TokenNameRequest>(json);
                var tokens = rqst.Tokens ?? myIaaProgram.TokenIdResponse?.Tokens;

                if (tokens == null || tokens.Length <= 0)
                {
                    return(JsonEncodedResponse(new TokenNameResponse
                    {
                        Msg = "parse failed",
                        St = MetadataTokenStatus.Error
                    }));
                }

                var names = myIaaProgram.UtilityMethods.ResolveAllTokenNames(tokens);
                Console.Write('\n');
                var tokenNameRspn = new TokenNameResponse {
                    Names = names.ToArray()
                };
                if (rqst.MapFullCallStack && myIaaProgram.TokenTypeResponse != null &&
                    myIaaProgram.TokenIdResponse != null)
                {
                    var nameRoot      = tokenNameRspn.GetAsRoot();
                    var tokenRoot     = myIaaProgram.TokenIdResponse.GetAsRoot();
                    var typeRoot      = myIaaProgram.TokenTypeResponse.GetAsRoot();
                    var asmRspn       = myIaaProgram.AsmIndicies;
                    var fullCallStack =
                        MetadataTokenName.BuildMetadataTokenName(nameRoot, tokenRoot, asmRspn, typeRoot, MyProgram.PrintToConsole);
                    if (fullCallStack?.Items != null)
                    {
                        tokenNameRspn.Names = fullCallStack.Items;
                    }
                }

                myIaaProgram.TokenNameResponse = tokenNameRspn;
                return(JsonEncodedResponse(tokenNameRspn));
            }
            catch (Exception ex)
            {
                MyProgram.PrintToConsole(ex);
                return(JsonEncodedResponse(new TokenNameResponse
                {
                    Msg = string.Format(ex.Message),
                    St = MetadataTokenStatus.Error
                }));
            }
        }