nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.43k stars 1.47k forks source link

SSL was compiled in 32-bit Nim's 'release' mode and ran incorrectly on Windows systems. #16268

Open lihf8515 opened 3 years ago

lihf8515 commented 3 years ago

In Windows,I wanted to use SSL to send the request, so I wrote the following simple little example. I compile with debug mode, and the program runs normally. But when I compile in Release mode, I can't run the program properly.

Example

import net, httpclient

var client: HttpClient
var ctx: SslContext
echo "ok1"
ctx = newContext(certFile = "cert/apiclient_cert.pem",
                 keyFile = "cert/apiclient_key.pem")
echo "ok2"
client = newHttpClient(sslContext = ctx, timeout = 6000)
client.headers = newHttpHeaders({ "Content-Type": "application/xml" })
let response = client.request(url = "https://www.test.com/", httpMethod = HttpPost,
                              body = "<xml><name>lhf</name></xml>")
ctx.destroyContext()
echo response.body

Current Output

D:\Project\wxpay-main\tests>nim c -d:ssl -d:release t
Hint: used config file 'E:\tool\develop\nim\nim-1.4.0-32\config\nim.cfg' [Conf]
Hint: used config file 'E:\tool\develop\nim\nim-1.4.0-32\config\config.nims' [Conf]
Hint: used config file 'D:\Project\wxpay-main\nim.cfg' [Conf]
Hint: used config file 'D:\Project\wxpay-main\tests\config.nims' [Conf]
.............................................
Hint:  [Link]
Hint: 103088 lines; 1.682s; 74.625MiB peakmem; Release build; proj: t; out: D:\Project\wxpay-main\tests\t.exe [SuccessX]

D:\Project\wxpay-main\tests>t.exe
ok1
SIGSEGV: Illegal storage access. (Attempt to read from nil?)

nim -v

C:\Users\Administrator>nim -v
Nim Compiler Version 1.4.0 [Windows: i386]
Compiled at 2020-10-18
Copyright (c) 2006-2020 by Andreas Rumpf

In addition, I compiled with the following version, which runs normally in either Debug mode or Release mode. It seems that only the Nim 32bit version has a problem. Nim Compiler Version 1.4.0 [Windows: amd64] Nim Compiler Version 1.4.0 [Linux: AMD64]

Dankr4d commented 3 years ago

Stumbled over this issue too and at me the program crashed without any exception. I did some investigation and I could get it working when compiling with -d:nimDisableCertificateValidation and creating the SslContext without setting cert/key parameter (for sure, this is not a solution). Furthermore I experienced also the crash when compiling with --opt:speed parameter (also in debug build). I guess the issue is regarding to c compiler optimization flags.

It crashes at this function call (SSL_CTX_use_certificate_chain_file): https://github.com/nim-lang/Nim/blob/devel/lib/pure/net.nim#L551

I'm using Nim 1.4.6 and openssl version 1.0.2u.

Nim Compiler Version 1.4.6 [Linux: amd64]
Compiled at 2021-04-16
Copyright (c) 2006-2020 by Andreas Rumpf
Dankr4d commented 3 years ago

Some testings resulted in, that all gcc optimization levels (-O1 to -O3) crash the application, in constellation with OpenSSL and 32 bit build. In my case it's caused by the -fomit-frame-pointer optimization flag which is set in all optimization levels.

Workaround: Add --passC:-fno-omit-frame-pointer when compiling your application.