Author Topic: Unicode string alignment bug  (Read 5952 times)

Yuri

  • Member
  • **
  • Posts: 175
Unicode string alignment bug
« on: December 28, 2013, 04:34:30 AM »
If a literal Unicode string is passed to a function, GoAsm may place it at an odd address. Some APIs apparently don't like this. RegSetValueExW returns ERROR_NOACCESS because of it (tested on Win 7 and 8.1).

dedndave

  • Member
  • *****
  • Posts: 8827
  • Still using Abacus 2.0
    • DednDave
Re: Unicode string alignment bug
« Reply #1 on: December 28, 2013, 04:53:54 AM »
unicode registry functions require 16-alignment
i think raymond chen wrote a page on that

Yuri

  • Member
  • **
  • Posts: 175
Re: Unicode string alignment bug
« Reply #2 on: December 28, 2013, 02:48:43 PM »
If he meant 16 bits, then it's exactly what I found out. If 16 bytes, then I don't know. Those that I have used work well with 2-byte alignment.

dedndave

  • Member
  • *****
  • Posts: 8827
  • Still using Abacus 2.0
    • DednDave
Re: Unicode string alignment bug
« Reply #3 on: December 28, 2013, 03:37:57 PM »
my mistake - it does say 16-bit, not 16-byte

in the Community Additions section, Raymond Chen makes a comment...

http://msdn.microsoft.com/en-us/library/windows/desktop/ms724911%28v=vs.85%29.aspx

Yuri

  • Member
  • **
  • Posts: 175
Re: Unicode string alignment bug
« Reply #4 on: December 28, 2013, 04:01:45 PM »
Quote
[Raymond Chen (MSFT)] Alignment requirements are implied by the data type LPCWSTR. If unaligned data were permitted, then it would have been declared as UNALIGNED. This requirement applies to all functions which take Unicode strings, not just registry functions.

Well, at least MessageBoxW worked fine for the misaligned string, which added to my confusion.

dedndave

  • Member
  • *****
  • Posts: 8827
  • Still using Abacus 2.0
    • DednDave
Re: Unicode string alignment bug
« Reply #5 on: December 28, 2013, 04:31:02 PM »
i wrote some UNICODE string functions that 16-byte aligned strings - lol
now, i have to go back and update them   :(

wjr

  • Member
  • **
  • Posts: 217
    • WJR's website
Re: Unicode string alignment bug
« Reply #6 on: January 06, 2014, 05:44:33 AM »
GoAsm does this alignment for the 64-bit case. Removing two lines of code looks like a quick fix for the 32-bit case, which will be in version 0.58.0.5 relatively soon. I just want to make sure that there are no other repercussions...

Yuri

  • Member
  • **
  • Posts: 175
Re: Unicode string alignment bug
« Reply #7 on: January 06, 2014, 02:55:15 PM »
Thanks, Wayne, but that doesn't work if you make the strings Unicode by saving the source as UTF-8 or UTF-16.

Code: [Select]
D:\PROG\TEST\GoAsm>stralign32
Error: 998
D:\PROG\TEST\GoAsm>stralign64
Error: 998

Code: [Select]
#define UNICODE
#define LINKFILES
#define LINKVCRT
#include <windows.h>

DATA SECTION

hKey    PTR 0
a       DB 0

CODE SECTION

Start:
    invoke RegCreateKeyEx, HKEY_CURRENT_USER, "TestKey", 0, NULL, 0, \
                            KEY_WRITE, NULL, addr hKey, NULL
    cmp eax,ERROR_SUCCESS
    jne >.error

    invoke printf, A"Success"
#if x86
    add esp,4
#endif
.exit
    ret
.error
    invoke printf, A"Error: %d", eax
#if x86
    add esp,8
#endif
    jmp .exit

wjr

  • Member
  • **
  • Posts: 217
    • WJR's website
Re: Unicode string alignment bug
« Reply #8 on: January 06, 2014, 05:28:00 PM »
So close, I tested with the string overrides and the STRINGS UNICODE directive, but I missed that possibility, so a few more lines get added back in. GoAsm version 0.58.0.6 on its way soon...

Yuri

  • Member
  • **
  • Posts: 175
Re: Unicode string alignment bug
« Reply #9 on: January 07, 2014, 05:13:32 PM »
Yes, 0.58.0.6 fixed it. Thanks! :icon_cool: