Skip to content

OLEDB: Fix for AV Exception on x86 architecture#32150

Closed
maryamariyan wants to merge 3 commits intodotnet:masterfrom
maryamariyan:oledb
Closed

OLEDB: Fix for AV Exception on x86 architecture#32150
maryamariyan wants to merge 3 commits intodotnet:masterfrom
maryamariyan:oledb

Conversation

@maryamariyan
Copy link
Contributor

Fixes: #981

@maryamariyan maryamariyan self-assigned this Feb 12, 2020
@maryamariyan maryamariyan changed the title Fixing AccessViolationException on x86 architecture OLEDB: Fix for AV Exception on x86 architecture Feb 12, 2020
ApplyParameterBindings(commandWithParameters, bindings.BindInfo);
if (bindings.BindInfo_x86 != null)
{
ApplyParameterBindings(commandWithParameters, bindings.BindInfo_x86);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can make the ApplyParameterBindings to take bindings as an argument, and then do the switch for x86 inside the method. I believe there will be less duplicated code that way.

#endif

#if (WIN32 && !ARCH_arm)
[StructLayoutAttribute(LayoutKind.Sequential, Pack = 2)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment highlighting the difference in the x86 version may be useful

}
#endif

#if (WIN32 && !ARCH_arm)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the same ifdef on many other structs in this file. Do all of them need to be fixed?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either that or we shoukd remove the attributes/defines for them

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other structs will need to be fixed as well. I can take those up in another PR. I vote for this particular fix to be merged as this has been reported by users and can be caught in the tests as well (while running on x86 platform). The tests are not catching the other cases, so I think there may be more tests needed to validate the changes to the other structs here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am fine with doing just a baby step if we believe that this library is useful for real workloads on x86 without fixing all/most of these. What are the chances that customers hitting this crash will immediately hit the next once once this one is fixed? Do we have some more complex sample app that uses this library that we can use to validate that it can work for a real app on x86?

{
internal sealed class Bindings
{
private static readonly bool s_runningOnX86 = RuntimeInformation.ProcessArchitecture == Architecture.X86;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old defines referenced "ARCH_arm". (I assume that referred to 32 bit.) Do we expect this library to work on ARM/ARM64? If so will the right things happen?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the right things will happen for ARM/ARM64. This is the condition that we are replicating from oledb.h from Windows SDK:

#if defined(_WIN64) || defined(_ARM_)
#include <pshpack8.h>	// 8-byte structure packing
#else
#include <pshpack2.h>	// 2-byte structure packing
#endif

set
{
_bindInfo[_index].pwszName = value;
if (s_runningOnX86)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ternary expression might be neater for these (?)

}
#endif

#if (WIN32 && !ARCH_arm)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either that or we shoukd remove the attributes/defines for them

internal byte bPrecision;
internal byte bScale;

#if DEBUG
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you keep this I suggest "#if 0" since one would only need it in DEBUG if specifically looking at this problem. It's not a big deal but it does override a member that in theory other code could call.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did this to keep the style in the rest of this file. Otherwise I don't mind just removing the whole thing

}
OleDbHResult hr = commandWithParameters.SetParameterInfo((IntPtr)bindInfo.Length, ordinals, bindInfo);
OleDbHResult hr;
fixed (tagDBPARAMBINDINFO* p = &bindInfo[0])
Copy link
Contributor

@saurabh500 saurabh500 Feb 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jkotas Would it be better to take the bindInfo here and check if we are running on x86 platform, and if we are, then create an array of tagDBPARAMBINDINFO_x86 and Marshal the tagDBPARAMBINDINFO to the array of x86 structs?
This would avoid all the if statements and multiple parameters accepting multiple structures based on the platform. I mean something like the following.

private unsafe void ApplyParameterBindings(UnsafeNativeMethods.ICommandWithParameters commandWithParameters, tagDBPARAMBINDINFO[] bindInfo)
        {
            IntPtr[] ordinals = new IntPtr[bindInfo.Length];
            for (int i = 0; i < ordinals.Length; ++i)
            {
                ordinals[i] = (IntPtr)(i + 1);
            }

            OleDbHResult hr;

            if (s_runningOnX86)
            {
                tagDBPARAMBINDINFO_x86[] bindInfo_x86 = new tagDBPARAMBINDINFO_x86[bindInfo.Length];
                for (int i = 0; i < bindInfo.Length; i++)
                {
                    fixed (tagDBPARAMBINDINFO* p = &bindInfo[i])
                    {
                        bindInfo_x86[i] = (tagDBPARAMBINDINFO_x86)Marshal.PtrToStructure((IntPtr)p, typeof(tagDBPARAMBINDINFO_x86));
                    }
                }
                fixed (tagDBPARAMBINDINFO_x86* p = &bindInfo_x86[0])
                {
                    hr = commandWithParameters.SetParameterInfo((IntPtr)bindInfo.Length, ordinals, (IntPtr)p);
                }
            }
            else
            {
                fixed (tagDBPARAMBINDINFO* p = &bindInfo[0])
                {
                    hr = commandWithParameters.SetParameterInfo((IntPtr)bindInfo.Length, ordinals, (IntPtr)p);
                }
            }

            if (hr < 0)
            {
                ProcessResults(hr);
            }
        }

And ofcourse we change the signature of SetParameterInfo to

System.Data.OleDb.OleDbHResult SetParameterInfo(
                [In] IntPtr cParams,
                [In, MarshalAs(UnmanagedType.LPArray)] IntPtr[] rgParamOrdinals,
                [In] IntPtr rgParamBindInfo);

as @maryamariyan has already done?

There would be data duplication and manipulation for x86 scenario, but it does simplify the code greatly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to take the bindInfo here and check if we are running on x86 platform, and if we are, then create an array of tagDBPARAMBINDINFO_x86 and Marshal the tagDBPARAMBINDINFO to the array of x86 structs?

Yes, that would be an option as well, especially given that this fix will need replicated in many more places. As you have said, it will result into extra data copies on x86, and the code will take very different path on x86 in general. We need to make sure to test it well.

@saurabh500 saurabh500 mentioned this pull request Feb 14, 2020
@maryamariyan
Copy link
Contributor Author

closing in favor of PR #32207

@ghost ghost locked as resolved and limited conversation to collaborators Dec 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AccessViolationException using System.Data.OleDb

4 participants