Skip to content

Commit

Permalink
[WIP] UnalignedAccess Android tests
Browse files Browse the repository at this point in the history
  • Loading branch information
matouskozak committed Apr 25, 2024
1 parent 66272bb commit 18e622f
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<RunAOTCompilation>false</RunAOTCompilation>
<TestRuntime>true</TestRuntime>
<TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
<MainLibraryFileName>Android.Device_Emulator.UnalignedAccess.Test.dll</MainLibraryFileName>
<ExpectedExitCode>42</ExpectedExitCode>
</PropertyGroup>

<ItemGroup>
<Compile Include="Program.cs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.InteropServices;
using System.Buffers.Binary;

public class Program
{
[StructLayout(LayoutKind.Sequential, Size=12)]
struct S {
public double d1;
public int i2;
}

[StructLayout(LayoutKind.Sequential)]
struct H {
public S s1;
public int i3;
public byte b4;
}

public static int Main()
{
Console.WriteLine("Hello, Android!");

Console.WriteLine(Marshal.SizeOf(typeof(S)));
var a2 = new S[2];
a2[1].d1 = 2.12;
Console.WriteLine (a2[1].d1);
var h1 = new H();
h1.s1.d1 = 4.0;
h1.i3 = 15;
h1.b4 = 42;
unsafe {
fixed (S* parr = &a2[0])
{
byte* ptr = (byte*)parr;
ptr += 12;
Console.WriteLine ($"0x{((IntPtr)ptr):x}");
Console.WriteLine (*(ptr + 0));
Console.WriteLine (*(ptr + 1));
Console.WriteLine (*(ptr + 2));
Console.WriteLine (*(ptr + 3));
Console.WriteLine (*(ptr + 4));
Console.WriteLine (*(ptr + 5));
Console.WriteLine (*(ptr + 6));
Console.WriteLine (*(ptr + 7));
Span<byte> tmp = stackalloc byte[8];
new Span<byte>(ptr, 8).CopyTo(tmp);
double d = BinaryPrimitives.ReadDoubleLittleEndian(tmp);
Console.WriteLine ($"tmp = {d}");
if (d != 2.12) // if this prints 2.12 - unaligned load happened
return 1;
}
{
byte* ptr = (byte*)&h1;
Console.WriteLine (*(ptr + 12));
Console.WriteLine (*(ptr + 13));
Console.WriteLine (*(ptr + 14));
Console.WriteLine (*(ptr + 15));
Console.WriteLine (*(ptr + 16));
}
}

return 42;
}
}

0 comments on commit 18e622f

Please sign in to comment.