Merge pull request #12793 from wateret/fix-legacy-12685
[platform/upstream/coreclr.git] / tests / src / JIT / Regression / Dev11 / External / dev11_77709 / BadCheckedAdd1.cs
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4
5 using System;
6 using System.IO;
7 using System.Runtime.CompilerServices;
8
9 namespace BadCheckedAdd1
10 {
11
12
13     internal enum SniContext
14     {
15         Undefined = 0,
16         Snix_Connect,
17         Snix_PreLoginBeforeSuccessfullWrite,
18         Snix_PreLogin,
19         Snix_LoginSspi,
20         Snix_ProcessSspi,
21         Snix_Login,
22         Snix_EnableMars,
23         Snix_AutoEnlist,
24         Snix_GetMarsSession,
25         Snix_Execute,
26         Snix_Read,
27         Snix_Close,
28         Snix_SendRows,
29     }
30
31
32     internal static class ADP
33     {
34         [MethodImpl(MethodImplOptions.NoInlining)]
35         static internal bool IsCatchableExceptionType(Exception e)
36         {
37             return false;
38         }
39     }
40
41
42     sealed internal class SQL
43     {
44         [MethodImpl(MethodImplOptions.NoInlining)]
45         static internal Exception InvalidSSPIPacketSize()
46         {
47             return null;
48         }
49     }
50
51
52     sealed internal class SqlLogin
53     {
54         internal int timeout;                                                       // login timeout
55         internal bool userInstance = false;                                   // user instance
56         internal string hostName = "";                                      // client machine name
57         internal string userName = "";                                      // user id
58         internal string password = "";                                      // password
59         internal string applicationName = "";                                      // application name
60         internal string serverName = "";                                      // server name
61         internal string language = "";                                      // initial language
62         internal string database = "";                                      // initial database
63         internal string attachDBFilename = "";                                      // DB filename to be attached
64         internal string newPassword = "";                                      // new password for reset password
65         internal bool useReplication = false;                                   // user login for replication
66         internal bool useSSPI = false;                                   // use integrated security
67         internal int packetSize = 8000;                                    // packet size
68     }
69
70
71     internal sealed class TdsParserStaticMethods
72     {
73         [MethodImpl(MethodImplOptions.NoInlining)]
74         static internal byte[] GetNetworkPhysicalAddressForTdsLoginOnly()
75         {
76             return new Byte[8];
77         }
78
79         [MethodImpl(MethodImplOptions.NoInlining)]
80         static internal Byte[] EncryptPassword(string password)
81         {
82             if (password == "")
83             {
84                 return new Byte[0];
85             }
86             else
87             {
88
89                 return new Byte[] {
90                     0x86, 0xa5,
91                     0x36, 0xa5, 0x22, 0xa5,
92                     0x33, 0xa5, 0xb3, 0xa5,
93                     0xb2, 0xa5, 0x77, 0xa5,
94                     0xb6, 0xa5, 0x92, 0xa5
95                 };
96             }
97         }
98     }
99
100
101     sealed internal class TdsParserStateObject
102     {
103         public uint ReadDwordFromPostHeaderContentAtByteOffset(int offset)
104         {
105             offset += 8;
106
107             using (var reader = new BinaryReader(new MemoryStream(this._outBuff, offset, 4)))
108             {
109                 return reader.ReadUInt32();
110             }
111         }
112
113         internal byte[] _bTmp = new byte[8];
114         internal byte[] _outBuff = new Byte[1000];
115         internal int _outBytesUsed = 8;
116         internal readonly int _outputHeaderLen = 8;
117         internal byte _outputMessageType = 0;
118         internal byte _outputPacketNumber = 1;
119         internal bool _pendingData = false;
120         private int _timeoutSeconds;
121         private long _timeoutTime;
122         internal int _traceChangePasswordOffset = 0;
123         internal int _traceChangePasswordLength = 0;
124         internal int _tracePasswordOffset = 0;
125         internal int _tracePasswordLength = 0;
126
127
128         private SniContext _sniContext = SniContext.Undefined;
129
130         internal SniContext SniContext
131         {
132             get { return _sniContext; }
133             set { _sniContext = value; }
134         }
135
136
137         internal void SetTimeoutSeconds(int timeout)
138         {
139             _timeoutSeconds = timeout;
140             if (timeout == 0)
141             {
142                 _timeoutTime = Int64.MaxValue;
143             }
144         }
145
146
147         internal void ResetBuffer()
148         {
149             this._outBytesUsed = this._outputHeaderLen;
150             return;
151         }
152
153
154         [MethodImpl(MethodImplOptions.NoInlining)]
155         internal void WritePacket(byte flushMode)
156         {
157             return;
158         }
159     }
160
161
162     internal class TdsParser
163     {
164         internal TdsParserStateObject _physicalStateObj = new TdsParserStateObject();
165
166         private volatile static UInt32 s_maxSSPILength = 0;
167
168         private static byte[] s_nicAddress;
169
170
171         [MethodImpl(MethodImplOptions.NoInlining)]
172         private void SSPIData(byte[] receivedBuff, UInt32 receivedLength, byte[] sendBuff, ref UInt32 sendLength)
173         {
174             return;
175         }
176
177
178         internal void WriteByte(byte b, TdsParserStateObject stateObj)
179         {
180             if (stateObj._outBytesUsed == stateObj._outBuff.Length)
181             {
182                 stateObj.WritePacket(0);
183             }
184
185             stateObj._outBuff[stateObj._outBytesUsed++] = b;
186         }
187
188         internal void WriteByteArray(Byte[] b, int len, int offsetBuffer, TdsParserStateObject stateObj)
189         {
190             int offset = offsetBuffer;
191
192             while (len > 0)
193             {
194                 if ((stateObj._outBytesUsed + len) > stateObj._outBuff.Length)
195                 {
196                     int remainder = stateObj._outBuff.Length - stateObj._outBytesUsed;
197
198                     Buffer.BlockCopy(b, offset, stateObj._outBuff, stateObj._outBytesUsed, remainder);
199
200                     offset += remainder;
201                     stateObj._outBytesUsed += remainder;
202
203                     if (stateObj._outBytesUsed == stateObj._outBuff.Length)
204                     {
205                         stateObj.WritePacket(0);
206                     }
207
208                     len -= remainder;
209                 }
210                 else
211                 {
212                     Buffer.BlockCopy(b, offset, stateObj._outBuff, stateObj._outBytesUsed, len);
213
214                     stateObj._outBytesUsed += len;
215
216                     break;
217                 }
218             }
219         }
220
221         internal void WriteShort(int v, TdsParserStateObject stateObj)
222         {
223             if ((stateObj._outBytesUsed + 2) > stateObj._outBuff.Length)
224             {
225                 WriteByte((byte)(v & 0xff), stateObj);
226                 WriteByte((byte)((v >> 8) & 0xff), stateObj);
227             }
228             else
229             {
230                 stateObj._outBuff[stateObj._outBytesUsed++] = (byte)(v & 0xFF);
231                 stateObj._outBuff[stateObj._outBytesUsed++] = (byte)((v >> 8) & 0xFF);
232             }
233         }
234
235         internal void WriteInt(int v, TdsParserStateObject stateObj)
236         {
237             WriteByteArray(BitConverter.GetBytes(v), 4, 0, stateObj);
238         }
239
240         private unsafe static void CopyStringToBytes(string source, int sourceOffset, byte[] dest, int destOffset, int charLength)
241         {
242             int byteLength = checked(charLength * 2);
243
244             fixed (char* sourcePtr = source)
245             {
246                 char* srcPtr = sourcePtr;
247                 srcPtr += sourceOffset;
248                 fixed (byte* destinationPtr = dest)
249                 {
250                     byte* destPtr = destinationPtr;
251                     destPtr += destOffset;
252
253                     byte* destByteAddress = destPtr;
254                     byte* srcByteAddress = (byte*)srcPtr;
255
256                     for (int index = 0; index < byteLength; index++)
257                     {
258                         *destByteAddress = *srcByteAddress;
259                         destByteAddress++;
260                         srcByteAddress++;
261                     }
262                 }
263             }
264         }
265
266         internal void WriteString(string s, int length, int offset, TdsParserStateObject stateObj)
267         {
268             int cBytes = 2 * length;
269
270             if (cBytes < (stateObj._outBuff.Length - stateObj._outBytesUsed))
271             {
272                 CopyStringToBytes(s, offset, stateObj._outBuff, stateObj._outBytesUsed, length);
273                 stateObj._outBytesUsed += cBytes;
274             }
275             else
276             {
277                 if (stateObj._bTmp == null || stateObj._bTmp.Length < cBytes)
278                 {
279                     stateObj._bTmp = new byte[cBytes];
280                 }
281
282                 CopyStringToBytes(s, offset, stateObj._bTmp, 0, length);
283                 WriteByteArray(stateObj._bTmp, cBytes, 0, stateObj);
284             }
285         }
286
287         private void WriteString(string s, TdsParserStateObject stateObj)
288         {
289             WriteString(s, s.Length, 0, stateObj);
290         }
291
292
293         [MethodImpl(MethodImplOptions.NoInlining)]
294         internal void TdsLogin(SqlLogin rec)
295         {
296             _physicalStateObj.SetTimeoutSeconds(rec.timeout);
297
298             byte[] encryptedPassword = null;
299             encryptedPassword = TdsParserStaticMethods.EncryptPassword(rec.password);
300
301             byte[] encryptedChangePassword = null;
302             encryptedChangePassword = TdsParserStaticMethods.EncryptPassword(rec.newPassword);
303
304             _physicalStateObj._outputMessageType = 16;
305
306             int length = 0x5e;
307
308             string clientInterfaceName = ".Net SqlClient Data Provider";
309
310             checked
311             {
312                 length += (rec.hostName.Length + rec.applicationName.Length +
313                             rec.serverName.Length + clientInterfaceName.Length +
314                             rec.language.Length + rec.database.Length +
315                             rec.attachDBFilename.Length) * 2;
316             }
317
318             byte[] outSSPIBuff = null;
319             UInt32 outSSPILength = 0;
320
321             if (!rec.useSSPI)
322             {
323                 checked
324                 {
325                     length += (rec.userName.Length * 2) + encryptedPassword.Length
326                     + encryptedChangePassword.Length;
327                 }
328             }
329             else
330             {
331                 if (rec.useSSPI)
332                 {
333                     outSSPIBuff = new byte[s_maxSSPILength];
334                     outSSPILength = s_maxSSPILength;
335
336                     _physicalStateObj.SniContext = SniContext.Snix_LoginSspi;
337
338                     SSPIData(null, 0, outSSPIBuff, ref outSSPILength);
339
340                     if (outSSPILength > Int32.MaxValue)
341                     {
342                         throw SQL.InvalidSSPIPacketSize();  // SqlBu 332503
343                     }
344
345                     _physicalStateObj.SniContext = SniContext.Snix_Login;
346
347                     checked
348                     {
349                         length += (Int32)outSSPILength;
350                     }
351                 }
352             }
353
354             try
355             {
356                 WriteInt(length, _physicalStateObj);
357                 WriteInt(0x730a0003, _physicalStateObj);
358                 WriteInt(rec.packetSize, _physicalStateObj);
359                 WriteInt(0x06000000, _physicalStateObj);
360                 WriteInt(0xa10, _physicalStateObj);
361                 WriteInt(0, _physicalStateObj);
362
363                 WriteByte(0xe0, _physicalStateObj);
364                 WriteByte(0x3, _physicalStateObj);
365                 WriteByte(0, _physicalStateObj);
366                 WriteByte(0, _physicalStateObj);
367                 WriteInt(0, _physicalStateObj);
368                 WriteInt(0, _physicalStateObj);
369
370
371                 int offset = 0x5e;
372
373
374                 WriteShort(offset, _physicalStateObj);
375                 WriteShort(rec.hostName.Length, _physicalStateObj);
376                 offset += rec.hostName.Length * 2;
377
378
379                 if (rec.useSSPI == false)
380                 {
381                     WriteShort(offset, _physicalStateObj);
382                     WriteShort(rec.userName.Length, _physicalStateObj);
383                     offset += rec.userName.Length * 2;
384
385                     WriteShort(offset, _physicalStateObj);
386                     WriteShort(encryptedPassword.Length / 2, _physicalStateObj);
387                     offset += encryptedPassword.Length;
388                 }
389                 else
390                 {
391                     WriteShort(0, _physicalStateObj);
392                     WriteShort(0, _physicalStateObj);
393                     WriteShort(0, _physicalStateObj);
394                     WriteShort(0, _physicalStateObj);
395                 }
396
397
398                 WriteShort(offset, _physicalStateObj);
399                 WriteShort(rec.applicationName.Length, _physicalStateObj);
400                 offset += rec.applicationName.Length * 2;
401
402
403                 WriteShort(offset, _physicalStateObj);
404                 WriteShort(rec.serverName.Length, _physicalStateObj);
405                 offset += rec.serverName.Length * 2;
406
407
408                 WriteShort(offset, _physicalStateObj);
409                 WriteShort(0, _physicalStateObj);
410
411
412                 WriteShort(offset, _physicalStateObj);
413                 WriteShort(clientInterfaceName.Length, _physicalStateObj);
414                 offset += clientInterfaceName.Length * 2;
415
416
417                 WriteShort(offset, _physicalStateObj);
418                 WriteShort(rec.language.Length, _physicalStateObj);
419                 offset += rec.language.Length * 2;
420
421
422                 WriteShort(offset, _physicalStateObj);
423                 WriteShort(rec.database.Length, _physicalStateObj);
424                 offset += rec.database.Length * 2;
425
426
427                 if (null == s_nicAddress)
428                 {
429                     s_nicAddress = TdsParserStaticMethods.GetNetworkPhysicalAddressForTdsLoginOnly();
430                 }
431
432                 WriteByteArray(s_nicAddress, s_nicAddress.Length, 0, _physicalStateObj);
433
434
435                 WriteShort(offset, _physicalStateObj);
436
437                 if (rec.useSSPI)
438                 {
439                     WriteShort((int)outSSPILength, _physicalStateObj);
440                     offset += (int)outSSPILength;
441                 }
442                 else
443                 {
444                     WriteShort(0, _physicalStateObj);
445                 }
446
447
448                 WriteShort(offset, _physicalStateObj);
449                 WriteShort(rec.attachDBFilename.Length, _physicalStateObj);
450                 offset += rec.attachDBFilename.Length * 2;
451
452
453                 WriteShort(offset, _physicalStateObj);
454                 WriteShort(encryptedChangePassword.Length / 2, _physicalStateObj);
455
456
457                 WriteInt(0, _physicalStateObj);
458
459
460                 WriteString(rec.hostName, _physicalStateObj);
461
462
463                 if (!rec.useSSPI)
464                 {
465                     WriteString(rec.userName, _physicalStateObj);
466
467                     _physicalStateObj._tracePasswordOffset = _physicalStateObj._outBytesUsed;
468                     _physicalStateObj._tracePasswordLength = encryptedPassword.Length;
469
470                     WriteByteArray(encryptedPassword, encryptedPassword.Length, 0, _physicalStateObj);
471                 }
472
473
474                 WriteString(rec.applicationName, _physicalStateObj);
475                 WriteString(rec.serverName, _physicalStateObj);
476                 WriteString(clientInterfaceName, _physicalStateObj);
477                 WriteString(rec.language, _physicalStateObj);
478                 WriteString(rec.database, _physicalStateObj);
479
480
481                 if (rec.useSSPI)
482                 {
483                     WriteByteArray(outSSPIBuff, (int)outSSPILength, 0, _physicalStateObj);
484                 }
485
486
487                 WriteString(rec.attachDBFilename, _physicalStateObj);
488
489
490                 if (!rec.useSSPI)
491                 {
492                     _physicalStateObj._traceChangePasswordOffset = _physicalStateObj._outBytesUsed;
493                     _physicalStateObj._traceChangePasswordLength = encryptedChangePassword.Length;
494                     WriteByteArray(encryptedChangePassword, encryptedChangePassword.Length, 0, _physicalStateObj);
495                 }
496             }
497             catch (Exception e)
498             {
499                 if (ADP.IsCatchableExceptionType(e))
500                 {
501                     _physicalStateObj._outputPacketNumber = 1;
502                     _physicalStateObj.ResetBuffer();
503                 }
504
505                 throw;
506             }
507
508             _physicalStateObj.WritePacket(1);
509             _physicalStateObj._pendingData = true;
510             return;
511         }
512     }
513
514     internal static class App
515     {
516         private static SqlLogin MakeSqlLoginForRepro()
517         {
518             var login = new SqlLogin();
519
520             login.hostName = "CHRISAHNA1";
521             login.userName = "etcmuser";
522             login.password = "29xiaq-1s";
523             login.applicationName = ".Net SqlClient Data Provider";
524             login.serverName = "csetcmdb.redmond.corp.microsoft.com";
525             login.language = "";
526             login.database = "Tcm_Global";
527             login.attachDBFilename = "";
528             login.newPassword = "";
529
530             return login;
531         }
532
533
534         private static int Main()
535         {
536             var tdsParser = new TdsParser();
537
538             tdsParser.TdsLogin(App.MakeSqlLoginForRepro());
539
540             uint computedLengthValue = tdsParser._physicalStateObj.ReadDwordFromPostHeaderContentAtByteOffset(0x0);
541
542             if (computedLengthValue == 0x15e)
543             {
544                 Console.WriteLine("Test passed.");
545                 return 100;
546             }
547             else
548             {
549                 Console.WriteLine("Test failed: ComputedLength=({0:x8})", computedLengthValue);
550             }
551             return 101;
552         }
553     }
554 }