summaryrefslogtreecommitdiff
path: root/tests/src/jit/Directed/pinvoke/pinvoke-bug.cs
blob: 2d4b5f6aeac0dcaee17fced43607b2da3e89087a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Runtime.InteropServices;

// Test includes an intentional unreachable return 
#pragma warning disable 162

namespace PInvokeTest
{
    internal class Test
    {
        [DllImport("msvcrt", EntryPoint = "sin")]
        private static extern double sin(double x);

        private static double g;
        private static bool b;

        public static int Main(string[] args)
        {
            bool result = false;
            g = 0.0;
            double val = 1.0;
            b = false;
            try
            {
                Func(val);
            }
            catch(Exception)
            {
                result = (Math.Abs(g - sin(val)) < 0.0001);
            }

            return (result ? 100 : -1);
        }

        // An inline pinvoke in a method with float math followed by a
        // throw may causes trouble for liveness models for the inline
        // frame var.
        static double Func(double x)
        {
            g = sin(x);

            // A bit of control flow to throw off rareness detection
            // Also we need float in here 
            if (b)
            {
                g = 0.0;
            }

            throw new Exception();

            // Deliberately unreachable return
            return g;
        }
    }
}