openssl: Unable to run asm code on OpenBSD

perlasm files should keep constants/data in ‘.section .rodata’:

Running openssl tests on OpenBSD is futile unless build is configured with ./Configure no-asm which disables optimized routines written in assembly. I’ll demonstrate the issue on simple example. This is a main.c

#include <stdio.h>

extern int get_fee();
extern int get_foo();

int
main(int argc, const char *argv[0])
{
        printf("%x\n", get_fee());
        printf("%x\n", get_foo());

        return (0);
}

The code above is linked with .o file which is produced from .s below:

.global  get_foo
.type   get_foo,@function
get_foo:
        mov .foo(%rip), %rax
        ret

.global  get_fee
.type   get_fee,@function
get_fee:
        mov .fee(%rip), %rax
        ret

.foo:
        .long 0x00f00f00

.section .rodata

.fee:
        .byte 0x00,0xfe,0xef,0xee

The module above defines functions get_foo() and get_fee(). both functions return int which is a constant foo (fee) in module test.s Code can be built using Makefile as follows:

test: main.o test.o
        $(CC) -o test main.o test.o

main.o: main.c
        $(CC) -g -O0 -c -o main.o main.c

test.o: test.s
        $(CC) -c  test.s 

clean:
        rm -f test.o main.o

Building it and running it as-is on linux (Ubuntu) yields following output:

sashan@ubuntu:~/rodata$ make && ./test
cc -g -O0 -c -o main.o main.c
cc -c  test.s 
cc -o test main.o test.o
eeeffe00
f00f00

doing the same on OpenBSD box gives me a .core file.

lifty$ make && ./test
cc -g -O0 -c -o main.o main.c
cc -c  test.s 
cc -o test main.o test.o
eeeffe00
Segmentation fault (core dumped) 

Poking at core file we see we die as soon as we load constant foo to eax output register

lifty$ egdb test test.core  
...
Core was generated by `test'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00000b9e9d689a20 in get_foo ()
(gdb) disassemble get_foo
Dump of assembler code for function get_foo:
=> 0x00000b9e9d689a20 <+0>:     mov    0x9(%rip),%rax        # 0xb9e9d689a30 <.foo>
   0x00000b9e9d689a27 <+7>:     retq   
End of assembler dump.
(gdb) quit

Apart from returning different constants the functions get_foo() and get_fee() are identical

(gdb) disassemble get_fee
Dump of assembler code for function get_fee:
   0x00000b9e9d689a28 <+0>:     mov    -0x1523(%rip),%rax        # 0xb9e9d68850c
   0x00000b9e9d689a2f <+7>:     retq   
End of assembler dump.

What makes difference here is where fee and foo constants/data are placed. The constant fee is found in .rodata segment, while constant foo is placed to .text segment along the code. OpenBSD does not like variables/constants (a.k.a. data) to be placed into .text segment where code lives.

The CPU I was using to convey my test was amd ryzen.

About this issue

  • Original URL
  • State: closed
  • Created 5 months ago
  • Comments: 20 (19 by maintainers)

Commits related to this issue

Most upvoted comments

I have been maintaining patches for the OpenSSL ASM for the OpenBSD ports tree for the 3.1 and 3.2 branches: https://github.com/openbsd/ports/tree/master/security/openssl/3.2/patches . I have an ICLA on file, you are more than welcome to take them and bring them into shape acceptable for the OpenSSL project.

To my knowledge only the T4 assembly is broken on sparc64 #20523

Edit: There’s one thing I haven’t patched since we don’t support SM4 on hardware. That should also be fixed in a similar way to all the other patches:

https://github.com/openssl/openssl/blob/5b2d8bc28a8ff59689da98f31459819db09a9099/crypto/sm4/asm/vpsm4_ex-armv8.pl#L546

The only slightly tricky bit there is this one:

https://github.com/openssl/openssl/blob/5b2d8bc28a8ff59689da98f31459819db09a9099/crypto/sm4/asm/vpsm4_ex-armv8.pl#L477-L484

Ah, for x86_64, these are the transformations we added. Or rather, by “we”, I mean we accepted a contribution by @botovq to add them. 😃 https://boringssl-review.googlesource.com/c/boringssl/+/57445/7/crypto/perlasm/x86_64-xlate.pl

I suspect that will clear the error. macOS and Windows name these sections differently. See this discussion for Windows: https://boringssl-review.googlesource.com/c/boringssl/+/57445/comment/0f56c161_48c7db5f/

For macOS, this documentation has the names of sections: https://developer.apple.com/library/archive/documentation/Performance/Conceptual/CodeFootprint/Articles/MachOOverview.html

No objections, please go ahead. thanks. On Wed, Jan 17, 2024 at 12:33:21AM -0800, Tom Cosgrove wrote:

Any objections if I change the title to “Unable to run asm code on OpenBSD”? That might make this issue more discoverable…

– Reply to this email directly or view it on GitHub: https://github.com/openssl/openssl/issues/23312#issuecomment-1895326574 You are receiving this because you were assigned.

Message ID: @.***>

Any objections if I change the title to “Unable to run asm code on OpenBSD”? That might make this issue more discoverable…