The MASM Forum

Projects => Rarely Used Projects => GoAsm => Topic started by: Yuri on December 28, 2013, 04:34:30 AM

Title: Unicode string alignment bug
Post by: Yuri 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).
Title: Re: Unicode string alignment bug
Post by: dedndave on December 28, 2013, 04:53:54 AM
unicode registry functions require 16-alignment
i think raymond chen wrote a page on that
Title: Re: Unicode string alignment bug
Post by: Yuri 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.
Title: Re: Unicode string alignment bug
Post by: dedndave 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 (http://msdn.microsoft.com/en-us/library/windows/desktop/ms724911%28v=vs.85%29.aspx)
Title: Re: Unicode string alignment bug
Post by: Yuri 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.
Title: Re: Unicode string alignment bug
Post by: dedndave 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   :(
Title: Re: Unicode string alignment bug
Post by: wjr 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...
Title: Re: Unicode string alignment bug
Post by: Yuri 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.


D:\PROG\TEST\GoAsm>stralign32
Error: 998
D:\PROG\TEST\GoAsm>stralign64
Error: 998



#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
Title: Re: Unicode string alignment bug
Post by: wjr 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...
Title: Re: Unicode string alignment bug
Post by: Yuri on January 07, 2014, 05:13:32 PM
Yes, 0.58.0.6 fixed it. Thanks! :icon_cool: