Chilkat.OAuth2 Crashing When Refreshing AccessToken

I’m using the Chilkat plugin to work with Google Sheets, which requires authentication. The application that I’m writing authenticates properly when run on my Mac, but it crashes when running in the remote debugger on a raspberrypi where I ultimately will be running this application.

What’s very curious is that setting a break point on the line where the actual HTTP POST occurs allows the code to run fine in the remote debugger. See the last line of the code block below.

Dim oauth2 As New Chilkat.OAuth2

oauth2.TokenEndpoint = “https://www.googleapis.com/oauth2/v4/token

// Replace these with actual values.
oauth2.ClientId = “myClientIDisHere”
oauth2.ClientSecret = “mySecretIsHere”

// Get the “refresh_token”
oauth2.RefreshToken = jsonToken.StringOf(“refresh_token”)
System.DebugLog("jsonToken = " + jsonToken.StringOf(“refresh_token”))

// Send the HTTP POST to refresh the access token.
success = oauth2.RefreshAccessToken()

p.s. How does one properly format a code block in this forum to make it appear with the usual font and color distinguishing it from the other part of the conversation? And is it possible to preview a post before it’s submitted?

Where is the crash log or do you get an exception? Either you post on StackOverFlow with Chilkat as tag or you contact Matt directly.

I get a hard crash without a break on any exception in the Xojo IDE.

Inspecting /var/log/syslog on the pi revealed what appears to be the problem:

Sep 12 11:08:34 raspberrypi kernel: [124950.396318] Alignment trap: not handling instruction f460caef at []
Sep 12 11:08:34 raspberrypi kernel: [124950.396325] 8<— cut here —
Sep 12 11:08:34 raspberrypi kernel: [124950.396336] Unhandled fault: alignment exception (0x221) at 0x021e3ac8
Sep 12 11:08:34 raspberrypi kernel: [124950.396345] pgd = f191bee4
Sep 12 11:08:34 raspberrypi kernel: [124950.396352] [021e3ac8] *pgd=0c10b003, *pmd=1efe43003

This looks pretty specific to the Chilkat plugin for Xojo, so I’ll pass along this portion of the log and its context to support@chilkatsoft.com

The syslog file above strongly suggests that this crash is caused by a memory alignment failure. The pi runs on ARM which requires memory alignment that is not required of code running on x86 chips. If code ported from x86 to ARM is not fully tweaked to satisfy the need for memory alignment, then the Linux kernel has a mechanism to “fix” the alignment.

Apparently, the Chilkat.OAuth2 has a memory alignment issue that is slipping through this safety net. Inspection of the “/proc/cpu/alignment” file before and after running my code supports this conclusion.

Before running my code the User count of misalignments is 0:

pi@raspberrypi:~ $ cat /proc/cpu/alignment
User:    0
System:  0 (0x0)
Skipped: 0
Half:    0
Word:    0
DWord:   0
Multi:   0  

User faults: 2 (fixup)

After running my code the User count is 1:

pi@raspberrypi:~ $ cat /proc/cpu/alignment
User:    1
System:  0    (0x0)
Skipped: 1
Half:    0
Word:    0
DWord:   0
Multi:   0

User faults: 2 (fixup)

I’ve not found documentation for the “Skipped” count, but I’m guessing that it represents misalignments that were not “fixed” and that’s why the application crashes.

A very informative treatment of “what, why and solutions” for memory alignment issues can be found here: https://blog.quarkslab.com/unaligned-accesses-in-cc-what-why-and-solutions-to-do-it-properly.html#id17

I built a workaround to the Chilkat.OAuth2 crash when running on a raspberry pi by obtaining a Refreshed Access Token with my own POST that follows the example in:

/Applications/Xojo 2020 Release 1/Example Projects/Communication/Internet/URLConnection/URLConnectionPOST.xojo_binary_project

But now I’ve encountered another memory alignment issue, this time with Chilkat.Http:

Dim http As New Chilkat.Http
http.AuthToken = jsonToken.StringOf("access_token")

//  443 is the SSL/TLS port for HTTPS.
Dim resp As Chilkat.HttpResponse
resp = http.SynchronousRequest("sheets.googleapis.com",443,True,req)

The syslog output points to another memory alignment issue causing this crash:

Sep 13 11:11:03 raspberrypi kernel: [14361.853911] Alignment trap: not handling instruction f460caef at [<b0febe40>]
Sep 13 11:11:03 raspberrypi kernel: [14361.853921] 8<--- cut here ---
Sep 13 11:11:03 raspberrypi kernel: [14361.853938] Unhandled fault: alignment exception (0x221) at 0x02a25fb8
Sep 13 11:11:03 raspberrypi kernel: [14361.853951] pgd = e3bbbad3
Sep 13 11:11:03 raspberrypi kernel: [14361.853961] [02a25fb8] *pgd=2f245003, *pmd=1fbd24003

I’ll send this to Matt so he’s aware as well.

This issue has been addressed with an update by Chilkat. Contact (admin@chilkatsoft.com) Matt for an update between releases if you are in need.